Search in sources :

Example 31 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class DataContainerTest method testIncompatibleTypes.

/**
 * method being tested: addRowToTable().
 */
public final void testIncompatibleTypes() {
    String[] colNames = new String[] { "Column 1", "Column 2" };
    DataType[] colTypes = new DataType[] { StringCell.TYPE, IntCell.TYPE };
    DataTableSpec spec1 = new DataTableSpec(colNames, colTypes);
    DataContainer c = new DataContainer(spec1);
    RowKey r1Key = new RowKey("row 1");
    DataCell r1Cell1 = new StringCell("Row 1, Cell 1");
    DataCell r1Cell2 = new IntCell(12);
    DataRow r1 = new DefaultRow(r1Key, new DataCell[] { r1Cell1, r1Cell2 });
    RowKey r2Key = new RowKey("row 2");
    DataCell r2Cell1 = new StringCell("Row 2, Cell 1");
    DataCell r2Cell2 = new IntCell(22);
    DataRow r2 = new DefaultRow(r2Key, new DataCell[] { r2Cell1, r2Cell2 });
    RowKey r3Key = new RowKey("row 3");
    DataCell r3Cell1 = new StringCell("Row 3, Cell 1");
    DataCell r3Cell2 = new IntCell(32);
    DataRow r3 = new DefaultRow(r3Key, new DataCell[] { r3Cell1, r3Cell2 });
    c.addRowToTable(r1);
    c.addRowToTable(r2);
    c.addRowToTable(r3);
    // add incompatible types
    RowKey r4Key = new RowKey("row 4");
    DataCell r4Cell1 = new StringCell("Row 4, Cell 1");
    // not allowed
    DataCell r4Cell2 = new DoubleCell(42.0);
    DataRow r4 = new DefaultRow(r4Key, new DataCell[] { r4Cell1, r4Cell2 });
    try {
        c.addRowToTable(r4);
        c.close();
        fail("Expected " + DataContainerException.class + " not thrown");
    } catch (DataContainerException e) {
        if (!(e.getCause() instanceof IllegalArgumentException)) {
            throw e;
        } else {
            NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getCause().getClass(), e.getCause());
        }
    } catch (IllegalArgumentException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass(), e);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 32 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class DataCellToJavaConversionTest method testCollectionTypes.

/**
 * Test ListCell(IntCell) -> Integer[] conversion.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testCollectionTypes() throws Exception {
    ArrayList<DataCell> coll = new ArrayList<>();
    for (int i = 0; i < 5; ++i) {
        coll.add(new IntCell(i * i));
    }
    // collection cells can always contain missing cells.
    coll.add(new MissingCell("42"));
    final ListCell listCell = CollectionCellFactory.createListCell(coll);
    final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[].class).stream().findFirst();
    assertTrue(factory.isPresent());
    final DataCellToJavaConverter<DataCell, Integer[]> converter = (DataCellToJavaConverter<DataCell, Integer[]>) factory.get().create();
    assertNotNull(converter);
    final Integer[] array = converter.convert(listCell);
    for (int i = 0; i < 5; ++i) {
        assertEquals(new Integer(i * i), array[i]);
    }
    assertNull(array[5]);
}
Also used : DataCellToJavaConverter(org.knime.core.data.convert.java.DataCellToJavaConverter) MissingCell(org.knime.core.data.MissingCell) ListCell(org.knime.core.data.collection.ListCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell) BinaryObjectDataCell(org.knime.core.data.blob.BinaryObjectDataCell) IntCell(org.knime.core.data.def.IntCell) Test(org.junit.Test)

Example 33 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class AdapterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    if (getNrInPorts() == 0 && getNrOutPorts() == 1) {
        // assume simple source node with one table output
        BufferedDataContainer cnt = exec.createDataContainer(createDefaultOutputSpec());
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new DataCell[] { new StringCell("Cell-1.1"), new IntCell(12), new DoubleCell(1.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(1), new DataCell[] { new StringCell("Cell-2.1"), new IntCell(22), new DoubleCell(2.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(2), new DataCell[] { new StringCell("Cell-3.1"), new IntCell(32), new DoubleCell(3.3) }));
        cnt.close();
        return new BufferedDataTable[] { cnt.getTable() };
    }
    return inObjects;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 34 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class AddEmptyRowsNodeModel method createNewRowsTable.

private BufferedDataTable createNewRowsTable(final DataTableSpec inSpec, final long rowCount, final ExecutionContext subExec) throws CanceledExecutionException {
    DataCell[] cells = new DataCell[inSpec.getNumColumns()];
    for (int c = 0; c < cells.length; c++) {
        DataType type = inSpec.getColumnSpec(c).getType();
        if (type.isASuperTypeOf(DoubleCell.TYPE)) {
            if (m_config.isUseMissingDouble()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new DoubleCell(m_config.getFillValueDouble());
            }
        } else if (type.isASuperTypeOf(IntCell.TYPE)) {
            if (m_config.isUseMissingInt()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new IntCell(m_config.getFillValueInt());
            }
        } else if (type.isASuperTypeOf(StringCell.TYPE)) {
            if (m_config.isUseMissingString()) {
                cells[c] = DataType.getMissingCell();
            } else {
                cells[c] = new StringCell(m_config.getFillValueString());
            }
        } else {
            cells[c] = DataType.getMissingCell();
        }
    }
    BufferedDataContainer cont = subExec.createDataContainer(inSpec);
    for (long i = 0; i < rowCount; i++) {
        RowKey key = new RowKey(m_config.getNewRowKeyPrefix() + i);
        subExec.setProgress(i / (double) rowCount, "Creating row \"" + key + "\", " + i + "/" + rowCount);
        subExec.checkCanceled();
        cont.addRowToTable(new DefaultRow(key, cells));
    }
    cont.close();
    return cont.getTable();
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 35 with IntCell

use of org.knime.core.data.def.IntCell in project knime-core by knime.

the class AggregateOutputNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // retrieve variables from the stack which the head of this
    // loop hopefully put there:
    int count;
    int maxCount;
    try {
        count = peekFlowVariableInt("currentIteration");
        maxCount = peekFlowVariableInt("maxIterations");
    } catch (NoSuchElementException e) {
        throw new Exception("No matching Loop Start node!", e);
    }
    if (count < 0 || count >= maxCount) {
        throw new Exception("Conflicting loop variables, count is " + count + " and max count is " + maxCount);
    }
    final BufferedDataTable in = inData[0];
    final DataTableSpec inSpec = in.getDataTableSpec();
    if (count == 0) {
        m_firstIterationSpec = in.getDataTableSpec();
        m_predictionTable = exec.createDataContainer(createPredictionSpec(in.getDataTableSpec()));
    } else if (m_predictionTable == null) {
        throw new Exception("Loop Head claims this is NOT the first iteration" + " but the tail believes it is?!");
    } else {
        if (!inSpec.equalStructure(m_firstIterationSpec)) {
            StringBuilder error = new StringBuilder("Input table's structure differs from reference " + "(first iteration) table: ");
            if (inSpec.getNumColumns() != m_firstIterationSpec.getNumColumns()) {
                error.append("different column counts ");
                error.append(inSpec.getNumColumns());
                error.append(" vs. ").append(m_firstIterationSpec.getNumColumns());
            } else {
                for (int i = 0; i < inSpec.getNumColumns(); i++) {
                    DataColumnSpec inCol = inSpec.getColumnSpec(i);
                    DataColumnSpec predCol = m_firstIterationSpec.getColumnSpec(i);
                    if (!inCol.equalStructure(predCol)) {
                        error.append("Column ").append(i).append(" [");
                        error.append(inCol).append("] vs. [");
                        error.append(predCol).append("]");
                    }
                }
            }
            throw new IllegalArgumentException(error.toString());
        }
    }
    final int rowCount = in.getRowCount();
    final int targetColIndex = in.getDataTableSpec().findColumnIndex(m_settings.targetColumn());
    final int predictColIndex = in.getDataTableSpec().findColumnIndex(m_settings.predictionColumn());
    final boolean numericMode = in.getDataTableSpec().getColumnSpec(predictColIndex).getType().isCompatible(DoubleValue.class);
    ExecutionMonitor subExec = exec.createSubProgress(count == maxCount - 1 ? 0.9 : 1);
    final DataCell foldNumber = new IntCell(m_foldStatistics.size());
    if (numericMode) {
        double errorSum = 0;
        int r = 0;
        for (DataRow row : in) {
            RowKey key = row.getKey();
            DoubleValue target = (DoubleValue) row.getCell(targetColIndex);
            DoubleValue predict = (DoubleValue) row.getCell(predictColIndex);
            double d = (target.getDoubleValue() - predict.getDoubleValue());
            errorSum += d * d;
            r++;
            if (m_settings.addFoldId()) {
                m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
            } else {
                m_predictionTable.addRowToTable(row);
            }
            subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
            subExec.checkCanceled();
        }
        DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(errorSum), new DoubleCell(errorSum / rowCount), new IntCell(rowCount));
        m_foldStatistics.add(stats);
    } else {
        int incorrect = 0;
        int r = 0;
        for (DataRow row : in) {
            RowKey key = row.getKey();
            DataCell target = row.getCell(targetColIndex);
            DataCell predict = row.getCell(predictColIndex);
            if (!target.equals(predict)) {
                incorrect++;
            }
            r++;
            if (m_settings.addFoldId()) {
                m_predictionTable.addRowToTable(new AppendedColumnRow(row.getKey(), row, foldNumber));
            } else {
                m_predictionTable.addRowToTable(row);
            }
            subExec.setProgress(r / (double) rowCount, "Calculating output " + r + "/" + rowCount + " (\"" + key + "\")");
            subExec.checkCanceled();
        }
        DataRow stats = new DefaultRow(new RowKey("fold " + m_foldStatistics.size()), new DoubleCell(100.0 * incorrect / rowCount), new IntCell(rowCount), new IntCell(incorrect));
        m_foldStatistics.add(stats);
    }
    if (count < maxCount - 1) {
        continueLoop();
        return new BufferedDataTable[2];
    } else {
        BufferedDataContainer cont = exec.createDataContainer(numericMode ? NUMERIC_STATISTICS_SPEC : NOMINAL_STATISTICS_SPEC);
        for (DataRow row : m_foldStatistics) {
            cont.addRowToTable(row);
        }
        cont.close();
        m_predictionTable.close();
        return new BufferedDataTable[] { m_predictionTable.getTable(), cont.getTable() };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) IntCell(org.knime.core.data.def.IntCell) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow) NoSuchElementException(java.util.NoSuchElementException) AppendedColumnRow(org.knime.base.data.append.column.AppendedColumnRow)

Aggregations

IntCell (org.knime.core.data.def.IntCell)109 DataCell (org.knime.core.data.DataCell)79 DoubleCell (org.knime.core.data.def.DoubleCell)67 StringCell (org.knime.core.data.def.StringCell)55 DefaultRow (org.knime.core.data.def.DefaultRow)46 DataRow (org.knime.core.data.DataRow)33 DataTableSpec (org.knime.core.data.DataTableSpec)21 RowKey (org.knime.core.data.RowKey)21 ArrayList (java.util.ArrayList)20 DataType (org.knime.core.data.DataType)20 LongCell (org.knime.core.data.def.LongCell)14 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)14 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 BufferedDataTable (org.knime.core.node.BufferedDataTable)12 Test (org.junit.Test)11 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)9 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 DataContainer (org.knime.core.data.container.DataContainer)8 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)8