Search in sources :

Example 1 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class RuleSetToTable method execute.

/**
 * Performs the conversion.
 *
 * @param exec An {@link ExecutionContext}.
 * @param pmmlPo The input {@link PMMLPortObject}.
 * @return The created {@link BufferedDataTable}.
 * @throws CanceledExecutionException Execition was cancelled.
 * @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
 */
public BufferedDataTable execute(final ExecutionContext exec, final PMMLPortObject pmmlPo) throws CanceledExecutionException, InvalidSettingsException {
    // TODO should the rule selection method be an output flow variable?
    if (pmmlPo.getPMMLValue().getModels(PMMLModelType.RuleSetModel).size() != 1) {
        throw new InvalidSettingsException("Only a single RuleSet model is supported.");
    }
    PMMLRuleTranslator ruleTranslator = new PMMLRuleTranslator();
    pmmlPo.initializeModelTranslator(ruleTranslator);
    List<Rule> rules = ruleTranslator.getRules();
    final DataTableSpec confSpec = configure(pmmlPo.getSpec());
    final List<String> scoreValues = new ArrayList<>();
    final DataTableSpec properSpec = confSpec != null ? confSpec : properSpec(rules, scoreValues);
    BufferedDataContainer container = exec.createDataContainer(properSpec);
    List<DataColumnSpec> targetCols = pmmlPo.getSpec().getTargetCols();
    DataType outcomeType = targetCols.get(0).getType();
    long idx = 0L;
    int rulesSize = rules.size();
    Map<String, DataType> types = new LinkedHashMap<>();
    for (DataColumnSpec col : pmmlPo.getSpec().getLearningCols()) {
        types.put(col.getName(), col.getType());
    }
    for (Rule rule : rules) {
        exec.checkCanceled();
        exec.setProgress(1.0 * idx++ / rulesSize);
        container.addRowToTable(new DefaultRow(RowKey.createRowKey(idx), createRow(rule, outcomeType, types, scoreValues)));
    }
    container.close();
    return container.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ArrayList(java.util.ArrayList) PMMLRuleTranslator(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator) LinkedHashMap(java.util.LinkedHashMap) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) Rule(org.knime.base.node.rules.engine.pmml.PMMLRuleTranslator.Rule) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 2 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class MovingAverageNodeModel method addMissingCells.

/**
 * adds a fixed number of missing cells to the given row.
 * or replaces the  original ones with missings.
 * @param colindexex
 */
private DataRow addMissingCells(final int nrMiss, final int[] colindexex, final DataRow oldrow) {
    DataCell[] cell = new DataCell[oldrow.getNumCells() + nrMiss];
    int counter = 0;
    for (DataCell c : oldrow) {
        cell[counter] = c;
        counter++;
    }
    if (nrMiss <= 0) {
        // we replace the colindexex with missings.
        for (int i : colindexex) {
            cell[i] = DataType.getMissingCell();
        }
    } else {
        for (; counter < cell.length; counter++) {
            cell[counter] = DataType.getMissingCell();
        }
    }
    return new DefaultRow(oldrow.getKey(), cell);
}
Also used : DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 3 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class MovingAggregationTableFactory method getBackwardTable.

private BufferedDataTable getBackwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = row.getCell(colIdx);
        }
        final boolean windowFull = window.size() >= m_windowLength;
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (windowFull || m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        if (windowFull) {
            // remove the first row only when the window is full
            // not during the missing value handling phase!
            window.removeFirst();
        }
        dc.addRowToTable(new DefaultRow(row.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 4 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class MovingAggregationTableFactory method getCenterTable.

private BufferedDataTable getCenterTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    int centerIdx = -1;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        // we have to subtract 1 since the indexing of the array starts with 0
        centerIdx = window.size() - m_windowLength / 2 - 1;
        if (centerIdx < 0) {
            // we have to fill the window first
            continue;
        }
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        final DataRow centerRow = window.get(centerIdx);
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = centerRow.getCell(colIdx);
        }
        final boolean windowFull = window.size() >= m_windowLength;
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (windowFull || m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
        // not during the missing value handling phase!
        if (windowFull) {
            window.removeFirst();
        }
    }
    // we have to handle the remaining rows in the window
    while (window.size() > centerIdx) {
        exec.checkCanceled();
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        final DataRow centerRow = window.get(centerIdx);
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = centerRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        window.removeFirst();
        dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 5 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class MovingAggregationTableFactory method getForwardTable.

private BufferedDataTable getForwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
    final int rowCount = table.getRowCount();
    final LinkedList<DataRow> window = new LinkedList<>();
    int rowIdx = 0;
    for (final DataRow row : table) {
        exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
        exec.checkCanceled();
        window.add(row);
        final boolean windowFull = window.size() >= m_windowLength;
        if (!windowFull) {
            // we have to fill the window first
            continue;
        }
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        final DataRow firstRow = window.getFirst();
        int idx = 0;
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = firstRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            final int colIdx = m_aggrColIdxs[i];
            final AggregationOperator op = m_ops[i];
            for (final DataRow windowRow : window) {
                op.compute(windowRow, colIdx);
            }
            cells[idx++] = op.getResult();
            op.reset();
        }
        dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
        // remove the first row only when the window is full
        // not during the missing value handling phase!
        window.removeFirst();
    }
    // we have to handle the remaining rows in the window
    while (!window.isEmpty()) {
        exec.checkCanceled();
        final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
        int idx = 0;
        final DataRow firstRow = window.getFirst();
        // handle the retained columns
        for (final int colIdx : m_cols2KeepIdxs) {
            cells[idx++] = firstRow.getCell(colIdx);
        }
        for (int i = 0, length = m_ops.length; i < length; i++) {
            if (m_handleMissings) {
                final int colIdx = m_aggrColIdxs[i];
                final AggregationOperator op = m_ops[i];
                for (final DataRow windowRow : window) {
                    op.compute(windowRow, colIdx);
                }
                cells[idx++] = op.getResult();
                op.reset();
            } else {
                // the window is not yet full return missing cells
                cells[idx++] = DataType.getMissingCell();
            }
        }
        window.removeFirst();
        dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : AggregationOperator(org.knime.base.data.aggregation.AggregationOperator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Aggregations

DefaultRow (org.knime.core.data.def.DefaultRow)207 DataCell (org.knime.core.data.DataCell)165 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)94 DataTableSpec (org.knime.core.data.DataTableSpec)92 DataRow (org.knime.core.data.DataRow)88 RowKey (org.knime.core.data.RowKey)80 DoubleCell (org.knime.core.data.def.DoubleCell)66 StringCell (org.knime.core.data.def.StringCell)65 BufferedDataTable (org.knime.core.node.BufferedDataTable)56 IntCell (org.knime.core.data.def.IntCell)46 ArrayList (java.util.ArrayList)26 DataType (org.knime.core.data.DataType)26 DataColumnSpec (org.knime.core.data.DataColumnSpec)22 DataContainer (org.knime.core.data.container.DataContainer)21 HashSet (java.util.HashSet)18 LinkedHashMap (java.util.LinkedHashMap)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 LinkedHashSet (java.util.LinkedHashSet)14 DoubleValue (org.knime.core.data.DoubleValue)14 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)14