use of org.knime.core.node.BufferedDataTable in project knime-core by knime.
the class SampleDataNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] data, final ExecutionContext exec) throws Exception {
DataTableSpec[] outSpecs = configure(new DataTableSpec[2]);
DataTableSpec dataSpec = outSpecs[0];
DataTableSpec clusterSpec = outSpecs[1];
BufferedDataTableRowOutput dataOut = new BufferedDataTableRowOutput(exec.createDataContainer(dataSpec));
BufferedDataTableRowOutput clusterOut = new BufferedDataTableRowOutput(exec.createDataContainer(clusterSpec));
run(dataSpec, dataOut, clusterSpec, clusterOut, exec);
return new BufferedDataTable[] { dataOut.getDataTable(), clusterOut.getDataTable() };
}
use of org.knime.core.node.BufferedDataTable in project knime-core by knime.
the class ShapeManagerNodeModel method execute.
/**
* Is invoked during the node's execution to make the shape settings.
*
* @param data the input data array
* @param exec the execution monitor
* @return the same input data table with assigned shapes to one column
* @throws CanceledExecutionException if user canceled execution
*
* @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
*/
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws CanceledExecutionException {
BufferedDataTable inData = (BufferedDataTable) data[INPORT];
ShapeHandler shapeHandler = new ShapeHandler(new ShapeModelNominal(m_map));
final DataTableSpec newSpec = appendShapeHandler(inData.getSpec(), m_column, shapeHandler);
BufferedDataTable changedSpecTable = exec.createSpecReplacerTable(inData, newSpec);
DataTableSpec modelSpec = new DataTableSpec(newSpec.getColumnSpec(m_column));
ShapeHandlerPortObject viewPort = new ShapeHandlerPortObject(modelSpec, shapeHandler.toString() + " based on column \"" + m_column + "\"");
return new PortObject[] { changedSpecTable, viewPort };
}
use of org.knime.core.node.BufferedDataTable in project knime-core by knime.
the class SizeManager2NodeModel method execute.
/**
* Is invoked during the node's execution to make the size settings.
*
* @param data the input data array
* @param exec the execution monitor
* @return the same input data table whereby the DataTableSpec contains
* additional size infos
* @throws CanceledExecutionException if user canceled execution
*
* @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
*/
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws CanceledExecutionException {
final DataTableSpec inSpec = (DataTableSpec) data[INPORT].getSpec();
final String columnName = m_column.getStringValue();
final DataColumnSpec cspec = inSpec.getColumnSpec(columnName);
SizeHandler sizeHandler = createSizeHandler(cspec);
final DataTableSpec newSpec = appendSizeHandler(inSpec, columnName, sizeHandler);
BufferedDataTable changedSpecTable = exec.createSpecReplacerTable((BufferedDataTable) data[INPORT], newSpec);
DataTableSpec modelSpec = new DataTableSpec(newSpec.getColumnSpec(m_column.getStringValue()));
SizeHandlerPortObject viewPort = new SizeHandlerPortObject(modelSpec, sizeHandler.toString() + " based on column \"" + m_column.getStringValue() + "\"");
return new PortObject[] { changedSpecTable, viewPort };
}
use of org.knime.core.node.BufferedDataTable in project knime-core by knime.
the class LiftChartNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
ConvenienceMethods.checkTableSize(inData[0]);
int predColIndex = inData[0].getDataTableSpec().findColumnIndex(m_responseColumn.getStringValue());
List<String> inclList = new LinkedList<String>();
inclList.add(m_probabilityColumn.getStringValue());
boolean[] order = new boolean[] { false };
SortedTable st = new SortedTable(inData[0], inclList, order, exec);
long totalResponses = 0;
double partWidth = Double.parseDouble(m_intervalWidth.getStringValue());
int nrParts = (int) Math.ceil(100.0 / partWidth);
List<Integer> positiveResponses = new LinkedList<Integer>();
int rowIndex = 0;
for (DataRow row : st) {
if (row.getCell(predColIndex).isMissing()) {
setWarningMessage("There are missing values." + " Please check your data.");
continue;
}
String response = ((StringValue) row.getCell(predColIndex)).getStringValue().trim();
if (response.equalsIgnoreCase(m_responseLabel.getStringValue())) {
totalResponses++;
positiveResponses.add(rowIndex);
}
rowIndex++;
}
int[] counter = new int[nrParts];
int partWidthAbsolute = (int) Math.ceil(rowIndex / (double) nrParts);
double avgResponse = (double) positiveResponses.size() / rowIndex;
for (int rIndex : positiveResponses) {
int index = rIndex / partWidthAbsolute;
counter[index]++;
}
DataColumnSpec[] colSpec = new DataColumnSpec[3];
colSpec[0] = new DataColumnSpecCreator("Lift", DoubleCell.TYPE).createSpec();
colSpec[1] = new DataColumnSpecCreator("Baseline", DoubleCell.TYPE).createSpec();
colSpec[2] = new DataColumnSpecCreator("Cumulative Lift", DoubleCell.TYPE).createSpec();
DataTableSpec tableSpec = new DataTableSpec(colSpec);
DataContainer cont = exec.createDataContainer(tableSpec);
colSpec = new DataColumnSpec[2];
colSpec[0] = new DataColumnSpecCreator("Actual", DoubleCell.TYPE).createSpec();
colSpec[1] = new DataColumnSpecCreator("Baseline", DoubleCell.TYPE).createSpec();
tableSpec = new DataTableSpec(colSpec);
DataContainer responseCont = exec.createDataContainer(tableSpec);
long cumulativeCounter = 0;
responseCont.addRowToTable(new DefaultRow(new RowKey("0"), 0.0, 0.0));
for (int i = 0; i < counter.length; i++) {
cumulativeCounter += counter[i];
double responseRate = (double) counter[i] / partWidthAbsolute;
double lift = responseRate / avgResponse;
double cumResponseRate = (double) cumulativeCounter / totalResponses;
long number = partWidthAbsolute * (i + 1);
// well.. rounding problems
if (number > rowIndex) {
number = rowIndex;
}
double cumulativeLift = // (double)cumulativeCounter / (partWidthAbsolute * (i + 1));
(double) cumulativeCounter / number;
cumulativeLift /= avgResponse;
// cumulativeLift = lifts / (i+1);
double rowKey = ((i + 1) * partWidth);
if (rowKey > 100) {
rowKey = 100;
}
cont.addRowToTable(new DefaultRow(new RowKey("" + rowKey), lift, 1.0, cumulativeLift));
double cumBaseline = (i + 1) * partWidth;
if (cumBaseline > 100) {
cumBaseline = 100;
}
responseCont.addRowToTable(new DefaultRow(new RowKey("" + rowKey), cumResponseRate * 100, cumBaseline));
}
cont.close();
responseCont.close();
m_dataArray[0] = new DefaultDataArray(cont.getTable(), 1, (int) cont.size());
m_dataArray[1] = new DefaultDataArray(responseCont.getTable(), 1, (int) responseCont.size());
return new BufferedDataTable[] { st.getBufferedDataTable() };
}
use of org.knime.core.node.BufferedDataTable in project knime-core by knime.
the class TableSorterTest method runMemoryTest.
private void runMemoryTest(final int numRows, final int maxNumRowsPerContainer, final int maxOpenContainers) throws CanceledExecutionException {
// Create data with fields that consume a lot memory
DataTable inputTable = new TestData(numRows, 1);
BufferedDataTable bdt = m_exec.createBufferedDataTable(inputTable, m_exec);
BufferedDataTableSorter sorter = new BufferedDataTableSorter(bdt, Arrays.asList("Index"), new boolean[] { true });
sorter.setMaxOpenContainers(maxOpenContainers);
BufferedDataTable defaultResult = sorter.sort(m_exec);
sorter.setMaxRows(maxNumRowsPerContainer);
// 10MB free memory
long currentlyUsed = MemoryAlertSystem.getUsedMemory();
double fraction = Math.min(1, (currentlyUsed + (10 << 20)) / (double) MemoryAlertSystem.getMaximumMemory());
MemoryAlertSystem.getInstance().setFractionUsageThreshold(fraction);
try {
sorter.setMemService(MemoryAlertSystem.getInstance());
// run again with change settings
BufferedDataTable result = sorter.sort(m_exec);
// Check if column is sorted in ascending order
int prevValue = Integer.MIN_VALUE;
for (DataRow row : result) {
int thisValue = ((IntValue) row.getCell(0)).getIntValue();
Assert.assertTrue(thisValue >= prevValue);
}
// Check if it has the same results as defaultResult
Assert.assertTrue(defaultResult.getRowCount() == result.getRowCount());
RowIterator defaultIter = defaultResult.iterator();
RowIterator iter = result.iterator();
while (defaultIter.hasNext()) {
DataRow defaultRow = defaultIter.next();
DataRow row = iter.next();
Assert.assertTrue(defaultRow.getKey().getString().equals(row.getKey().getString()));
Iterator<DataCell> defaultCellIter = defaultRow.iterator();
Iterator<DataCell> cellIter = row.iterator();
while (defaultCellIter.hasNext()) {
Assert.assertTrue(defaultCellIter.next().equals(cellIter.next()));
}
}
} finally {
MemoryAlertSystem.getInstance().setFractionUsageThreshold(MemoryAlertSystem.DEFAULT_USAGE_THRESHOLD);
}
}
Aggregations