[Solved] How to create new tables, attributes and examples with a script?

qwertzqwertz Member Posts: 130  Maven
edited November 2018 in Help
Dear all,

initially I thought that this question would go along with my last post but after spending some hours trying, I thought that it might better fit in a new topic.

Thanks to Marco I found a way to manipulate example sets / tables with the script operator
(http://rapid-i.com/rapidforum/index.php/topic,8342.0.html)

But a short time after I ended up with the questions
-How to create a new table / example set with scripting?
-How to add new attributes and examples to the input example set?
-How to provide newly created and input example set together to the scripting output?

As mentioned in the linked post, I found a kind of documentation but probably I do something wrong with the syntax. Struggling for hours now :(
Please help :)


Cheers
Sachs

Answers

  • Marco_BoeckMarco_Boeck Team Lead Software Engineering Administrator, Moderator, Employee, Member, University Professor Posts: 1,908   RM Engineering
    Hi,

    let me just start with a warning: Using "Execute Script" on a RapidMiner Server will result in problems, mainly eating up memory and never ever releasing it again unless the whole Server is restarted. Which is one of the reasons why we don't use this operator to solve customer problems.
    Now that we got the warning out of the way, here you go ;)

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.1.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="6.1.000" expanded="true" name="Process">
        <process expanded="true">
          <operator activated="true" class="retrieve" compatibility="6.1.000" expanded="true" height="60" name="Retrieve Iris" width="90" x="45" y="30">
            <parameter key="repository_entry" value="//Samples/data/Iris"/>
          </operator>
          <operator activated="true" class="execute_script" compatibility="6.1.000" expanded="true" height="94" name="Execute Script" width="90" x="179" y="30">
            <parameter key="script" value="import java.util.LinkedList;&#10;import java.util.List;&#10;&#10;import com.rapidminer.example.Attribute;&#10;import com.rapidminer.example.ExampleSet;&#10;import com.rapidminer.example.table.AttributeFactory;&#10;import com.rapidminer.example.table.DoubleArrayDataRow;&#10;import com.rapidminer.example.table.MemoryExampleTable;&#10;import com.rapidminer.tools.Ontology;&#10;&#10;&#10;List&lt;Attribute&gt; listOfAtts = new LinkedList&lt;&gt;();&#10;// you can create any attribute type here, see Ontology class for more information&#10;Attribute newNumericAtt = AttributeFactory.createAttribute(&quot;Numerical Att&quot;, Ontology.ATTRIBUTE_VALUE_TYPE.NUMERICAL);&#10;listOfAtts.add(newNumericAtt);&#10;Attribute newNominalAtt = AttributeFactory.createAttribute(&quot;Nominal Att&quot;, Ontology.ATTRIBUTE_VALUE_TYPE.POLYNOMINAL);&#10;listOfAtts.add(newNominalAtt);&#10;MemoryExampleTable table = new MemoryExampleTable(listOfAtts);&#10;// every row is a double array internally&#10;double[] doubleArray = new double[listOfAtts.size()];&#10;doubleArray[0] = 42;&#10;doubleArray[1] = newNominalAtt.getMapping().mapString(&quot;hello&quot;);&#10;table.addDataRow(new DoubleArrayDataRow(doubleArray));&#10;ExampleSet exSet = table.createExampleSet();&#10;&#10;// return multiple outputs via List&#10;List&lt;IOObject&gt; returnList = new LinkedList&lt;&gt;();&#10;returnList.add(input[0]);&#10;returnList.add(exSet);&#10;return returnList;"/>
          </operator>
          <connect from_op="Retrieve Iris" from_port="output" to_op="Execute Script" to_port="input 1"/>
          <connect from_op="Execute Script" from_port="output 1" to_port="result 1"/>
          <connect from_op="Execute Script" from_port="output 2" to_port="result 2"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
          <portSpacing port="sink_result 3" spacing="0"/>
        </process>
      </operator>
    </process>
    Regards,
    Marco
  • qwertzqwertz Member Posts: 130  Maven

    Thanks a lot! It's so helpful to have a piece of example code in order to understand the basics!

    For all other users that work on the same topic I would like to reference to two similar yet slighly different posts:
    http://rapid-i.com/rapidforum/index.php?topic=2214.0
    http://rapidminernotes.blogspot.de/2013/06/using-groovy-to-make-arbitrary-example.html


    Best regards
    Sachs
  • qwertzqwertz Member Posts: 130  Maven
    I just came across the manual "How to extend Rapidminer" which is linked in the documentation overview http://rapidminer.com/documentation/

    This is definitely worth reading for everyone who wants to start with scripts!
Sign In or Register to comment.