The Altair Community is migrating to a new platform to provide a better experience for you. In preparation for the migration, the Altair Community is on read-only mode from October 28 - November 6, 2024. Technical support via cases will continue to work as is. For any urgent requests from Students/Faculty members, please submit the form linked here
Getting the output file from RM
Fireholder
Member Posts: 26 Contributor II
Hello everybody,
Does anyone knows how can I save the output generated by my code. This is the sample of very simple process, I'm trying to learn how to write RM process from the scratch without use of GUI.From the sample it can be seen that I connected the result to a result file using GUI options, so the output is being saved in a Result.md file.
Thanks in advance.
best regards, Fire
Does anyone knows how can I save the output generated by my code. This is the sample of very simple process, I'm trying to learn how to write RM process from the scratch without use of GUI.From the sample it can be seen that I connected the result to a result file using GUI options, so the output is being saved in a Result.md file.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>And now this is the code I wrote for this process (actually I found some snippets in this forum and used them for my process):
<process version="5.1.006">
<context>
<input>
<location>//NewLocalRepository/Project/Golf</location>
</input>
<output>
<location>result</location>
</output>
<macros/>
</context>
<operator activated="true" class="process" compatibility="5.1.006" expanded="true" name="Process">
<process expanded="true" height="440" width="625">
<operator activated="true" class="retrieve" compatibility="5.1.006" expanded="true" height="60" name="Retrieve" width="90" x="64" y="183">
<parameter key="repository_entry" value="Golf"/>
</operator>
<operator activated="true" class="naive_bayes" compatibility="5.1.006" expanded="true" height="76" name="Naive Bayes" width="90" x="249" y="164"/>
<connect from_op="Retrieve" from_port="output" to_op="Naive Bayes" to_port="training set"/>
<connect from_op="Naive Bayes" from_port="model" 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>
And the question is should I connect the "training set" port to "result 1" port like it in XML file, if yes how can I do it? (It's very stupid question but I'm learning at least ;D)And how can I save the output of this process to somewhere so I can use or read the data from that file?
import com.rapidminer.Process;
import com.rapidminer.RapidMiner;
import com.rapidminer.RapidMiner.ExecutionMode;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.io.RepositorySource;
import com.rapidminer.operator.learner.bayes.NaiveBayes;
import com.rapidminer.operator.ports.InputPort;
import com.rapidminer.operator.ports.InputPorts;
import com.rapidminer.operator.ports.OutputPort;
import com.rapidminer.repository.RepositoryLocation;
import com.rapidminer.tools.OperatorService;
public class Test {
private static Operator createRetrievalOperator(Process parentProcess)
throws Exception {
RepositoryLocation location = new RepositoryLocation(
"//NewLocalRepository/Project/Golf");
String loc = parentProcess.makeRelativeRepositoryLocation(location);
// create input operator
Operator retrieve = OperatorService
.createOperator(RepositorySource.class);
/*
* Set the parameter of the operator to the location of the repository
* entry
*/
retrieve.setParameter("repository_entry", loc);
return retrieve;
}
/**
* Connect the output-port <code>fromPortName</code> from Operator
* <code>from</code> with the input-port <code>toPortName</code> of Operator
* <code>to</code>.
*/
private static void connect(Operator from, String fromPortName,
Operator to, String toPortName) {
from.getOutputPorts().getPortByName(fromPortName).connectTo(
to.getInputPorts().getPortByName(toPortName));
}
/**
* Connect the output-port <code>fromPortName</code> from Subprocess
* <code>from</code> with the input-port <code>toPortName</code> of Operator
* <code>to</code>.
*/
public static void main (String[] argv) throws Exception {
// init rapidminer
RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
RapidMiner.init();
// Create a process
final Process process = new Process();
// all operators from "left to right"
final Operator retrieve = createRetrievalOperator(process);
final NaiveBayes naivebayes=OperatorService.createOperator(NaiveBayes.class);
// add operators to the main process and connect them
process.getRootOperator().getSubprocess(0).addOperator(retrieve);
process.getRootOperator().getSubprocess(0).addOperator(naivebayes);
connect(retrieve, "output", naivebayes, "training set");
// print process setup
System.out.println(process.getRootOperator().createProcessTree(0));
// perform process
process.run();
}
}
Thanks in advance.
best regards, Fire
Tagged:
0
Answers
I'm sry I don't quite understand your question, but I hope my answer will help anyway.
First, if you already have an XML process file, you can simply create the exact process by calling new Process(String xml). Simply feed the contents of the xml file as a string to the Process constructor, and you're good to go. This snippet will execute the process with the given IOContainer object. If your process does not have data coming from the input port, process.run() is fine.
However notice the IOContainer ioResult = process.run(). The result(s) will be saved in this IOContainer object, which basically acts like a list.
So to get the first result, just call The result must not always be an ExampleSet, but normally, it is.
To store this ExampleSet, you can call: where location is a RepositoryLocation object.
I hope this helps.
Regards,
Marco
regards, Fire
you are trying to use the local variable resultSet outside of the scope you defined it in. This is something you need to be wary of when working with Java, you can only access variables in their respective scope. A block like {...} also counts as a scope.
So to fix your problem, you need to define the variable outside of the if-statement: Alternatively, and probably better in this case would be to move the code which depends on the if-statement anyway into it: Regards,
Marco
best regards, FIre
regards, Fire.
now you have an ExampleSet object. You can use it to retrieve the actual data, e.g. the attributes and the values.
To check out what else you can do with it, just check out the methods on the ExampleSet object, the Attribute object and the Example object. Regards,
Marco
I'm sorry, but that code works just fine for me. Of course you need to make sure that you're actually getting an ExampleSet as a result from your process and that the index checked via ioResult.getElement(index) is correct.
What do you mean by "the compiler can't recognize it" - does it give you an error when you're trying to compile the program, or is the element simply not an ExampleSet during runtime?
Please check what's in the ioResult object via the Ecplise debug view and post it. If the ioResult container is empty, make sure your process actually generates a result and that you're checking the correct element position (via ioResult.getElement(index)).
If all that fails, post your process xml and the results of your inquiry.
Regards,
Marco
rgrds, Fire