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();
}
}
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();
}
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();
}
}
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 };
}
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 };
}
Aggregations