Search in sources :

Example 91 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class RegressionPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    PMMLPortObjectSpec regModelSpec = (PMMLPortObjectSpec) inSpecs[0];
    DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
    if (dataSpec == null || regModelSpec == null) {
        throw new InvalidSettingsException("No input specification available");
    }
    for (String learnColName : regModelSpec.getLearningFields()) {
        if (!dataSpec.containsName(learnColName)) {
            throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input data to be predicted");
        }
    }
    ColumnRearranger rearranger = createRearranger(dataSpec, regModelSpec, null);
    DataTableSpec outSpec = rearranger.createSpec();
    return new DataTableSpec[] { outSpec };
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 92 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class RegressionPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    PMMLPortObject regModel = (PMMLPortObject) inData[0];
    List<Node> models = regModel.getPMMLValue().getModels(PMMLModelType.RegressionModel);
    if (models.isEmpty()) {
        String msg = "No Regression Model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLRegressionTranslator trans = new PMMLRegressionTranslator();
    regModel.initializeModelTranslator(trans);
    BufferedDataTable data = (BufferedDataTable) inData[1];
    DataTableSpec spec = data.getDataTableSpec();
    ColumnRearranger c = createRearranger(spec, regModel.getSpec(), trans);
    BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
    return new BufferedDataTable[] { out };
}
Also used : PMMLRegressionTranslator(org.knime.base.node.mine.regression.PMMLRegressionTranslator) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Node(org.w3c.dom.Node) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 93 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class RegressionPredictorNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final PMMLPortObjectSpec regModelSpec, final PMMLRegressionTranslator regModel) throws InvalidSettingsException {
    if (regModelSpec == null) {
        throw new InvalidSettingsException("No input");
    }
    // exclude last (response column)
    String targetCol = "Response";
    for (String s : regModelSpec.getTargetFields()) {
        targetCol = s;
        break;
    }
    final List<String> learnFields;
    if (regModel != null) {
        RegressionTable regTable = regModel.getRegressionTable();
        learnFields = new ArrayList<String>();
        for (NumericPredictor p : regTable.getVariables()) {
            learnFields.add(p.getName());
        }
    } else {
        learnFields = new ArrayList<String>(regModelSpec.getLearningFields());
    }
    final int[] colIndices = new int[learnFields.size()];
    int k = 0;
    for (String learnCol : learnFields) {
        int index = inSpec.findColumnIndex(learnCol);
        if (index < 0) {
            throw new InvalidSettingsException("Missing column for " + "regressor variable : \"" + learnCol + "\"");
        }
        DataColumnSpec regressor = inSpec.getColumnSpec(index);
        String name = regressor.getName();
        DataColumnSpec col = inSpec.getColumnSpec(index);
        if (!col.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Incompatible type of " + "column \"" + name + "\": " + col.getType());
        }
        colIndices[k++] = index;
    }
    // try to use some smart naming scheme for the append column
    String oldName = targetCol;
    if (inSpec.containsName(oldName) && !oldName.toLowerCase().endsWith("(prediction)")) {
        oldName = oldName + " (prediction)";
    }
    String newColName = DataTableSpec.getUniqueColumnName(inSpec, oldName);
    DataColumnSpec newCol = new DataColumnSpecCreator(newColName, DoubleCell.TYPE).createSpec();
    SingleCellFactory fac = new SingleCellFactory(newCol) {

        @Override
        public DataCell getCell(final DataRow row) {
            RegressionTable t = regModel.getRegressionTable();
            int j = 0;
            double result = t.getIntercept();
            for (NumericPredictor p : t.getVariables()) {
                DataCell c = row.getCell(colIndices[j++]);
                if (c.isMissing()) {
                    return DataType.getMissingCell();
                }
                double v = ((DoubleValue) c).getDoubleValue();
                if (p.getExponent() != 1) {
                    v = Math.pow(v, p.getExponent());
                }
                result += p.getCoefficient() * v;
            }
            return new DoubleCell(result);
        }
    };
    ColumnRearranger c = new ColumnRearranger(inSpec);
    c.append(fac);
    return c;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) NumericPredictor(org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor) DataRow(org.knime.core.data.DataRow) RegressionTable(org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 94 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class OptionsPanel method getMissingColSpecName.

@SuppressWarnings("null")
private static String getMissingColSpecName(final DataTableSpec spec, final String[] includedNames, final String[] excludedNames) {
    ColumnRearranger r = new ColumnRearranger(spec);
    // remove columns we know from the include list
    for (String colName : includedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    // remove columns we know from the exclude list
    for (String colName : excludedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    DataTableSpec tableSpecWithMissing = r.createSpec();
    DataColumnSpec formerTargetSpec = null;
    // were either in the include or exclude list
    for (DataColumnSpec colSpec : tableSpecWithMissing) {
        DataType colType = colSpec.getType();
        if (colType.isCompatible(NominalValue.class) || colType.isCompatible(DoubleValue.class)) {
            formerTargetSpec = colSpec;
            break;
        }
    }
    assert formerTargetSpec != null : "The former target spec is no longer part of the table, please check.";
    return formerTargetSpec.getName();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) DataType(org.knime.core.data.DataType)

Example 95 with ColumnRearranger

use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.

the class SimpleStreamableFunctionNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec in = inSpecs[0];
    ColumnRearranger r = createColumnRearranger(in);
    DataTableSpec out = r.createSpec();
    return new DataTableSpec[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger)

Aggregations

ColumnRearranger (org.knime.core.data.container.ColumnRearranger)393 DataTableSpec (org.knime.core.data.DataTableSpec)221 BufferedDataTable (org.knime.core.node.BufferedDataTable)153 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)116 DataRow (org.knime.core.data.DataRow)79 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)69 DataCell (org.knime.core.data.DataCell)63 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)55 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)49 ExecutionContext (org.knime.core.node.ExecutionContext)46 PortObject (org.knime.core.node.port.PortObject)39 ArrayList (java.util.ArrayList)38 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)36 DataType (org.knime.core.data.DataType)34 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)32 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)27 DoubleValue (org.knime.core.data.DoubleValue)26 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)26 TreeEnsembleModelPortObjectSpec (org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec)25