use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class RuleSetToTable method execute.
/**
* Performs the conversion.
*
* @param exec An {@link ExecutionContext}.
* @param pmmlPo The input {@link PMMLPortObject}.
* @return The created {@link BufferedDataTable}.
* @throws CanceledExecutionException Execition was cancelled.
* @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
*/
public BufferedDataTable execute(final ExecutionContext exec, final PMMLPortObject pmmlPo) throws CanceledExecutionException, InvalidSettingsException {
// TODO should the rule selection method be an output flow variable?
if (pmmlPo.getPMMLValue().getModels(PMMLModelType.RuleSetModel).size() != 1) {
throw new InvalidSettingsException("Only a single RuleSet model is supported.");
}
PMMLRuleTranslator ruleTranslator = new PMMLRuleTranslator();
pmmlPo.initializeModelTranslator(ruleTranslator);
List<Rule> rules = ruleTranslator.getRules();
final DataTableSpec confSpec = configure(pmmlPo.getSpec());
final List<String> scoreValues = new ArrayList<>();
final DataTableSpec properSpec = confSpec != null ? confSpec : properSpec(rules, scoreValues);
BufferedDataContainer container = exec.createDataContainer(properSpec);
List<DataColumnSpec> targetCols = pmmlPo.getSpec().getTargetCols();
DataType outcomeType = targetCols.get(0).getType();
long idx = 0L;
int rulesSize = rules.size();
Map<String, DataType> types = new LinkedHashMap<>();
for (DataColumnSpec col : pmmlPo.getSpec().getLearningCols()) {
types.put(col.getName(), col.getType());
}
for (Rule rule : rules) {
exec.checkCanceled();
exec.setProgress(1.0 * idx++ / rulesSize);
container.addRowToTable(new DefaultRow(RowKey.createRowKey(idx), createRow(rule, outcomeType, types, scoreValues)));
}
container.close();
return container.getTable();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class MovingAverageNodeModel method addMissingCells.
/**
* adds a fixed number of missing cells to the given row.
* or replaces the original ones with missings.
* @param colindexex
*/
private DataRow addMissingCells(final int nrMiss, final int[] colindexex, final DataRow oldrow) {
DataCell[] cell = new DataCell[oldrow.getNumCells() + nrMiss];
int counter = 0;
for (DataCell c : oldrow) {
cell[counter] = c;
counter++;
}
if (nrMiss <= 0) {
// we replace the colindexex with missings.
for (int i : colindexex) {
cell[i] = DataType.getMissingCell();
}
} else {
for (; counter < cell.length; counter++) {
cell[counter] = DataType.getMissingCell();
}
}
return new DefaultRow(oldrow.getKey(), cell);
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class MovingAggregationTableFactory method getBackwardTable.
private BufferedDataTable getBackwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = row.getCell(colIdx);
}
final boolean windowFull = window.size() >= m_windowLength;
for (int i = 0, length = m_ops.length; i < length; i++) {
if (windowFull || m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
if (windowFull) {
// remove the first row only when the window is full
// not during the missing value handling phase!
window.removeFirst();
}
dc.addRowToTable(new DefaultRow(row.getKey(), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class MovingAggregationTableFactory method getCenterTable.
private BufferedDataTable getCenterTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
int centerIdx = -1;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
// we have to subtract 1 since the indexing of the array starts with 0
centerIdx = window.size() - m_windowLength / 2 - 1;
if (centerIdx < 0) {
// we have to fill the window first
continue;
}
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
final DataRow centerRow = window.get(centerIdx);
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = centerRow.getCell(colIdx);
}
final boolean windowFull = window.size() >= m_windowLength;
for (int i = 0, length = m_ops.length; i < length; i++) {
if (windowFull || m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
// not during the missing value handling phase!
if (windowFull) {
window.removeFirst();
}
}
// we have to handle the remaining rows in the window
while (window.size() > centerIdx) {
exec.checkCanceled();
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
final DataRow centerRow = window.get(centerIdx);
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = centerRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
if (m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
window.removeFirst();
dc.addRowToTable(new DefaultRow(centerRow.getKey(), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.def.DefaultRow in project knime-core by knime.
the class MovingAggregationTableFactory method getForwardTable.
private BufferedDataTable getForwardTable(final ExecutionMonitor exec, final BufferedDataTable table, final BufferedDataContainer dc) throws CanceledExecutionException {
final int rowCount = table.getRowCount();
final LinkedList<DataRow> window = new LinkedList<>();
int rowIdx = 0;
for (final DataRow row : table) {
exec.setProgress(rowIdx / (double) rowCount, "Processing row " + rowIdx++ + " of " + rowCount);
exec.checkCanceled();
window.add(row);
final boolean windowFull = window.size() >= m_windowLength;
if (!windowFull) {
// we have to fill the window first
continue;
}
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
final DataRow firstRow = window.getFirst();
int idx = 0;
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = firstRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
}
dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
// remove the first row only when the window is full
// not during the missing value handling phase!
window.removeFirst();
}
// we have to handle the remaining rows in the window
while (!window.isEmpty()) {
exec.checkCanceled();
final DataCell[] cells = new DataCell[m_ops.length + m_cols2KeepIdxs.length];
int idx = 0;
final DataRow firstRow = window.getFirst();
// handle the retained columns
for (final int colIdx : m_cols2KeepIdxs) {
cells[idx++] = firstRow.getCell(colIdx);
}
for (int i = 0, length = m_ops.length; i < length; i++) {
if (m_handleMissings) {
final int colIdx = m_aggrColIdxs[i];
final AggregationOperator op = m_ops[i];
for (final DataRow windowRow : window) {
op.compute(windowRow, colIdx);
}
cells[idx++] = op.getResult();
op.reset();
} else {
// the window is not yet full return missing cells
cells[idx++] = DataType.getMissingCell();
}
}
window.removeFirst();
dc.addRowToTable(new DefaultRow(firstRow.getKey(), cells));
}
dc.close();
return dc.getTable();
}
Aggregations