"Display Decision Tree from rapidminer"

rjrj MemberPosts:6Contributor II
edited May 2019 inHelp

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 graphCreator = gr.getGraphCreator(model, ioResult);
edu.uci.ics.jung.graph.Graph createGraph = graphCreator.createGraph();


As per Class TreeModelGraphCreator
Author IngoMierswa
一个适配器我图的创造者nterface. 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, Member, University ProfessorPosts:1,984RM 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 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 MemberPosts:2Contributor 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, Member, University ProfessorPosts:1,984RM Engineering
    Hi,

    I updated the example a bit:


    Process XML:








































    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 {

    公共静态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 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 MemberPosts:2Contributor 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 InorRegisterto comment.