Search in sources :

Example 66 with DataTableSpec

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

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

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

Example 69 with DataTableSpec

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

the class JoinedTableRowIterator method getLeftMissing.

/* Called when there is no corresponding left row to the right one. */
private DataRow getLeftMissing(final RowKey key) {
    DataTableSpec spec = m_table.getLeftTable().getDataTableSpec();
    if (m_leftMissingCells == null) {
        LOGGER.debug("Creating missing values for top table on key \"" + key + "\"");
        if (!m_table.isPrintedErrorOnMissing()) {
            printMissingError(true);
            m_table.setPrintedErrorOnMissing(true);
        }
        m_leftMissingCells = JoinedTable.createMissingCells(spec);
    }
    return new DefaultRow(key, m_leftMissingCells);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 70 with DataTableSpec

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

the class JoinedTableRowIterator method getRightMissing.

/* Called when there is no corresponding right row to the left one. */
private DataRow getRightMissing(final RowKey key) {
    DataTableSpec spec = m_table.getRightTable().getDataTableSpec();
    if (m_rightMissingCells == null) {
        LOGGER.debug("Creating missing values for bottom table on key \"" + key + "\"");
        if (!m_table.isPrintedErrorOnMissing()) {
            printMissingError(false);
            m_table.setPrintedErrorOnMissing(true);
        }
        m_rightMissingCells = JoinedTable.createMissingCells(spec);
    }
    return new DefaultRow(key, m_rightMissingCells);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultRow(org.knime.core.data.def.DefaultRow)

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