Groovy script request

wesselwessel Member Posts: 537 Maven
edited November 2018 in Help
Groovy script request

Could someone show me a groovy script which can roughly do the same thing as the "Moving Average" operator?
I hope that a script will make it more easy to make useful attributes.
For example create the attributes: "MA2_att1 - MA9_att1" and "MA2_att2 - MA9_att2".

I know you can also do this with basic RM operators, (see the script below) but having to rename all is rather cumbersome.
Also it is hard to create a wide range of functions.


RM xml code: generates a sinus time series with noise, and creates some derived attributes

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="5.0">
 <context>
   <input/>
   <output/>
   <macros/>
 </context>
 <operator activated="true" class="process" expanded="true" name="Process">
   <process expanded="true" height="573" width="975">
     <operator activated="true" class="generate_data" expanded="true" height="60" name="Generate Data" width="90" x="45" y="30">
       <parameter key="target_function" value="driller oscillation timeseries"/>
       <parameter key="attributes_lower_bound" value="-1.0"/>
       <parameter key="attributes_upper_bound" value="1.0"/>
     </operator>
     <operator activated="true" class="add_noise" expanded="true" height="94" name="Add Noise" width="90" x="180" y="30">
       <parameter key="label_noise" value="0.0"/>
       <parameter key="default_attribute_noise" value="0.3"/>
       <list key="noise"/>
     </operator>
     <operator activated="true" class="series:moving_average" expanded="true" height="76" name="MA:9 att1" width="90" x="315" y="30">
       <parameter key="attribute_name" value="att1"/>
       <parameter key="window_width" value="9"/>
     </operator>
     <operator activated="true" class="rename_by_replacing" expanded="true" height="76" name="MA9" width="90" x="450" y="30">
       <parameter key="replace_what" value="moving_average\("/>
       <parameter key="replace_by" value="MA9_"/>
     </operator>
     <operator activated="true" class="series:moving_average" expanded="true" height="76" name="MA:2 att1" width="90" x="585" y="30">
       <parameter key="attribute_name" value="att1"/>
       <parameter key="window_width" value="2"/>
     </operator>
     <operator activated="true" class="rename_by_replacing" expanded="true" height="76" name="MA2" width="90" x="720" y="30">
       <parameter key="replace_what" value="moving_average\("/>
       <parameter key="replace_by" value="MA2_"/>
     </operator>
     <operator activated="true" class="series:moving_average" expanded="true" height="76" name="MA:9 att2" width="90" x="855" y="30">
       <parameter key="attribute_name" value="att2"/>
       <parameter key="window_width" value="9"/>
     </operator>
     <operator activated="true" class="rename_by_replacing" expanded="true" height="76" name="MA9 (2)" width="90" x="45" y="120">
       <parameter key="replace_what" value="moving_average\("/>
       <parameter key="replace_by" value="MA9_"/>
     </operator>
     <operator activated="true" class="series:moving_average" expanded="true" height="76" name="MA:2 att2" width="90" x="180" y="120">
       <parameter key="attribute_name" value="att2"/>
       <parameter key="window_width" value="2"/>
     </operator>
     <operator activated="true" class="rename_by_replacing" expanded="true" height="76" name="MA2 (2)" width="90" x="315" y="120">
       <parameter key="replace_what" value="moving_average\("/>
       <parameter key="replace_by" value="MA2_"/>
     </operator>
     <operator activated="true" class="rename_by_replacing" expanded="true" height="76" name="rem \)" width="90" x="450" y="120">
       <parameter key="replace_what" value="\)"/>
     </operator>
     <operator activated="true" class="generate_attributes" expanded="true" height="76" name="Generate Attributes" width="90" x="585" y="120">
       <list key="function_descriptions">
         <parameter key="MA2_att1 - MA9_att1" value="MA2_att1 - MA9_att1"/>
         <parameter key="MA2_att2 - MA9_att2" value="MA2_att2 - MA9_att2"/>
       </list>
     </operator>
     <operator activated="true" class="select_attributes" expanded="true" height="76" name="Select Attributes" width="90" x="720" y="120">
       <parameter key="attribute_filter_type" value="single"/>
       <parameter key="attribute" value="att5"/>
       <parameter key="invert_selection" value="true"/>
     </operator>
     <connect from_op="Generate Data" from_port="output" to_op="Add Noise" to_port="example set input"/>
     <connect from_op="Add Noise" from_port="example set output" to_op="MA:9 att1" to_port="example set input"/>
     <connect from_op="MA:9 att1" from_port="example set output" to_op="MA9" to_port="example set input"/>
     <connect from_op="MA9" from_port="example set output" to_op="MA:2 att1" to_port="example set input"/>
     <connect from_op="MA:2 att1" from_port="example set output" to_op="MA2" to_port="example set input"/>
     <connect from_op="MA2" from_port="example set output" to_op="MA:9 att2" to_port="example set input"/>
     <connect from_op="MA:9 att2" from_port="example set output" to_op="MA9 (2)" to_port="example set input"/>
     <connect from_op="MA9 (2)" from_port="example set output" to_op="MA:2 att2" to_port="example set input"/>
     <connect from_op="MA:2 att2" from_port="example set output" to_op="MA2 (2)" to_port="example set input"/>
     <connect from_op="MA2 (2)" from_port="example set output" to_op="rem \)" to_port="example set input"/>
     <connect from_op="rem \)" from_port="example set output" to_op="Generate Attributes" to_port="example set input"/>
     <connect from_op="Generate Attributes" from_port="example set output" to_op="Select Attributes" to_port="example set input"/>
     <connect from_op="Select Attributes" from_port="example set output" to_port="result 1"/>
     <portSpacing port="source_input 1" spacing="0"/>
     <portSpacing port="sink_result 1" spacing="90"/>
     <portSpacing port="sink_result 2" spacing="0"/>
   </process>
 </operator>
</process>
Sign In or Register to comment.