Search in sources :

Example 91 with DataTableSpec

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

the class RegressionPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    PMMLPortObjectSpec regModelSpec = (PMMLPortObjectSpec) inSpecs[0];
    DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
    if (dataSpec == null || regModelSpec == null) {
        throw new InvalidSettingsException("No input specification available");
    }
    if (regModelSpec.getTargetCols().get(0).getType().isCompatible(DoubleValue.class) && m_settings.getIncludeProbabilities()) {
        setWarningMessage("The option \"Append columns with predicted probabilities\"" + " has only an effect for nominal targets");
    }
    if (null != RegressionPredictorCellFactory.createColumnSpec(regModelSpec, dataSpec, m_settings)) {
        ColumnRearranger c = new ColumnRearranger(dataSpec);
        c.append(new RegressionPredictorCellFactory(regModelSpec, dataSpec, m_settings) {

            @Override
            public DataCell[] getCells(final DataRow row) {
                // not called during configure
                return null;
            }
        });
        DataTableSpec outSpec = c.createSpec();
        return new DataTableSpec[] { outSpec };
    } else {
        return null;
    }
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataRow(org.knime.core.data.DataRow)

Example 92 with DataTableSpec

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

the class BitVectorGeneratorNodeModel 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 {
    DataTableSpec spec = inSpecs[0];
    if (!m_fromString) {
        // check if there is at least one numeric column selected
        if (m_includedColumns.isEnabled() && m_includedColumns.getIncludeList().isEmpty()) {
            // the includeColumns model cannot be empty
            // through the dialog (see #validateSettings)
            // only case where !m_fromString and includeColumns evaluates
            // to true is for old workflows.
            // For backward compatiblity include all numeric columns
            // which was the behavior before 2.0
            List<String> allNumericColumns = new ArrayList<String>();
            for (DataColumnSpec colSpec : spec) {
                if (colSpec.getType().isCompatible(DoubleValue.class)) {
                    allNumericColumns.add(colSpec.getName());
                }
            }
            m_includedColumns.setIncludeList(allNumericColumns);
            m_loadedSettingsDontHaveIncludeColumns = false;
        }
        for (String inclColName : m_includedColumns.getIncludeList()) {
            DataColumnSpec colSpec = spec.getColumnSpec(inclColName);
            if (colSpec == null) {
                throw new InvalidSettingsException("Column " + inclColName + " not found in input table. " + "Please re-configure the node.");
            }
            if (!colSpec.getType().isCompatible(DoubleValue.class)) {
                throw new InvalidSettingsException("Column " + inclColName + " is not a numeric column");
            }
        }
    } else {
        // parse from string column
        if (m_stringColumn.getStringValue() == null) {
            throw new InvalidSettingsException("No string column selected. " + "Please (re-)configure the node.");
        }
        // -> check if selected column is a string column
        if (!spec.containsName(m_stringColumn.getStringValue()) || !(spec.getColumnSpec(m_stringColumn.getStringValue()).getType().isCompatible(StringValue.class))) {
            throw new InvalidSettingsException("Selected string column " + m_stringColumn.getStringValue() + " not in the input table");
        }
    }
    if (m_fromString) {
        int stringColIdx = inSpecs[0].findColumnIndex(m_stringColumn.getStringValue());
        ColumnRearranger c = createColumnRearranger(inSpecs[0], stringColIdx);
        return new DataTableSpec[] { c.createSpec() };
    } else {
        // numeric input
        DataTableSpec newSpec;
        DataColumnSpec newColSpec = createNumericOutputSpec(spec);
        if (m_replace) {
            ColumnRearranger colR = new ColumnRearranger(spec);
            colR.remove(m_includedColumns.getIncludeList().toArray(new String[m_includedColumns.getIncludeList().size()]));
            newSpec = new DataTableSpec(colR.createSpec(), new DataTableSpec(newColSpec));
        } else {
            newSpec = new DataTableSpec(spec, new DataTableSpec(newColSpec));
        }
        return new DataTableSpec[] { newSpec };
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) StringValue(org.knime.core.data.StringValue)

Example 93 with DataTableSpec

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

the class AutoBinner method calcDomainBoundsIfNeccessary.

/**
 * Determines the per column min/max values of the given data if not already
 * present in the domain.
 * @param data the data
 * @param exec the execution context
 * @param recalcValuesFor The columns
 * @return The data with extended domain information
 * @throws InvalidSettingsException
 * @throws CanceledExecutionException
 */
public BufferedDataTable calcDomainBoundsIfNeccessary(final BufferedDataTable data, final ExecutionContext exec, final List<String> recalcValuesFor) throws InvalidSettingsException, CanceledExecutionException {
    if (null == recalcValuesFor || recalcValuesFor.isEmpty()) {
        return data;
    }
    List<Integer> valuesI = new ArrayList<Integer>();
    for (String colName : recalcValuesFor) {
        DataColumnSpec colSpec = data.getDataTableSpec().getColumnSpec(colName);
        if (!colSpec.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Can only process numeric " + "data. The column \"" + colSpec.getName() + "\" is not numeric.");
        }
        if (recalcValuesFor.contains(colName) && !colSpec.getDomain().hasBounds()) {
            valuesI.add(data.getDataTableSpec().findColumnIndex(colName));
        }
    }
    if (valuesI.isEmpty()) {
        return data;
    }
    Map<Integer, Double> min = new HashMap<Integer, Double>();
    Map<Integer, Double> max = new HashMap<Integer, Double>();
    for (int col : valuesI) {
        min.put(col, Double.MAX_VALUE);
        max.put(col, Double.MIN_VALUE);
    }
    int c = 0;
    for (DataRow row : data) {
        c++;
        exec.checkCanceled();
        exec.setProgress(c / (double) data.getRowCount());
        for (int col : valuesI) {
            double val = ((DoubleValue) row.getCell(col)).getDoubleValue();
            if (min.get(col) > val) {
                min.put(col, val);
            }
            if (max.get(col) < val) {
                min.put(col, val);
            }
        }
    }
    List<DataColumnSpec> newColSpecList = new ArrayList<DataColumnSpec>();
    int cc = 0;
    for (DataColumnSpec columnSpec : data.getDataTableSpec()) {
        if (recalcValuesFor.contains(columnSpec.getName())) {
            DataColumnSpecCreator specCreator = new DataColumnSpecCreator(columnSpec);
            DataColumnDomainCreator domainCreator = new DataColumnDomainCreator(new DoubleCell(min.get(cc)), new DoubleCell(max.get(cc)));
            specCreator.setDomain(domainCreator.createDomain());
            DataColumnSpec newColSpec = specCreator.createSpec();
            newColSpecList.add(newColSpec);
        } else {
            newColSpecList.add(columnSpec);
        }
        cc++;
    }
    DataTableSpec spec = new DataTableSpec(newColSpecList.toArray(new DataColumnSpec[0]));
    BufferedDataTable newDataTable = exec.createSpecReplacerTable(data, spec);
    return newDataTable;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 94 with DataTableSpec

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

the class AutoBinnerLearnNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec dataSpec = (DataTableSpec) inSpecs[0];
    AutoBinner binner = new AutoBinner(m_settings);
    return binner.getOutputSpec(dataSpec);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec)

Example 95 with DataTableSpec

use of org.knime.core.data.DataTableSpec 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)

Aggregations

DataTableSpec (org.knime.core.data.DataTableSpec)938 DataColumnSpec (org.knime.core.data.DataColumnSpec)340 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)306 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)228 BufferedDataTable (org.knime.core.node.BufferedDataTable)226 DataCell (org.knime.core.data.DataCell)186 DataRow (org.knime.core.data.DataRow)170 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)136 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)129 DataType (org.knime.core.data.DataType)109 ArrayList (java.util.ArrayList)106 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)98 DoubleValue (org.knime.core.data.DoubleValue)94 DefaultRow (org.knime.core.data.def.DefaultRow)92 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)90 ExecutionContext (org.knime.core.node.ExecutionContext)68 PortObject (org.knime.core.node.port.PortObject)66 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)62 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)61 RowKey (org.knime.core.data.RowKey)59