Search in sources :

Example 96 with DataRow

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);
}
Also used : DataTable(org.knime.core.data.DataTable) DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) Hashtable(java.util.Hashtable) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec)

Example 97 with DataRow

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());
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) Random(java.util.Random) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) Before(org.junit.Before)

Example 98 with DataRow

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])));
        }
    }
}
Also used : RowIterator(org.knime.core.data.RowIterator) DataRow(org.knime.core.data.DataRow)

Example 99 with DataRow

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++;
    }
}
Also used : DataRow(org.knime.core.data.DataRow)

Example 100 with DataRow

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
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) FileWriter(java.io.FileWriter) RowIterator(org.knime.core.data.RowIterator) File(java.io.File) DataRow(org.knime.core.data.DataRow) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Aggregations

DataRow (org.knime.core.data.DataRow)482 DataCell (org.knime.core.data.DataCell)268 DataTableSpec (org.knime.core.data.DataTableSpec)159 BufferedDataTable (org.knime.core.node.BufferedDataTable)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)109 RowKey (org.knime.core.data.RowKey)88 DefaultRow (org.knime.core.data.def.DefaultRow)88 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)80 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)76 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)73 DoubleValue (org.knime.core.data.DoubleValue)72 ArrayList (java.util.ArrayList)65 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)65 RowIterator (org.knime.core.data.RowIterator)62 DataType (org.knime.core.data.DataType)61 DoubleCell (org.knime.core.data.def.DoubleCell)57 StringCell (org.knime.core.data.def.StringCell)53 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)44 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)43