Search in sources :

Example 11 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec in project knime-core by knime.

the class TimeMissingValueHandlingNodeDialogPane method markIncompatibleTypedColumns.

private static void markIncompatibleTypedColumns(final ConfigType type, final List<DataColumnSpec> colSpecs) {
    ListIterator<DataColumnSpec> iterator = colSpecs.listIterator();
    while (iterator.hasNext()) {
        DataColumnSpec dataColumnSpec = iterator.next();
        if (isIncompatible(type, dataColumnSpec)) {
            iterator.remove();
            iterator.add(createAsIncompatibleMarkedColumnSpec(dataColumnSpec));
        }
    }
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec)

Example 12 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec in project knime-core by knime.

the class RuleEngineNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final List<Rule> rules, final long rowCount, final boolean updateColSpec) throws InvalidSettingsException {
    if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
        throw new InvalidSettingsException("No name for prediction column provided");
    }
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn();
    final DataType outType = computeOutputType(rules, RuleNodeSettings.RuleEngine, m_settings.isDisallowLongOutputForCompatibility());
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(newColName, outType);
    if (updateColSpec) {
        // only update in configure, execute will compute properly
        updateColSpec(rules, outType, colSpecCreator, this);
    }
    DataColumnSpec cs = colSpecCreator.createSpec();
    final boolean disallowLongOutputForCompatibility = m_settings.isDisallowLongOutputForCompatibility();
    VariableProvider.SingleCellFactoryProto cellFactory = new VariableProvider.SingleCellFactoryProto(cs) {

        private long m_rowIndex = -1L;

        @Override
        public DataCell getCell(final DataRow row) {
            m_rowIndex++;
            return getRulesOutcome(outType, row, rules, disallowLongOutputForCompatibility, this);
        }

        @Override
        public Object readVariable(final String name, final Class<?> type) {
            return RuleEngineNodeModel.this.readVariable(name, type);
        }

        @Deprecated
        @Override
        public int getRowIndex() {
            return (int) m_rowIndex;
        }

        @Override
        public long getRowIndexLong() {
            return m_rowIndex;
        }

        @Deprecated
        @Override
        public int getRowCount() {
            return (int) rowCount;
        }

        @Override
        public long getRowCountLong() {
            return rowCount;
        }
    };
    if (m_settings.isAppendColumn()) {
        crea.append(cellFactory);
    } else {
        crea.replace(cellFactory, m_settings.getReplaceColumn());
    }
    return crea;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FlowVariableProvider(org.knime.ext.sun.nodes.script.calculator.FlowVariableProvider) DataType(org.knime.core.data.DataType) DataRow(org.knime.core.data.DataRow)

Example 13 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec in project knime-core by knime.

the class PMMLRuleEditorNodeModel method computeSpecs.

/**
 * Computes the specs after applying the derived fields.
 *
 * @param specs The input table and the possible preprocessing port.
 * @return The computed (original+preproc) input table's specification.
 */
@Deprecated
static DataTableSpec computeSpecs(final PortObjectSpec[] specs) {
    final DataTableSpec tableSpec = (DataTableSpec) specs[0];
    if (specs[1] == null) {
        return tableSpec;
    }
    PMMLPortObjectSpec portObjectSpec = (PMMLPortObjectSpec) specs[1];
    List<DataColumnSpec> preprocessingCols = portObjectSpec.getPreprocessingCols();
    DataTableSpecCreator creator = new DataTableSpecCreator(tableSpec);
    for (DataColumnSpec spec : preprocessingCols) {
        creator.addColumns(spec);
    }
    return creator.createSpec();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator)

Example 14 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec in project knime-core by knime.

the class PMMLRuleEditorNodeModel method createPMMLPortObjectSpec.

/**
 * Initializes the {@link PMMLPortObjectSpec} based on the model, input and the used column.
 *
 * @param modelSpec The preprocessing model, can be {@code null}.
 * @param spec The input table spec.
 * @param usedColumns The columns used by the rules.
 * @return The {@link PMMLPortObjectSpec} filled with proper data for configuration.
 */
private PMMLPortObjectSpec createPMMLPortObjectSpec(final DataTableSpec spec, final List<String> usedColumns) {
    // this assumes that the new column is always the last column in the spec; which is the case if
    // #createRearranger uses ColumnRearranger.append.
    String targetCol = m_settings.isAppendColumn() ? spec.getColumnSpec(spec.getNumColumns() - 1).getName() : m_settings.getReplaceColumn();
    Set<String> set = new LinkedHashSet<>(usedColumns);
    List<String> learnCols = new LinkedList<>();
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec columnSpec = spec.getColumnSpec(i);
        String col = columnSpec.getName();
        if (!col.equals(targetCol) && set.contains(col) && (columnSpec.getType().isCompatible(DoubleValue.class) || columnSpec.getType().isCompatible(NominalValue.class) && (/*!m_skipColumns.getBooleanValue() ||*/
        columnSpec.getDomain().hasValues()))) {
            learnCols.add(spec.getColumnSpec(i).getName());
        }
    }
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(spec);
    pmmlSpecCreator.setLearningColsNames(learnCols);
    pmmlSpecCreator.setTargetColName(targetCol);
    return pmmlSpecCreator.createSpec();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) LinkedList(java.util.LinkedList) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 15 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec in project knime-core by knime.

the class PMMLRuleSetPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec original = (DataTableSpec) inSpecs[DATA_INDEX];
    ColumnRearranger rearranger = new ColumnRearranger(original);
    PMMLPortObjectSpec portObjectSpec = (PMMLPortObjectSpec) inSpecs[MODEL_INDEX];
    List<DataColumnSpec> activeColumnList = portObjectSpec.getActiveColumnList();
    List<DataColumnSpec> notFound = new ArrayList<DataColumnSpec>();
    for (DataColumnSpec dataColumnSpec : activeColumnList) {
        if (original.containsName(dataColumnSpec.getName())) {
            DataColumnSpec origSpec = original.getColumnSpec(dataColumnSpec.getName());
            if (!origSpec.getType().equals(dataColumnSpec.getType())) {
                notFound.add(dataColumnSpec);
            }
        } else {
            notFound.add(dataColumnSpec);
        }
    }
    if (!notFound.isEmpty()) {
        StringBuilder sb = new StringBuilder("Incompatible to the table, the following columns are not present, or have a wrong type:");
        for (DataColumnSpec dataColumnSpec : notFound) {
            sb.append("\n   ").append(dataColumnSpec);
        }
        throw new InvalidSettingsException(sb.toString());
    }
    List<DataColumnSpec> targetCols = portObjectSpec.getTargetCols();
    final DataType dataType = targetCols.isEmpty() ? StringCell.TYPE : targetCols.get(0).getType();
    DataColumnSpecCreator specCreator;
    if (m_doReplaceColumn.getBooleanValue()) {
        String col = m_replaceColumn.getStringValue();
        specCreator = new DataColumnSpecCreator(col, dataType);
    } else {
        specCreator = new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(original, m_outputColumn.getStringValue()), dataType);
    }
    SingleCellFactory dummy = new SingleCellFactory(specCreator.createSpec()) {

        /**
         * {@inheritDoc}
         */
        @Override
        public DataCell getCell(final DataRow row) {
            throw new IllegalStateException();
        }
    };
    if (m_addConfidence.getBooleanValue()) {
        rearranger.append(new SingleCellFactory(new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(rearranger.createSpec(), m_confidenceColumn.getStringValue()), DoubleCell.TYPE).createSpec()) {

            @Override
            public DataCell getCell(final DataRow row) {
                throw new IllegalStateException();
            }
        });
    }
    if (m_doReplaceColumn.getBooleanValue()) {
        rearranger.replace(dummy, m_replaceColumn.getStringValue());
    } else {
        rearranger.append(dummy);
    }
    return new DataTableSpec[] { rearranger.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Aggregations

DataColumnSpec (org.knime.core.data.DataColumnSpec)800 DataTableSpec (org.knime.core.data.DataTableSpec)351 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)239 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)223 DataCell (org.knime.core.data.DataCell)187 ArrayList (java.util.ArrayList)167 DataType (org.knime.core.data.DataType)149 DataRow (org.knime.core.data.DataRow)124 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)123 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)119 DoubleValue (org.knime.core.data.DoubleValue)104 HashSet (java.util.HashSet)92 BufferedDataTable (org.knime.core.node.BufferedDataTable)77 LinkedHashSet (java.util.LinkedHashSet)65 LinkedHashMap (java.util.LinkedHashMap)56 LinkedList (java.util.LinkedList)47 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)46 DoubleCell (org.knime.core.data.def.DoubleCell)46 StringCell (org.knime.core.data.def.StringCell)45 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)43