Search in sources :

Example 1 with PMMLDocument

use of org.dmg.pmml.PMMLDocument in project knime-core by knime.

the class RuleEngine2PortsNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable data = (BufferedDataTable) inData[DATA_PORT];
    m_rowCount = data.size();
    try {
        Pair<ColumnRearranger, PortObject> rearrangerPair = createColumnRearranger((DataTableSpec) inData[DATA_PORT].getSpec(), new DataTableRowInput((DataTable) inData[RULE_PORT]));
        BufferedDataTable predictedTable = exec.createColumnRearrangeTable(data, rearrangerPair.getFirst(), exec);
        PortObject second = rearrangerPair.getSecond();
        if (m_settings.isPMMLRuleSet()) {
            if (m_settings.isProvideStatistics()) {
                PMMLPortObject po = (PMMLPortObject) rearrangerPair.getSecond();
                PMMLPortObject pmmlPortObject = new PMMLPortObject(m_copy.getSpec(), po);
                // Remove extra model.
                pmmlPortObject.addModelTranslater(new PMMLTranslator() {

                    @Override
                    public void initializeFrom(final PMMLDocument pmmlDoc) {
                    }

                    @Override
                    public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
                        return null;
                    }
                });
                second = pmmlPortObject;
            } else {
                second = m_copy;
            }
        }
        return new PortObject[] { predictedTable, second };
    } finally {
        m_rowCount = -1;
    }
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTable(org.knime.core.data.DataTable) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataTableRowInput(org.knime.core.node.streamable.DataTableRowInput) PMMLDocument(org.dmg.pmml.PMMLDocument) PMMLTranslator(org.knime.core.node.port.pmml.PMMLTranslator) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) SchemaType(org.apache.xmlbeans.SchemaType)

Example 2 with PMMLDocument

use of org.dmg.pmml.PMMLDocument in project knime-core by knime.

the class FromDecisionTreeNodeModel method execute.

/**
 * {@inheritDoc}
 * @throws CanceledExecutionException Execution cancelled.
 * @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    PMMLPortObject decTreeModel = (PMMLPortObject) inData[0];
    PMMLDecisionTreeTranslator treeTranslator = new PMMLDecisionTreeTranslator();
    decTreeModel.initializeModelTranslator(treeTranslator);
    DecisionTree decisionTree = treeTranslator.getDecisionTree();
    decisionTree.getRootNode();
    PMMLPortObject ruleSetModel = new PMMLPortObject(decTreeModel.getSpec());
    PMMLDocument document = PMMLDocument.Factory.newInstance();
    PMML pmml = document.addNewPMML();
    PMMLPortObjectSpec.writeHeader(pmml);
    pmml.setVersion(PMMLPortObject.PMML_V4_2);
    new PMMLDataDictionaryTranslator().exportTo(document, decTreeModel.getSpec());
    RuleSetModel newRuleSetModel = pmml.addNewRuleSetModel();
    PMMLMiningSchemaTranslator.writeMiningSchema(decTreeModel.getSpec(), newRuleSetModel);
    newRuleSetModel.setFunctionName(MININGFUNCTION.CLASSIFICATION);
    newRuleSetModel.setAlgorithmName("RuleSet");
    RuleSet ruleSet = newRuleSetModel.addNewRuleSet();
    ruleSet.addNewRuleSelectionMethod().setCriterion(Criterion.FIRST_HIT);
    addRules(ruleSet, new ArrayList<DecisionTreeNode>(), decisionTree.getRootNode());
    // TODO: Return a BufferedDataTable for each output port
    PMMLPortObject pmmlPortObject = new PMMLPortObject(ruleSetModel.getSpec(), document);
    return new PortObject[] { pmmlPortObject, new RuleSetToTable(m_rulesToTable).execute(exec, pmmlPortObject) };
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMML(org.dmg.pmml.PMMLDocument.PMML) DecisionTreeNodeSplitPMML(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML) PMMLDocument(org.dmg.pmml.PMMLDocument) PMMLDataDictionaryTranslator(org.knime.core.node.port.pmml.PMMLDataDictionaryTranslator) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) RuleSetToTable(org.knime.base.node.rules.engine.totable.RuleSetToTable) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 3 with PMMLDocument

use of org.dmg.pmml.PMMLDocument in project knime-core by knime.

the class PMMLRuleEditorNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    return new StreamableOperator() {

        private ColumnRearranger m_rearrangerx;

        private PMMLPortObject m_portObject;

        {
            try {
                final PMMLDocument doc = PMMLDocument.Factory.newInstance();
                final PMML pmml = doc.addNewPMML();
                RuleSetModel ruleSetModel = pmml.addNewRuleSetModel();
                RuleSet ruleSet = ruleSetModel.addNewRuleSet();
                PMMLRuleParser parser = new PMMLRuleParser(tableSpec, getAvailableInputFlowVariables());
                m_rearrangerx = createRearranger(tableSpec, ruleSet, parser);
            } catch (ParseException e) {
                throw new InvalidSettingsException(e);
            }
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            m_rearrangerx.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            super.loadInternals(internals);
            m_portObject = ((StreamInternalForPMMLPortObject) internals).getObject();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return createInitialStreamableOperatorInternals().setObject(m_portObject);
        }
    };
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) DataTableSpec(org.knime.core.data.DataTableSpec) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMML(org.dmg.pmml.PMMLDocument.PMML) PMMLDocument(org.dmg.pmml.PMMLDocument) ParseException(java.text.ParseException)

Example 4 with PMMLDocument

use of org.dmg.pmml.PMMLDocument in project knime-core by knime.

the class PMMLGeneralRegressionModelWrapper method createPMMLDocument.

/**
 * {@inheritDoc}
 */
@Override
public PMMLDocument createPMMLDocument(final DataDictionary dataDict) {
    PMMLDocument doc = createEmptyDocument(dataDict);
    doc.getPMML().setGeneralRegressionModelArray(new GeneralRegressionModel[] { m_model });
    return doc;
}
Also used : PMMLDocument(org.dmg.pmml.PMMLDocument)

Example 5 with PMMLDocument

use of org.dmg.pmml.PMMLDocument in project knime-core by knime.

the class PMMLPortObject method loadFrom.

/**
 * Initializes the pmml port object based on the xml input stream.
 * @param spec the referring spec of this object
 * @param is the pmml input stream
 * @throws IOException if the file cannot be found
 * @throws XmlException if something goes wrong during reading
 */
public void loadFrom(final PMMLPortObjectSpec spec, final InputStream is) throws IOException, XmlException {
    // disallow close in the factory -- we had indeterministic behavior
    // where close was called more than once (which should be OK) but as
    // the argument input stream is a NonClosableZipInput, which delegates
    // close to closeEntry(), we have to make sure that close is only
    // called once.
    // TODO: The document is read twice here. Could we "probe" into the file to check the version?
    XmlObject xmlDoc = XmlObject.Factory.parse(new NonClosableInputStream(is));
    is.close();
    if (xmlDoc instanceof PMMLDocument) {
        m_pmmlDoc = (PMMLDocument) xmlDoc;
    } else {
        /* Try to recover when reading a PMML 3.x/4.0 document that
             * was produced by KNIME by just replacing the PMML version and
             * namespace. */
        if (PMMLUtils.isOldKNIMEPMML(xmlDoc) || PMMLUtils.is4_1PMML(xmlDoc)) {
            try {
                String updatedPMML = PMMLUtils.getUpdatedVersionAndNamespace(xmlDoc);
                /* Parse the modified document and assign it to a
                     * PMMLDocument.*/
                m_pmmlDoc = PMMLDocument.Factory.parse(updatedPMML);
            } catch (Exception e) {
                throw new RuntimeException("Parsing of PMML v 3.x/4.0 document failed.", e);
            }
            LOGGER.info("KNIME produced PMML 3.x/4.0  converted to PMML 4.1.");
        } else {
            throw new RuntimeException("Parsing of PMML v 3.x/4.0 document failed.");
        }
    }
    m_spec = spec;
}
Also used : NonClosableInputStream(org.knime.core.data.util.NonClosableInputStream) XmlObject(org.apache.xmlbeans.XmlObject) PMMLDocument(org.dmg.pmml.PMMLDocument) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

PMMLDocument (org.dmg.pmml.PMMLDocument)19 PMML (org.dmg.pmml.PMMLDocument.PMML)7 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)6 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)5 RuleSet (org.dmg.pmml.RuleSetDocument.RuleSet)4 RuleSetModel (org.dmg.pmml.RuleSetModelDocument.RuleSetModel)4 DataRow (org.knime.core.data.DataRow)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 PortObject (org.knime.core.node.port.PortObject)3 Document (org.w3c.dom.Document)3 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 XmlException (org.apache.xmlbeans.XmlException)2 XmlObject (org.apache.xmlbeans.XmlObject)2 DataCell (org.knime.core.data.DataCell)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 StringValue (org.knime.core.data.StringValue)2