Search in sources :

Example 86 with DataColumnSpec

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

the class AttributeSelectionPanel method getMissingColSpecName.

@SuppressWarnings("null")
private static String getMissingColSpecName(final DataTableSpec spec, final String[] includedNames, final String[] excludedNames) {
    ColumnRearranger r = new ColumnRearranger(spec);
    // remove columns we know from the include list
    for (String colName : includedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    // remove columns we know from the exclude list
    for (String colName : excludedNames) {
        if (spec.containsName(colName)) {
            r.remove(colName);
        }
    }
    DataTableSpec tableSpecWithMissing = r.createSpec();
    DataColumnSpec formerTargetSpec = null;
    // were either in the include or exclude list
    for (DataColumnSpec colSpec : tableSpecWithMissing) {
        DataType colType = colSpec.getType();
        if (colType.isCompatible(NominalValue.class) || colType.isCompatible(DoubleValue.class)) {
            formerTargetSpec = colSpec;
            break;
        }
    }
    assert formerTargetSpec != null : "The former target spec is no longer part of the table, please check.";
    return formerTargetSpec.getName();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) DataType(org.knime.core.data.DataType)

Example 87 with DataColumnSpec

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

the class TreeEnsembleClassificationPredictorCellFactory2 method createFactory.

/**
 * Creates a TreeEnsembleClassificationPredictorCellFactory from the provided <b>predictor</b>
 * @param predictor
 * @return an instance of TreeEnsembleClassificationPredictorCellFactory configured according to the settings of the provided
 * <b>predictor<b>
 * @throws InvalidSettingsException
 */
public static TreeEnsembleClassificationPredictorCellFactory2 createFactory(final TreeEnsemblePredictor predictor) throws InvalidSettingsException {
    DataTableSpec testDataSpec = predictor.getDataSpec();
    TreeEnsembleModelPortObjectSpec modelSpec = predictor.getModelSpec();
    TreeEnsembleModelPortObject modelObject = predictor.getModelObject();
    TreeEnsemblePredictorConfiguration configuration = predictor.getConfiguration();
    UniqueNameGenerator nameGen = new UniqueNameGenerator(testDataSpec);
    Map<String, DataCell> targetValueMap = modelSpec.getTargetColumnPossibleValueMap();
    List<DataColumnSpec> newColsList = new ArrayList<DataColumnSpec>();
    DataType targetColType = modelSpec.getTargetColumn().getType();
    String targetColName = configuration.getPredictionColumnName();
    DataColumnSpec targetCol = nameGen.newColumn(targetColName, targetColType);
    newColsList.add(targetCol);
    if (configuration.isAppendPredictionConfidence()) {
        newColsList.add(nameGen.newColumn(targetCol.getName() + " (Confidence)", DoubleCell.TYPE));
    }
    if (configuration.isAppendClassConfidences()) {
        // and this class is not called)
        assert targetValueMap != null : "Target column has no possible values";
        for (String v : targetValueMap.keySet()) {
            newColsList.add(nameGen.newColumn(v, DoubleCell.TYPE));
        }
    }
    if (configuration.isAppendModelCount()) {
        newColsList.add(nameGen.newColumn("model count", IntCell.TYPE));
    }
    // assigned
    assert modelObject == null || targetValueMap != null : "Target values must be known during execution";
    DataColumnSpec[] newCols = newColsList.toArray(new DataColumnSpec[newColsList.size()]);
    int[] learnColumnInRealDataIndices = modelSpec.calculateFilterIndices(testDataSpec);
    return new TreeEnsembleClassificationPredictorCellFactory2(predictor, targetValueMap, newCols, learnColumnInRealDataIndices);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) TreeEnsembleModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble2.node.predictor.TreeEnsemblePredictorConfiguration) ArrayList(java.util.ArrayList) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObject) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType)

Example 88 with DataColumnSpec

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

the class TreeEnsembleRegressionPredictorCellFactory method createFactory.

/**
 * Creates a TreeEnsembleRegressionPredictorCellFactory from the provided <b>predictor</b>
 *
 * @param predictor
 * @return an instance of TreeEnsembleRegressionPredictorCellFactory configured according to the settings of the provided
 * <b>predictor<b>
 * @throws InvalidSettingsException
 */
public static TreeEnsembleRegressionPredictorCellFactory createFactory(final TreeEnsemblePredictor predictor) throws InvalidSettingsException {
    DataTableSpec testDataSpec = predictor.getDataSpec();
    TreeEnsembleModelPortObjectSpec modelSpec = predictor.getModelSpec();
    // TreeEnsembleModelPortObject modelObject = predictor.getModelObject();
    TreeEnsemblePredictorConfiguration configuration = predictor.getConfiguration();
    UniqueNameGenerator nameGen = new UniqueNameGenerator(testDataSpec);
    List<DataColumnSpec> newColsList = new ArrayList<DataColumnSpec>();
    String targetColName = configuration.getPredictionColumnName();
    DataColumnSpec targetCol = nameGen.newColumn(targetColName, DoubleCell.TYPE);
    newColsList.add(targetCol);
    if (configuration.isAppendPredictionConfidence()) {
        newColsList.add(nameGen.newColumn(targetCol.getName() + " (Prediction Variance)", DoubleCell.TYPE));
    }
    if (configuration.isAppendModelCount()) {
        newColsList.add(nameGen.newColumn("model count", IntCell.TYPE));
    }
    DataColumnSpec[] newCols = newColsList.toArray(new DataColumnSpec[newColsList.size()]);
    int[] learnColumnInRealDataIndices = modelSpec.calculateFilterIndices(testDataSpec);
    return new TreeEnsembleRegressionPredictorCellFactory(predictor, newCols, learnColumnInRealDataIndices);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) TreeEnsembleModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObjectSpec) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble2.node.predictor.TreeEnsemblePredictorConfiguration) ArrayList(java.util.ArrayList) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator)

Example 89 with DataColumnSpec

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

the class MissingValueHandling2Panel method createWestLayout.

/**
 * @param setting
 * @param tabPanel
 * @param icon
 * @param name
 * @param spec
 */
private void createWestLayout(final MissingValueHandling2ColSetting setting, final JPanel tabPanel, final Icon icon, final String name, final DataColumnSpec... spec) {
    String shortName = name;
    if (setting.isMetaConfig()) {
        if (name.length() > 15) {
            shortName = name.substring(0, 14).concat("...");
        }
        JLabel jLabel = new JLabel(shortName, icon, SwingConstants.LEFT);
        jLabel.setToolTipText(name);
        JPanel typePanel = new JPanel(new BorderLayout());
        typePanel.add(jLabel, BorderLayout.NORTH);
        tabPanel.add(typePanel, BorderLayout.WEST);
    } else {
        final JList jList = new JList(spec);
        jList.setCellRenderer(new DataColumnSpecListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
                // The super method will reset the icon if we call this method
                // last. So we let super do its job first and then we take care
                // that everything is properly set.
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                String text = null;
                if (value instanceof DataColumnSpec && MissingValueHandling2NodeDialogPane.isIncompatible((DataColumnSpec) value)) {
                    setBorder(BorderFactory.createLineBorder(Color.YELLOW));
                    text = "Column " + ((DataColumnSpec) value).getName() + " is incompatible to the current settings";
                }
                setToolTipText(text);
                return this;
            }
        });
        jList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(final ListSelectionEvent e) {
                jList.setSelectedIndices(new int[0]);
            }
        });
        // jList.setSE
        JScrollPane columns = new JScrollPane(jList);
        columns.setMaximumSize(new Dimension(150, 150));
        columns.setPreferredSize(new Dimension(100, 150));
        tabPanel.add(columns, BorderLayout.WEST);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) DataColumnSpecListCellRenderer(org.knime.core.node.util.DataColumnSpecListCellRenderer) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) ListSelectionListener(javax.swing.event.ListSelectionListener) DataColumnSpec(org.knime.core.data.DataColumnSpec) BorderLayout(java.awt.BorderLayout) JComponent(javax.swing.JComponent) Component(java.awt.Component) JList(javax.swing.JList)

Example 90 with DataColumnSpec

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

the class MissingValueHandling2Panel method createContent.

/**
 * @param setting
 * @param spec
 * @throws InternalError
 */
private void createContent(final MissingValueHandling2ColSetting setting, final DataColumnSpec... spec) throws InternalError {
    final List<String> warningMessages = new ArrayList<String>();
    // if we got incompatible types the original type is overwritten by unkown.
    int settingTypeBackup = setting.getType();
    JPanel tabPanel = new JPanel(new BorderLayout(0, 5));
    final JPanel panel = new JPanel(new GridLayout(0, 2));
    final Icon icon;
    final String name;
    final Border border;
    final JComponent removePanel;
    if (setting.isMetaConfig()) {
        switch(setting.getType()) {
            case MissingValueHandling2ColSetting.TYPE_INT:
                icon = IntCell.TYPE.getIcon();
                name = "Integer";
                border = BorderFactory.createTitledBorder("Integer Columns");
                break;
            case MissingValueHandling2ColSetting.TYPE_STRING:
                icon = StringCell.TYPE.getIcon();
                name = "String";
                border = BorderFactory.createTitledBorder("String Columns");
                break;
            case MissingValueHandling2ColSetting.TYPE_DOUBLE:
                icon = DoubleCell.TYPE.getIcon();
                name = "Double";
                border = BorderFactory.createTitledBorder("Double Columns");
                break;
            case MissingValueHandling2ColSetting.TYPE_UNKNOWN:
                icon = DataType.getType(DataCell.class).getIcon();
                name = "Unknown";
                border = BorderFactory.createTitledBorder("Unknown Columns");
                break;
            default:
                throw new InternalError("No such type.");
        }
        removePanel = new JLabel();
    } else {
        final List<String> names = new ArrayList<String>(Arrays.asList(setting.getNames()));
        for (DataColumnSpec cspec : spec) {
            names.remove(cspec.getName());
        }
        if (!names.isEmpty()) {
            throw new NullPointerException("Not equal on init: '" + Arrays.toString(setting.getNames()) + "' vs. '" + Arrays.toString(spec) + "'.");
        }
        name = setting.getDisplayName();
        icon = spec[0].getType().getIcon();
        JButton requestRemoveButton = new JButton("Remove");
        requestRemoveButton.addActionListener(new ActionListener() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void actionPerformed(final ActionEvent e) {
                firePropertyChange(REMOVE_ACTION, null, null);
            }
        });
        removePanel = new JPanel();
        removePanel.setLayout(new GridLayout(2, 0));
        removePanel.add(requestRemoveButton);
        final List<DataColumnSpec> notExistingColumns = getNotExistingColumns(spec);
        final List<DataColumnSpec> incompatibleColumns = getIncompatibleTypedColumns(setting.getType(), spec);
        if (!notExistingColumns.isEmpty()) {
            warningMessages.add("Some columns no longer exist (red bordered)");
        }
        if (!incompatibleColumns.isEmpty()) {
            warningMessages.add(String.format("Some columns have an incompatible type to %s (yellow borderd)", typeToString(setting.getType())));
        }
        final Set<DataColumnSpec> invalidColumns = new HashSet<DataColumnSpec>();
        invalidColumns.addAll(notExistingColumns);
        invalidColumns.addAll(incompatibleColumns);
        if (!incompatibleColumns.isEmpty()) {
            setting.setType(MissingValueHandling2ColSetting.TYPE_UNKNOWN);
        }
        if (!invalidColumns.isEmpty() && // if all columns are invalid a clean is the same as a remove
        !(invalidColumns.size() == spec.length)) {
            JButton removeNotExistingColumns = new JButton("Clean");
            removeNotExistingColumns.setToolTipText("Removes all invalid columns from the configuration.");
            removeNotExistingColumns.addActionListener(new ActionListener() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public void actionPerformed(final ActionEvent e) {
                    MissingValueHandling2Panel.this.removeAll();
                    // recreate the content, based on the new settings with removed invalid columns
                    createContent(diff(m_setting, invalidColumns), diff(spec, invalidColumns));
                    firePropertyChange(REMOVED_INVALID_COLUMNS, null, invalidColumns.toArray(new DataColumnSpec[invalidColumns.size()]));
                }
            });
            removePanel.add(removeNotExistingColumns);
        }
        if (!warningMessages.isEmpty()) {
            LOGGER.warn("get warnings during panel validation: " + warningMessages);
            border = BorderFactory.createLineBorder(Color.RED, 2);
            tabPanel.add(createWarningLabel(warningMessages), BorderLayout.NORTH);
        } else {
            border = BorderFactory.createLineBorder(Color.BLACK);
        }
    }
    createWestLayout(setting, tabPanel, icon, name, spec);
    panel.add(createSpacer(1));
    panel.add(removePanel);
    createEastLayout(setting, panel, spec);
    setting.setType(settingTypeBackup);
    m_setting = setting;
    setBorder(border);
    tabPanel.add(panel, BorderLayout.CENTER);
    tabPanel.add(createSpacer(65), BorderLayout.SOUTH);
    add(tabPanel);
}
Also used : JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) JComponent(javax.swing.JComponent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) GridLayout(java.awt.GridLayout) DataColumnSpec(org.knime.core.data.DataColumnSpec) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) Icon(javax.swing.Icon) Border(javax.swing.border.Border) HashSet(java.util.HashSet)

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