How to use iteration value in a extracted macro to generate another macro?

Himanshu_Pant
Himanshu_Pant New Altair Community Member
edited November 5 in Community Q&A
I have extracted a macro inside 'loop example'. The loop example is connected to dataset which contains dates. The data set has one attribute called 'Event DateTime_adjusted'. The macro {START_DATE} is extracting 'Event DateTime_adjusted' attribute value. Now I want to generate a macro using this formula :
date_add(%{START_DATE},eval(%{example_index}),DATE_UNIT_DAY).

But it is throwing an error: The function date_add must have an argument of type 'date'
Tagged:

Best Answer

  • MartinLiebig
    MartinLiebig
    Altair Employee
    Answer ✓
    attached is the fixed process. I hope it does what you want. The challenge you had, was the default time format we use for strings. I just used date_str_custom and Extract Macro (format) to use another one.

    Best,
    Martin

    <?xml version="1.0" encoding="UTF-8"?><process version="9.9.002">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="9.9.002" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="utility:create_exampleset" compatibility="9.9.002" expanded="true" height="68" name="Create ExampleSet (2)" width="90" x="45" y="85">
            <parameter key="generator_type" value="comma separated text"/>
            <parameter key="number_of_examples" value="100"/>
            <parameter key="use_stepsize" value="false"/>
            <list key="function_descriptions"/>
            <parameter key="add_id_attribute" value="false"/>
            <list key="numeric_series_configuration"/>
            <list key="date_series_configuration"/>
            <list key="date_series_configuration (interval)"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="input_csv_text" value="Event DateTime&#10;2019-04-10T18:45:00.000Z&#10;2019-04-21T17:45:00.000Z"/>
            <parameter key="column_separator" value=","/>
            <parameter key="parse_all_as_nominal" value="false"/>
            <parameter key="decimal_point_character" value="."/>
            <parameter key="trim_attribute_names" value="true"/>
          </operator>
          <operator activated="true" class="nominal_to_date" compatibility="9.9.002" expanded="true" height="82" name="Nominal to Date (3)" width="90" x="179" y="85">
            <parameter key="attribute_name" value="Event DateTime"/>
            <parameter key="date_type" value="date_time"/>
            <parameter key="date_format" value="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="locale" value="English (United States)"/>
            <parameter key="keep_old_attribute" value="true"/>
          </operator>
          <operator activated="true" class="set_macros" compatibility="9.9.002" expanded="true" height="68" name="Set Macros" width="90" x="179" y="289">
            <list key="macros">
              <parameter key="wisi" value="2"/>
              <parameter key="sile" value="5"/>
              <parameter key="jum" value="1"/>
            </list>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="9.9.002" expanded="true" height="82" name="Generate Attributes (3)" width="90" x="313" y="85">
            <list key="function_descriptions">
              <parameter key="Event DateTime_adjusted" value="date_add([Event DateTime],-eval(%{sile}),DATE_UNIT_DAY)"/>
            </list>
            <parameter key="keep_all" value="true"/>
          </operator>
          <operator activated="false" class="date_to_nominal" compatibility="9.9.002" expanded="true" height="82" name="Date to Nominal" width="90" x="447" y="289">
            <parameter key="attribute_name" value="Event DateTime_adjusted"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="locale" value="English (United States)"/>
            <parameter key="keep_old_attribute" value="false"/>
          </operator>
          <operator activated="true" class="loop_examples" compatibility="9.9.002" expanded="true" height="103" name="Loop Examples" width="90" x="581" y="85">
            <parameter key="iteration_macro" value="example_index"/>
            <process expanded="true">
              <operator activated="true" class="operator_toolbox:extract_macro_enhanced" compatibility="3.0.000-SNAPSHOT" expanded="true" height="68" name="Extract Macro (Format)" width="90" x="112" y="34">
                <parameter key="macro" value="START_DATE"/>
                <parameter key="macro_type" value="data_value"/>
                <parameter key="statistics" value="average"/>
                <parameter key="attribute_name" value="Event DateTime_adjusted"/>
                <parameter key="example_index" value="%{example_index}"/>
                <list key="additional_macros"/>
                <parameter key="format_of_numericals" value="#.##"/>
                <parameter key="date_format" value="yyyy-MM-dd hh:mm:ss"/>
                <parameter key="time_zone" value="SYSTEM"/>
                <parameter key="locale" value="English (United States)"/>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro (7)" width="90" x="112" y="340">
                <list key="function_descriptions">
                  <parameter key="number_of_iteration" value="eval(%{sile})-eval(%{wisi})+1"/>
                </list>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro (2)" width="90" x="112" y="238">
                <list key="function_descriptions">
                  <parameter key="start_date_final" value="date_str_custom(date_add(date_parse_custom(%{START_DATE},&quot;yyyy-MM-dd hh:mm:ss&quot;),eval(%{example_index}),DATE_UNIT_DAY),&quot;yyyy-MM-dd hh:mm:ss&quot;)"/>
                </list>
              </operator>
              <operator activated="true" class="concurrency:loop" compatibility="9.9.002" expanded="true" height="82" name="Loop (3)" width="90" x="447" y="34">
                <parameter key="number_of_iterations" value="%{number_of_iteration}"/>
                <parameter key="iteration_macro" value="iteration"/>
                <parameter key="reuse_results" value="false"/>
                <parameter key="enable_parallel_execution" value="true"/>
                <process expanded="true">
                  <operator activated="true" class="utility:create_exampleset" compatibility="9.9.002" expanded="true" height="68" name="Create ExampleSet" width="90" x="380" y="136">
                    <parameter key="generator_type" value="date series"/>
                    <parameter key="number_of_examples" value="192"/>
                    <parameter key="use_stepsize" value="true"/>
                    <list key="function_descriptions"/>
                    <parameter key="add_id_attribute" value="false"/>
                    <list key="numeric_series_configuration"/>
                    <list key="date_series_configuration"/>
                    <list key="date_series_configuration (interval)">
                      <parameter key="timestamp" value="%{start_date_final}.15.minute"/>
                    </list>
                    <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
                    <parameter key="time_zone" value="SYSTEM"/>
                    <parameter key="column_separator" value=","/>
                    <parameter key="parse_all_as_nominal" value="false"/>
                    <parameter key="decimal_point_character" value="."/>
                    <parameter key="trim_attribute_names" value="true"/>
                  </operator>
                  <connect from_op="Create ExampleSet" from_port="output" to_port="output 1"/>
                  <portSpacing port="source_input 1" spacing="0"/>
                  <portSpacing port="sink_output 1" spacing="0"/>
                  <portSpacing port="sink_output 2" spacing="0"/>
                </process>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro" width="90" x="112" y="136">
                <list key="function_descriptions">
                  <parameter key="days" value="96 *(eval(%{wisi}) + 1) + %{example_index}"/>
                </list>
              </operator>
              <operator activated="false" class="extract_macro" compatibility="9.9.002" expanded="true" height="68" name="Extract Macro" width="90" x="246" y="34">
                <parameter key="macro" value="START_DATE"/>
                <parameter key="macro_type" value="data_value"/>
                <parameter key="statistics" value="average"/>
                <parameter key="attribute_name" value="Event DateTime_adjusted"/>
                <parameter key="example_index" value="%{example_index}"/>
                <list key="additional_macros"/>
              </operator>
              <connect from_port="example set" to_op="Extract Macro (Format)" to_port="example set"/>
              <connect from_op="Loop (3)" from_port="output 1" to_port="output 1"/>
              <portSpacing port="source_example set" spacing="0"/>
              <portSpacing port="sink_example set" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
            </process>
          </operator>
          <operator activated="true" class="append" compatibility="9.9.002" expanded="true" height="82" name="Append" width="90" x="715" y="85">
            <parameter key="datamanagement" value="double_array"/>
            <parameter key="data_management" value="auto"/>
            <parameter key="merge_type" value="all"/>
          </operator>
          <connect from_op="Create ExampleSet (2)" from_port="output" to_op="Nominal to Date (3)" to_port="example set input"/>
          <connect from_op="Nominal to Date (3)" from_port="example set output" to_op="Generate Attributes (3)" to_port="example set input"/>
          <connect from_op="Generate Attributes (3)" from_port="example set output" to_op="Loop Examples" to_port="example set"/>
          <connect from_op="Loop Examples" from_port="output 1" to_op="Append" to_port="example set 1"/>
          <connect from_op="Append" from_port="merged set" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>


Answers

  • Marco_Barradas
    Marco_Barradas
    Altair Employee
    Hi @Himanshu_Pant

    You'll need to date parse the macro date you are using.

    date_add(date_parse_custom(%{START_DATE},"yyy-MM-dd hh:mm:ss"),eval(%{example_index}),DATE_UNIT_DAY)

    Just adjust your date string to your format.
  • Himanshu_Pant
    Himanshu_Pant New Altair Community Member
    This method worked. However, while using this macro in 'Create Example Set" getting wrong Date format error.
  • MartinLiebig
    MartinLiebig
    Altair Employee
    Hi,
    likely you need to adjust the date format setting? or maybe its because Marco had a spelling error with only 3 y's.

    Best,
    Martin
  • Himanshu_Pant
    Himanshu_Pant New Altair Community Member
    Hi @mschmitz,

    Adding the process. Please have a look.
  • MartinLiebig
    MartinLiebig
    Altair Employee
    Hi,
    can you try Extract Macro (Format) from operator toolbox? It allows you to specify the date format manually.
    Best,
    Martin
  • Himanshu_Pant
    Himanshu_Pant New Altair Community Member
    Yes, but in extract macro I can't use the formula:
    date_add(date_parse_custom(%{START_DATE},"yyy-MM-dd hh:mm:ss"),eval(%{example_index}),DATE_UNIT_DAY)
  • Himanshu_Pant
    Himanshu_Pant New Altair Community Member
    MarcoBarradas,

    Please help with this.
  • MartinLiebig
    MartinLiebig
    Altair Employee
    Answer ✓
    attached is the fixed process. I hope it does what you want. The challenge you had, was the default time format we use for strings. I just used date_str_custom and Extract Macro (format) to use another one.

    Best,
    Martin

    <?xml version="1.0" encoding="UTF-8"?><process version="9.9.002">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="9.9.002" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="utility:create_exampleset" compatibility="9.9.002" expanded="true" height="68" name="Create ExampleSet (2)" width="90" x="45" y="85">
            <parameter key="generator_type" value="comma separated text"/>
            <parameter key="number_of_examples" value="100"/>
            <parameter key="use_stepsize" value="false"/>
            <list key="function_descriptions"/>
            <parameter key="add_id_attribute" value="false"/>
            <list key="numeric_series_configuration"/>
            <list key="date_series_configuration"/>
            <list key="date_series_configuration (interval)"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="input_csv_text" value="Event DateTime&#10;2019-04-10T18:45:00.000Z&#10;2019-04-21T17:45:00.000Z"/>
            <parameter key="column_separator" value=","/>
            <parameter key="parse_all_as_nominal" value="false"/>
            <parameter key="decimal_point_character" value="."/>
            <parameter key="trim_attribute_names" value="true"/>
          </operator>
          <operator activated="true" class="nominal_to_date" compatibility="9.9.002" expanded="true" height="82" name="Nominal to Date (3)" width="90" x="179" y="85">
            <parameter key="attribute_name" value="Event DateTime"/>
            <parameter key="date_type" value="date_time"/>
            <parameter key="date_format" value="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="locale" value="English (United States)"/>
            <parameter key="keep_old_attribute" value="true"/>
          </operator>
          <operator activated="true" class="set_macros" compatibility="9.9.002" expanded="true" height="68" name="Set Macros" width="90" x="179" y="289">
            <list key="macros">
              <parameter key="wisi" value="2"/>
              <parameter key="sile" value="5"/>
              <parameter key="jum" value="1"/>
            </list>
          </operator>
          <operator activated="true" class="generate_attributes" compatibility="9.9.002" expanded="true" height="82" name="Generate Attributes (3)" width="90" x="313" y="85">
            <list key="function_descriptions">
              <parameter key="Event DateTime_adjusted" value="date_add([Event DateTime],-eval(%{sile}),DATE_UNIT_DAY)"/>
            </list>
            <parameter key="keep_all" value="true"/>
          </operator>
          <operator activated="false" class="date_to_nominal" compatibility="9.9.002" expanded="true" height="82" name="Date to Nominal" width="90" x="447" y="289">
            <parameter key="attribute_name" value="Event DateTime_adjusted"/>
            <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
            <parameter key="time_zone" value="SYSTEM"/>
            <parameter key="locale" value="English (United States)"/>
            <parameter key="keep_old_attribute" value="false"/>
          </operator>
          <operator activated="true" class="loop_examples" compatibility="9.9.002" expanded="true" height="103" name="Loop Examples" width="90" x="581" y="85">
            <parameter key="iteration_macro" value="example_index"/>
            <process expanded="true">
              <operator activated="true" class="operator_toolbox:extract_macro_enhanced" compatibility="3.0.000-SNAPSHOT" expanded="true" height="68" name="Extract Macro (Format)" width="90" x="112" y="34">
                <parameter key="macro" value="START_DATE"/>
                <parameter key="macro_type" value="data_value"/>
                <parameter key="statistics" value="average"/>
                <parameter key="attribute_name" value="Event DateTime_adjusted"/>
                <parameter key="example_index" value="%{example_index}"/>
                <list key="additional_macros"/>
                <parameter key="format_of_numericals" value="#.##"/>
                <parameter key="date_format" value="yyyy-MM-dd hh:mm:ss"/>
                <parameter key="time_zone" value="SYSTEM"/>
                <parameter key="locale" value="English (United States)"/>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro (7)" width="90" x="112" y="340">
                <list key="function_descriptions">
                  <parameter key="number_of_iteration" value="eval(%{sile})-eval(%{wisi})+1"/>
                </list>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro (2)" width="90" x="112" y="238">
                <list key="function_descriptions">
                  <parameter key="start_date_final" value="date_str_custom(date_add(date_parse_custom(%{START_DATE},&quot;yyyy-MM-dd hh:mm:ss&quot;),eval(%{example_index}),DATE_UNIT_DAY),&quot;yyyy-MM-dd hh:mm:ss&quot;)"/>
                </list>
              </operator>
              <operator activated="true" class="concurrency:loop" compatibility="9.9.002" expanded="true" height="82" name="Loop (3)" width="90" x="447" y="34">
                <parameter key="number_of_iterations" value="%{number_of_iteration}"/>
                <parameter key="iteration_macro" value="iteration"/>
                <parameter key="reuse_results" value="false"/>
                <parameter key="enable_parallel_execution" value="true"/>
                <process expanded="true">
                  <operator activated="true" class="utility:create_exampleset" compatibility="9.9.002" expanded="true" height="68" name="Create ExampleSet" width="90" x="380" y="136">
                    <parameter key="generator_type" value="date series"/>
                    <parameter key="number_of_examples" value="192"/>
                    <parameter key="use_stepsize" value="true"/>
                    <list key="function_descriptions"/>
                    <parameter key="add_id_attribute" value="false"/>
                    <list key="numeric_series_configuration"/>
                    <list key="date_series_configuration"/>
                    <list key="date_series_configuration (interval)">
                      <parameter key="timestamp" value="%{start_date_final}.15.minute"/>
                    </list>
                    <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
                    <parameter key="time_zone" value="SYSTEM"/>
                    <parameter key="column_separator" value=","/>
                    <parameter key="parse_all_as_nominal" value="false"/>
                    <parameter key="decimal_point_character" value="."/>
                    <parameter key="trim_attribute_names" value="true"/>
                  </operator>
                  <connect from_op="Create ExampleSet" from_port="output" to_port="output 1"/>
                  <portSpacing port="source_input 1" spacing="0"/>
                  <portSpacing port="sink_output 1" spacing="0"/>
                  <portSpacing port="sink_output 2" spacing="0"/>
                </process>
              </operator>
              <operator activated="true" class="generate_macro" compatibility="9.9.002" expanded="true" height="68" name="Generate Macro" width="90" x="112" y="136">
                <list key="function_descriptions">
                  <parameter key="days" value="96 *(eval(%{wisi}) + 1) + %{example_index}"/>
                </list>
              </operator>
              <operator activated="false" class="extract_macro" compatibility="9.9.002" expanded="true" height="68" name="Extract Macro" width="90" x="246" y="34">
                <parameter key="macro" value="START_DATE"/>
                <parameter key="macro_type" value="data_value"/>
                <parameter key="statistics" value="average"/>
                <parameter key="attribute_name" value="Event DateTime_adjusted"/>
                <parameter key="example_index" value="%{example_index}"/>
                <list key="additional_macros"/>
              </operator>
              <connect from_port="example set" to_op="Extract Macro (Format)" to_port="example set"/>
              <connect from_op="Loop (3)" from_port="output 1" to_port="output 1"/>
              <portSpacing port="source_example set" spacing="0"/>
              <portSpacing port="sink_example set" spacing="0"/>
              <portSpacing port="sink_output 1" spacing="0"/>
              <portSpacing port="sink_output 2" spacing="0"/>
            </process>
          </operator>
          <operator activated="true" class="append" compatibility="9.9.002" expanded="true" height="82" name="Append" width="90" x="715" y="85">
            <parameter key="datamanagement" value="double_array"/>
            <parameter key="data_management" value="auto"/>
            <parameter key="merge_type" value="all"/>
          </operator>
          <connect from_op="Create ExampleSet (2)" from_port="output" to_op="Nominal to Date (3)" to_port="example set input"/>
          <connect from_op="Nominal to Date (3)" from_port="example set output" to_op="Generate Attributes (3)" to_port="example set input"/>
          <connect from_op="Generate Attributes (3)" from_port="example set output" to_op="Loop Examples" to_port="example set"/>
          <connect from_op="Loop Examples" from_port="output 1" to_op="Append" to_port="example set 1"/>
          <connect from_op="Append" from_port="merged set" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>