use of org.knime.core.data.RowIterator in project knime-core by knime.
the class JoinedTable method create.
/**
* Creates new join table, does the sanity checks. Called from the
* {@link org.knime.core.node.ExecutionContext#createJoinedTable(
* BufferedDataTable, BufferedDataTable, ExecutionMonitor)} method.
* @param left The left table.
* @param right The right table.
* @param prog For progress/cancel.
* @return A joined table.
* @throws CanceledExecutionException When canceled.
* @throws IllegalArgumentException If row keys don't match or there are
* duplicate columns.
*/
public static JoinedTable create(final BufferedDataTable left, final BufferedDataTable right, final ExecutionMonitor prog) throws CanceledExecutionException {
long cnt = left.size();
if (cnt != right.size()) {
throw new IllegalArgumentException("Tables can't be joined, non " + "matching row counts: " + cnt + " vs. " + right.size());
}
// throws exception when duplicates encountered.
DataTableSpec joinSpec = new DataTableSpec(left.getDataTableSpec(), right.getDataTableSpec());
// check if rows come in same order
RowIterator leftIt = left.iterator();
RowIterator rightIt = right.iterator();
int rowIndex = 0;
while (leftIt.hasNext()) {
RowKey leftKey = leftIt.next().getKey();
RowKey rightKey = rightIt.next().getKey();
if (!leftKey.equals(rightKey)) {
throw new IllegalArgumentException("Tables contain non-matching rows or are sorted " + "differently, keys in row " + rowIndex + " do not match: \"" + leftKey + "\" vs. \"" + rightKey + "\"");
}
prog.checkCanceled();
prog.setProgress(rowIndex / (double) cnt, "\"" + leftKey + "\" (" + rowIndex + "/" + cnt + ")");
rowIndex++;
}
return new JoinedTable(left, right, joinSpec);
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class RearrangeColumnsTable method calcNewColsSynchronously.
/**
* Processes input sequentially in the caller thread.
*/
private static void calcNewColsSynchronously(final BufferedDataTable table, final ExecutionMonitor subProgress, final NewColumnsProducerMapping newColsProducerMapping, final DataContainer container) throws CanceledExecutionException {
long finalRowCount = table.size();
Set<CellFactory> newColsFactories = newColsProducerMapping.getUniqueCellFactoryMap().keySet();
final int factoryCount = newColsFactories.size();
int r = 0;
CellFactory facForProgress = factoryCount > 0 ? newColsFactories.iterator().next() : null;
for (RowIterator it = table.iterator(); it.hasNext(); r++) {
DataRow row = it.next();
DataRow append = calcNewCellsForRow(row, newColsProducerMapping);
container.addRowToTable(append);
if (facForProgress == null) {
// no factory added means at least one columns gets type converted.
assert !newColsProducerMapping.getConverterToIndexMap().isEmpty();
} else {
facForProgress.setProgress(r + 1, finalRowCount, row.getKey(), subProgress);
}
subProgress.checkCanceled();
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class JoinerTest method compareTables.
private void compareTables(final BufferedDataTable reference, final BufferedDataTable test) {
// Check if it has the same results as defaultResult
assertThat("Unequal number of rows in result table", test.getRowCount(), is(reference.getRowCount()));
RowIterator referenceIter = reference.iterator();
RowIterator testIter = test.iterator();
while (referenceIter.hasNext()) {
DataRow refRow = referenceIter.next();
DataRow testRow = testIter.next();
assertThat("Unexpected row key", testRow.getKey(), is(refRow.getKey()));
Iterator<DataCell> refCell = refRow.iterator();
Iterator<DataCell> testCell = testRow.iterator();
while (refCell.hasNext()) {
assertThat("Unexpected cell in row " + refRow.getKey(), testCell.next(), is(refCell.next()));
}
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class FilterColumnTableTest method tableTest.
/*
* Invoked on each testXXX() method to test all rows and cells on equality
* by iterating through the entire table, that is, the filter as well as the
* original data table. @param The filter table to test equality on.
*/
private void tableTest(final FilterColumnTable f) {
final int[] columns = f.getColumnIndices();
RowIterator fIt = f.iterator();
RowIterator tIt = m_table.iterator();
for (; fIt.hasNext() && tIt.hasNext(); ) {
DataRow rf = fIt.next();
DataRow rt = tIt.next();
// check also if the same rows are compared
assertTrue(rf.getKey().equals(rt.getKey()));
for (int i = 0; i < columns.length; i++) {
// check cell from original with the mapped one
assertTrue(rf.getCell(i).equals(rt.getCell(columns[i])));
}
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class ARFFTableTest method testARFFTableMissVals.
/**
* tests stuff like '?' the ARFF missing value, comment lines and empty
* lines in the file.
*
* @throws IOException some time.
* @throws InvalidSettingsException sometimes.
*/
public void testARFFTableMissVals() throws IOException, InvalidSettingsException {
// "% Comment\n"
// + "% comment line 2\n"
// + "@attribute col1 string\n"
// + "@attribute col2 string\n"
// + "\n\n"
// + "@data\n\n"
// + "foo, poo\n"
// + "foo, ?\n"
// + "?, foo\n"
// + "%\n"
// + "%\n"
// + "\n";
File tempFile = File.createTempFile("ARFFReaderUnitTest", "table");
tempFile.deleteOnExit();
Writer out = new BufferedWriter(new FileWriter(tempFile));
out.write(ARFF_FIES);
out.close();
try {
ARFFTable table = new ARFFTable(tempFile.toURI().toURL(), ARFFTable.createDataTableSpecFromARFFfile(tempFile.toURI().toURL(), null), "Row");
assertEquals(table.getDataTableSpec().getNumColumns(), 2);
assertEquals(table.getDataTableSpec().getColumnSpec(0).getType(), StringCell.TYPE);
assertEquals(table.getDataTableSpec().getColumnSpec(1).getType(), StringCell.TYPE);
assertNull(table.getDataTableSpec().getColumnSpec(0).getDomain().getValues());
assertNull(table.getDataTableSpec().getColumnSpec(1).getDomain().getValues());
DataRow row;
RowIterator rIter = table.iterator();
assertTrue(rIter.hasNext());
row = rIter.next();
assertEquals(row.getKey().toString(), "Row0");
assertEquals(row.getCell(0).toString(), "foo");
assertEquals(row.getCell(1).toString(), "poo");
assertTrue(rIter.hasNext());
row = rIter.next();
assertEquals(row.getKey().toString(), "Row1");
assertEquals(row.getCell(0).toString(), "foo");
assertTrue(row.getCell(1).isMissing());
assertTrue(rIter.hasNext());
row = rIter.next();
assertEquals(row.getKey().toString(), "Row2");
assertTrue(row.getCell(0).isMissing());
assertEquals(row.getCell(1).toString(), "foo");
assertFalse(rIter.hasNext());
} catch (CanceledExecutionException cee) {
// no exec monitor, no cancel
}
}
Aggregations