Search in sources :

Example 1 with IRowFilter

use of org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter 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)

Example 2 with IRowFilter

use of org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter in project knime-core by knime.

the class RowFilter2PortNodeModel method loadOrValidateSettingsFrom.

private void loadOrValidateSettingsFrom(final NodeSettingsRO settings, final boolean verifyOnly) throws InvalidSettingsException {
    IRowFilter tmpFilter = null;
    if (settings.containsKey(CFGFILTER)) {
        NodeSettingsRO filterCfg = settings.getNodeSettings(CFGFILTER);
        tmpFilter = RowFilterFactory.createRowFilter(filterCfg);
    } else {
        throw new InvalidSettingsException("Row Filter config contains no" + " row filter.");
    }
    if (verifyOnly) {
        return;
    }
    // take over settings
    m_rowFilter = tmpFilter;
    return;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)

Example 3 with IRowFilter

use of org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter in project knime-core by knime.

the class AbstractSamplingNodeModel method getSamplingRowFilter.

/**
 * Method to be used in the execute method to determine the row filter for
 * the sampling.
 *
 * @param in the data table from the inport
 * @param exec the execution monitor to check for cancelation
 * @return a row filter for sampling according to current settings
 * @throws CanceledExecutionException if exec request canceling
 * @throws InvalidSettingsException if current settings are invalid
 * @since 3.0
 */
protected IRowFilter getSamplingRowFilter(final BufferedDataTable in, final ExecutionMonitor exec) throws CanceledExecutionException, InvalidSettingsException {
    Random rand;
    if (m_settings.samplingMethod().equals(SamplingMethods.Random) || m_settings.samplingMethod().equals(SamplingMethods.Stratified)) {
        if (m_settings.seed() != null) {
            rand = new Random(m_settings.seed());
        } else {
            long seed = System.nanoTime() ^ ((hashCode() << 32) + (m_settings.hashCode()));
            rand = new Random(seed);
            getLogger().debug("Using random seed " + seed);
        }
    } else {
        rand = null;
    }
    int rowCount;
    if (m_settings.countMethod().equals(SamplingNodeSettings.CountMethods.Relative)) {
        rowCount = (int) (m_settings.fraction() * in.getRowCount());
    } else {
        rowCount = m_settings.count();
    }
    IRowFilter rowFilter;
    if (m_settings.samplingMethod().equals(SamplingMethods.Random)) {
        rowFilter = Sampler.createSampleFilter(in, rowCount, rand, exec);
    } else if (m_settings.samplingMethod().equals(SamplingMethods.Stratified)) {
        rowFilter = new StratifiedSamplingRowFilter(in, m_settings.classColumn(), rowCount, rand, exec);
    } else if (m_settings.samplingMethod().equals(SamplingMethods.Linear)) {
        rowFilter = new LinearSamplingRowFilter(in.getRowCount(), rowCount);
    } else {
        rowFilter = Sampler.createRangeFilter(rowCount);
    }
    return rowFilter;
}
Also used : Random(java.util.Random) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)

Example 4 with IRowFilter

use of org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter in project knime-core by knime.

the class SamplingNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable in = inData[0];
    // he following line does not need the exec monitor. It's
    // only used when the table is traversed in order to count the rows.
    // This is done only if "in" does not support getRowCount().
    // But the argument in the execute method surely does!
    IRowFilter filter = getSamplingRowFilter(in, exec);
    BufferedDataContainer container = exec.createDataContainer(in.getDataTableSpec());
    try {
        int count = 0;
        RowFilterIterator it = new RowFilterIterator(in, filter, exec);
        while (it.hasNext()) {
            DataRow row = it.next();
            exec.setMessage("Adding row " + count + " (\"" + row.getKey() + "\")");
            count++;
            container.addRowToTable(row);
        }
    } catch (RowFilterIterator.RuntimeCanceledExecutionException rce) {
        throw rce.getCause();
    } finally {
        container.close();
    }
    BufferedDataTable out = container.getTable();
    if (filter instanceof StratifiedSamplingRowFilter) {
        int classCount = ((StratifiedSamplingRowFilter) filter).getClassCount();
        if (classCount > out.getRowCount()) {
            setWarningMessage("Class column contains more classes (" + classCount + ") than sampled rows (" + out.getRowCount() + ")");
        }
    }
    return new BufferedDataTable[] { out };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowFilterIterator(org.knime.base.node.preproc.filter.row.RowFilterIterator) BufferedDataTable(org.knime.core.node.BufferedDataTable) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter) DataRow(org.knime.core.data.DataRow)

Example 5 with IRowFilter

use of org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter in project knime-core by knime.

the class RowFilterNodeModel method loadOrValidateSettingsFrom.

private void loadOrValidateSettingsFrom(final NodeSettingsRO settings, final boolean verifyOnly) throws InvalidSettingsException {
    IRowFilter tmpFilter = null;
    if (settings.containsKey(CFGFILTER)) {
        NodeSettingsRO filterCfg = settings.getNodeSettings(CFGFILTER);
        // because we don't know what type of filter is in the config we
        // must ask the factory to figure it out for us (actually the type
        // is also saved in a valid config). When we save row filters they
        // will (hopefully!!) add their type to the config.
        tmpFilter = RowFilterFactory.createRowFilter(filterCfg);
    } else {
        throw new InvalidSettingsException("Row Filter config contains no" + " row filter.");
    }
    // if we got so far settings are valid.
    if (verifyOnly) {
        return;
    }
    // take over settings
    m_rowFilter = tmpFilter;
    return;
}
Also used : InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) IRowFilter(org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)

Aggregations

IRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.IRowFilter)7 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 DataRow (org.knime.core.data.DataRow)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 Random (java.util.Random)1 RowFilterIterator (org.knime.base.node.preproc.filter.row.RowFilterIterator)1 AttrValueRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.AttrValueRowFilter)1 ColValFilterOldObsolete (org.knime.base.node.preproc.filter.row.rowfilter.ColValFilterOldObsolete)1 EndOfTableException (org.knime.base.node.preproc.filter.row.rowfilter.EndOfTableException)1 IncludeFromNowOn (org.knime.base.node.preproc.filter.row.rowfilter.IncludeFromNowOn)1 RowIDRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.RowIDRowFilter)1 RowNoRowFilter (org.knime.base.node.preproc.filter.row.rowfilter.RowNoRowFilter)1 StratifiedSamplingRowFilter (org.knime.base.node.preproc.sample.StratifiedSamplingRowFilter)1 NotConfigurableException (org.knime.core.node.NotConfigurableException)1