Search in sources :

Example 31 with PMMLPortObjectSpecCreator

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

the class StringToNumberNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    StringBuilder warnings = new StringBuilder();
    BufferedDataTable inData = (BufferedDataTable) inObjects[0];
    DataTableSpec inSpec = inData.getDataTableSpec();
    // find indices to work on.
    List<String> inclcols = m_inclCols.getIncludeList();
    BufferedDataTable resultTable = null;
    if (inclcols.size() == 0) {
        // nothing to convert, let's return the input table.
        resultTable = inData;
        setWarningMessage("No columns selected," + " returning input DataTable.");
    } else {
        int[] indices = findColumnIndices(inSpec);
        ConverterFactory converterFac = new ConverterFactory(indices, inSpec, m_parseType);
        ColumnRearranger colre = new ColumnRearranger(inSpec);
        colre.replace(converterFac, indices);
        resultTable = exec.createColumnRearrangeTable(inData, colre, exec);
        String errorMessage = converterFac.getErrorMessage();
        if (errorMessage.length() > 0) {
            warnings.append("Problems occurred, see Console messages.\n");
        }
        if (warnings.length() > 0) {
            LOGGER.warn(errorMessage);
            setWarningMessage(warnings.toString());
        }
    }
    // the optional PMML in port (can be null)
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inObjects[1] : null;
    PMMLStringConversionTranslator trans = new PMMLStringConversionTranslator(m_inclCols.getIncludeList(), m_parseType, new DerivedFieldMapper(inPMMLPort));
    PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, inSpec);
    PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort, inSpec);
    outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
    return new PortObject[] { resultTable, outPMMLPort };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) PMMLStringConversionTranslator(org.knime.base.node.preproc.pmml.PMMLStringConversionTranslator) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 32 with PMMLPortObjectSpecCreator

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

the class PMMLImport method init.

/**
 * @param pmmlDoc
 */
private void init(final PMMLDocument pmmlDoc) {
    PMMLDataDictionaryTranslator dictTrans = new PMMLDataDictionaryTranslator();
    dictTrans.initializeFrom(pmmlDoc);
    DataTableSpec tableSpec = dictTrans.getDataTableSpec();
    List<String> activeDerivedFields = dictTrans.getActiveDerivedFields();
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(tableSpec);
    PMMLMiningSchemaTranslator miningTrans = new PMMLMiningSchemaTranslator();
    miningTrans.initializeFrom(pmmlDoc);
    Set<String> activeFields = new LinkedHashSet<String>();
    List<String> miningFields = miningTrans.getActiveFields();
    /* If we have a model all active fields of the data dictionary
         * are passed through the mining schema. */
    activeFields.addAll(miningFields);
    activeFields.addAll(activeDerivedFields);
    specCreator.setLearningColsNames(new LinkedList<String>(activeFields));
    specCreator.addPreprocColNames(activeDerivedFields);
    specCreator.setTargetColsNames(miningTrans.getTargetFields());
    PMMLPortObjectSpec portObjectSpec = specCreator.createSpec();
    m_portObject = new PMMLPortObject(portObjectSpec, pmmlDoc);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PMMLMiningSchemaTranslator(org.knime.core.node.port.pmml.PMMLMiningSchemaTranslator) DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLDataDictionaryTranslator(org.knime.core.node.port.pmml.PMMLDataDictionaryTranslator) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 33 with PMMLPortObjectSpecCreator

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

the class PMMLReaderNodeModel method createPMMLOutSpec.

/**
 * @param inModelSpec the spec of the optional PMML in port
 * @param parsedSpec the spec of the parsed PMML document
 * @return the merged {@link PMMLPortObjectSpec}
 */
private PMMLPortObjectSpec createPMMLOutSpec(final PMMLPortObjectSpec inModelSpec, final PMMLPortObjectSpec parsedSpec) throws InvalidSettingsException {
    PMMLPortObjectSpec outSpec = parsedSpec;
    if (inModelSpec != null) {
        List<String> preprocCols = inModelSpec.getPreprocessingFields();
        for (String colName : preprocCols) {
            if (!parsedSpec.getActiveFields().contains(colName)) {
                throw new InvalidSettingsException("Preprocessing column " + colName + " is not contained in the read PMML file.");
            }
        }
        PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(parsedSpec);
        creator.addPreprocColNames(preprocCols);
        outSpec = creator.createSpec();
    }
    return outSpec;
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 34 with PMMLPortObjectSpecCreator

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

the class DBNumericBinnerNodeModel method createPMMLPortObject.

private PMMLPortObject createPMMLPortObject(final DataTableSpec inDataTableSpec, final DataTableSpec outDataTableSpec) {
    PMMLPortObjectSpec initPMMLSpec = new PMMLPortObjectSpecCreator(outDataTableSpec).createSpec();
    PMMLPortObject initPMMLPortObject = new PMMLPortObject(initPMMLSpec, null, outDataTableSpec);
    PMMLBinningTranslator pmmlBinningTranslator = new PMMLBinningTranslator(m_columnToBins, m_columnToAppended, new DerivedFieldMapper(initPMMLPortObject));
    PMMLPortObject outPMMLPortObject = new PMMLPortObject(initPMMLSpec, initPMMLPortObject, inDataTableSpec);
    outPMMLPortObject.addGlobalTransformations(pmmlBinningTranslator.exportToTransDict());
    return outPMMLPortObject;
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 35 with PMMLPortObjectSpecCreator

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

the class DBAutoBinner method translate.

/**
 * This method translates a {@link PMMLPreprocDiscretize} object into {@link PMMLPortObject}.
 *
 * @param pmmlDiscretize {@link PMMLPreprocDiscretize} object
 * @param dataTableSpec {@link DataTableSpec} if incoming {@link BufferedDataTable}
 * @return a {@link PMMLPortObject} containing required parameters for binning operation
 */
public static PMMLPortObject translate(final PMMLPreprocDiscretize pmmlDiscretize, final DataTableSpec dataTableSpec) {
    final Map<String, Bin[]> columnToBins = new HashMap<>();
    final Map<String, String> columnToAppend = new HashMap<>();
    List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
    for (String replacedColumnName : replacedColumnNames) {
        PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
        List<PMMLDiscretizeBin> bins = discretize.getBins();
        String originalColumnName = discretize.getField();
        if (replacedColumnName.equals(originalColumnName)) {
            // wenn replaced, dann nicht anhängen
            columnToAppend.put(originalColumnName, null);
        } else {
            // nicht replaced -> anhängen
            columnToAppend.put(originalColumnName, replacedColumnName);
        }
        NumericBin[] numericBin = new NumericBin[bins.size()];
        int counter = 0;
        for (PMMLDiscretizeBin bin : bins) {
            String binName = bin.getBinValue();
            List<PMMLInterval> intervals = bin.getIntervals();
            boolean leftOpen = false;
            boolean rightOpen = false;
            double leftMargin = 0;
            double rightMargin = 0;
            // always returns only one interval
            for (PMMLInterval interval : intervals) {
                Closure closure = interval.getClosure();
                switch(closure) {
                    case openClosed:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                    case openOpen:
                        leftOpen = true;
                        rightOpen = true;
                        break;
                    case closedOpen:
                        leftOpen = false;
                        rightOpen = true;
                    case closedClosed:
                        leftOpen = false;
                        rightOpen = false;
                        break;
                    default:
                        leftOpen = true;
                        rightOpen = false;
                        break;
                }
                leftMargin = interval.getLeftMargin();
                rightMargin = interval.getRightMargin();
            }
            numericBin[counter] = new NumericBin(binName, leftOpen, leftMargin, rightOpen, rightMargin);
            counter++;
        }
        columnToBins.put(originalColumnName, numericBin);
    }
    // ColumnRearranger createColReg = createColReg(dataTableSpec, columnToBins, columnToAppended);
    DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
    PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
    PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
    TransformationDictionary exportToTransDict = trans.exportToTransDict();
    pmmlPortObject.addGlobalTransformations(exportToTransDict);
    return pmmlPortObject;
}
Also used : NumericBin(org.knime.base.node.preproc.pmml.binner.NumericBin) DataTableSpec(org.knime.core.data.DataTableSpec) Closure(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval.Closure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) PMMLDiscretizeBin(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretizeBin) PMMLInterval(org.knime.base.node.preproc.autobinner.pmml.PMMLInterval) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.base.node.preproc.pmml.binner.PMMLBinningTranslator) PMMLDiscretize(org.knime.base.node.preproc.autobinner.pmml.PMMLDiscretize) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Aggregations

PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)62 DataTableSpec (org.knime.core.data.DataTableSpec)35 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)24 DataColumnSpec (org.knime.core.data.DataColumnSpec)21 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)21 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)19 BufferedDataTable (org.knime.core.node.BufferedDataTable)15 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)14 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)13 PortObject (org.knime.core.node.port.PortObject)13 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)11 ArrayList (java.util.ArrayList)10 DoubleValue (org.knime.core.data.DoubleValue)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)9 LinkedList (java.util.LinkedList)6 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)6 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 DataCell (org.knime.core.data.DataCell)3 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)3