Search in sources :

Example 51 with ColumnRearranger

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 };
}
Also used : ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Numeric2BitVectorThresholdCellFactory(org.knime.base.data.bitvector.Numeric2BitVectorThresholdCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) Numeric2BitVectorMeanCellFactory(org.knime.base.data.bitvector.Numeric2BitVectorMeanCellFactory) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 52 with ColumnRearranger

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;
}
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 53 with ColumnRearranger

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() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 54 with ColumnRearranger

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;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 55 with ColumnRearranger

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 };
}
Also used : DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Aggregations

ColumnRearranger (org.knime.core.data.container.ColumnRearranger)393 DataTableSpec (org.knime.core.data.DataTableSpec)221 BufferedDataTable (org.knime.core.node.BufferedDataTable)153 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)125 DataColumnSpec (org.knime.core.data.DataColumnSpec)116 DataRow (org.knime.core.data.DataRow)79 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)69 DataCell (org.knime.core.data.DataCell)63 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)55 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)49 ExecutionContext (org.knime.core.node.ExecutionContext)46 PortObject (org.knime.core.node.port.PortObject)39 ArrayList (java.util.ArrayList)38 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)36 DataType (org.knime.core.data.DataType)34 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)32 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)27 DoubleValue (org.knime.core.data.DoubleValue)26 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)26 TreeEnsembleModelPortObjectSpec (org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec)25