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

"Display Decision Tree from rapidminer"

rjrj Member Posts: 6 Contributor II
edited May 2019 in Help

    Hi

I have generated xml from java
then i passed xml to process and run the process
after that how can i get the  tree img from rapidminer?

Process process = new Process(readFileAsString(x));
        IOContainer ioResult = process.run();
TreeModelGraphRenderer gr = new TreeModelGraphRenderer();
        Component visualizationComponent = gr.getVisualizationComponent(model, ioResult);
        GraphCreator<String, String> graphCreator = gr.getGraphCreator(model, ioResult);
        edu.uci.ics.jung.graph.Graph createGraph = graphCreator.createGraph();


As per Class TreeModelGraphCreator
Author IngoMierswa 
An adaptor for the graph creator interface. Subclasses might want to override some of the implemented methods and have to define the GraphCreator.createGraph() method.
do we need to override createGraph() method. if so can u give some clue how can i do it to get the graph?
Tagged:

Answers

  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    you should be able to do this by using this general approach:

    RapidMiner.setExecutionMode(ExecutionMode.EMBEDDED_WITHOUT_UI); // or whatever you need
    RapidMiner.init();
    IOContainer ioResult = process.run();
    IOObject result = ioResult.getElementAt(0); // or whatever index you need
    String name = RendererService.getName(result.getClass());
    List<Renderer> renderers = RendererService.getRenderers(name);
    for (Renderer renderer : renderers) { // you don't really need to iterate over this, it's probably only one anyway ;)
           IOContainer dummy = new IOContainer();
    int imgWidth = 300;
    int imgHeight = 300;
           Reportable reportable = renderer.createReportable(result, dummy, imgWidth, imgHeight);
    if (reportable instanceof Renderable) {
    Renderable renderable = (Renderable) reportable;
    renderable.prepareRendering();
    int preferredWidth = renderable.getRenderWidth(800);
    int preferredHeight = renderable.getRenderHeight(800);
    final BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) img.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, imgWidth, imgHeight);
    double scale = Math.min((double) imgWidth / (double) preferredWidth, (double) imgHeight / (double) preferredHeight);
    graphics.scale(scale, scale);
    renderable.render(graphics, preferredWidth, preferredHeight);
           }
    }
    Regards,
    Marco
  • Mariusz_GrabowsMariusz_Grabows Member Posts: 2 Contributor I
    Hello Marco,

    I'm wondering if you could provide me any suggestions why I’m getting truncated decision tree graph… ? I’m using general approach that you provided above but I have added one line:
    ImageIO.write(img,”png”,out);
    to write image into OutputStream out which I’m using to display image on my web page.
    Unfortunately I’m getting truncated decision tree graph for only 300px width (it’s not changing when I set 500px) few DT nodes are not displayed at all.
  • Marco_BoeckMarco_Boeck Administrator, Moderator, Employee-RapidMiner, Member, University Professor Posts: 1,996 RM Engineering
    Hi,

    I updated the example a bit:


    Process XML:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.2.003">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="5.2.003" 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"/>
        <parameter key="parallelize_main_process" value="false"/>
        <process expanded="true" height="161" width="279">
          <operator activated="true" class="retrieve" compatibility="5.2.003" expanded="true" height="60" name="Retrieve" width="90" x="45" y="75">
            <parameter key="repository_entry" value="//Samples/data/Sonar"/>
          </operator>
          <operator activated="true" class="decision_tree" compatibility="5.2.003" expanded="true" height="76" name="Decision Tree" width="90" x="179" y="75">
            <parameter key="criterion" value="gain_ratio"/>
            <parameter key="minimal_size_for_split" value="4"/>
            <parameter key="minimal_leaf_size" value="2"/>
            <parameter key="minimal_gain" value="0.1"/>
            <parameter key="maximal_depth" value="20"/>
            <parameter key="confidence" value="0.25"/>
            <parameter key="number_of_prepruning_alternatives" value="3"/>
            <parameter key="no_pre_pruning" value="false"/>
            <parameter key="no_pruning" value="false"/>
          </operator>
          <connect from_op="Retrieve" from_port="output" to_op="Decision Tree" to_port="training set"/>
          <connect from_op="Decision Tree" 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>

    Complete code of a test class which visualizes the image quick&dirty:

    package com.testpackage;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.List;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;

    import com.rapidminer.Process;
    import com.rapidminer.RapidMiner;
    import com.rapidminer.RapidMiner.ExecutionMode;
    import com.rapidminer.RepositoryProcessLocation;
    import com.rapidminer.gui.renderer.Renderer;
    import com.rapidminer.gui.renderer.RendererService;
    import com.rapidminer.operator.IOContainer;
    import com.rapidminer.operator.IOObject;
    import com.rapidminer.operator.OperatorException;
    import com.rapidminer.report.Renderable;
    import com.rapidminer.report.Reportable;
    import com.rapidminer.repository.Entry;
    import com.rapidminer.repository.ProcessEntry;
    import com.rapidminer.repository.RepositoryException;
    import com.rapidminer.repository.RepositoryLocation;
    import com.rapidminer.tools.XMLException;

    public class PlotterTest {

    public static void main(String[] args) {
    RapidMiner.setExecutionMode(ExecutionMode.EMBEDDED_WITHOUT_UI);
    RapidMiner.init();
    JFrame frame = new JFrame("Decision Tree Renderer Test");
    frame.setLocation(0, 0);
    JLabel label = new JLabel();
    frame.add(new JScrollPane(label));
    try {
    RepositoryLocation location;
    RapidMiner.setExecutionMode(ExecutionMode.COMMAND_LINE);
    RapidMiner.init();
    location = new RepositoryLocation("//LocalRepository/Test/DecTree");
    Entry entry = location.locateEntry();
    if (entry instanceof ProcessEntry) {
    Process process = new RepositoryProcessLocation(location).load(null);

    IOContainer ioResult = process.run();
    IOObject result = ioResult.getElementAt(0); // or whatever index you need
    String name = RendererService.getName(result.getClass());
    List<Renderer> renderers = RendererService.getRenderers(name);
    for (Renderer renderer : renderers) { // you don't really need to
    // iterate over this, it's
    // probably only one anyway
    // ;)
    IOContainer dummy = new IOContainer();
    // edit size of image here
    int imgWidth = 1600;
    int imgHeight = 950;
    Reportable reportable = renderer.createReportable(result, dummy, imgWidth, imgHeight);
    if (reportable instanceof Renderable) {
    Renderable renderable = (Renderable) reportable;
    renderable.prepareRendering();
    int preferredWidth = renderable.getRenderWidth(800);
    int preferredHeight = renderable.getRenderHeight(800);
    final BufferedImage img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) img.getGraphics();
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, imgWidth, imgHeight);
    // scale to whatever you need
    graphics.scale(2, 2);
    renderable.render(graphics, preferredWidth, preferredHeight);
    label.setIcon(new ImageIcon(img));
    }
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (XMLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (OperatorException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (RepositoryException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    frame.setVisible(true);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    }

    You will just need to update the repository location to match your repository structure.

    Regards,
    Marco
  • Mariusz_GrabowsMariusz_Grabows Member Posts: 2 Contributor I
    Hi,
    thank You very much for example once again but my problem was related to clipRect of VectorGraphics / Graphics2D object. When scaling with Graphics2D#scale method, clipRect was the same size so I’ve looked inside yours Renderable#render method and try few values for “preferredWidth and Height” - which are in fact used inside setSize – and after a while I got optimal values ;-)
Sign In or Register to comment.