Search in sources :

Example 36 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class AppendedRowsTableTest method testGetRowIterator.

/**
 * Test method for getRowIterator().
 */
public void testGetRowIterator() {
    DataTable firstTable = new DefaultTable(DATA, DATA_H, DATA_TYPES);
    DataTable firstTableShuffle = new DefaultTable(DATA_SHUFFLE, DATA_SHUFFLE_H, DATA_SHUFFLE_TYPES);
    DataTable ap = new AppendedRowsTable(new DataTable[] { firstTable, firstTableShuffle });
    RowIterator apIt = ap.iterator();
    for (RowIterator fiIt = firstTable.iterator(); fiIt.hasNext(); ) {
        assertTrue(apIt.hasNext());
        DataRow apRow = apIt.next();
        DataRow fiRow = fiIt.next();
        assertEquals(apRow.getKey(), fiRow.getKey());
        assertEquals(apRow.getCell(0), fiRow.getCell(0));
        assertEquals(apRow.getCell(1), fiRow.getCell(1));
        assertEquals(apRow.getCell(2), fiRow.getCell(2));
    }
    for (RowIterator seIt = firstTableShuffle.iterator(); seIt.hasNext(); ) {
        assertTrue(apIt.hasNext());
        DataRow apRow = apIt.next();
        DataRow seRow = seIt.next();
        assertEquals(apRow.getKey(), seRow.getKey());
        // first and second are swapped!
        assertEquals(apRow.getCell(0), seRow.getCell(1));
        assertEquals(apRow.getCell(1), seRow.getCell(0));
        assertEquals(apRow.getCell(2), seRow.getCell(2));
    }
    assertFalse(apIt.hasNext());
    DataTable duplicateTable = new AppendedRowsTable(new DataTable[] { firstTable, firstTable });
    RowIterator dupIt = duplicateTable.iterator();
    for (RowIterator fiIt = firstTable.iterator(); fiIt.hasNext(); ) {
        dupIt.next();
        fiIt.next();
    }
    // it should not return duplicate keys.
    assertFalse(dupIt.hasNext());
}
Also used : DataTable(org.knime.core.data.DataTable) RowIterator(org.knime.core.data.RowIterator) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow)

Example 37 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class JoinedTableTest method checkForEquality.

private static int checkForEquality(final DataTable merge, final DataRow[] left, final Hashtable<RowKey, DataRow> rightHash) {
    int rowC = 0;
    for (RowIterator it = merge.iterator(); it.hasNext(); rowC++) {
        DataRow next = it.next();
        RowKey nextKey = next.getKey();
        DataRow rightNext = rightHash.get(nextKey);
        DataRow leftNext = (rowC < left.length ? left[rowC] : null);
        // at most one can be missing
        if (rightNext == null) {
            rightNext = new DefaultRow(nextKey, MISSINGS);
        } else if (leftNext == null) {
            leftNext = new DefaultRow(nextKey, MISSINGS);
        }
        assertEquals(next.getNumCells(), COLS.length);
        assertEquals(next.getKey().getString(), leftNext.getKey().getString());
        for (int i = 0; i < COLS.length; i++) {
            DataCell cell = next.getCell(i);
            DataCell compareCell;
            if (i < 3) {
                compareCell = leftNext.getCell(i);
            } else {
                compareCell = rightNext.getCell(i - 3);
            }
            assertEquals(cell, compareCell);
        }
    }
    return rowC;
}
Also used : RowKey(org.knime.core.data.RowKey) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow)

Example 38 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class FilterRowIteratorTest method check.

/*
     * Used for all test* methods to check if all only these c's appear in the
     * filer. @param d Initial array of possible double values. @param c An
     * array of double values to check.
     */
private void check(final double[] d, final double[] c) {
    FilterRowGenerator gen = new MyFilterRowGenerator(c);
    RowIterator it = new FilterRowIterator(new MyRowIterator(d), gen);
    ArrayList<Double> list = new ArrayList<Double>();
    // copy c's in list to remove them easily
    for (int i = 0; i < c.length; i++) {
        list.add(c[i]);
    }
    // check all r's in list of c's
    for (int i = 0; it.hasNext(); i++) {
        DataRow row = it.next();
        double r = ((DoubleValue) row.getCell(0)).getDoubleValue();
        assertTrue(list.remove(r));
    }
    // check for no d's in list
    for (int i = 0; i < d.length; i++) {
        assertFalse(list.remove(d[i]));
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) RowIterator(org.knime.core.data.RowIterator) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow)

Example 39 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class ARFFTableTest method testARFFwithMissingSpace.

/**
 * Customer file. Weka is able to read it. We failed on the missing space
 * in the last "@attribute" line.
 *
 * @throws IOException some time.
 * @throws InvalidSettingsException sometimes.
 */
public void testARFFwithMissingSpace() throws IOException, InvalidSettingsException {
    final String missingSpace = "@relation kredit_bereinigt\n" + "\n" + "@attribute REPAYMENT_PROBLEM {0,1}\n" + /* Col 0*/
    "@attribute RSV {0,1}\n" + /* Col 1*/
    "@attribute GENDER {0,1}\n" + /* Col 2*/
    "@attribute AGE real\n" + /* Col 3*/
    "@attribute PHONE {0,1}\n" + /* Col 4*/
    "@attribute NUMBER_CHILDREN real\n" + /* Col 5*/
    "@attribute ADDRESS_CHANGED {0,1}\n" + /* Col 6*/
    "@attribute GUARANTOR {0,1}\n" + /* Col 7*/
    "@attribute JOB_DURATION real\n" + /* Col 8*/
    "@attribute INCOME real\n" + /* Col 9*/
    "@attribute DISP_INCOME real\n" + /* Col 10*/
    "@attribute RENTAL_FEE real\n" + /* Col 11*/
    "@attribute CAR {0,1}\n" + /* Col 12*/
    "@attribute OTHER_CONTRACTS {0,1}\n" + /* Col 13*/
    "@attribute OTHER_LOANS {0,1}\n" + /* Col 14*/
    "@attribute EXPENSE real\n" + /* Col 15*/
    "@attribute SAVINGS {0,1}\n" + /* following line is missing a space */
    "@attribute STOCK{0,1}\n" + /* Col 17*/
    "\n" + "@data\n" + "1,1,1,27,0,1,1,1,2,2900,1335,330,1,0,0,1565,1,0\n" + "1,1,0,28,1,0,1,0,20,2000,1100,150,0,1,0,900,1,0\n";
    File tempFile = File.createTempFile("ARFFReaderUnitTest", "missSpace");
    tempFile.deleteOnExit();
    Writer out = new BufferedWriter(new FileWriter(tempFile));
    out.write(missingSpace);
    out.close();
    try {
        ARFFTable table = new ARFFTable(tempFile.toURI().toURL(), ARFFTable.createDataTableSpecFromARFFfile(tempFile.toURI().toURL(), null), "Row");
        assertEquals(table.getDataTableSpec().getNumColumns(), 18);
        assertEquals(table.getDataTableSpec().getColumnSpec(0).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(1).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(2).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(3).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(4).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(5).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(6).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(7).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(8).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(9).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(10).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(11).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(12).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(13).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(14).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(15).getType(), DoubleCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(16).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(17).getType(), StringCell.TYPE);
        /*
        + "1,1,1,27,0,1,1,1,2,2900,1335,330,1,0,0,1565,1,0\n"
*/
        DataRow row;
        RowIterator rIter = table.iterator();
        assertTrue(rIter.hasNext());
        row = rIter.next();
        assertEquals(row.getKey().toString(), "Row0");
        assertEquals(row.getCell(0).toString(), "1");
        assertEquals(row.getCell(1).toString(), "1");
        assertEquals(row.getCell(2).toString(), "1");
        assertEquals(row.getCell(3).toString(), "27.0");
        assertEquals(row.getCell(4).toString(), "0");
        assertEquals(row.getCell(5).toString(), "1.0");
        assertEquals(row.getCell(6).toString(), "1");
        assertEquals(row.getCell(7).toString(), "1");
        assertEquals(row.getCell(8).toString(), "2.0");
        assertEquals(row.getCell(9).toString(), "2900.0");
        assertEquals(row.getCell(10).toString(), "1335.0");
        assertEquals(row.getCell(11).toString(), "330.0");
        assertEquals(row.getCell(12).toString(), "1");
        assertEquals(row.getCell(13).toString(), "0");
        assertEquals(row.getCell(14).toString(), "0");
        assertEquals(row.getCell(15).toString(), "1565.0");
        assertEquals(row.getCell(16).toString(), "1");
        assertEquals(row.getCell(17).toString(), "0");
        /*
 *         + "1,1,0,28,1,0,1,0,20,2000,1100,150,0,1,0,900,1,0\n";
 */
        assertTrue(rIter.hasNext());
        row = rIter.next();
        assertEquals(row.getKey().toString(), "Row1");
        assertEquals(row.getCell(0).toString(), "1");
        assertEquals(row.getCell(1).toString(), "1");
        assertEquals(row.getCell(2).toString(), "0");
        assertEquals(row.getCell(3).toString(), "28.0");
        assertEquals(row.getCell(4).toString(), "1");
        assertEquals(row.getCell(5).toString(), "0.0");
        assertEquals(row.getCell(6).toString(), "1");
        assertEquals(row.getCell(7).toString(), "0");
        assertEquals(row.getCell(8).toString(), "20.0");
        assertEquals(row.getCell(9).toString(), "2000.0");
        assertEquals(row.getCell(10).toString(), "1100.0");
        assertEquals(row.getCell(11).toString(), "150.0");
        assertEquals(row.getCell(12).toString(), "0");
        assertEquals(row.getCell(13).toString(), "1");
        assertEquals(row.getCell(14).toString(), "0");
        assertEquals(row.getCell(15).toString(), "900.0");
        assertEquals(row.getCell(16).toString(), "1");
        assertEquals(row.getCell(17).toString(), "0");
        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)

Example 40 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class JoinerJoinAnyTest method toIntegerArray.

/**
 * Get the data of an DataTable as an integer array.
 * @param dataTable the data table
 * @return the data as an integer array
 */
private Integer[][] toIntegerArray(final BufferedDataTable dataTable) {
    int rowCount = ConvenienceMethods.checkTableSize(dataTable.size());
    int colCount = dataTable.getDataTableSpec().getNumColumns();
    Integer[][] data = new Integer[rowCount][colCount];
    RowIterator iter = dataTable.iterator();
    int r = 0;
    while (iter.hasNext()) {
        DataRow row = iter.next();
        Iterator<DataCell> cellIter = row.iterator();
        int c = 0;
        while (cellIter.hasNext()) {
            DataCell cell = cellIter.next();
            data[r][c] = !cell.isMissing() ? ((IntCell) cell).getIntValue() : null;
            c++;
        }
        r++;
    }
    return data;
}
Also used : RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell)

Aggregations

RowIterator (org.knime.core.data.RowIterator)77 DataRow (org.knime.core.data.DataRow)62 DataCell (org.knime.core.data.DataCell)28 DataTableSpec (org.knime.core.data.DataTableSpec)20 RowKey (org.knime.core.data.RowKey)16 DoubleValue (org.knime.core.data.DoubleValue)14 BufferedDataTable (org.knime.core.node.BufferedDataTable)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 ArrayList (java.util.ArrayList)9 DefaultRow (org.knime.core.data.def.DefaultRow)8 PreparedStatement (java.sql.PreparedStatement)7 DataType (org.knime.core.data.DataType)6 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 HashSet (java.util.HashSet)5 Random (java.util.Random)5 TimeZone (java.util.TimeZone)5 DataTable (org.knime.core.data.DataTable)5 DoubleCell (org.knime.core.data.def.DoubleCell)5 StringCell (org.knime.core.data.def.StringCell)5 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)5