Search in sources :

Example 71 with DataColumnSpec

use of org.knime.core.data.DataColumnSpec 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 72 with DataColumnSpec

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

the class ColumnToGridConfiguration method autoGuessIncludeColumns.

/**
 * Auto-guessing: choose first column that is not string, int, double
 * as include; if no such column exists use first string column, otherwise
 * use non (null returned).
 * @param spec Input spec.
 * @return A meaningful default or null.
 */
static String[] autoGuessIncludeColumns(final DataTableSpec spec) {
    String firstStringCol = null;
    String firstUnknownCol = null;
    for (DataColumnSpec col : spec) {
        DataType type = col.getType();
        if (type.equals(StringCell.TYPE)) {
            if (firstStringCol == null) {
                firstStringCol = col.getName();
            }
        } else if (type.equals(DoubleCell.TYPE)) {
        // ignore
        } else if (type.equals(IntCell.TYPE)) {
        // ignore
        } else {
            if (firstUnknownCol == null) {
                firstUnknownCol = col.getName();
            }
        }
    }
    String sel = null;
    if (firstUnknownCol != null) {
        sel = firstUnknownCol;
    } else if (firstStringCol != null) {
        sel = firstStringCol;
    } else {
        return null;
    }
    return new String[] { sel };
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType)

Example 73 with DataColumnSpec

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

the class MissingValueHandling2NodeDialogPane method addToIndividualPanel.

private void addToIndividualPanel(final MissingValueHandling2Panel panel) {
    panel.addPropertyChangeListener(MissingValueHandling2Panel.REMOVE_ACTION, new PropertyChangeListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void propertyChange(final PropertyChangeEvent evt) {
            removeFromIndividualPanel((MissingValueHandling2Panel) evt.getSource());
        }
    });
    panel.addPropertyChangeListener(MissingValueHandling2Panel.REMOVED_INVALID_COLUMNS, new PropertyChangeListener() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void propertyChange(final PropertyChangeEvent evt) {
            DataColumnSpec[] removedSpecs = (DataColumnSpec[]) evt.getNewValue();
            if (removedSpecs != null) {
                for (DataColumnSpec spec : removedSpecs) {
                    if (m_searchableListPanel.isAdditionalColumn(spec.getName())) {
                        m_searchableListModifier.removeAdditionalColumn(spec.getName());
                    }
                }
            }
            checkButtonStatus();
            m_searchableListPanel.repaint();
        }
    });
    m_individualsPanel.add(panel);
    m_individualsPanel.revalidate();
// // force the outer parent to render out first individual if there does none exist previously
// getPanel().repaint();
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) DataColumnSpec(org.knime.core.data.DataColumnSpec) PropertyChangeListener(java.beans.PropertyChangeListener)

Example 74 with DataColumnSpec

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

the class MissingValueHandling2NodeDialogPane method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
    m_searchableListModifier = m_searchableListPanel.update(specs[0]);
    MissingValueHandling2ColSetting[] defaults = MissingValueHandling2ColSetting.loadMetaColSettings(settings, specs[0]);
    MissingValueHandling2ColSetting[] individuals = MissingValueHandling2ColSetting.loadIndividualColSettings(settings, specs[0]);
    m_defaultsPanel.removeAll();
    for (int i = 0; i < defaults.length; i++) {
        final MissingValueHandling2Panel p = new MissingValueHandling2Panel(defaults[i], (DataColumnSpec) null);
        m_defaultsPanel.add(p);
    }
    m_individualsPanel.removeAll();
    Set<String> invalidColumns = new LinkedHashSet<String>();
    for (int i = 0; i < individuals.length; i++) {
        String[] names = individuals[i].getNames();
        ArrayList<DataColumnSpec> colSpecs = new ArrayList<DataColumnSpec>();
        for (int j = 0; j < names.length; j++) {
            final DataColumnSpec cspec = specs[0].getColumnSpec(names[j]);
            if (cspec == null) {
                LOGGER.debug("No such column in spec: " + names[j]);
                DataColumnSpec createUnkownSpec = createUnkownSpec(names[j], individuals[i]);
                colSpecs.add(createUnkownSpec);
                invalidColumns.add(names[j]);
            } else {
                colSpecs.add(cspec);
            }
        }
        if (!colSpecs.isEmpty()) {
            names = new String[colSpecs.size()];
            for (int j = 0; j < names.length; j++) {
                names[j] = colSpecs.get(j).getName();
            }
            individuals[i].setNames(names);
            markIncompatibleTypedColumns(individuals[i].getType(), colSpecs);
            final MissingValueHandling2Panel p = new MissingValueHandling2Panel(individuals[i], colSpecs.toArray(new DataColumnSpec[0]));
            p.registerMouseListener(new MouseAdapter() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void mouseClicked(final MouseEvent me) {
                    selectColumns(p.getSettings());
                }
            });
            addToIndividualPanel(p);
        }
    }
    m_searchableListModifier.addInvalidColumns(invalidColumns.toArray(new String[invalidColumns.size()]));
    m_individualsPanel.setPreferredSize(m_defaultsPanel.getPreferredSize());
    checkButtonStatus();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MouseEvent(java.awt.event.MouseEvent) ArrayList(java.util.ArrayList) MouseAdapter(java.awt.event.MouseAdapter) DataColumnSpec(org.knime.core.data.DataColumnSpec)

Example 75 with DataColumnSpec

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

the class CategoryToNumberNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inSpec = (DataTableSpec) inSpecs[0];
    List<String> inputCols = new ArrayList<String>();
    for (DataColumnSpec column : inSpec) {
        if (column.getType().isCompatible(StringValue.class)) {
            inputCols.add(column.getName());
        }
    }
    // Auto configuration when included columns are not set
    if (null == m_settings.getIncludedColumns()) {
        // when there is no column with nominal data
        if (inputCols.isEmpty()) {
            throw new InvalidSettingsException("No column in " + "the input compatible to \"StringValue\".");
        } else {
            // auto-configure
            m_settings.setIncludedColumns(inputCols.toArray(new String[inputCols.size()]));
        }
    } else {
        if (!m_settings.getIncludeAll()) {
            if (m_settings.getIncludedColumns().length == 0) {
                setWarningMessage("No columns selected.");
            }
            List<String> included = new ArrayList<String>();
            included.addAll(Arrays.asList(m_settings.getIncludedColumns()));
            included.removeAll(inputCols);
            if (!included.isEmpty()) {
                // output first missing column
                throw new InvalidSettingsException("Missing column: " + included.get(0).toString());
            }
        } else {
            if (inputCols.isEmpty()) {
                throw new InvalidSettingsException("No column in " + "the input compatible to \"StringValue\".");
            } else {
                // automatically add all columns
                m_settings.setIncludedColumns(inputCols.toArray(new String[inputCols.size()]));
            }
        }
    }
    ColumnRearranger rearranger = createRearranger(inSpec);
    PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(pmmlSpec, inSpec);
    pmmlSpecCreator.addPreprocColNames(inputCols);
    return new PortObjectSpec[] { rearranger.createSpec(), pmmlSpecCreator.createSpec() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

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