Search in sources :

Example 41 with DataColumnSpec

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

the class TimeMissingValueHandlingPanel method createContent.

private void createContent(final TimeMissingValueHandlingColSetting 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.
    ConfigType settingTypeBackup = setting.getType();
    JPanel tabPanel = new JPanel(new BorderLayout(0, 5));
    final JPanel comboRemovePanel = new JPanel(new BorderLayout(0, 2));
    Border border;
    final JComponent removePanel;
    final String typeName = ConfigType.NUMERICAL.equals(setting.getType()) ? "Numeric Columns" : "Non-numeric Columns";
    if (setting.isMetaConfig()) {
        border = BorderFactory.createTitledBorder(typeName);
        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) + "'.");
        }
        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 BorderLayout());
        removePanel.add(requestRemoveButton, BorderLayout.NORTH);
        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)", ConfigType.typeToString(setting.getType())));
        }
        final Set<DataColumnSpec> invalidColumns = new HashSet<DataColumnSpec>();
        invalidColumns.addAll(notExistingColumns);
        invalidColumns.addAll(incompatibleColumns);
        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) {
                    TimeMissingValueHandlingPanel.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, typeName, spec);
    comboRemovePanel.add(m_comboBox, BorderLayout.CENTER);
    comboRemovePanel.add(removePanel, BorderLayout.EAST);
    fillComboBox(setting);
    setting.setType(settingTypeBackup);
    m_setting = setting;
    setBorder(border != null ? BorderFactory.createTitledBorder(border, typeName) : null);
    tabPanel.add(comboRemovePanel, BorderLayout.NORTH);
    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) DataColumnSpec(org.knime.core.data.DataColumnSpec) BorderLayout(java.awt.BorderLayout) ActionListener(java.awt.event.ActionListener) ConfigType(org.knime.timeseries.node.timemissvaluehandler.TimeMissingValueHandlingColSetting.ConfigType) Border(javax.swing.border.Border) HashSet(java.util.HashSet)

Example 42 with DataColumnSpec

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

the class TimeFieldExtractorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec inSpec = inSpecs[0];
    // contains timestamp?
    if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
        throw new InvalidSettingsException("No timestamp found in input table!");
    }
    // currently selected column still there?
    String selectedColName = m_selectedColumn.getStringValue();
    if (selectedColName != null && !selectedColName.isEmpty()) {
        if (!inSpec.containsName(selectedColName)) {
            throw new InvalidSettingsException("Column " + selectedColName + " not found in input spec!");
        }
    } else {
        // no value set: auto-configure -> choose first timeseries
        for (DataColumnSpec colSpec : inSpec) {
            if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
                String colName = colSpec.getName();
                m_selectedColumn.setStringValue(colName);
                setWarningMessage("Auto-configure: selected " + colName);
                break;
            }
        }
    }
    // create outputspec
    ColumnRearranger colRearranger = createColumnRearranger(inSpec).getColumnRearranger();
    return new DataTableSpec[] { colRearranger.createSpec() };
}
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) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 43 with DataColumnSpec

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

the class TimePresetNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected DataTableSpec[] configure(final DataTableSpec[] inSpecs) throws InvalidSettingsException {
    // does input spec contain a date and time col?
    DataTableSpec inSpec = inSpecs[0];
    if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
        throw new InvalidSettingsException("Input table must contain at least one time column!");
    }
    String selectedColName = m_selectedCol.getStringValue();
    if (selectedColName != null && !selectedColName.isEmpty()) {
        // already set -> search for column name in input table
        if (!inSpec.containsName(selectedColName)) {
            throw new InvalidSettingsException("Column " + selectedColName + " not found in input table!");
        } else {
            // check if it is of correct type
            DataColumnSpec colSpec = inSpec.getColumnSpec(selectedColName);
            if (!colSpec.getType().isCompatible(DateAndTimeValue.class)) {
                throw new InvalidSettingsException("Selected column (" + selectedColName + ") must contain date or time!");
            }
        }
    } else {
        // not yet set -> auto-configure: choose first time column
        for (DataColumnSpec colSpec : inSpec) {
            if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
                String colName = colSpec.getName();
                m_selectedCol.setStringValue(colName);
                setWarningMessage("Auto-configure: selected " + colName);
                // take the first compatible column
                break;
            }
        }
    }
    // values in time column are "enriched"
    return inSpecs;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 44 with DataColumnSpec

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

the class JoinedTable method createSpec.

/**
 * Creates a new DataTableSpec as an result of merging a <code>left</code>
 * and a <code>right</code> table. Duplicate of column names are treated
 * as given by the <code>duplicateMethod</code> argument, i.e.
 * <ul>
 * <li> {@link #METHOD_FAIL} throw exception</li>
 * <li> {@link #METHOD_FILTER} ignore duplicates in the right column</li>
 * <li> {@link #METHOD_APPEND_SUFFIX} append a suffix given by the
 * <code>suffix</code> argument to occuring duplicates</li>
 * </ul>
 *
 * @param left the left part of the this table
 * @param right and the corresponding right part
 * @param duplicateMethod the method on how to treat duplicates
 * @param suffix the suffix that is used when the method is
 *            {@link #METHOD_APPEND_SUFFIX}. In case of another any other
 *            method this argument is ignored.
 * @return the spec as result of merging both table specs
 * @throws IllegalArgumentException in case of duplicate column names and no
 *             special treatment is requested
 * @throws NullPointerException if either table is <code>null</code>
 */
public static final DataTableSpec createSpec(final DataTableSpec left, final DataTableSpec right, final String duplicateMethod, final String suffix) {
    DataColumnSpec[] leftCols;
    DataColumnSpec[] rightCols;
    if (METHOD_FAIL.equals(duplicateMethod)) {
        leftCols = new DataColumnSpec[left.getNumColumns()];
        rightCols = new DataColumnSpec[right.getNumColumns()];
        Set<String> hash = new HashSet<String>();
        for (int i = 0; i < left.getNumColumns(); i++) {
            leftCols[i] = left.getColumnSpec(i);
            hash.add(leftCols[i].getName());
        }
        for (int i = 0; i < right.getNumColumns(); i++) {
            rightCols[i] = right.getColumnSpec(i);
            if (hash.contains(rightCols[i].getName())) {
                throw new IllegalArgumentException("Duplicate column: " + rightCols[i].getName());
            }
        }
    } else if (METHOD_FILTER.equals(duplicateMethod)) {
        String[] survivers = getSurvivers(left, right);
        DataTableSpec newRight = FilterColumnTable.createFilterTableSpec(right, survivers);
        leftCols = new DataColumnSpec[left.getNumColumns()];
        rightCols = new DataColumnSpec[newRight.getNumColumns()];
        for (int i = 0; i < left.getNumColumns(); i++) {
            leftCols[i] = left.getColumnSpec(i);
        }
        for (int i = 0; i < newRight.getNumColumns(); i++) {
            rightCols[i] = newRight.getColumnSpec(i);
        }
    } else if (METHOD_APPEND_SUFFIX.equals(duplicateMethod)) {
        final int rightColCount = right.getNumColumns();
        HashSet<String> newInvented = new HashSet<String>();
        DataColumnSpec[] newCols = new DataColumnSpec[rightColCount];
        for (int i = 0; i < rightColCount; i++) {
            DataColumnSpec col = right.getColumnSpec(i);
            String name = col.getName();
            boolean invented = false;
            while (left.containsName(name) || newInvented.contains(name)) {
                invented = true;
                do {
                    name = name.toString() + suffix;
                // we need also the keep track that we don't "invent" a
                // name that is used in the right table already
                } while (right.containsName(name));
            }
            if (invented) {
                newInvented.add(name);
                DataColumnSpecCreator creator = new DataColumnSpecCreator(col);
                creator.setName(name);
                newCols[i] = creator.createSpec();
            } else {
                newCols[i] = col;
            }
        }
        DataTableSpec newRight = new DataTableSpec(newCols);
        leftCols = new DataColumnSpec[left.getNumColumns()];
        rightCols = new DataColumnSpec[newRight.getNumColumns()];
        for (int i = 0; i < left.getNumColumns(); i++) {
            leftCols[i] = left.getColumnSpec(i);
        }
        for (int i = 0; i < right.getNumColumns(); i++) {
            rightCols[i] = newRight.getColumnSpec(i);
        }
    } else {
        throw new IllegalArgumentException("Unknown method: " + duplicateMethod);
    }
    boolean isLeftContainColorHandler = false;
    boolean isLeftContainSizeHandler = false;
    boolean isLeftContainShapeHandler = false;
    for (DataColumnSpec s : leftCols) {
        isLeftContainColorHandler |= s.getColorHandler() != null;
        isLeftContainSizeHandler |= s.getSizeHandler() != null;
        isLeftContainShapeHandler |= s.getShapeHandler() != null;
    }
    for (int i = 0; i < rightCols.length; i++) {
        DataColumnSpec s = rightCols[i];
        boolean removeColorHandler = false;
        if (s.getColorHandler() != null && isLeftContainColorHandler) {
            removeColorHandler = true;
        }
        boolean removeSizeHandler = false;
        if (s.getSizeHandler() != null && isLeftContainSizeHandler) {
            removeSizeHandler = true;
        }
        boolean removeShapeHandler = false;
        if (s.getShapeHandler() != null && isLeftContainShapeHandler) {
            removeShapeHandler = true;
        }
        if (removeColorHandler || removeSizeHandler || removeShapeHandler) {
            DataColumnSpecCreator c = new DataColumnSpecCreator(s);
            if (removeColorHandler) {
                c.setColorHandler(null);
            }
            if (removeSizeHandler) {
                c.setSizeHandler(null);
            }
            if (removeShapeHandler) {
                c.setShapeHandler(null);
            }
            rightCols[i] = c.createSpec();
        }
    }
    DataColumnSpec[] sp = new DataColumnSpec[leftCols.length + rightCols.length];
    System.arraycopy(leftCols, 0, sp, 0, leftCols.length);
    System.arraycopy(rightCols, 0, sp, leftCols.length, rightCols.length);
    return new DataTableSpec(sp);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashSet(java.util.HashSet)

Example 45 with DataColumnSpec

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

the class StatisticsTable method calculateAllMoments.

/**
 * Calculates <b>all the statistical moments in one pass </b>. After the
 * call of this operation, the statistical moments can be obtained very fast
 * from all the other methods.
 *
 * @param rowCount Row count of table for progress, may be NaN if unknown.
 * @param exec object to check with if user canceled the operation
 * @throws CanceledExecutionException if user canceled
 * @throws IllegalArgumentException if rowCount argument < 0
 */
protected void calculateAllMoments(final double rowCount, final ExecutionMonitor exec) throws CanceledExecutionException {
    if (rowCount < 0.0) {
        throw new IllegalArgumentException("rowCount argument must not < 0: " + rowCount);
    }
    DataTableSpec origSpec = m_table.getDataTableSpec();
    int numOfCols = origSpec.getNumColumns();
    // the number of non-missing cells in each column
    int[] validCount = new int[numOfCols];
    double[] sumsquare = new double[numOfCols];
    final DataValueComparator[] comp = new DataValueComparator[numOfCols];
    for (int i = 0; i < numOfCols; i++) {
        sumsquare[i] = 0.0;
        validCount[i] = 0;
        comp[i] = origSpec.getColumnSpec(i).getType().getComparator();
        assert comp[i] != null;
    }
    int nrRows = 0;
    for (RowIterator rowIt = m_table.iterator(); rowIt.hasNext(); nrRows++) {
        DataRow row = rowIt.next();
        if (exec != null) {
            double prog = Double.isNaN(rowCount) ? 0.0 : nrRows / rowCount;
            exec.setProgress(prog, "Calculating statistics, processing row " + (nrRows + 1) + " (\"" + row.getKey() + "\")");
            // throws exception if user canceled
            exec.checkCanceled();
        }
        for (int c = 0; c < numOfCols; c++) {
            final DataCell cell = row.getCell(c);
            if (!(cell.isMissing())) {
                // keep the min and max for each column
                if ((m_minValues[c] == null) || (comp[c].compare(cell, m_minValues[c]) < 0)) {
                    m_minValues[c] = cell;
                }
                if ((m_maxValues[c] == null) || (comp[c].compare(m_maxValues[c], cell) < 0)) {
                    m_maxValues[c] = cell;
                }
                // for double columns we calc the sum (for the mean calc)
                DataType type = origSpec.getColumnSpec(c).getType();
                if (type.isCompatible(DoubleValue.class)) {
                    double d = ((DoubleValue) cell).getDoubleValue();
                    if (Double.isNaN(m_sum[c])) {
                        m_sum[c] = d;
                    } else {
                        m_sum[c] += d;
                    }
                    sumsquare[c] += d * d;
                    validCount[c]++;
                }
            } else {
                m_missingValueCnt[c]++;
            }
        }
        calculateMomentInSubClass(row);
    }
    m_nrRows = nrRows;
    for (int j = 0; j < numOfCols; j++) {
        // missing values
        if (validCount[j] == 0 || m_minValues[j] == null) {
            DataCell mc = DataType.getMissingCell();
            m_minValues[j] = mc;
            m_maxValues[j] = mc;
            m_meanValues[j] = Double.NaN;
            m_varianceValues[j] = Double.NaN;
        } else {
            m_meanValues[j] = m_sum[j] / validCount[j];
            if (validCount[j] > 1) {
                m_varianceValues[j] = (sumsquare[j] - ((m_sum[j] * m_sum[j]) / validCount[j])) / (validCount[j] - 1);
            } else {
                m_varianceValues[j] = 0.0;
            }
            // round-off errors resulting in negative variance values
            if (m_varianceValues[j] < 0.0 && m_varianceValues[j] > -1.0E8) {
                m_varianceValues[j] = 0.0;
            }
            assert m_varianceValues[j] >= 0.0 : "Variance cannot be negative (column \"" + origSpec.getColumnSpec(j).getName() + "\": " + m_varianceValues[j];
        }
    }
    // compute resulting table spec
    int nrCols = m_table.getDataTableSpec().getNumColumns();
    DataColumnSpec[] cSpec = new DataColumnSpec[nrCols];
    for (int c = 0; c < nrCols; c++) {
        DataColumnSpec s = m_table.getDataTableSpec().getColumnSpec(c);
        // we create domains with our bounds.
        Set<DataCell> values = (s.getDomain() == null ? null : s.getDomain().getValues());
        DataColumnDomain newDomain = new DataColumnDomainCreator(values, (m_minValues[c] == null || m_minValues[c].isMissing()) ? null : m_minValues[c], (m_maxValues[c] == null || m_maxValues[c].isMissing()) ? null : m_maxValues[c]).createDomain();
        DataColumnSpecCreator creator = new DataColumnSpecCreator(s);
        creator.setDomain(newDomain);
        cSpec[c] = creator.createSpec();
    }
    m_tSpec = new DataTableSpec(cSpec);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataValueComparator(org.knime.core.data.DataValueComparator) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DoubleValue(org.knime.core.data.DoubleValue) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType)

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