Search in sources :

Example 21 with CellFactory

use of org.knime.core.data.container.CellFactory in project knime-core by knime.

the class One2ManyColPMMLNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inDataSpec = (DataTableSpec) inSpecs[0];
    m_appendOrgColName = false;
    if (m_includedColumns.getIncludeList() == null || m_includedColumns.getIncludeList().size() <= 0) {
        setWarningMessage("No columns to transfrom selected. Will have no effect!");
    }
    // check if the values are present in the current spec
    if (m_includedColumns.getIncludeList() != null && m_includedColumns.getIncludeList().size() > 0) {
        checkColumnsSpecs(inDataSpec);
    }
    CellFactory cellFactory = new One2ManyCellFactory(inDataSpec, m_includedColumns.getIncludeList(), m_appendOrgColName);
    ColumnRearranger rearranger = createRearranger(inDataSpec, cellFactory);
    if (m_pmmlEnabled) {
        PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
        PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inDataSpec);
        return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
    } else {
        return new PortObjectSpec[] { rearranger.createSpec() };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) One2ManyCellFactory(org.knime.base.node.preproc.columnTrans.One2ManyCellFactory) One2ManyCellFactory(org.knime.base.node.preproc.columnTrans.One2ManyCellFactory) CellFactory(org.knime.core.data.container.CellFactory) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 22 with CellFactory

use of org.knime.core.data.container.CellFactory in project knime-core by knime.

the class CollectionCreate2NodeModel method createColumnRearranger.

@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec in) throws InvalidSettingsException {
    FilterResult filterResult = m_includeModel.applyTo(in);
    List<String> includes = Arrays.asList(filterResult.getIncludes());
    String[] names = includes.toArray(new String[includes.size()]);
    final int[] colIndices = new int[names.length];
    for (int i = 0; i < names.length; i++) {
        int index = in.findColumnIndex(names[i]);
        if (index < 0) {
            throw new InvalidSettingsException("No column \"" + names[i] + "\" in input table");
        }
        colIndices[i] = index;
    }
    DataType comType;
    if (includes.size() == 0) {
        comType = DataType.getType(DataCell.class);
    } else {
        comType = CollectionCellFactory.getElementType(in, colIndices);
    }
    String newColName = m_newColName.getStringValue();
    DataType type;
    if (m_createSet.getBooleanValue()) {
        type = SetCell.getCollectionType(comType);
    } else {
        type = ListCell.getCollectionType(comType);
    }
    DataColumnSpecCreator newColSpecC = new DataColumnSpecCreator(newColName, type);
    newColSpecC.setElementNames(names);
    DataColumnSpec newColSpec = newColSpecC.createSpec();
    CellFactory appendFactory = new SingleCellFactory(newColSpec) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell getCell(final DataRow row) {
            int[] validColIndices = colIndices;
            // iff ignore missing value is switched on
            if (m_ignoreMissing.getBooleanValue()) {
                List<Integer> vCI = new ArrayList<Integer>();
                for (int i : validColIndices) {
                    if (!row.getCell(i).isMissing()) {
                        vCI.add(i);
                    }
                }
                validColIndices = buildIntArray(vCI);
            }
            // based on given column indices
            if (m_createSet.getBooleanValue()) {
                return CollectionCellFactory.createSetCell(row, validColIndices);
            } else {
                return CollectionCellFactory.createListCell(row, validColIndices);
            }
        }
    };
    ColumnRearranger rearranger = new ColumnRearranger(in);
    if (m_removeCols.getBooleanValue()) {
        rearranger.remove(colIndices);
    }
    rearranger.append(appendFactory);
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) 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) DataCell(org.knime.core.data.DataCell) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) CollectionCellFactory(org.knime.core.data.collection.CollectionCellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 23 with CellFactory

use of org.knime.core.data.container.CellFactory in project knime-core by knime.

the class SearchReplaceDictNodeModel method createColumnRearranger.

@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
    ColumnRearranger result = new ColumnRearranger(spec);
    final int targetColIndex = spec.findColumnIndex(m_targetColumnName);
    DataColumnSpecCreator newColCreator;
    if (m_newColumnName == null) {
        DataColumnSpec old = spec.getColumnSpec(m_targetColumnName);
        newColCreator = new DataColumnSpecCreator(old);
        newColCreator.setType(StringCell.TYPE);
        newColCreator.setDomain(null);
    } else {
        newColCreator = new DataColumnSpecCreator(m_newColumnName, StringCell.TYPE);
    }
    CellFactory amendedCol = new SingleCellFactory(newColCreator.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell c = row.getCell(targetColIndex);
            if (c.isMissing()) {
                return c;
            }
            String cellContent = c.toString();
            String replacement = m_replacementMap.get(cellContent);
            if (replacement != null) {
                return new StringCell(replacement);
            } else {
                // StringCell.TYPE!)
                return new StringCell(cellContent);
            }
        }
    };
    if (m_newColumnName != null) {
        result.append(amendedCol);
    } else {
        result.replace(amendedCol, targetColIndex);
    }
    return result;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 24 with CellFactory

use of org.knime.core.data.container.CellFactory in project knime-core by knime.

the class RowKeyUtil2 method createColumnRearranger.

/**
 * Creates the {@link ColumnRearranger} that appends a new column with the
 * values of the row id to a data table.
 *
 * @param inSpec the <code>DataTableSpec</code> of table were the column
 * should be appended
 * @param newColName the name of the added column
 * @param type the <code>DataType</code> of the new column
 * @return the {@link ColumnRearranger} to use
 */
public static ColumnRearranger createColumnRearranger(final DataTableSpec inSpec, final String newColName, final DataType type) {
    final ColumnRearranger c = new ColumnRearranger(inSpec);
    // column specification of the appended column
    final DataColumnSpecCreator colSpecCreater = new DataColumnSpecCreator(newColName, type);
    final DataColumnSpec newColSpec = colSpecCreater.createSpec();
    // utility object that performs the calculation
    final CellFactory factory = new SingleCellFactory(newColSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            return new StringCell(row.getKey().getString());
        }
    };
    c.append(factory);
    return c;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 25 with CellFactory

use of org.knime.core.data.container.CellFactory in project knime-core by knime.

the class ThreadedColAppenderNodeModel method createOutputSpec.

/**
 * Returns the output spec based on the input spec and the cell factory.
 *
 * @param inSpec the input spec
 * @param cellFactory the cell factory used
 * @return the output spec
 */
protected static DataTableSpec createOutputSpec(final DataTableSpec inSpec, final ExtendedCellFactory cellFactory) {
    ColumnRearranger rea = new ColumnRearranger(inSpec);
    ColumnDestination[] dests = cellFactory.getColumnDestinations();
    for (int k = 0; k < dests.length; k++) {
        ColumnDestination cd = dests[k];
        CellFactory cf = new SingleCellFactory(cellFactory.getColumnSpecs()[k]) {

            @Override
            public DataCell getCell(final DataRow row) {
                return null;
            }
        };
        if (cd instanceof AppendColumn) {
            rea.append(cf);
        } else if (cd instanceof InsertColumn) {
            rea.insertAt(((InsertColumn) cd).getIndex(), cf);
        } else {
            rea.replace(cf, ((ReplaceColumn) cd).getIndex());
        }
    }
    return rea.createSpec();
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Aggregations

CellFactory (org.knime.core.data.container.CellFactory)26 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)20 DataRow (org.knime.core.data.DataRow)20 DataCell (org.knime.core.data.DataCell)13 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 RowKey (org.knime.core.data.RowKey)8 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 DataTableSpec (org.knime.core.data.DataTableSpec)7 BufferedDataTable (org.knime.core.node.BufferedDataTable)7 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)7 StringCell (org.knime.core.data.def.StringCell)6 PortObject (org.knime.core.node.port.PortObject)6 Matrix (Jama.Matrix)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 DataType (org.knime.core.data.DataType)3 DoubleValue (org.knime.core.data.DoubleValue)3