Search in sources :

Example 71 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class DataColumnSpecFilterPMMLNodeModel method createPMMLOut.

private PMMLPortObject createPMMLOut(final PMMLPortObject pmmlIn, final DataTableSpec outSpec, final FilterResult res) throws XmlException {
    StringBuffer warningBuffer = new StringBuffer();
    if (pmmlIn == null) {
        return new PMMLPortObject(createPMMLSpec(null, outSpec, res));
    } else {
        PMMLDocument pmmldoc;
        try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
            pmmldoc = PMMLDocument.Factory.parse(supplier.get());
        }
        // Inspect models to check if they use any excluded columns
        List<PMMLModelWrapper> models = PMMLModelWrapper.getModelListFromPMMLDocument(pmmldoc);
        for (PMMLModelWrapper model : models) {
            MiningSchema ms = model.getMiningSchema();
            for (MiningField mf : ms.getMiningFieldList()) {
                if (isExcluded(mf.getName(), res)) {
                    if (warningBuffer.length() != 0) {
                        warningBuffer.append("\n");
                    }
                    warningBuffer.append(model.getModelType().name() + " uses excluded column " + mf.getName());
                }
            }
        }
        ArrayList<String> warningFields = new ArrayList<String>();
        PMML pmml = pmmldoc.getPMML();
        // Now check the transformations if they exist
        if (pmml.getTransformationDictionary() != null) {
            for (DerivedField df : pmml.getTransformationDictionary().getDerivedFieldList()) {
                FieldRef fr = df.getFieldRef();
                if (fr != null && isExcluded(fr.getField(), res)) {
                    warningFields.add(fr.getField());
                }
                Aggregate a = df.getAggregate();
                if (a != null && isExcluded(a.getField(), res)) {
                    warningFields.add(a.getField());
                }
                Apply ap = df.getApply();
                if (ap != null) {
                    for (FieldRef fieldRef : ap.getFieldRefList()) {
                        if (isExcluded(fieldRef.getField(), res)) {
                            warningFields.add(fieldRef.getField());
                            break;
                        }
                    }
                }
                Discretize d = df.getDiscretize();
                if (d != null && isExcluded(d.getField(), res)) {
                    warningFields.add(d.getField());
                }
                MapValues mv = df.getMapValues();
                if (mv != null) {
                    for (FieldColumnPair fcp : mv.getFieldColumnPairList()) {
                        if (isExcluded(fcp.getField(), res)) {
                            warningFields.add(fcp.getField());
                        }
                    }
                }
                NormContinuous nc = df.getNormContinuous();
                if (nc != null && isExcluded(nc.getField(), res)) {
                    warningFields.add(nc.getField());
                }
                NormDiscrete nd = df.getNormDiscrete();
                if (nd != null && isExcluded(nd.getField(), res)) {
                    warningFields.add(nd.getField());
                }
            }
        }
        DataDictionary dict = pmml.getDataDictionary();
        List<DataField> fields = dict.getDataFieldList();
        // Apply filter to spec
        int numFields = 0;
        for (int i = fields.size() - 1; i >= 0; i--) {
            if (isExcluded(fields.get(i).getName(), res)) {
                dict.removeDataField(i);
            } else {
                numFields++;
            }
        }
        dict.setNumberOfFields(new BigInteger(Integer.toString(numFields)));
        pmml.setDataDictionary(dict);
        pmmldoc.setPMML(pmml);
        // generate warnings and set as warning message
        for (String s : warningFields) {
            if (warningBuffer.length() != 0) {
                warningBuffer.append("\n");
            }
            warningBuffer.append("Transformation dictionary uses excluded column " + s);
        }
        if (warningBuffer.length() > 0) {
            setWarningMessage(warningBuffer.toString().trim());
        }
        PMMLPortObject outport = null;
        try {
            outport = new PMMLPortObject(createPMMLSpec(pmmlIn.getSpec(), outSpec, res), pmmldoc);
        } catch (IllegalArgumentException e) {
            if (res.getIncludes().length == 0) {
                throw new IllegalArgumentException("Excluding all columns produces invalid PMML", e);
            } else {
                throw e;
            }
        }
        return outport;
    }
}
Also used : MiningField(org.dmg.pmml.MiningFieldDocument.MiningField) NormContinuous(org.dmg.pmml.NormContinuousDocument.NormContinuous) Apply(org.dmg.pmml.ApplyDocument.Apply) ArrayList(java.util.ArrayList) FieldColumnPair(org.dmg.pmml.FieldColumnPairDocument.FieldColumnPair) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) MapValues(org.dmg.pmml.MapValuesDocument.MapValues) Discretize(org.dmg.pmml.DiscretizeDocument.Discretize) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) PMMLModelWrapper(org.knime.core.node.port.pmml.PMMLModelWrapper) NormDiscrete(org.dmg.pmml.NormDiscreteDocument.NormDiscrete) MiningSchema(org.dmg.pmml.MiningSchemaDocument.MiningSchema) DataField(org.dmg.pmml.DataFieldDocument.DataField) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMML(org.dmg.pmml.PMMLDocument.PMML) BigInteger(java.math.BigInteger) PMMLDocument(org.dmg.pmml.PMMLDocument) Aggregate(org.dmg.pmml.AggregateDocument.Aggregate) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 72 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class BinnerNodeModel method createPMMLModel.

/**
 * Creates the pmml port object.
 * @param the in-port pmml object. Can be <code>null</code> (optional in-port)
 */
private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec, final DataTableSpec outSpec) {
    PMMLBinningTranslator trans = new PMMLBinningTranslator(m_columnToBins, m_columnToAppended, new DerivedFieldMapper(inPMMLPort));
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(inPMMLPort, outSpec);
    PMMLPortObject outPMMLPort = new PMMLPortObject(pmmlSpecCreator.createSpec(), inPMMLPort, inSpec);
    outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
    return outPMMLPort;
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 73 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class BinnerNodeModel method createStreamableOperator.

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

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            ColumnRearranger colre = createColumnRearranger((DataTableSpec) inSpecs[0]);
            colre.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
            if (m_pmmlOutEnabled) {
                // handle the optional PMML in port (can be null)
                PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) ((PortObjectInput) inputs[1]).getPortObject() : null;
                PMMLPortObject outPMMLPort = createPMMLModel(inPMMLPort, (DataTableSpec) inSpecs[0], colre.createSpec());
                ((PortObjectOutput) outputs[1]).setPortObject(outPMMLPort);
            }
        }
    };
}
Also used : ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) PortObjectOutput(org.knime.core.node.streamable.PortObjectOutput)

Example 74 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class MissingValueApplyNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[DATA_PORT_IDX];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    PMMLPortObject pmmlIn = (PMMLPortObject) inData[PMML_PORT_IDX];
    MissingCellReplacingDataTable mvTable;
    try (LockedSupplier<Document> supplier = pmmlIn.getPMMLValue().getDocumentSupplier()) {
        mvTable = new MissingCellReplacingDataTable(inSpec, PMMLDocument.Factory.parse(supplier.get()));
    }
    // Calculate the statistics
    mvTable.init(inTable, exec.createSubExecutionContext(0.5));
    long rowCounter = 0;
    final long numOfRows = inTable.size();
    DataContainer container = exec.createDataContainer(mvTable.getDataTableSpec());
    for (DataRow row : mvTable) {
        exec.checkCanceled();
        if (row != null) {
            exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows + " (\"" + row.getKey() + "\")");
            container.addRowToTable(row);
        } else {
            exec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows);
        }
    }
    container.close();
    // Collect warning messages
    String warnings = mvTable.finish();
    // Handle the warnings
    if (warnings.length() > 0) {
        setWarningMessage(warnings);
    }
    return new PortObject[] { (BufferedDataTable) container.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataContainer(org.knime.core.data.container.DataContainer) MissingCellReplacingDataTable(org.knime.base.node.preproc.pmml.missingval.MissingCellReplacingDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLDocument(org.dmg.pmml.PMMLDocument) Document(org.w3c.dom.Document) DataRow(org.knime.core.data.DataRow) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject)

Example 75 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class Many2OneCol2PMMLNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    BufferedDataTable inData = (BufferedDataTable) inObjects[0];
    AbstractMany2OneCellFactory cellFactory = getCellFactory(inData.getDataTableSpec());
    BufferedDataTable outData = exec.createColumnRearrangeTable(inData, createRearranger(inData.getDataTableSpec(), cellFactory), exec);
    if (m_pmmlOutEnabled) {
        if (IncludeMethod.valueOf(m_includeMethod.getStringValue()) == IncludeMethod.RegExpPattern) {
            setWarningMessage("Regular Expressions are not supported in PMML. " + "The generated PMML document is invalid.");
        }
        // the optional PMML in port (can be null)
        PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inObjects[1] : null;
        /*
             PMMLOne2ManyTranslator trans = new PMMLOne2ManyTranslator(
                    cellFactory.getColumnMapping(),
                    new DerivedFieldMapper(inPMMLPort));
            */
        int[] sourceColIndices = cellFactory.getIncludedColIndices();
        String[] sourceColNames = new String[sourceColIndices.length];
        for (int i = 0; i < sourceColIndices.length; i++) {
            sourceColNames[i] = inData.getDataTableSpec().getColumnSpec(sourceColIndices[i]).getName();
        }
        PMMLMany2OneTranslator trans = new PMMLMany2OneTranslator(cellFactory.getAppendedColumnName(), sourceColNames, IncludeMethod.valueOf(m_includeMethod.getStringValue()));
        PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, outData.getDataTableSpec());
        PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
        outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
        return new PortObject[] { outData, outPMMLPort };
    } else {
        return new PortObject[] { outData };
    }
}
Also used : AbstractMany2OneCellFactory(org.knime.base.node.preproc.columntrans2.AbstractMany2OneCellFactory) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Aggregations

PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)90 BufferedDataTable (org.knime.core.node.BufferedDataTable)53 DataTableSpec (org.knime.core.data.DataTableSpec)50 PortObject (org.knime.core.node.port.PortObject)47 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)35 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)25 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)21 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)18 ExecutionContext (org.knime.core.node.ExecutionContext)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)12 DataRow (org.knime.core.data.DataRow)12 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)11 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)11 Node (org.w3c.dom.Node)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)9 IOException (java.io.IOException)8 DataCell (org.knime.core.data.DataCell)8 PMMLDocument (org.dmg.pmml.PMMLDocument)7 PMMLGeneralRegressionTranslator (org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionTranslator)7 StreamableFunction (org.knime.core.node.streamable.StreamableFunction)7