Search in sources :

Example 1 with DenseByteVectorCellFactory

use of org.knime.core.data.vector.bytevector.DenseByteVectorCellFactory in project knime-core by knime.

the class CreateByteVectorNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec) {
    final ColumnRearranger ret = new ColumnRearranger(inSpec);
    final String[] includes = m_inputColumns.applyTo(inSpec).getIncludes();
    if (m_removeInput.getBooleanValue()) {
        ret.remove(includes);
    }
    final DataColumnSpecCreator newCol = new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(inSpec, m_outputColumn.getStringValue()), DenseByteVectorCell.TYPE);
    newCol.setElementNames(includes);
    final int[] sourceColumnIndices = SourceColumnsAsProperties.indices(m_inputColumns.applyTo(inSpec), inSpec);
    for (int i = sourceColumnIndices.length; i-- > 0; ) {
        if (sourceColumnIndices[i] < 0) {
            throw new IllegalStateException("Unknown column: " + includes[i]);
        }
    }
    ret.append(new SingleCellFactory(newCol.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            final DenseByteVectorCellFactory fac = new DenseByteVectorCellFactory(sourceColumnIndices.length);
            for (int i = sourceColumnIndices.length; i-- > 0; ) {
                DataCell cell = row.getCell(sourceColumnIndices[i]);
                if (cell.isMissing()) {
                    if (m_failOnMissing.getBooleanValue()) {
                        throw new IllegalArgumentException("Missing value in the row: " + row.getKey() + "\nin the column: " + includes[i]);
                    } else {
                        // return DataType.getMissingCell();
                        fac.setValue(i, 0);
                    }
                } else if (cell instanceof IntValue) {
                    int intValue = ((IntValue) cell).getIntValue();
                    if (intValue < 0 || intValue > 255) {
                        if (m_failOnOutOfInterval.getBooleanValue()) {
                            throw new IllegalArgumentException("Invalid value: " + intValue + "\nin row: " + row.getKey() + "\nin the column: " + includes[i]);
                        } else {
                            fac.setValue(i, 0);
                        }
                    } else {
                        fac.setValue(i, intValue);
                    }
                } else {
                    throw new IllegalStateException("Not an int value: " + cell + " (" + cell.getType() + ")");
                }
            }
            return fac.createDataCell();
        }
    });
    return ret;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DenseByteVectorCellFactory(org.knime.core.data.vector.bytevector.DenseByteVectorCellFactory) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) IntValue(org.knime.core.data.IntValue)

Aggregations

DataCell (org.knime.core.data.DataCell)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataRow (org.knime.core.data.DataRow)1 IntValue (org.knime.core.data.IntValue)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)1 DenseByteVectorCellFactory (org.knime.core.data.vector.bytevector.DenseByteVectorCellFactory)1 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)1