use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class BitVectorGeneratorNodeModel method createBitVectorsFromNumericData.
private BufferedDataTable[] createBitVectorsFromNumericData(final BufferedDataTable data, final ExecutionContext exec) throws CanceledExecutionException {
DataColumnSpec colSpec = createNumericOutputSpec(data.getDataTableSpec());
// get the indices for included columns
List<Integer> colIndices = new ArrayList<Integer>();
for (String colName : m_includedColumns.getIncludeList()) {
int index = data.getDataTableSpec().findColumnIndex(colName);
if (index < 0) {
throw new IllegalArgumentException("Column " + colName + " is not available in " + "current data. Please re-configure the node.");
}
colIndices.add(index);
}
// calculate bits from numeric data
if (m_useMean) {
// either from a percentage of the mean
double[] meanValues = new double[0];
double meanFactor = m_meanPercentage / 100.0;
meanValues = calculateMeanValues(data);
m_factory = new Numeric2BitVectorMeanCellFactory(colSpec, meanValues, meanFactor, colIndices);
} else {
// or dependend on fixed threshold
m_factory = new Numeric2BitVectorThresholdCellFactory(colSpec, m_threshold, colIndices);
}
ColumnRearranger c = new ColumnRearranger(data.getDataTableSpec());
c.append(m_factory);
if (m_replace) {
List<String> includeList = m_includedColumns.getIncludeList();
c.remove(includeList.toArray(new String[includeList.size()]));
}
BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
return new BufferedDataTable[] { out };
}
use of org.knime.core.data.container.ColumnRearranger 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;
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class ColCombineNodeModel method configure.
/**
* {@inheritDoc}
*/
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
if (m_columns == null) {
throw new InvalidSettingsException("No settings available");
}
DataTableSpec spec = inSpecs[0];
for (String s : m_columns) {
if (!spec.containsName(s)) {
throw new InvalidSettingsException("No such column: " + s);
}
}
if (spec.containsName(m_newColName)) {
throw new InvalidSettingsException("Column already exits: " + m_newColName);
}
ColumnRearranger arranger = createColumnRearranger(spec);
return new DataTableSpec[] { arranger.createSpec() };
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class ColCombineNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
ColumnRearranger result = new ColumnRearranger(spec);
DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
final int[] indices = new int[m_columns.length];
List<String> colNames = Arrays.asList(m_columns);
int j = 0;
for (int k = 0; k < spec.getNumColumns(); k++) {
DataColumnSpec cs = spec.getColumnSpec(k);
if (colNames.contains(cs.getName())) {
indices[j] = k;
j++;
}
}
// ", " -> ","
// " " -> " " (do not let the resulting string be empty)
// " bla bla " -> "bla bla"
final String delimTrim = trimDelimString(m_delimString);
result.append(new SingleCellFactory(append) {
@Override
public DataCell getCell(final DataRow row) {
String[] cellContents = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
DataCell c = row.getCell(indices[i]);
String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
cellContents[i] = s;
}
return new StringCell(handleContent(cellContents, delimTrim));
}
});
return result;
}
use of org.knime.core.data.container.ColumnRearranger in project knime-core by knime.
the class CategoryToNumberNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
if (m_settings.getIncludedColumns().length == 0) {
// nothing to convert, let's return the input table.
setWarningMessage("No columns selected," + " returning input.");
}
BufferedDataTable inData = (BufferedDataTable) inObjects[0];
DataTableSpec inSpec = (DataTableSpec) inObjects[0].getSpec();
ColumnRearranger rearranger = createRearranger(inSpec);
BufferedDataTable outTable = exec.createColumnRearrangeTable(inData, rearranger, exec);
// the optional PMML in port (can be null)
PMMLPortObject inPMMLPort = (PMMLPortObject) inObjects[1];
PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(inPMMLPort, rearranger.createSpec());
PMMLPortObject outPMMLPort = new PMMLPortObject(creator.createSpec(), inPMMLPort);
for (CategoryToNumberCellFactory factory : m_factories) {
PMMLMapValuesTranslator trans = new PMMLMapValuesTranslator(factory.getConfig(), new DerivedFieldMapper(inPMMLPort));
outPMMLPort.addGlobalTransformations(trans.exportToTransDict());
}
return new PortObject[] { outTable, outPMMLPort };
}
Aggregations