Search in sources :

Example 96 with DataColumnSpec

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

the class CollectionSplitNodeModel method countNewColumns.

/**
 * Iterate the argument table, determine maximum element count,
 * return freshly created column specs.
 */
private DataColumnSpec[] countNewColumns(final BufferedDataTable table, final ExecutionMonitor exec) throws InvalidSettingsException, CanceledExecutionException {
    DataTableSpec spec = table.getDataTableSpec();
    long i = 0;
    long rowCount = table.size();
    int maxColumns = 0;
    int targetColIndex = getTargetColIndex(spec);
    for (DataRow row : table) {
        DataCell c = row.getCell(targetColIndex);
        if (!c.isMissing()) {
            maxColumns = Math.max(((CollectionDataValue) c).size(), maxColumns);
        }
        exec.setProgress((i++) / (double) rowCount, "Determining maximum element count, row \"" + row.getKey() + "\" (" + i + "/" + rowCount + ")");
        exec.checkCanceled();
    }
    HashSet<String> hashNames = new HashSet<String>();
    for (DataColumnSpec s : spec) {
        hashNames.add(s.getName());
    }
    if (m_settings.isReplaceInputColumn()) {
        hashNames.remove(spec.getColumnSpec(targetColIndex).getName());
    }
    DataType elementType = spec.getColumnSpec(targetColIndex).getType().getCollectionElementType();
    DataColumnSpec[] newColSpec = new DataColumnSpec[maxColumns];
    for (int j = 0; j < newColSpec.length; j++) {
        String baseName = "Split Value " + (j + 1);
        String newName = baseName;
        int uniquifier = 1;
        while (!hashNames.add(newName)) {
            newName = baseName + "(#" + (uniquifier++) + ")";
        }
        newColSpec[j] = new DataColumnSpecCreator(newName, elementType).createSpec();
    }
    return newColSpec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) HashSet(java.util.HashSet)

Example 97 with DataColumnSpec

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

the class RuleEngineNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final DataTableSpec[] specs) throws NotConfigurableException {
    if (specs == null || specs.length == 0 || specs[0] == null || specs[0].getNumColumns() == 0) {
        throw new NotConfigurableException("No columns available!");
    }
    m_ruleEditor.setText("");
    m_spec = specs[0];
    m_variableModel.clear();
    for (DataColumnSpec s : getVariables()) {
        m_variableModel.addElement(s);
    }
    m_operatorModel.clear();
    for (String op : getOperators()) {
        m_operatorModel.addElement(op);
    }
    RuleEngineSettings ruleSettings = new RuleEngineSettings();
    ruleSettings.loadSettingsForDialog(settings);
    String defaultLabel = ruleSettings.getDefaultLabel();
    m_defaultLabelEditor.setText(defaultLabel);
    m_defaultLabelIsColumn.setSelected(ruleSettings.getDefaultLabelIsColumn());
    String newColName = ruleSettings.getNewColName();
    m_newColumnName.setText(newColName);
    m_ruleModel.clear();
    for (String rs : ruleSettings.rules()) {
        try {
            Rule r = new Rule(rs, m_spec);
            m_ruleModel.addElement(r);
        } catch (ParseException e) {
            LOGGER.warn("Rule '" + rs + "' removed, because of " + e.getMessage());
        }
    }
    m_lastUsedTextfield = null;
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ParseException(java.text.ParseException)

Example 98 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) throws InvalidSettingsException {
    ColumnRearranger crea = new ColumnRearranger(inSpec);
    String newColName = DataTableSpec.getUniqueColumnName(inSpec, m_settings.getNewColName());
    final int defaultLabelColumnIndex;
    if (m_settings.getDefaultLabelIsColumn()) {
        if (m_settings.getDefaultLabel().length() < 3) {
            throw new InvalidSettingsException("Default label is not a column reference");
        }
        if (!m_settings.getDefaultLabel().startsWith("$") || !m_settings.getDefaultLabel().endsWith("$")) {
            throw new InvalidSettingsException("Column references in default label must be enclosed in $");
        }
        String colRef = m_settings.getDefaultLabel().substring(1, m_settings.getDefaultLabel().length() - 1);
        defaultLabelColumnIndex = inSpec.findColumnIndex(colRef);
        if (defaultLabelColumnIndex == -1) {
            throw new InvalidSettingsException("Column '" + m_settings.getDefaultLabel() + "' for default label does not exist in input table");
        }
    } else {
        defaultLabelColumnIndex = -1;
    }
    // determine output type
    List<DataType> types = new ArrayList<DataType>();
    // add outcome column types
    for (Rule r : rules) {
        if (r.getOutcome() instanceof ColumnReference) {
            types.add(((ColumnReference) r.getOutcome()).spec.getType());
        } else if (r.getOutcome() instanceof Double) {
            types.add(DoubleCell.TYPE);
        } else if (r.getOutcome() instanceof Integer) {
            types.add(IntCell.TYPE);
        } else if (r.getOutcome().toString().length() > 0) {
            types.add(StringCell.TYPE);
        }
    }
    if (defaultLabelColumnIndex >= 0) {
        types.add(inSpec.getColumnSpec(defaultLabelColumnIndex).getType());
    } else if (m_settings.getDefaultLabel().length() > 0) {
        try {
            Integer.parseInt(m_settings.getDefaultLabel());
            types.add(IntCell.TYPE);
        } catch (NumberFormatException ex) {
            try {
                Double.parseDouble(m_settings.getDefaultLabel());
                types.add(DoubleCell.TYPE);
            } catch (NumberFormatException ex1) {
                types.add(StringCell.TYPE);
            }
        }
    }
    final DataType outType;
    if (types.size() > 0) {
        DataType temp = types.get(0);
        for (int i = 1; i < types.size(); i++) {
            temp = DataType.getCommonSuperType(temp, types.get(i));
        }
        if ((temp.getValueClasses().size() == 1) && temp.getValueClasses().contains(DataValue.class)) {
            // a non-native type, we replace it with string
            temp = StringCell.TYPE;
        }
        outType = temp;
    } else {
        outType = StringCell.TYPE;
    }
    DataColumnSpec cs = new DataColumnSpecCreator(newColName, outType).createSpec();
    crea.append(new SingleCellFactory(cs) {

        @Override
        public DataCell getCell(final DataRow row) {
            for (Rule r : rules) {
                if (r.matches(row)) {
                    Object outcome = r.getOutcome();
                    if (outcome instanceof ColumnReference) {
                        DataCell cell = row.getCell(((ColumnReference) outcome).index);
                        if (outType.equals(StringCell.TYPE) && !cell.isMissing() && !cell.getType().equals(StringCell.TYPE)) {
                            return new StringCell(cell.toString());
                        } else {
                            return cell;
                        }
                    } else if (outType.equals(IntCell.TYPE)) {
                        return new IntCell((Integer) outcome);
                    } else if (outType.equals(DoubleCell.TYPE)) {
                        return new DoubleCell((Double) outcome);
                    } else {
                        return new StringCell(outcome.toString());
                    }
                }
            }
            if (defaultLabelColumnIndex >= 0) {
                DataCell cell = row.getCell(defaultLabelColumnIndex);
                if (outType.equals(StringCell.TYPE) && !cell.getType().equals(StringCell.TYPE)) {
                    return new StringCell(cell.toString());
                } else {
                    return cell;
                }
            } else if (m_settings.getDefaultLabel().length() > 0) {
                String l = m_settings.getDefaultLabel();
                if (outType.equals(StringCell.TYPE)) {
                    return new StringCell(l);
                }
                try {
                    int i = Integer.parseInt(l);
                    return new IntCell(i);
                } catch (NumberFormatException ex) {
                    try {
                        double d = Double.parseDouble(l);
                        return new DoubleCell(d);
                    } catch (NumberFormatException ex1) {
                        return new StringCell(l);
                    }
                }
            } else {
                return DataType.getMissingCell();
            }
        }
    });
    return crea;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataValue(org.knime.core.data.DataValue) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) ColumnReference(org.knime.base.node.rules.Rule.ColumnReference)

Example 99 with DataColumnSpec

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

the class LogRegLearnerNodeDialogPane method createTargetOptionsPanel.

/**
 * Create options panel for the target.
 */
private JPanel createTargetOptionsPanel() {
    JPanel p = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0;
    c.weighty = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.BASELINE_LEADING;
    c.insets = new Insets(5, 5, 0, 0);
    p.add(new JLabel("Target Column:"), c);
    c.gridx++;
    m_selectionPanel = new ColumnSelectionPanel(new EmptyBorder(0, 0, 0, 0), NominalValue.class);
    m_selectionPanel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            updateTargetCategories((DataCell) m_targetReferenceCategory.getSelectedItem());
        }
    });
    p.add(m_selectionPanel, c);
    c.gridx = 0;
    c.gridy++;
    p.add(new JLabel("Reference Category:"), c);
    c.gridx++;
    m_targetReferenceCategory = new JComboBox();
    p.add(m_targetReferenceCategory, c);
    c.gridx = 0;
    c.gridy++;
    c.gridwidth = 3;
    c.weightx = 1;
    m_notSortTarget = new JCheckBox("Use order from target column domain (only relevant for output representation)");
    p.add(m_notSortTarget, c);
    m_selectionPanel.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            Object selected = e.getItem();
            if (selected instanceof DataColumnSpec) {
                m_filterPanel.resetHiding();
                m_filterPanel.hideColumns((DataColumnSpec) selected);
            }
        }
    });
    return p;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) ItemEvent(java.awt.event.ItemEvent) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) JComboBox(javax.swing.JComboBox) NominalValue(org.knime.core.data.NominalValue) ActionEvent(java.awt.event.ActionEvent) JLabel(javax.swing.JLabel) JCheckBox(javax.swing.JCheckBox) DataColumnSpec(org.knime.core.data.DataColumnSpec) ActionListener(java.awt.event.ActionListener) DataCell(org.knime.core.data.DataCell) ItemListener(java.awt.event.ItemListener) ColumnSelectionPanel(org.knime.core.node.util.ColumnSelectionPanel) EmptyBorder(javax.swing.border.EmptyBorder)

Example 100 with DataColumnSpec

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

the class LinRegLinePlotterProperties method update.

/**
 * Updates the selection boxes with the new
 * {@link org.knime.core.data.DataTableSpec} and selects the passed indices.
 * Takes care, that the x column selection box only contains the columns
 * used for model calculation. For this purpose the ItemListeners of this
 * box are removed and afterwards added again in order to avoid event loops.
 *
 * @param spec the new data table spec.
 * @param xPreSelect the x column index (-1 if unknown)
 * @param yPreSelect the y column (-1 if unknown)
 */
@Override
public void update(final DataTableSpec spec, final int xPreSelect, final int yPreSelect) {
    try {
        m_xSelector.update(spec, spec.getColumnSpec(xPreSelect).getName(), true);
        m_ySelector.update(spec, spec.getColumnSpec(yPreSelect).getName(), true);
        // store the old selected one
        Object oldSelected = m_xSelector.getSelectedItem();
        // suppress events
        ItemListener[] listeners = m_xSelector.getItemListeners();
        for (ItemListener listener : listeners) {
            m_xSelector.removeItemListener(listener);
        }
        if (m_includs != null) {
            // cleanup -> remove all items and add only the included
            m_xSelector.removeAllItems();
            List<String> survivors = Arrays.asList(m_includs);
            for (DataColumnSpec colSpec : spec) {
                if (!colSpec.getName().equals(m_targetColumn) && survivors.contains(colSpec.getName())) {
                    m_xSelector.addItem(colSpec);
                }
            }
            // restore the previously selected
            m_xSelector.setSelectedItem(oldSelected);
            for (ItemListener listener : listeners) {
                m_xSelector.addItemListener(listener);
            }
        }
    } catch (NotConfigurableException e) {
        LOGGER.warn(e.getMessage(), e);
    }
    DataColumnSpec x = (DataColumnSpec) m_xSelector.getSelectedItem();
    DataColumnSpec y = (DataColumnSpec) m_ySelector.getSelectedItem();
    m_ySelector.setEnabled(false);
    updateRangeSpinner(x, y);
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ItemListener(java.awt.event.ItemListener)

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