Search in sources :

Example 1 with BitVectorType

use of org.knime.core.data.vector.bitvector.BitVectorType in project knime-core by knime.

the class IdString2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    if (!row.getCell(getColumnIndex()).getType().isCompatible(StringValue.class)) {
        printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not a string value!");
        return DataType.getMissingCell();
    }
    if (row.getCell(getColumnIndex()).isMissing()) {
        return DataType.getMissingCell();
    }
    String toParse = ((StringValue) row.getCell(getColumnIndex())).getStringValue().trim();
    toParse = toParse.trim();
    try {
        int newlySetBits = 0;
        final BitVectorType type = getVectorType();
        final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(m_maxPos);
        if (!toParse.isEmpty()) {
            final String[] numbers = toParse.split("\\s");
            for (int i = 0; i < numbers.length; i++) {
                int pos = Integer.parseInt(numbers[i].trim());
                if (pos < 0) {
                    printError(LOGGER, row, "Invalid negative index in index string: " + toParse);
                    return DataType.getMissingCell();
                }
                if (!factory.get(pos)) {
                    factory.set(pos);
                    newlySetBits++;
                }
            }
        }
        m_nrOfSetBits += newlySetBits;
        return factory.createDataCell();
    } catch (NumberFormatException nfe) {
        String nfeMsg = nfe.getMessage();
        if (nfeMsg == null) {
            nfeMsg = "<sorry, no further details>";
        }
        printError(LOGGER, row, "Unable to convert \"" + toParse + "\" to " + "bit vector: " + nfeMsg);
        return DataType.getMissingCell();
    }
}
Also used : BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

Example 2 with BitVectorType

use of org.knime.core.data.vector.bitvector.BitVectorType in project knime-core by knime.

the class BitString2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    String bitString;
    DataCell cell = row.getCell(getColumnIndex());
    if (cell.isMissing()) {
        return DataType.getMissingCell();
    }
    if (!cell.getType().isCompatible(StringValue.class)) {
        printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not of type string.");
        return DataType.getMissingCell();
    }
    bitString = ((StringValue) cell).getStringValue().trim();
    final BitVectorType type = getVectorType();
    final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(bitString.length());
    int pos = 0;
    int numberOf0s = 0;
    int numberOf1s = 0;
    for (int i = bitString.length() - 1; i >= 0; i--) {
        char c = bitString.charAt(i);
        if (c == '0') {
            pos++;
            numberOf0s++;
        } else if (c == '1') {
            factory.set(pos++);
            numberOf1s++;
        } else {
            printError(LOGGER, row, "Invalid character ('" + c + "') in bitvector string");
            return DataType.getMissingCell();
        }
    }
    m_nrOfNotSetBits += numberOf0s;
    m_nrOfSetBits += numberOf1s;
    return factory.createDataCell();
}
Also used : DataCell(org.knime.core.data.DataCell) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

Example 3 with BitVectorType

use of org.knime.core.data.vector.bitvector.BitVectorType in project knime-core by knime.

the class Hex2BitVectorCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    incrementNrOfRows();
    final DataCell old = row.getCell(getColumnIndex());
    if (old.isMissing()) {
        return DataType.getMissingCell();
    }
    if (old instanceof StringValue) {
        String val = ((StringValue) old).getStringValue();
        DataCell newCell;
        try {
            String hexString = val.trim();
            BitVectorType type = getVectorType();
            BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(hexString);
            // hopefully int does it
            int card = (int) factory.cardinality();
            m_nrOfSetBits += card;
            m_nrOfNotSetBits += factory.length() - card;
            newCell = factory.createDataCell();
        } catch (IllegalArgumentException nfe) {
            String nfeMsg = nfe.getMessage();
            if (nfeMsg == null) {
                nfeMsg = "<sorry, no further details>";
            }
            printError(LOGGER, row, "Unable to convert \"" + val + "\" to bit vector: " + nfeMsg);
            newCell = DataType.getMissingCell();
        }
        return newCell;
    } else {
        printError(LOGGER, row, "Unable to convert \"" + old + "\" to bit vector, not a string value cell.");
        return DataType.getMissingCell();
    }
}
Also used : DataCell(org.knime.core.data.DataCell) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) StringValue(org.knime.core.data.StringValue)

Example 4 with BitVectorType

use of org.knime.core.data.vector.bitvector.BitVectorType in project knime-core by knime.

the class CreateBitVectorNodeModel method configure.

/**
 * Assume to get numeric data only. Output is one column of type BitVector.
 *
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    final DataTableSpec spec = inSpecs[0];
    // check the uniqueness of the output column
    if (spec.containsName(m_outputColumn.getStringValue())) {
        throw new InvalidSettingsException("Input table contains column with name " + m_outputColumn.getStringValue() + " please specifiy a new output column name.");
    }
    final ColumnType columnType = ColumnType.getType(m_columnType.getStringValue());
    final BitVectorType vectorType = BitVectorType.getType(m_vectorType.getStringValue());
    final DataTableSpec newSpec;
    if (columnType.isMultiColumn()) {
        final FilterResult multiColFilter = m_multiColumnsConfig.applyTo(spec);
        final String[] includes = multiColFilter.getIncludes();
        if (includes == null || includes.length < 1) {
            throw new InvalidSettingsException("No column selected in the multi column selection panel. Please (re-)configure the node.");
        }
        final String[] removedFromIncludes = multiColFilter.getRemovedFromIncludes();
        if (removedFromIncludes != null && removedFromIncludes.length > 0) {
            setWarningMessage("Columns " + convert2String(5, removedFromIncludes) + " not found in input table. ");
        }
        // create the output spec
        final DataColumnSpec newColSpec = createMultiColumnOutputSpec(spec, includes, vectorType);
        if (m_remove.getBooleanValue()) {
            final ColumnRearranger colR = new ColumnRearranger(spec);
            colR.remove(includes);
            newSpec = new DataTableSpec(colR.createSpec(), new DataTableSpec(newColSpec));
        } else {
            newSpec = new DataTableSpec(spec, new DataTableSpec(newColSpec));
        }
    } else {
        // parse from single column
        if (m_singleColumn.getStringValue() == null) {
            throw new InvalidSettingsException("No single column selected. Please (re-)configure the node.");
        }
        final int stringColIdx = spec.findColumnIndex(m_singleColumn.getStringValue());
        // -> check if selected column exists in the input table
        if (stringColIdx < 0) {
            throw new InvalidSettingsException("Selected column " + m_singleColumn.getStringValue() + " not in the input table");
        }
        // check that the data type is supported by the selected method
        final DataType selectedDataType = spec.getColumnSpec(m_singleColumn.getStringValue()).getType();
        if (!columnType.isCompatible(selectedDataType)) {
            throw new InvalidSettingsException("Data type of column " + m_singleColumn.getStringValue() + " is not compatible with selected method");
        }
        // create the output spec
        final ColumnRearranger c = createSingleColumnRearranger(inSpecs[0], stringColIdx, columnType, vectorType);
        newSpec = c.createSpec();
    }
    return new DataTableSpec[] { newSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 5 with BitVectorType

use of org.knime.core.data.vector.bitvector.BitVectorType in project knime-core by knime.

the class CreateBitVectorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable data = inData[0];
    final DataTableSpec spec = data.getDataTableSpec();
    final ColumnType columnType = ColumnType.getType(m_columnType.getStringValue());
    final BitVectorType vectorType = BitVectorType.getType(m_vectorType.getStringValue());
    final String[] parsedColumnNames;
    final BitVectorCellFactory factory;
    if (columnType.isMultiColumn()) {
        final FilterResult multiColFilter = m_multiColumnsConfig.applyTo(spec);
        parsedColumnNames = multiColFilter.getIncludes();
        factory = createMultiColumnCellFactory(data, exec, columnType, vectorType, parsedColumnNames);
    } else {
        final int colIdx = spec.findColumnIndex(m_singleColumn.getStringValue());
        factory = getSingleColFactory(exec, colIdx, spec, data, columnType, vectorType);
        parsedColumnNames = new String[] { m_singleColumn.getStringValue() };
    }
    final ColumnRearranger c = new ColumnRearranger(spec);
    if (m_remove.getBooleanValue()) {
        if (columnType.isMultiColumn()) {
            c.remove(parsedColumnNames);
            c.append(factory);
        } else {
            c.replace(factory, m_singleColumn.getStringValue());
        }
    } else {
        c.append(factory);
    }
    factory.setFailOnError(m_failOnError.getBooleanValue());
    final ExecutionMonitor subExec;
    if (ColumnType.MULTI_NUMERICAL.equals(columnType) || (ColumnType.MULTI_NUMERICAL.equals(columnType) && StringType.ID.equals(StringType.getType(m_singleStringColumnType.getStringValue()))) || ColumnType.SINGLE_COLLECTION.equals(columnType)) {
        subExec = exec.createSubProgress(0.5);
    } else {
        subExec = exec;
    }
    final BufferedDataTable out = exec.createColumnRearrangeTable(data, c, subExec);
    if (!factory.wasSuccessful() && data.size() > 0) {
        final String errorMessage = factory.getNoOfPrintedErrors() + " errors found. Last message: " + factory.getLastErrorMessage() + ". See log file for details on all errors.";
        setWarningMessage(errorMessage);
    }
    m_nrOfProcessedRows = factory.getNrOfProcessedRows();
    m_totalNrOf0s = factory.getNumberOfNotSetBits();
    m_totalNrOf1s = factory.getNumberOfSetBits();
    return new BufferedDataTable[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) 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) BufferedDataTable(org.knime.core.node.BufferedDataTable) BitVectorType(org.knime.core.data.vector.bitvector.BitVectorType) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) ExecutionMonitor(org.knime.core.node.ExecutionMonitor)

Aggregations

BitVectorType (org.knime.core.data.vector.bitvector.BitVectorType)5 StringValue (org.knime.core.data.StringValue)3 DataCell (org.knime.core.data.DataCell)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 FilterResult (org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)2 BitString2BitVectorCellFactory (org.knime.base.data.bitvector.BitString2BitVectorCellFactory)1 BitVectorCellFactory (org.knime.base.data.bitvector.BitVectorCellFactory)1 Collection2BitVectorCellFactory (org.knime.base.data.bitvector.Collection2BitVectorCellFactory)1 Hex2BitVectorCellFactory (org.knime.base.data.bitvector.Hex2BitVectorCellFactory)1 IdString2BitVectorCellFactory (org.knime.base.data.bitvector.IdString2BitVectorCellFactory)1 MultiString2BitVectorCellFactory (org.knime.base.data.bitvector.MultiString2BitVectorCellFactory)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataType (org.knime.core.data.DataType)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1