use of org.knime.core.data.DataTable in project knime-core by knime.
the class TreeEnsembleLearnerNodeView method newModel.
private void newModel(final int index) {
assert SwingUtilities.isEventDispatchThread();
final MODEL nodeModel = getNodeModel();
TreeEnsembleModel model = nodeModel.getEnsembleModel();
DataTable hiliteRowSample = nodeModel.getHiliteRowSample();
UpdateTreeWorker updateWorker = new UpdateTreeWorker(hiliteRowSample, model, index);
UpdateTreeWorker old = m_updateWorkerRef.getAndSet(updateWorker);
if (old != null) {
old.cancel(true);
}
updateWorker.execute();
}
use of org.knime.core.data.DataTable in project knime-core by knime.
the class AbstractTableSorter method sortInternal.
/**
* Sorts the table passed in the constructor according to the settings and returns the sorted output table.
*
* @param exec To report progress
* @return The sorted output.
* @throws CanceledExecutionException If canceled.
*/
DataTable sortInternal(final ExecutionMonitor exec) throws CanceledExecutionException {
DataTable result;
if (m_sortInMemory && (m_rowsInInputTable <= Integer.MAX_VALUE)) {
result = sortInMemory(exec);
} else {
if (m_rowsInInputTable > Integer.MAX_VALUE) {
LOGGER.info("Not sorting table in memory, because it has more than " + Integer.MAX_VALUE + " rows.");
}
result = sortOnDisk(exec);
}
exec.setProgress(1.0);
return result;
}
use of org.knime.core.data.DataTable in project knime-core by knime.
the class DataTableSpecExtractor method extract.
/**
* Creates and returns a data table containing the meta information of the given spec. The meta information is
* referred to as the table data specification and contains information such as column names, types, domain values
* (list of possible values for categorical columns) and lower and upper bounds. It also contains the information
* which of the columns have a view handler associated, as well the possible values, if specified. Each column in
* the given table spec is represented as a row in the returned table.
*
* @param spec The spec to extract the meta information from.
* @return The data table containing the meta information of the given spec.
*/
public DataTable extract(final DataTableSpec spec) {
List<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
if (m_extractColumnNameAsColumn) {
colSpecs.add(new DataColumnSpecCreator("Column Name", StringCell.TYPE).createSpec());
}
colSpecs.add(new DataColumnSpecCreator("Column Type", StringCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Column Index", IntCell.TYPE).createSpec());
switch(m_propertyHandlerOutputFormat) {
case Hide:
break;
case Boolean:
colSpecs.add(new DataColumnSpecCreator("Color Handler", BooleanCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Size Handler", BooleanCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Shape Handler", BooleanCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Filter Handler", BooleanCell.TYPE).createSpec());
break;
default:
colSpecs.add(new DataColumnSpecCreator("Color Handler", StringCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Size Handler", StringCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Shape Handler", StringCell.TYPE).createSpec());
colSpecs.add(new DataColumnSpecCreator("Filter Handler", StringCell.TYPE).createSpec());
}
// likely number (important for sorting)
DataType lowerBoundColType = null;
DataType upperBoundColType = null;
for (DataColumnSpec c : spec) {
DataColumnDomain domain = c.getDomain();
if (domain.hasLowerBound()) {
DataType curLowerType = domain.getLowerBound().getType();
if (lowerBoundColType == null) {
lowerBoundColType = curLowerType;
} else {
lowerBoundColType = DataType.getCommonSuperType(lowerBoundColType, curLowerType);
}
}
if (domain.hasUpperBound()) {
DataType curUpperType = domain.getUpperBound().getType();
if (upperBoundColType == null) {
upperBoundColType = curUpperType;
} else {
upperBoundColType = DataType.getCommonSuperType(upperBoundColType, curUpperType);
}
}
}
lowerBoundColType = lowerBoundColType == null ? GENERIC_DATA_TYPE : lowerBoundColType;
upperBoundColType = upperBoundColType == null ? GENERIC_DATA_TYPE : upperBoundColType;
colSpecs.add(new DataColumnSpecCreator("Lower Bound", lowerBoundColType).createSpec());
colSpecs.add(new DataColumnSpecCreator("Upper Bound", upperBoundColType).createSpec());
int maxPossValues = 0;
switch(m_possibleValueOutputFormat) {
case Hide:
break;
case Collection:
colSpecs.add(new DataColumnSpecCreator("Possible Values", SetCell.getCollectionType(GENERIC_DATA_TYPE)).createSpec());
break;
default:
for (DataColumnSpec c : spec) {
DataColumnDomain domain = c.getDomain();
if (domain.hasValues()) {
maxPossValues = Math.max(domain.getValues().size(), maxPossValues);
}
}
for (int i = 0; i < maxPossValues; i++) {
colSpecs.add(new DataColumnSpecCreator("Value " + i, GENERIC_DATA_TYPE).createSpec());
}
}
/* fill it */
DataContainer dc = new DataContainer(new DataTableSpec(colSpecs.toArray(new DataColumnSpec[colSpecs.size()])));
for (int i = 0; i < spec.getNumColumns(); i++) {
DataColumnSpec colSpec = spec.getColumnSpec(i);
List<DataCell> cells = new ArrayList<DataCell>();
if (m_extractColumnNameAsColumn) {
cells.add(new StringCell(colSpec.getName()));
}
cells.add(new StringCell(colSpec.getType().toString()));
cells.add(new IntCell(i));
ColorHandler colorHandler = colSpec.getColorHandler();
SizeHandler sizeHandler = colSpec.getSizeHandler();
ShapeHandler shapeHandler = colSpec.getShapeHandler();
Optional<FilterHandler> filterHandler = colSpec.getFilterHandler();
switch(m_propertyHandlerOutputFormat) {
case Hide:
break;
case Boolean:
cells.add(BooleanCellFactory.create(colorHandler != null));
cells.add(BooleanCellFactory.create(sizeHandler != null));
cells.add(BooleanCellFactory.create(shapeHandler != null));
cells.add(BooleanCellFactory.create(filterHandler.isPresent()));
break;
default:
cells.add(new StringCell(colorHandler == null ? "" : colorHandler.toString()));
cells.add(new StringCell(sizeHandler == null ? "" : sizeHandler.toString()));
cells.add(new StringCell(shapeHandler == null ? "" : shapeHandler.toString()));
cells.add(new StringCell(filterHandler.map(f -> f.toString()).orElse("")));
}
DataColumnDomain domain = colSpec.getDomain();
DataCell lb = domain.getLowerBound();
if (lb != null) {
cells.add(lb);
} else {
cells.add(DataType.getMissingCell());
}
DataCell ub = domain.getUpperBound();
if (ub != null) {
cells.add(ub);
} else {
cells.add(DataType.getMissingCell());
}
switch(m_possibleValueOutputFormat) {
case Hide:
break;
case Collection:
if (domain.hasValues()) {
cells.add(CollectionCellFactory.createSetCell(domain.getValues()));
} else {
cells.add(DataType.getMissingCell());
}
break;
default:
Set<DataCell> set = domain.hasValues() ? domain.getValues() : Collections.EMPTY_SET;
int nrColsToWrite = maxPossValues;
for (DataCell c : set) {
cells.add(c);
nrColsToWrite -= 1;
}
while (nrColsToWrite > 0) {
cells.add(DataType.getMissingCell());
nrColsToWrite -= 1;
}
}
dc.addRowToTable(new DefaultRow(new RowKey(colSpec.getName()), cells));
}
dc.close();
return dc.getTable();
}
use of org.knime.core.data.DataTable in project knime-core by knime.
the class TableContentModelTest method testShowOnlyHiLited.
// testCachingStrategy()
/**
* Tests the correctness of the model when only hilited are shown.
*/
public final void testShowOnlyHiLited() {
final String[] colnames = new String[] { "C1" };
final DataType[] colclasses = new DataType[] { DoubleCell.TYPE };
final DataRow[] data = new DefaultRow[500];
for (int i = 0; i < data.length; i++) {
data[i] = new DefaultRow(new RowKey("Row_" + i), new double[] { i });
}
// override DataTable to set own iterator
final DataTable table = new DefaultTable(data, colnames, colclasses);
TableContentModel model = new TableContentModel(table);
// change default chunk and cache size
final int chunkSize = 25;
final int cacheSize = 2 * chunkSize;
model.setChunkSize(chunkSize);
assertEquals(model.getChunkSize(), chunkSize);
model.setCacheSize(cacheSize);
assertEquals(model.getCacheSize(), cacheSize);
model.setTableContentFilter(TableContentFilter.HiliteOnly);
assertEquals(model.getRowCount(), 0);
assertTrue(model.isRowCountFinal());
final HiLiteHandler hiliter = new HiLiteHandler();
model.setHiLiteHandler(hiliter);
final Random rand = new Random();
int nrHiLitKeys = 0;
// reflected in the model
for (int i = 0; i < 500; i++) {
if (i % 100 == 0) {
// clear all, also that should work
hiliter.fireClearHiLiteEvent();
flushEDTQueue();
nrHiLitKeys = 0;
} else {
// let at most 20% change
int count = rand.nextInt(data.length / 5);
// change randomly drawn keys
for (int c = 0; c < count; c++) {
int index = rand.nextInt(data.length);
RowKey keyForIndex = data[index].getKey();
boolean isHilit = hiliter.isHiLit(keyForIndex);
if (isHilit) {
hiliter.fireUnHiLiteEvent(keyForIndex);
flushEDTQueue();
nrHiLitKeys--;
} else {
hiliter.fireHiLiteEvent(keyForIndex);
flushEDTQueue();
nrHiLitKeys++;
}
}
}
flushEDTQueue();
// now the sanity checks
for (int row = 0; row < model.getRowCount(); row++) {
RowKey key = model.getRow(row).getKey();
assertTrue(hiliter.isHiLit(key));
assertTrue(model.isHiLit(row));
}
assertEquals(model.getRowCount(), nrHiLitKeys);
assertTrue(model.isRowCountFinal());
}
}
use of org.knime.core.data.DataTable in project knime-core by knime.
the class TableContentModelTest method testCachingStrategy.
/**
* Tests ring buffer chaching strategy of the
* <code>TableContentModel</code>. This method uses a modified
* <code>Iterator</code> that throws an <code>Exception</code> when called
* at an inappropriate time.
*/
public final void testCachingStrategy() {
final String[] colnames = new String[] { "C1" };
final DataType[] colclasses = new DataType[] { DoubleCell.TYPE };
final DataRow[] data = new DefaultRow[500];
for (int i = 0; i < data.length; i++) {
data[i] = new DefaultRow(new RowKey("Row_" + i), new double[] { i });
}
// these flags keep track when the iterator of the table may be
// accessed (will be changed in this method, first flag) and when the
// iterator is indeed being accessed (set in the iterator below to true,
// reset here, second flag)
final boolean[] flags = new boolean[] { true, false };
// override DataTable to set own iterator
final DataTable table = new DefaultTable(data, colnames, colclasses) {
@Override
public RowIterator iterator() {
return new RestrictedAccessIterator(getRowsInList(), flags);
}
};
TableContentModel model = new TableContentModel(table);
// change default chunk and cache size
final int chunkSize = 25;
final int cacheSize = 2 * chunkSize;
model.setChunkSize(chunkSize);
assertEquals(model.getChunkSize(), chunkSize);
model.setCacheSize(cacheSize);
assertEquals(model.getCacheSize(), cacheSize);
// init of table uses iterator
assertTrue(flags[1]);
flags[1] = false;
// allow table access
flags[0] = true;
// get first row, iterator is used
model.getRow(0);
// is true when iterator has indeed been used
assertTrue(flags[1]);
// simulate scrolling down - for first "chunksize" rows
// iterator access
flags[0] = false;
flags[1] = false;
for (int i = 0; i < chunkSize; i++) {
// access rows 0 - 24
// will throw exception when iterator is accessed
model.getRow(i);
}
assertFalse(flags[0]);
assertFalse(flags[1]);
// row "chunksize": update cache!
flags[0] = true;
// now in cache: Row_1 - Row_50 (release Row_0)
model.getRow(chunkSize);
// iterator has been used
assertTrue(flags[1]);
flags[0] = false;
flags[1] = false;
// cache is full, containing Row_1 - Row_50
for (int i = 1; i < cacheSize; i++) {
// must not access Row_50 (will use iterator)
model.getRow(i);
}
assertFalse(flags[0]);
assertFalse(flags[1]);
// ok, let's simulate arbitrary jumping in the table and check the
// cache, let's say 20 different positions, the rows in the cache are:
// [row+chunksize-cachesize+1 : row+chunksize-1]
final Random rand = new Random();
for (int i = 0; i < 200; i++) {
// draw some row to access
int row = rand.nextInt(500);
flags[0] = true;
flags[1] = false;
model.getRow(row);
if (!flags[1]) {
// cache not changed, we may continue
continue;
}
// disallow access
flags[0] = false;
flags[1] = false;
int firstRow = Math.max(0, row + chunkSize - cacheSize + 1);
int lastRow = Math.min(row + chunkSize, 500);
for (int r = firstRow; r < lastRow; r++) {
model.getRow(r);
}
assertFalse(flags[0]);
assertFalse(flags[1]);
}
}
Aggregations