Search in sources :

Example 36 with PMMLPortObjectSpecCreator

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

the class One2ManyColPMMLNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inDataSpec = (DataTableSpec) inSpecs[0];
    m_appendOrgColName = false;
    if (m_includedColumns.getIncludeList() == null || m_includedColumns.getIncludeList().size() <= 0) {
        setWarningMessage("No columns to transfrom selected. Will have no effect!");
    }
    // check if the values are present in the current spec
    if (m_includedColumns.getIncludeList() != null && m_includedColumns.getIncludeList().size() > 0) {
        checkColumnsSpecs(inDataSpec);
    }
    CellFactory cellFactory = new One2ManyCellFactory(inDataSpec, m_includedColumns.getIncludeList(), m_appendOrgColName);
    ColumnRearranger rearranger = createRearranger(inDataSpec, cellFactory);
    if (m_pmmlEnabled) {
        PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
        PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inDataSpec);
        return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
    } else {
        return new PortObjectSpec[] { rearranger.createSpec() };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) One2ManyCellFactory(org.knime.base.node.preproc.columnTrans.One2ManyCellFactory) One2ManyCellFactory(org.knime.base.node.preproc.columnTrans.One2ManyCellFactory) CellFactory(org.knime.core.data.container.CellFactory) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 37 with PMMLPortObjectSpecCreator

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

the class One2ManyColPMMLNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    BufferedDataTable inData = (BufferedDataTable) inObjects[0];
    DataTableSpec dts = inData.getDataTableSpec();
    checkColumnsSpecs(dts);
    One2ManyCellFactory cellFactory = new One2ManyCellFactory(dts, m_includedColumns.getIncludeList(), m_appendOrgColName);
    BufferedDataTable outData = exec.createColumnRearrangeTable(inData, createRearranger(dts, cellFactory), exec);
    if (m_pmmlEnabled) {
        // the optional PMML in port (can be null)
        PMMLPortObject inPMMLPort = (PMMLPortObject) inObjects[1];
        PMMLOne2ManyTranslator trans = new PMMLOne2ManyTranslator(cellFactory.getColumnMapping(), new DerivedFieldMapper(inPMMLPort));
        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 : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) One2ManyCellFactory(org.knime.base.node.preproc.columnTrans.One2ManyCellFactory) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 38 with PMMLPortObjectSpecCreator

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

the class TreeEnsembleModelPortObject method createDecisionTreePMMLPortObject.

public PMMLPortObject createDecisionTreePMMLPortObject(final int modelIndex) {
    DataTableSpec attributeLearnSpec = m_ensembleModel.getLearnAttributeSpec(m_spec.getLearnTableSpec());
    DataColumnSpec targetSpec = m_spec.getTargetColumn();
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(new DataTableSpec(attributeLearnSpec, new DataTableSpec(targetSpec)));
    try {
        pmmlSpecCreator.setLearningCols(attributeLearnSpec);
    } catch (InvalidSettingsException e) {
        // (as of KNIME v2.5.1)
        throw new IllegalStateException(e);
    }
    pmmlSpecCreator.setTargetCol(targetSpec);
    PMMLPortObjectSpec pmmlSpec = pmmlSpecCreator.createSpec();
    PMMLPortObject portObject = new PMMLPortObject(pmmlSpec);
    TreeModelClassification model = m_ensembleModel.getTreeModelClassification(modelIndex);
    portObject.addModelTranslater(new TreeModelPMMLTranslator(model));
    return portObject;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 39 with PMMLPortObjectSpecCreator

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

the class LogRegCoordinator method init.

/**
 * Initialize instance and check if settings are consistent.
 */
private void init(final DataTableSpec inSpec, final Set<String> exclude) throws InvalidSettingsException {
    List<String> inputCols = new ArrayList<String>();
    FilterResult includedColumns = m_settings.getIncludedColumns().applyTo(inSpec);
    for (String column : includedColumns.getIncludes()) {
        inputCols.add(column);
    }
    inputCols.remove(m_settings.getTargetColumn());
    if (inputCols.isEmpty()) {
        throw new InvalidSettingsException("At least one column must " + "be included.");
    }
    DataColumnSpec targetColSpec = null;
    List<DataColumnSpec> regressorColSpecs = new ArrayList<DataColumnSpec>();
    // Auto configuration when target is not set
    if (null == m_settings.getTargetColumn() && m_settings.getIncludedColumns().applyTo(inSpec).getExcludes().length == 0) {
        for (int i = 0; i < inSpec.getNumColumns(); i++) {
            DataColumnSpec colSpec = inSpec.getColumnSpec(i);
            String colName = colSpec.getName();
            inputCols.remove(colName);
            if (colSpec.getType().isCompatible(NominalValue.class)) {
                m_settings.setTargetColumn(colName);
            }
        }
        // when there is no column with nominal data
        if (null == m_settings.getTargetColumn()) {
            throw new InvalidSettingsException("No column in " + "spec compatible to \"NominalValue\".");
        }
    }
    // remove all columns that should not be used
    inputCols.removeAll(exclude);
    m_specialColumns = new LinkedList<>();
    for (int i = 0; i < inSpec.getNumColumns(); i++) {
        DataColumnSpec colSpec = inSpec.getColumnSpec(i);
        String colName = colSpec.getName();
        final DataType type = colSpec.getType();
        if (m_settings.getTargetColumn().equals(colName)) {
            if (type.isCompatible(NominalValue.class)) {
                targetColSpec = colSpec;
            } else {
                throw new InvalidSettingsException("Type of column \"" + colName + "\" is not nominal.");
            }
        } else if (inputCols.contains(colName)) {
            if (type.isCompatible(DoubleValue.class) || type.isCompatible(NominalValue.class)) {
                regressorColSpecs.add(colSpec);
            } else if (type.isCompatible(BitVectorValue.class) || type.isCompatible(ByteVectorValue.class) || (type.isCollectionType() && type.getCollectionElementType().isCompatible(DoubleValue.class))) {
                m_specialColumns.add(colSpec);
                // We change the table spec later to encode it as a string.
                regressorColSpecs.add(new DataColumnSpecCreator(colSpec.getName(), StringCell.TYPE).createSpec());
            } else {
                throw new InvalidSettingsException("Type of column \"" + colName + "\" is not one of the allowed types, " + "which are numeric or nomial.");
            }
        }
    }
    if (null != targetColSpec) {
        // Check if target has at least two categories.
        final Set<DataCell> targetValues = targetColSpec.getDomain().getValues();
        if (targetValues != null && targetValues.size() < 2) {
            throw new InvalidSettingsException("The target column \"" + targetColSpec.getName() + "\" has one value, only. " + "At least two target categories are expected.");
        }
        String[] learnerCols = new String[regressorColSpecs.size() + 1];
        for (int i = 0; i < regressorColSpecs.size(); i++) {
            learnerCols[i] = regressorColSpecs.get(i).getName();
        }
        learnerCols[learnerCols.length - 1] = targetColSpec.getName();
        final DataColumnSpec[] updatedSpecs = new DataColumnSpec[inSpec.getNumColumns()];
        for (int i = updatedSpecs.length; i-- > 0; ) {
            final DataColumnSpec columnSpec = inSpec.getColumnSpec(i);
            final DataType type = columnSpec.getType();
            if (type.isCompatible(BitVectorValue.class) || type.isCompatible(ByteVectorValue.class)) {
                final DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(columnSpec.getName(), StringCell.TYPE);
                colSpecCreator.setProperties(new DataColumnProperties(Collections.singletonMap("realType", type.isCompatible(BitVectorValue.class) ? "BitVector" : "ByteVector")));
                updatedSpecs[i] = colSpecCreator.createSpec();
            } else {
                updatedSpecs[i] = columnSpec;
            }
        }
        DataTableSpec updated = new DataTableSpec(updatedSpecs);
        PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(updated);
        creator.setTargetCols(Arrays.asList(targetColSpec));
        creator.setLearningCols(regressorColSpecs);
        // creator.addPreprocColNames(m_specialColumns.stream().flatMap(spec -> ));
        m_pmmlOutSpec = creator.createSpec();
    } else {
        throw new InvalidSettingsException("The target is " + "not in the input.");
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DataColumnProperties(org.knime.core.data.DataColumnProperties) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 40 with PMMLPortObjectSpecCreator

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

the class NumberToStringNodeModel 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(inData.getSpec());
        ConverterFactory converterFac = new ConverterFactory(indices, inSpec);
        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) {
            getLogger().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(), StringCell.TYPE, 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)

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