use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class FilterApplyNodeModel 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 out = (RowOutput) outputs[0];
PortObjectInput portObjectInput = (PortObjectInput) inputs[1];
DataTableSpec filterSpec = portObjectInput == null ? in.getDataTableSpec() : ((FilterDefinitionHandlerPortObject) portObjectInput.getPortObject()).getSpec();
FilterApplyNodeModel.this.execute(in, out, filterSpec, exec, -1);
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class MissingValueHandlerNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inData[0];
DataTableSpec inSpec = inTable.getDataTableSpec();
MissingCellReplacingDataTable mvTable = new MissingCellReplacingDataTable(inSpec, m_settings);
// Calculate the statistics
exec.setMessage("Calculating statistics");
mvTable.init(inTable, exec.createSubExecutionContext(0.5));
long rowCounter = 0;
final long numOfRows = inTable.size();
DataContainer container = exec.createDataContainer(mvTable.getDataTableSpec());
ExecutionContext tableSubExec = exec.createSubExecutionContext(0.4);
exec.setMessage("Replacing missing values");
for (DataRow row : mvTable) {
tableSubExec.checkCanceled();
if (row != null) {
tableSubExec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows + " (\"" + row.getKey() + "\")");
container.addRowToTable(row);
} else {
tableSubExec.setProgress(++rowCounter / (double) numOfRows, "Processed row " + rowCounter + "/" + numOfRows);
}
}
container.close();
// Collect warning messages
String warnings = mvTable.finish();
// Handle the warnings
if (warnings.length() > 0) {
setWarningMessage(warnings);
}
exec.setMessage("Generating PMML");
// Init PMML output port
PMMLPortObject pmmlPort = new PMMLPortObject(new PMMLPortObjectSpecCreator(inSpec).createSpec());
pmmlPort.addModelTranslater(mvTable.getPMMLTranslator());
return new PortObject[] { (BufferedDataTable) container.getTable(), pmmlPort };
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class ColumnAggregatorNodeModel method createStreamableOperator.
/* ================= STREAMING ================= */
/**
* {@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 DataTableSpec origSpec = (DataTableSpec) inSpecs[0];
final FilterResult filterResult = m_aggregationCols.applyTo(origSpec);
final List<String> selectedCols = Arrays.asList(filterResult.getIncludes());
final GlobalSettings globalSettings = GlobalSettings.builder().setFileStoreFactory(FileStoreFactory.createWorkflowFileStoreFactory(exec)).setGroupColNames(selectedCols).setMaxUniqueValues(m_maxUniqueValues.getIntValue()).setValueDelimiter(getDefaultValueDelimiter()).setDataTableSpec(origSpec).setNoOfRows(-1).setAggregationContext(AggregationContext.COLUMN_AGGREGATION).build();
final AggregationCellFactory cellFactory = new AggregationCellFactory(origSpec, selectedCols, globalSettings, m_methods);
final ColumnRearranger cr = createRearranger(origSpec, cellFactory);
cr.createStreamableFunction().runFinal(inputs, outputs, exec);
}
};
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class CorrelationComputeNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
final BufferedDataTable in = (BufferedDataTable) inData[0];
final DataTableSpec inSpec = in.getDataTableSpec();
ColumnRearranger filteredTableRearranger = new ColumnRearranger(inSpec);
String[] includeNames = m_columnFilterModel.applyTo(inSpec).getIncludes();
filteredTableRearranger.keepOnly(includeNames);
final BufferedDataTable filteredTable = exec.createColumnRearrangeTable(in, filteredTableRearranger, exec.createSilentSubExecutionContext(0.0));
final DataTableSpec filteredTableSpec = filteredTable.getDataTableSpec();
double progStep1 = 0.48;
double progStep2 = 0.48;
double progFinish = 1.0 - progStep1 - progStep2;
CorrelationComputer calculator = new CorrelationComputer(filteredTableSpec, m_maxPossValueCountModel.getIntValue());
exec.setMessage("Calculating table statistics");
ExecutionContext execStep1 = exec.createSubExecutionContext(progStep1);
calculator.calculateStatistics(filteredTable, execStep1);
execStep1.setProgress(1.0);
exec.setMessage("Calculating correlation values");
ExecutionMonitor execStep2 = exec.createSubExecutionContext(progStep2);
HalfDoubleMatrix correlationMatrix = calculator.calculateOutput(filteredTable, execStep2);
execStep2.setProgress(1.0);
exec.setMessage("Assembling output");
ExecutionContext execFinish = exec.createSubExecutionContext(progFinish);
PMCCPortObjectAndSpec pmccModel = new PMCCPortObjectAndSpec(includeNames, correlationMatrix);
BufferedDataTable out = pmccModel.createCorrelationMatrix(execFinish);
m_correlationTable = out;
String missValueString = calculator.getNumericMissingValueWarning(4);
StringBuilder warning = null;
if (missValueString != null) {
LOGGER.debug(calculator.getNumericMissingValueWarning(1000));
warning = new StringBuilder(missValueString);
}
String constantColString = calculator.getNumericConstantColumnPairs(4);
if (constantColString != null) {
LOGGER.debug(calculator.getNumericConstantColumnPairs(1000));
if (warning == null) {
warning = new StringBuilder(constantColString);
} else {
warning.append("\n");
warning.append(constantColString);
}
}
if (warning != null) {
setWarningMessage(warning.toString());
}
return new PortObject[] { out, pmccModel };
}
use of org.knime.core.node.ExecutionContext in project knime-core by knime.
the class ColumnToGrid2NodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
String groupColumn = m_configuration.getGroupColumn();
final ExecutionMonitor mainExec;
final BufferedDataTable inputTable;
if (groupColumn != null) {
exec.setMessage("Sorting input table");
BufferedDataTable in = inData[0];
ExecutionContext sortExec = exec.createSubExecutionContext(0.5);
ColumnRearranger sortFilterRearranger = new ColumnRearranger(in.getDataTableSpec());
String[] relevantCols = new String[m_included.length + 1];
System.arraycopy(m_included, 0, relevantCols, 0, m_included.length);
relevantCols[relevantCols.length - 1] = groupColumn;
sortFilterRearranger.keepOnly(relevantCols);
BufferedDataTable toBeSortedTable = exec.createColumnRearrangeTable(in, sortFilterRearranger, exec.createSubProgress(0.0));
SortedTable sorter = new SortedTable(toBeSortedTable, Collections.singletonList(groupColumn), new boolean[] { true }, sortExec);
inputTable = sorter.getBufferedDataTable();
mainExec = exec.createSubProgress(0.5);
} else {
inputTable = inData[0];
mainExec = exec;
}
exec.setMessage("Assembling output");
DataTableSpec spec = inputTable.getDataTableSpec();
DataTableSpec outSpec = createOutputSpec(spec);
BufferedDataContainer cont = exec.createDataContainer(outSpec);
int[] includeIndices = new int[m_included.length];
for (int i = 0; i < m_included.length; i++) {
int index = spec.findColumnIndex(m_included[i]);
includeIndices[i] = index;
}
int gridCount = m_configuration.getColCount();
final int cellCount;
final int groupColIndex;
if (groupColumn != null) {
cellCount = includeIndices.length * gridCount + 1;
groupColIndex = spec.findColumnIndex(groupColumn);
} else {
cellCount = includeIndices.length * gridCount;
groupColIndex = -1;
}
final DataCell[] cells = new DataCell[cellCount];
PushBackRowIterator it = new PushBackRowIterator(inputTable.iterator());
long currentRow = 0;
long totalRows = inputTable.size();
long currentOutRow = 0;
DataCell curGroupValue = null;
while (it.hasNext()) {
Arrays.fill(cells, DataType.getMissingCell());
// assign group column (if enabled)
if (groupColIndex >= 0) {
DataRow row = it.next();
curGroupValue = row.getCell(groupColIndex);
cells[cells.length - 1] = curGroupValue;
it.pushBack(row);
}
for (int grid = 0; grid < gridCount; grid++) {
if (!it.hasNext()) {
break;
}
DataRow inRow = it.next();
DataCell groupValue = groupColIndex < 0 ? null : inRow.getCell(groupColIndex);
if (ConvenienceMethods.areEqual(curGroupValue, groupValue)) {
mainExec.setProgress(currentRow / (double) totalRows, "Processing row " + currentRow + "/" + totalRows + ": " + inRow.getKey());
currentRow += 1;
mainExec.checkCanceled();
for (int i = 0; i < includeIndices.length; i++) {
cells[grid * includeIndices.length + i] = inRow.getCell(includeIndices[i]);
}
} else {
// start new group, i.e. new row
it.pushBack(inRow);
break;
}
}
RowKey key = RowKey.createRowKey(currentOutRow++);
cont.addRowToTable(new DefaultRow(key, cells));
}
cont.close();
return new BufferedDataTable[] { cont.getTable() };
}
Aggregations