Search in sources :

Example 1 with StratifiedSamplingRowFilter

use of org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter in project knime-core by knime.

the class PartitionNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws CanceledExecutionException, Exception {
    BufferedDataTable in = inData[0];
    BufferedDataTable[] outs = new BufferedDataTable[2];
    IRowFilter filter = getSamplingRowFilter(in, exec);
    BufferedDataContainer firstOutCont = exec.createDataContainer(in.getDataTableSpec());
    BufferedDataContainer secondOutCont = exec.createDataContainer(in.getDataTableSpec());
    // floating point op. below
    final double rowCount = in.size();
    // one of the flags will be set if one of the exceptions below
    // is thrown.
    boolean putRestInOut1 = false;
    boolean putRestInOut2 = false;
    try {
        int count = 0;
        for (DataRow row : in) {
            boolean matches = putRestInOut1;
            try {
                // conditional check, will call "matches" only if necessary
                matches |= (!putRestInOut2 && filter.matches(row, count));
            } catch (IncludeFromNowOn icf) {
                assert !putRestInOut2;
                putRestInOut1 = true;
                matches = true;
            } catch (EndOfTableException ete) {
                assert !putRestInOut1;
                putRestInOut2 = true;
                matches = false;
            }
            if (matches) {
                firstOutCont.addRowToTable(row);
            } else {
                secondOutCont.addRowToTable(row);
            }
            exec.setProgress(count / rowCount, "Processed row " + count + " (\"" + row.getKey() + "\")");
            exec.checkCanceled();
            count++;
        }
    } finally {
        firstOutCont.close();
        secondOutCont.close();
    }
    outs[0] = firstOutCont.getTable();
    outs[1] = secondOutCont.getTable();
    if (filter instanceof StratifiedSamplingRowFilter) {
        int classCount = ((StratifiedSamplingRowFilter) filter).getClassCount();
        if (classCount > outs[0].size()) {
            setWarningMessage("Class column contains more classes (" + classCount + ") than sampled rows (" + outs[0].size() + ")");
        }
    }
    return outs;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) BufferedDataTable(org.knime.core.node.BufferedDataTable) IncludeFromNowOn(org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn) EndOfTableException(org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter) StratifiedSamplingRowFilter(org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter) DataRow(org.knime.core.data.DataRow)

Aggregations

EndOfTableException (org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException)1 IRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)1 IncludeFromNowOn (org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn)1 StratifiedSamplingRowFilter (org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter)1 DataRow (org.knime.core.data.DataRow)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1