Search in sources :

Example 11 with PMMLDocument

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

the class PMMLTreeModelWrapper method createPMMLDocument.

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

Example 12 with PMMLDocument

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

the class PMMLRuleSetModelWrapper method createPMMLDocument.

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

Example 13 with PMMLDocument

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

the class PMMLKNNTranslator method exportTo.

/**
 * {@inheritDoc}
 */
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
    LinkedHashMap<Integer, String> columnNames = new LinkedHashMap<Integer, String>();
    DataTableSpec tSpec = m_table.getDataTableSpec();
    // Find learning columns and store them in the map for later
    for (String lc : m_includes) {
        columnNames.put(tSpec.findColumnIndex(lc), "col" + columnNames.size());
    }
    // Create initial XML elements
    PMML pmml = pmmlDoc.getPMML();
    NearestNeighborModel knn = pmml.addNewNearestNeighborModel();
    PMMLMiningSchemaTranslator.writeMiningSchema(spec, knn);
    knn.setAlgorithmName("K-Nearest Neighbors");
    knn.setFunctionName(org.dmg.pmml.MININGFUNCTION.CLASSIFICATION);
    knn.setNumberOfNeighbors(BigInteger.valueOf(m_numNeighbors));
    // Only euclidean is supported so far
    ComparisonMeasure cm = knn.addNewComparisonMeasure();
    cm.addNewEuclidean();
    // KNNInputs is a list of the fields used for determining the distance
    KNNInputs inputs = knn.addNewKNNInputs();
    for (int i : columnNames.keySet()) {
        KNNInput input = inputs.addNewKNNInput();
        String col = tSpec.getColumnSpec(i).getName();
        input.setField(col);
        input.setCompareFunction(COMPAREFUNCTION.ABS_DIFF);
    }
    TrainingInstances ti = knn.addNewTrainingInstances();
    // Here we create a mapping from column name to name of the XML element for the column's values
    InstanceFields instanceFields = ti.addNewInstanceFields();
    for (int i : columnNames.keySet()) {
        InstanceField instanceField = instanceFields.addNewInstanceField();
        String col = tSpec.getColumnSpec(i).getName();
        instanceField.setField(col);
        instanceField.setColumn(columnNames.get(i));
    }
    int targetIdx = tSpec.findColumnIndex(spec.getTargetFields().get(0));
    InstanceField target = instanceFields.addNewInstanceField();
    target.setField(spec.getTargetFields().get(0));
    target.setColumn("target");
    // The inline table holds the actual data.
    // We use the map we created in the beginning to determine the element xml-element-names
    InlineTable it = ti.addNewInlineTable();
    Document doc = it.getDomNode().getOwnerDocument();
    int counter = 0;
    for (DataRow row : m_table) {
        // Stop if we have reached the maximum number of records
        if (m_maxRecords > -1 && ++counter > m_maxRecords) {
            break;
        }
        Row inlineRow = it.addNewRow();
        Element rowNode = (Element) inlineRow.getDomNode();
        for (int col : columnNames.keySet()) {
            Element field = doc.createElementNS(PMMLUtils.getPMMLCurrentVersionNamespace(), columnNames.get(col));
            field.appendChild(doc.createTextNode(row.getCell(col).toString()));
            rowNode.appendChild(field);
        }
        Element targetField = doc.createElementNS(PMMLUtils.getPMMLCurrentVersionNamespace(), "target");
        targetField.appendChild(doc.createTextNode(row.getCell(targetIdx).toString()));
        rowNode.appendChild(targetField);
    }
    return NearestNeighborModel.type;
}
Also used : NearestNeighborModel(org.dmg.pmml.NearestNeighborModelDocument.NearestNeighborModel) InlineTable(org.dmg.pmml.InlineTableDocument.InlineTable) DataTableSpec(org.knime.core.data.DataTableSpec) InstanceFields(org.dmg.pmml.InstanceFieldsDocument.InstanceFields) Element(org.w3c.dom.Element) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) DataRow(org.knime.core.data.DataRow) LinkedHashMap(java.util.LinkedHashMap) KNNInput(org.dmg.pmml.KNNInputDocument.KNNInput) BigInteger(java.math.BigInteger) TrainingInstances(org.dmg.pmml.TrainingInstancesDocument.TrainingInstances) KNNInputs(org.dmg.pmml.KNNInputsDocument.KNNInputs) InstanceField(org.dmg.pmml.InstanceFieldDocument.InstanceField) PMML(org.dmg.pmml.PMMLDocument.PMML) DataRow(org.knime.core.data.DataRow) Row(org.dmg.pmml.RowDocument.Row) ComparisonMeasure(org.dmg.pmml.ComparisonMeasureDocument.ComparisonMeasure)

Example 14 with PMMLDocument

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

the class XML2PMMLNodeModel method createColRearranger.

private ColumnRearranger createColRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    if (m_xmlColumnName.getStringValue() == null) {
        guessDefaultXMLColumn(spec);
    }
    String xmlColName = m_xmlColumnName.getStringValue();
    String newColName = m_newColumnName.getStringValue();
    final int colIndex = spec.findColumnIndex(xmlColName);
    CheckUtils.checkSetting(colIndex >= 0, "Column: '%s' does not exist anymore.", xmlColName);
    final DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    CheckUtils.checkSetting(colSpec.getType().isCompatible(StringValue.class), "Selected column '%s' is not string/xml-compatible", xmlColName);
    DataColumnSpecCreator colSpecCreator;
    if (newColName != null && !m_replaceColumn.getBooleanValue()) {
        String newName = DataTableSpec.getUniqueColumnName(spec, newColName);
        colSpecCreator = new DataColumnSpecCreator(newName, PMMLCell.TYPE);
    } else {
        colSpecCreator = new DataColumnSpecCreator(colSpec);
        colSpecCreator.setType(PMMLCell.TYPE);
        colSpecCreator.removeAllHandlers();
        colSpecCreator.setDomain(null);
    }
    DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    CellFactory fac = new SingleCellFactory(outColumnSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIndex);
            if (cell.isMissing()) {
                return DataType.getMissingCell();
            } else {
                PMMLDocument pmmlDoc = null;
                String failure = null;
                XmlObject xmlDoc;
                try (LockedSupplier<Document> supplier = ((XMLValue<Document>) cell).getDocumentSupplier()) {
                    xmlDoc = XmlObject.Factory.parse(supplier.get().cloneNode(true));
                    if (xmlDoc instanceof PMMLDocument) {
                        pmmlDoc = (PMMLDocument) xmlDoc;
                    } else if (PMMLUtils.isOldKNIMEPMML(xmlDoc) || PMMLUtils.is4_1PMML(xmlDoc)) {
                        String updatedPMML = PMMLUtils.getUpdatedVersionAndNamespace(xmlDoc);
                        /* Parse the modified document and assign it to a
                                 * PMMLDocument.*/
                        pmmlDoc = PMMLDocument.Factory.parse(updatedPMML);
                    } else {
                        failure = "No valid PMML v 3.x/4.0/4.1 document";
                    }
                } catch (XmlException e) {
                    if (!m_failOnInvalid.getBooleanValue()) {
                        LOGGER.error("Invalid PMML in row " + row.getKey() + ": " + e.getMessage(), e);
                    }
                    failure = e.getMessage();
                }
                if (failure != null) {
                    m_failCounter.incrementAndGet();
                    if (m_failOnInvalid.getBooleanValue()) {
                        throw new RuntimeException("Invalid PMML in row " + row.getKey() + ": " + failure);
                    } else {
                        return new MissingCell(failure);
                    }
                } else {
                    try {
                        return PMMLCellFactory.create(pmmlDoc.toString());
                    } catch (Exception e) {
                        return new MissingCell(e.getMessage());
                    }
                }
            }
        }
    };
    if (m_replaceColumn.getBooleanValue()) {
        rearranger.replace(fac, colIndex);
    } else {
        rearranger.append(fac);
    }
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) MissingCell(org.knime.core.data.MissingCell) XmlException(org.apache.xmlbeans.XmlException) DataCell(org.knime.core.data.DataCell) XmlObject(org.apache.xmlbeans.XmlObject) PMMLDocument(org.dmg.pmml.PMMLDocument) XMLValue(org.knime.core.data.xml.XMLValue) StringValue(org.knime.core.data.StringValue) PMMLCellFactory(org.knime.core.data.xml.PMMLCellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 15 with PMMLDocument

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

the class PMMLNaiveBayesModelWrapper method createPMMLDocument.

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

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