Search in sources :

Example 11 with DataCell

use of org.knime.core.data.DataCell 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 12 with DataCell

use of org.knime.core.data.DataCell 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 13 with DataCell

use of org.knime.core.data.DataCell 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)

Example 14 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class StringManipulationVariableNodeModel method calculate.

/**
 * @throws CompilationFailedException
 * @throws InstantiationException
 * @throws Exception
 */
private void calculate() throws InvalidSettingsException, CompilationFailedException, InstantiationException {
    if (m_settings == null || m_settings.getExpression() == null) {
        throw new InvalidSettingsException("No expression has been set.");
    }
    JavaScriptingSettings settings = m_settings.createJavaScriptingSettings();
    settings.setInputAndCompile(new DataTableSpec());
    // calculate the result
    ColumnCalculator cc = new ColumnCalculator(settings, this);
    DataCell calculate = null;
    try {
        calculate = cc.calculate(new DefaultRow(new RowKey(""), new DataCell[] {}));
    } catch (NoSuchElementException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    String newVariableName;
    Map<String, FlowVariable> inputFlowVariables = getAvailableInputFlowVariables();
    if (m_settings.isReplace()) {
        newVariableName = m_settings.getColName();
        CheckUtils.checkSettingNotNull(inputFlowVariables.get(newVariableName), "Can't replace input variable '%s' -- it does not exist in the input", newVariableName);
    } else {
        newVariableName = new UniqueNameGenerator(inputFlowVariables.keySet()).newName(m_settings.getColName());
    }
    // convert and push result as flow variable
    CheckUtils.checkSetting(!calculate.isMissing(), "Calculation returned missing value");
    Class<? extends DataCell> cellType = calculate.getClass();
    if (cellType.equals(IntCell.class)) {
        pushFlowVariableInt(newVariableName, ((IntCell) calculate).getIntValue());
    } else if (cellType.equals(DoubleCell.class)) {
        pushFlowVariableDouble(newVariableName, ((DoubleCell) calculate).getDoubleValue());
    } else if (cellType.equals(StringCell.class)) {
        pushFlowVariableString(newVariableName, ((StringCell) calculate).getStringValue());
    } else {
        throw new RuntimeException("Invalid variable class: " + cellType);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) JavaScriptingSettings(org.knime.ext.sun.nodes.script.settings.JavaScriptingSettings) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ColumnCalculator(org.knime.ext.sun.nodes.script.calculator.ColumnCalculator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 15 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class ExpressionFactory method or.

/**
 * {@inheritDoc}
 */
@Override
public Expression or(final List<Expression> boolExpressions) {
    final boolean allIsConstant = checkBooleansAndConstant(boolExpressions);
    return new Expression.Base(boolExpressions) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataType getOutputType() {
            return BooleanCell.TYPE;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public List<DataType> getInputArgs() {
            return Collections.singletonList(BooleanCell.TYPE);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ExpressionValue evaluate(final DataRow row, final VariableProvider provider) {
            DataCell ret = BooleanCell.FALSE;
            Map<String, Map<String, String>> matchedObjects = new HashMap<String, Map<String, String>>();
            for (Expression boolExpression : boolExpressions) {
                ExpressionValue v = boolExpression.evaluate(row, provider);
                DataCell cell = v.getValue();
                assert !cell.isMissing();
                if (cell instanceof BooleanValue) {
                    BooleanValue bool = (BooleanValue) cell;
                    if (!bool.getBooleanValue()) {
                        matchedObjects = Util.mergeObjects(matchedObjects, v.getMatchedObjects());
                    } else {
                        return new ExpressionValue(BooleanCell.TRUE, matchedObjects);
                    }
                } else if (cell.isMissing()) {
                    ret = DataType.getMissingCell();
                } else {
                    throw new IllegalStateException("Not boolean: " + v.getValue());
                }
            }
            return new ExpressionValue(ret, matchedObjects);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isConstant() {
            return allIsConstant;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String toString() {
            return "or(" + boolExpressions + ")";
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ASTType getTreeType() {
            return ASTType.Or;
        }
    };
}
Also used : HashMap(java.util.HashMap) DataRow(org.knime.core.data.DataRow) BooleanValue(org.knime.core.data.BooleanValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DataCell (org.knime.core.data.DataCell)780 DataRow (org.knime.core.data.DataRow)268 DataTableSpec (org.knime.core.data.DataTableSpec)175 DataColumnSpec (org.knime.core.data.DataColumnSpec)170 DefaultRow (org.knime.core.data.def.DefaultRow)169 ArrayList (java.util.ArrayList)141 StringCell (org.knime.core.data.def.StringCell)131 DoubleCell (org.knime.core.data.def.DoubleCell)129 DoubleValue (org.knime.core.data.DoubleValue)111 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)109 DataType (org.knime.core.data.DataType)97 RowKey (org.knime.core.data.RowKey)94 BufferedDataTable (org.knime.core.node.BufferedDataTable)93 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)91 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)84 LinkedHashMap (java.util.LinkedHashMap)81 IntCell (org.knime.core.data.def.IntCell)79 HashMap (java.util.HashMap)60 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)57 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)56