Search in sources :

Example 11 with ColumnRearranger

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

the class TimeMissValueNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec spec = inSpecs[0];
    Map<String, TSMissVHandler> nameToMVHandler = checkInputAndCreateHandlerMap(spec);
    Map<String, Integer> nameToInt = findColumns(spec, nameToMVHandler);
    ColumnRearranger c = createColumnRearranger(nameToMVHandler, nameToInt, spec);
    return new DataTableSpec[] { c.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) TSMissVHandler(org.knime.timeseries.node.timemissvaluehandler.tshandler.TSMissVHandler)

Example 12 with ColumnRearranger

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

the class RuleEngineNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    try {
        m_rowCount = -1;
        final List<Rule> rules = parseRules(inSpecs[0], RuleNodeSettings.RuleEngine);
        ColumnRearranger crea = createRearranger(inSpecs[0], rules, -1, true);
        return new DataTableSpec[] { crea.createSpec() };
    } catch (ParseException ex) {
        throw new InvalidSettingsException(ex.getMessage(), ex);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ParseException(java.text.ParseException)

Example 13 with ColumnRearranger

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

the class RuleEngineNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules, final long rowCount, final boolean updateColSpec) throws InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn();
    final DataType outType = computeOutputType(rules, RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, outType);
    if (updateColSpec) {
        // only update in configure, execute will compute properly
        updateColSpec(rules, outType, colSpecCreator, this);
    }
    DataColumnSpec cs = colSpecCreator.createSpec();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto cellFactory = new VariableProvider.SingleCellFactoryProto(cs) {

        private long m_rowIndex = -1L;

        @Override
        public DataCell getCell(final DataRow row) {
            m_rowIndex++;
            return getRulesOutcome(outType, row, rules, disallowLongOutputForCompatibility, this);
        }

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineNodeModel.this.readVariable(name, type);
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            return (int) m_rowIndex;
        }

        @Override
        public long getRowIndexLong() {
            return m_rowIndex;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return (int) rowCount;
        }

        @Override
        public long getRowCountLong() {
            return rowCount;
        }
    };
    if (m_settings.isAppendColumn()) {
        crea.append(cellFactory);
    } else {
        crea.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return crea;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataType(org.knime.core.data.DataType) DataRow(org.knime.core.data.DataRow)

Example 14 with ColumnRearranger

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

the class PMMLRuleEditorNodeModel method createRearranger.

/**
 * Creates the {@link ColumnRearranger} that can compute the new column.
 *
 * @param tableSpec The spec of the input table.
 * @param ruleSet The {@link RuleSet} xml object where the rules should be added.
 * @param parser The parser for the rules.
 * @return The {@link ColumnRearranger}.
 * @throws ParseException Problem during parsing.
 * @throws InvalidSettingsException if settings are invalid
 */
private ColumnRearranger createRearranger(final DataTableSpec tableSpec, final RuleSet ruleSet, final PMMLRuleParser parser) throws ParseException, InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    Set<String> outcomes = new LinkedHashSet<String>();
    List<DataType> outcomeTypes = new ArrayList<DataType>();
    int line = 0;
    final List<Pair<PMMLPredicate, Expression>> rules = new ArrayList<Pair<PMMLPredicate, Expression>>();
    for (String ruleText : m_settings.rules()) {
        ++line;
        if (RuleSupport.isComment(ruleText)) {
            continue;
        }
        try {
            ParseState state = new ParseState(ruleText);
            PMMLPredicate expression = parser.parseBooleanExpression(state);
            SimpleRule simpleRule = ruleSet.addNewSimpleRule();
            setCondition(simpleRule, expression);
            state.skipWS();
            state.consumeText("=>");
            state.skipWS();
            Expression outcome = parser.parseOutcomeOperand(state, null);
            // Only constants are allowed in the outcomes.
            assert outcome.isConstant() : outcome;
            rules.add(new Pair<PMMLPredicate, Expression>(expression, outcome));
            outcomeTypes.add(outcome.getOutputType());
            simpleRule.setScore(outcome.toString());
            // simpleRule.setConfidence(confidenceForRule(simpleRule, line, ruleText));
            simpleRule.setWeight(weightForRule(simpleRule, line, ruleText));
            outcomes.add(simpleRule.getScore());
        } catch (ParseException e) {
            throw Util.addContext(e, ruleText, line);
        }
    }
    DataType outcomeType = RuleEngineNodeModel.computeOutputType(outcomeTypes, true);
    ColumnRearranger rearranger = new ColumnRearranger(tableSpec);
    DataColumnSpecCreator specProto = new DataColumnSpecCreator(m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(tableSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn(), outcomeType);
    specProto.setDomain(new DataColumnDomainCreator(toCells(outcomes, outcomeType)).createDomain());
    SingleCellFactory cellFactory = new SingleCellFactory(true, specProto.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            for (Pair<PMMLPredicate, Expression> pair : rules) {
                if (pair.getFirst().evaluate(row, tableSpec) == Boolean.TRUE) {
                    return pair.getSecond().evaluate(row, null).getValue();
                }
            }
            return DataType.getMissingCell();
        }
    };
    if (m_settings.isAppendColumn()) {
        rearranger.append(cellFactory);
    } else {
        rearranger.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return rearranger;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) ParseState(org.knime.base.node.rules.engine.BaseRuleParser.ParseState) DataRow(org.knime.core.data.DataRow) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Expression(org.knime.base.node.rules.engine.Expression) DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) Pair(org.knime.core.util.Pair)

Example 15 with ColumnRearranger

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

the class PMMLRuleEditorNodeModel method createStreamableOperator.

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

        private ColumnRearranger m_rearrangerx;

        private PMMLPortObject m_portObject;

        {
            try {
                final PMMLDocument doc = PMMLDocument.Factory.newInstance();
                final PMML pmml = doc.addNewPMML();
                RuleSetModel ruleSetModel = pmml.addNewRuleSetModel();
                RuleSet ruleSet = ruleSetModel.addNewRuleSet();
                PMMLRuleParser parser = new PMMLRuleParser(tableSpec, getAvailableInputFlowVariables());
                m_rearrangerx = createRearranger(tableSpec, ruleSet, parser);
            } catch (ParseException e) {
                throw new InvalidSettingsException(e);
            }
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            m_rearrangerx.createStreamableFunction(0, 0).runFinal(inputs, outputs, exec);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void loadInternals(final StreamableOperatorInternals internals) {
            super.loadInternals(internals);
            m_portObject = ((StreamInternalForPMMLPortObject) internals).getObject();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return createInitialStreamableOperatorInternals().setObject(m_portObject);
        }
    };
}
Also used : RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) DataTableSpec(org.knime.core.data.DataTableSpec) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMML(org.dmg.pmml.PMMLDocument.PMML) PMMLDocument(org.dmg.pmml.PMMLDocument) ParseException(java.text.ParseException)

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