use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class AutoBinner method execute.
/**
* Determine bins.
*
* @param data the input data
* @param exec the execution context
* @return the operation with the discretisation information
* @throws Exception ...
*/
public PMMLPreprocDiscretize execute(final BufferedDataTable data, final ExecutionContext exec) throws Exception {
final DataTableSpec spec = data.getDataTableSpec();
// determine intervals
if (m_settings.getMethod().equals(Method.fixedNumber)) {
if (m_settings.getEqualityMethod().equals(EqualityMethod.width)) {
BufferedDataTable inData = calcDomainBoundsIfNeccessary(data, exec.createSubExecutionContext(0.9), Arrays.asList(m_included));
init(inData.getDataTableSpec());
Map<String, double[]> edgesMap = new HashMap<String, double[]>();
for (String target : m_included) {
DataTableSpec inSpec = inData.getDataTableSpec();
DataColumnSpec targetCol = inSpec.getColumnSpec(target);
// bounds of the domain
double min = ((DoubleValue) targetCol.getDomain().getLowerBound()).getDoubleValue();
double max = ((DoubleValue) targetCol.getDomain().getUpperBound()).getDoubleValue();
// the edges of the bins
int binCount = m_settings.getBinCount();
double[] edges = calculateBounds(binCount, min, max);
if (m_settings.getIntegerBounds()) {
edges = toIntegerBoundaries(edges);
}
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else {
// EqualityMethod.equalCount
Map<String, double[]> edgesMap = new HashMap<String, double[]>();
for (String target : m_included) {
int colIndex = data.getDataTableSpec().findColumnIndex(target);
List<Double> values = new ArrayList<Double>();
for (DataRow row : data) {
if (!row.getCell(colIndex).isMissing()) {
values.add(((DoubleValue) row.getCell(colIndex)).getDoubleValue());
}
}
edgesMap.put(target, findEdgesForEqualCount(values, m_settings.getBinCount()));
}
return createDisretizeOp(edgesMap);
}
} else if (m_settings.getMethod().equals(Method.sampleQuantiles)) {
init(spec);
Map<String, double[]> edgesMap = new LinkedHashMap<String, double[]>();
final int colCount = m_included.length;
// contains all numeric columns if include all is set!
for (String target : m_included) {
exec.setMessage("Calculating quantiles (column \"" + target + "\")");
ExecutionContext colSortContext = exec.createSubExecutionContext(0.7 / colCount);
ExecutionContext colCalcContext = exec.createSubExecutionContext(0.3 / colCount);
ColumnRearranger singleRearranger = new ColumnRearranger(spec);
singleRearranger.keepOnly(target);
BufferedDataTable singleColSorted = colSortContext.createColumnRearrangeTable(data, singleRearranger, colSortContext);
SortedTable sorted = new SortedTable(singleColSorted, Collections.singletonList(target), new boolean[] { true }, colSortContext);
colSortContext.setProgress(1.0);
double[] edges = createEdgesFromQuantiles(sorted.getBufferedDataTable(), colCalcContext, m_settings.getSampleQuantiles());
colCalcContext.setProgress(1.0);
exec.clearTable(singleColSorted);
if (m_settings.getIntegerBounds()) {
edges = toIntegerBoundaries(edges);
}
edgesMap.put(target, edges);
}
return createDisretizeOp(edgesMap);
} else {
throw new IllegalStateException("Unknown binning method.");
}
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class FilterApplyRowSplitterNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final DataTableRowInput in = (DataTableRowInput) inputs[0];
final RowOutput out1 = (RowOutput) outputs[0];
final RowOutput out2 = (RowOutput) outputs[1];
PortObjectInput portObjectInput = (PortObjectInput) inputs[1];
DataTableSpec filterSpec = portObjectInput == null ? in.getDataTableSpec() : ((FilterDefinitionHandlerPortObject) portObjectInput.getPortObject()).getSpec();
FilterApplyRowSplitterNodeModel.this.execute(in, out1, out2, filterSpec, exec, -1);
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class NominalValueRowFilterNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@SuppressWarnings("null")
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
RowInput in = (RowInput) inputs[0];
RowOutput match = (RowOutput) outputs[0];
RowOutput miss = m_splitter ? (RowOutput) outputs[1] : null;
try {
long rowIdx = -1;
DataRow row;
while ((row = in.poll()) != null) {
rowIdx++;
exec.setProgress("Adding row " + rowIdx + ".");
exec.checkCanceled();
if (matches(row)) {
match.push(row);
} else if (m_splitter) {
miss.push(row);
}
}
} finally {
match.close();
if (m_splitter) {
miss.close();
}
}
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class ColumnRenameRegexNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
// just pass through since only the column names have been changed
RowInput rowInput = (RowInput) inputs[0];
RowOutput rowOutput = (RowOutput) outputs[0];
DataRow row;
while ((row = rowInput.poll()) != null) {
rowOutput.push(row);
}
rowInput.close();
rowOutput.close();
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class ExtractMissingValueCauseNodeModel method createStreamableOperator.
/**
* {@inheritDoc}
*/
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
return new StreamableOperator() {
@Override
public StreamableOperatorInternals saveInternals() {
return null;
}
@Override
public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
final RowInput in = (RowInput) inputs[0];
final RowOutput out = (RowOutput) outputs[0];
ExtractMissingValueCauseNodeModel.this.execute(in, out, exec, -1);
}
};
}
Aggregations