Search in sources :

Example 6 with PMMLDecisionTreeTranslator

use of org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator in project knime-core by knime.

the class DecTreeToImageNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    exec.setMessage("Decision Tree To Image: Loading model...");
    PMMLPortObject port = (PMMLPortObject) inPorts[0];
    List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
    if (models.isEmpty()) {
        String msg = "Decision Tree evaluation failed: " + "No tree model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
    port.initializeModelTranslator(trans);
    m_decTree = trans.getDecisionTree();
    m_decTree.resetColorInformation();
    String colorColumn = null;
    if (null != inPorts[1]) {
        BufferedDataTable inData = (BufferedDataTable) inPorts[1];
        // get column with color information
        for (DataColumnSpec s : inData.getDataTableSpec()) {
            if (s.getColorHandler() != null) {
                colorColumn = s.getName();
                break;
            }
        }
        m_decTree.setColorColumn(colorColumn);
        for (DataRow thisRow : inData) {
            m_decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
        }
    }
    // create PNG via streamed string
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int width = m_settings.getWidth();
    int height = m_settings.getHeight();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    BufferedImage image = null;
    if (env.isHeadlessInstance()) {
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    } else {
        // create compatible image for better performance
        GraphicsConfiguration gfxConf = env.getDefaultScreenDevice().getDefaultConfiguration();
        // image = gfxConf.createCompatibleImage(width, height);
        // with binary transparency
        image = gfxConf.createCompatibleImage(width, height, Transparency.BITMASK);
    // with transparency
    // image = gfxConf.createCompatibleImage(width, height,
    // Transparency.TRANSLUCENT);
    }
    Graphics2D g = (Graphics2D) image.getGraphics();
    DecisionTreeNode root = null != getDecisionTree() ? getDecisionTree().getRootNode() : null;
    DecTreeGraphView graph = new DecTreeToImageGraphView(root, colorColumn, m_settings);
    // draw graph
    graph.getView().paint(g);
    // write png
    ImageIO.write(image, "png", os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    m_imageContent = new PNGImageContent(is);
    ImagePortObjectSpec outSpec = new ImagePortObjectSpec(PNGImageContent.TYPE);
    // return image object
    PortObject po = new ImagePortObject(m_imageContent, outSpec);
    return new PortObject[] { po };
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) ImagePortObjectSpec(org.knime.core.node.port.image.ImagePortObjectSpec) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GraphicsEnvironment(java.awt.GraphicsEnvironment) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DataRow(org.knime.core.data.DataRow) BufferedImage(java.awt.image.BufferedImage) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) DataColumnSpec(org.knime.core.data.DataColumnSpec) DecTreeGraphView(org.knime.base.node.mine.decisiontree2.view.DecTreeGraphView) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Aggregations

PMMLDecisionTreeTranslator (org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator)6 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)6 DecisionTree (org.knime.base.node.mine.decisiontree2.model.DecisionTree)5 DataColumnSpec (org.knime.core.data.DataColumnSpec)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)4 DataTableSpec (org.knime.core.data.DataTableSpec)4 PortObject (org.knime.core.node.port.PortObject)4 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)4 DataRow (org.knime.core.data.DataRow)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 Node (org.w3c.dom.Node)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 DataCell (org.knime.core.data.DataCell)2 DefaultRow (org.knime.core.data.def.DefaultRow)2 DoubleCell (org.knime.core.data.def.DoubleCell)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2