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;
}
Aggregations