use of org.knime.core.data.DataRow in project knime-core by knime.
the class JoinedTableTest method testGetRowIterator.
/**
* Test for RowIterator. That is the one that is most likely to fail ...
*/
public final void testGetRowIterator() {
DataColumnSpec[] leftCols = new DataColumnSpec[3];
DataColumnSpec[] rightCols = new DataColumnSpec[3];
System.arraycopy(COLS, 0, leftCols, 0, 3);
System.arraycopy(COLS, 3, rightCols, 0, 3);
final int allLength = 100;
DataRow[] leftRows = new DataRow[allLength];
DataRow[] rightRows = new DataRow[allLength];
Hashtable<RowKey, DataRow> rightHash = new Hashtable<RowKey, DataRow>();
for (int i = 0; i < allLength; i++) {
String id = "Id_" + i;
leftRows[i] = getRandomRow(id);
rightRows[i] = getRandomRow(id);
rightHash.put(rightRows[i].getKey(), rightRows[i]);
}
final DataTable leftTable = new DefaultTable(leftRows, new DataTableSpec(leftCols));
final DataTable rightTable = new DefaultTable(rightRows, new DataTableSpec(rightCols));
JoinedTable t = new JoinedTable(leftTable, rightTable);
// everything comes in order, shouldn't make a problem.
int count = checkForEquality(t, leftRows, rightHash);
assertEquals(count, allLength);
// shuffle the right table
DataRow[] shuffledRightRows = new DataRow[allLength];
System.arraycopy(rightRows, 0, shuffledRightRows, 0, allLength);
List<DataRow> c = Arrays.asList(shuffledRightRows);
Collections.shuffle(c, RAND);
shuffledRightRows = c.toArray(shuffledRightRows);
DataTable shuffleRightTable = new DefaultTable(shuffledRightRows, new DataTableSpec(rightCols));
t = new JoinedTable(leftTable, shuffleRightTable);
count = checkForEquality(t, leftRows, rightHash);
assertEquals(count, allLength);
// wow, it survived that.
// let's delete some of the rows in the right table.
// supposedly, the table will fill it with missing values ...
final int newLength = (int) (0.8 * allLength);
DataRow[] shuffledAndTruncRightRows = new DataRow[newLength];
System.arraycopy(shuffledRightRows, 0, shuffledAndTruncRightRows, 0, newLength);
Hashtable<RowKey, DataRow> newHash = new Hashtable<RowKey, DataRow>(rightHash);
for (int i = newLength; i < allLength; i++) {
RowKey removeMe = shuffledRightRows[i].getKey();
newHash.remove(removeMe);
}
DataTable shuffleAndTruncRightTable = new DefaultTable(shuffledAndTruncRightRows, new DataTableSpec(rightCols));
t = new JoinedTable(leftTable, shuffleAndTruncRightTable);
count = checkForEquality(t, leftRows, newHash);
assertEquals(count, allLength);
// now cut shorten the left table
DataRow[] truncLeftRows = new DataRow[newLength];
System.arraycopy(leftRows, 0, truncLeftRows, 0, newLength);
DataTable truncLeftTable = new DefaultTable(truncLeftRows, new DataTableSpec(leftCols));
t = new JoinedTable(truncLeftTable, rightTable);
count = checkForEquality(t, truncLeftRows, rightHash);
assertEquals(count, allLength);
// tables share no rows at all
final int halfLength = allLength / 2;
DataRow[] halfLeftRows = new DataRow[halfLength];
DataRow[] halfRightRows = new DataRow[halfLength];
System.arraycopy(leftRows, 0, halfLeftRows, 0, halfLength);
System.arraycopy(rightRows, halfLength, halfRightRows, 0, halfLength);
Hashtable<RowKey, DataRow> halfRightHash = new Hashtable<RowKey, DataRow>();
for (int i = 0; i < halfLength; i++) {
DataRow current = halfRightRows[i];
halfRightHash.put(current.getKey(), current);
}
DataTable halfLeftTable = new DefaultTable(halfLeftRows, new DataTableSpec(leftCols));
DataTable halfRightTable = new DefaultTable(halfRightRows, new DataTableSpec(rightCols));
t = new JoinedTable(halfLeftTable, halfRightTable);
count = checkForEquality(t, halfLeftRows, halfRightHash);
assertEquals(count, 2 * halfLength);
// left table is empty
DataTable emptyLeftTable = new DefaultTable(new DataRow[0], new DataTableSpec(leftCols));
t = new JoinedTable(emptyLeftTable, halfRightTable);
count = checkForEquality(t, new DataRow[0], halfRightHash);
assertEquals(count, halfLength);
// right table is empty
DataTable emptyRightTable = new DefaultTable(new DataRow[0], new DataTableSpec(rightCols));
t = new JoinedTable(halfLeftTable, emptyRightTable);
count = checkForEquality(t, halfLeftRows, new Hashtable<RowKey, DataRow>());
assertEquals(count, halfLength);
}
use of org.knime.core.data.DataRow in project knime-core by knime.
the class MedianTableTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
Random random = new Random(37);
DataColumnSpec[] colSpecs = new DataColumnSpec[] { new DataColumnSpecCreator("AscendingDouble", DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator("DescendingDouble", DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator("ThreeValuesSingleMedian", DoubleCell.TYPE).createSpec(), new DataColumnSpecCreator("ThreeValuesDifferentMedian", DoubleCell.TYPE).createSpec() // new DataColumnSpecCreator("FourValuesSingleMedian", DoubleCell.TYPE).createSpec(),
// new DataColumnSpecCreator("FourValuesDifferentMedian", DoubleCell.TYPE).createSpec()
};
DataTableSpec spec = new DataTableSpec(colSpecs);
final BufferedDataContainer container = EXEC_CONTEXT.createDataContainer(spec);
try {
int count = 100;
for (int i = 0; i < count; ++i) {
int col = 0;
DataCell[] rowVals = new DataCell[colSpecs.length];
rowVals[col++] = new DoubleCell(i);
rowVals[col++] = new DoubleCell(count - i - 1);
rowVals[col++] = i == 4 ? DataType.getMissingCell() : new DoubleCell(i < count / 2 ? 0 : i * 2 >= count + 2 ? 4 : 1);
rowVals[col++] = new DoubleCell(i < count / 2 ? 0 : i * 2 >= count + 2 ? 4 : 1);
container.addRowToTable(new DefaultRow(Integer.toString(i), rowVals));
}
} finally {
container.close();
}
smallTable = container.getTable();
NodeLogger.getLogger(getClass()).debug("Contents of test table:");
for (DataRow row : smallTable) {
StringBuilder buf = new StringBuilder();
for (DataCell dataCell : row) {
buf.append(dataCell + "\t");
}
NodeLogger.getLogger(getClass()).debug(buf.toString());
}
}
use of org.knime.core.data.DataRow 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.DataRow in project knime-core by knime.
the class CovarianceMatrixCalculatorTest method assertCovarianceMatrixEquality.
/**
* @param covMatrixUnderTest
* @param referenceCovarianceMatrix
* @param covTableUnderTest
*/
private static void assertCovarianceMatrixEquality(final RealMatrix covMatrixUnderTest, final RealMatrix referenceCovarianceMatrix, final BufferedDataTable covTableUnderTest, final DataTableSpec spec, final boolean considerVarianceBzwDiagonal) {
// make sure, that we have data
assertTrue(covMatrixUnderTest.getColumnDimension() == spec.getNumColumns() && covMatrixUnderTest.getRowDimension() == spec.getNumColumns() && covTableUnderTest.getRowCount() == spec.getNumColumns());
assertEquals(referenceCovarianceMatrix.getColumnDimension(), covMatrixUnderTest.getColumnDimension());
assertEquals(referenceCovarianceMatrix.getRowDimension(), covMatrixUnderTest.getRowDimension());
int rowIndex = 0;
// check in memory and data table cov variance matrix
for (DataRow row : covTableUnderTest) {
for (int col = 0; col < referenceCovarianceMatrix.getRowDimension(); col++) {
// variance is on the diagonal
if (!(col == rowIndex) || considerVarianceBzwDiagonal) {
assertEquals("Col: " + col + " Row: " + rowIndex, referenceCovarianceMatrix.getEntry(rowIndex, col), covMatrixUnderTest.getEntry(rowIndex, col), 0.00001d);
assertEquals(referenceCovarianceMatrix.getEntry(rowIndex, col), ((DoubleValue) row.getCell(col)).getDoubleValue(), 0.00001d);
}
}
rowIndex++;
}
}
use of org.knime.core.data.DataRow 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