Search in sources :

Example 1 with BitString2BitVectorCellFactory

use of org.knime.base.data.bitvector.BitString2BitVectorCellFactory in project knime-core by knime.

the class BitVectorGeneratorNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec, final int colIdx) throws InvalidSettingsException {
    // BW: fixed here locally: the annotation and the column name
    // are taken from input spec (21 Sep 2006)
    DataColumnSpecCreator creator = new DataColumnSpecCreator(spec.getColumnSpec(colIdx));
    creator.setDomain(null);
    creator.setType(DenseBitVectorCell.TYPE);
    if (!m_replace) {
        String colName = spec.getColumnSpec(colIdx).getName() + "_bits";
        creator.setName(colName);
        if (spec.containsName(colName)) {
            throw new InvalidSettingsException("Column " + colName + " already exist in table!");
        }
    }
    if (m_type.equals(STRING_TYPES.BIT)) {
        m_factory = new BitString2BitVectorCellFactory(creator.createSpec(), colIdx);
    } else if (m_type.equals(STRING_TYPES.HEX)) {
        m_factory = new Hex2BitVectorCellFactory(creator.createSpec(), colIdx);
    } else if (m_type.equals(STRING_TYPES.ID)) {
        m_factory = new IdString2BitVectorCellFactory(creator.createSpec(), colIdx);
    } else {
        throw new InvalidSettingsException("String type to parse bitvectors" + " from unknown!");
    }
    ColumnRearranger c = new ColumnRearranger(spec);
    if (m_replace) {
        c.replace(m_factory, colIdx);
    } else {
        c.append(m_factory);
    }
    return c;
}
Also used : Hex2BitVectorCellFactory(org.knime.base.data.bitvector.Hex2BitVectorCellFactory) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) IdString2BitVectorCellFactory(org.knime.base.data.bitvector.IdString2BitVectorCellFactory) BitString2BitVectorCellFactory(org.knime.base.data.bitvector.BitString2BitVectorCellFactory)

Example 2 with BitString2BitVectorCellFactory

use of org.knime.base.data.bitvector.BitString2BitVectorCellFactory in project knime-core by knime.

the class CreateBitVectorNodeModel method getSingleColFactory.

private BitVectorCellFactory getSingleColFactory(final ExecutionMonitor exec, final int colIdx, final DataTableSpec spec, final BufferedDataTable data, final ColumnType columnType, final BitVectorType vectorType) throws InvalidSettingsException, CanceledExecutionException {
    final String outColName = m_outputColumn.getStringValue();
    final DataColumnSpecCreator creator = new DataColumnSpecCreator(outColName, vectorType.getCellDataType());
    final BitVectorCellFactory factory;
    if (ColumnType.SINGLE_STRING.equals(columnType)) {
        final StringType singleStringColType = StringType.getType(m_singleStringColumnType.getStringValue());
        final DataColumnSpec colSpec = creator.createSpec();
        switch(singleStringColType) {
            case BIT:
                factory = new BitString2BitVectorCellFactory(vectorType, colSpec, colIdx);
                break;
            case HEX:
                factory = new Hex2BitVectorCellFactory(vectorType, colSpec, colIdx);
                break;
            case ID:
                final int maxPosition;
                if (data != null) {
                    final ExecutionMonitor scanExec = exec.createSubProgress(0.5);
                    exec.setMessage("preparing");
                    maxPosition = scanMaxPos(data, scanExec);
                } else {
                    maxPosition = 0;
                }
                factory = new IdString2BitVectorCellFactory(vectorType, colSpec, colIdx, maxPosition);
                break;
            default:
                throw new InvalidSettingsException("String type to parse bit vectors from unknown type " + singleStringColType.getActionCommand());
        }
    } else if (ColumnType.SINGLE_COLLECTION.equals(columnType)) {
        final Map<String, Integer> idxMap;
        if (data != null) {
            final ExecutionMonitor scanExec = exec.createSubProgress(0.5);
            scanExec.setMessage("preparing");
            final List<String> elementNames = new ArrayList<>();
            idxMap = new HashMap<>();
            long nrRows = data.size();
            long currRow = 0;
            for (DataRow row : data) {
                currRow++;
                scanExec.setProgress((double) currRow / (double) nrRows, "Counting uniqe elements. Processing row " + currRow + " of " + nrRows);
                scanExec.checkCanceled();
                final DataCell cell = row.getCell(colIdx);
                if (cell.isMissing()) {
                    // ignore missing cells
                    continue;
                }
                if (cell instanceof CollectionDataValue) {
                    final CollectionDataValue collCell = (CollectionDataValue) cell;
                    for (DataCell collVal : collCell) {
                        String stringRep = collVal.toString();
                        Integer idx = idxMap.get(stringRep);
                        if (idx == null) {
                            idx = Integer.valueOf(idxMap.size());
                            idxMap.put(stringRep, idx);
                            elementNames.add(stringRep);
                        }
                    }
                } else {
                    throw new RuntimeException("Found incompatible type in row " + row.getKey().getString());
                }
            }
            creator.setElementNames(elementNames.toArray(new String[0]));
        } else {
            idxMap = Collections.EMPTY_MAP;
        }
        factory = new Collection2BitVectorCellFactory(vectorType, creator.createSpec(), colIdx, idxMap);
    } else {
        throw new java.lang.IllegalStateException("Single column type not implemented: " + columnType);
    }
    return factory;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashMap(java.util.HashMap) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Collection2BitVectorCellFactory(org.knime.base.data.bitvector.Collection2BitVectorCellFactory) DataRow(org.knime.core.data.DataRow) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) Hex2BitVectorCellFactory(org.knime.base.data.bitvector.Hex2BitVectorCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) BitVectorCellFactory(org.knime.base.data.bitvector.BitVectorCellFactory) Collection2BitVectorCellFactory(org.knime.base.data.bitvector.Collection2BitVectorCellFactory) Hex2BitVectorCellFactory(org.knime.base.data.bitvector.Hex2BitVectorCellFactory) IdString2BitVectorCellFactory(org.knime.base.data.bitvector.IdString2BitVectorCellFactory) BitString2BitVectorCellFactory(org.knime.base.data.bitvector.BitString2BitVectorCellFactory) MultiString2BitVectorCellFactory(org.knime.base.data.bitvector.MultiString2BitVectorCellFactory) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) IdString2BitVectorCellFactory(org.knime.base.data.bitvector.IdString2BitVectorCellFactory) Map(java.util.Map) HashMap(java.util.HashMap) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) BitString2BitVectorCellFactory(org.knime.base.data.bitvector.BitString2BitVectorCellFactory)

Aggregations

BitString2BitVectorCellFactory (org.knime.base.data.bitvector.BitString2BitVectorCellFactory)2 Hex2BitVectorCellFactory (org.knime.base.data.bitvector.Hex2BitVectorCellFactory)2 IdString2BitVectorCellFactory (org.knime.base.data.bitvector.IdString2BitVectorCellFactory)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BitVectorCellFactory (org.knime.base.data.bitvector.BitVectorCellFactory)1 Collection2BitVectorCellFactory (org.knime.base.data.bitvector.Collection2BitVectorCellFactory)1 MultiString2BitVectorCellFactory (org.knime.base.data.bitvector.MultiString2BitVectorCellFactory)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataRow (org.knime.core.data.DataRow)1 CollectionDataValue (org.knime.core.data.collection.CollectionDataValue)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)1 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)1