Search in sources :

Example 86 with DoubleValue

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

the class MedianTable method sortOnDisk.

/**
 * Sorts the data on the disk, it moves the missing values to the end.
 *
 * @param context An {@link ExecutionContext}.
 * @param k The indices to read from the different columns
 *        (first dim: length 2 (above & below median indices), second dim: columns)
 * @throws CanceledExecutionException Execution was cancelled.
 */
private void sortOnDisk(final ExecutionContext context, final long[][] k) throws CanceledExecutionException {
    final SortingDescription[] sorting = new SortingDescription[m_indices.length];
    final DataTableSpec spec = m_table.getSpec();
    for (int i = 0; i < m_indices.length; i++) {
        final DataColumnSpec columnSpec = spec.getColumnSpec(m_indices[i]);
        final DataValueComparator comparator = columnSpec.getType().getComparator();
        sorting[i] = new SortingDescription(columnSpec.getName()) {

            @Override
            public int compare(final DataRow o1, final DataRow o2) {
                // Move missing values to the end.
                final DataCell c1 = o1.getCell(0);
                final DataCell c2 = o2.getCell(0);
                if (c1.isMissing()) {
                    return c2.isMissing() ? 0 : 1;
                }
                if (c2.isMissing()) {
                    return -1;
                }
                return comparator.compare(c1, c2);
            }
        };
    }
    final ColumnBufferedDataTableSorter tableSorter;
    try {
        tableSorter = new ColumnBufferedDataTableSorter(m_table.getSpec(), m_table.size(), sorting);
    } catch (InvalidSettingsException e) {
        throw new IllegalStateException(e);
    }
    final MutableLong counter = new MutableLong();
    final DoubleValue[][] cells = new DoubleValue[2][m_indices.length];
    tableSorter.sort(m_table, context, new SortingConsumer() {

        @Override
        public void consume(final DataRow row) {
            for (int kindex = 0; kindex < 2; kindex++) {
                for (int i = 0; i < m_indices.length; i++) {
                    if (counter.longValue() == k[kindex][i]) {
                        DataCell cell = row.getCell(i);
                        if (cell instanceof DoubleValue) {
                            DoubleValue dv = (DoubleValue) cell;
                            cells[kindex][i] = dv;
                        } else {
                            cells[kindex][i] = new DoubleCell(Double.NaN);
                        }
                    }
                }
            }
            counter.increment();
        }
    });
    for (int index = m_indices.length; index-- > 0; ) {
        if (cells[0][index] == null || cells[1][index] == null) {
            // No non-missing rows
            m_medians[index] = Double.NaN;
        } else {
            m_medians[index] = (cells[0][index].getDoubleValue() + cells[1][index].getDoubleValue()) / 2;
        }
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DoubleCell(org.knime.core.data.def.DoubleCell) SortingDescription(org.knime.core.data.sort.SortingDescription) DataValueComparator(org.knime.core.data.DataValueComparator) DataRow(org.knime.core.data.DataRow) ColumnBufferedDataTableSorter(org.knime.core.data.sort.ColumnBufferedDataTableSorter) MutableLong(org.apache.commons.lang.mutable.MutableLong) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) SortingConsumer(org.knime.core.data.sort.SortingConsumer) DataCell(org.knime.core.data.DataCell)

Example 87 with DoubleValue

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

the class Statistics3Table method allApplicableColumns.

/**
 * Finds those columns that have applicable columns.
 *
 * @param spec A {@link DataTableSpec}.
 * @param nominalValueColumns The list of names of nominal values to check.
 * @return The indices (in ascending order) which are {@link DoubleValue}d, or one of the nominal values.
 * @since 2.9
 */
protected static int[] allApplicableColumns(final DataTableSpec spec, final List<String> nominalValueColumns) {
    final int[] allColumns = allColumns(spec.getNumColumns());
    int toRemove = 0;
    for (int i = allColumns.length; i-- > 0; ) {
        final DataColumnSpec colSpec = spec.getColumnSpec(i);
        if (colSpec.getType().isCompatible(DoubleValue.class) || nominalValueColumns.contains(colSpec.getName())) {
            allColumns[i] = i;
        } else {
            allColumns[i] = -1;
            ++toRemove;
        }
    }
    if (toRemove > 0) {
        int[] filtered = new int[allColumns.length - toRemove];
        int j = 0;
        for (int i = 0; i < allColumns.length; ++i) {
            if (allColumns[i] >= 0) {
                filtered[j++] = allColumns[i];
            }
        }
        return filtered;
    }
    return allColumns;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue)

Example 88 with DoubleValue

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

the class SpecialDoubleCells method consumeRow.

/**
 * {@inheritDoc}
 */
@Override
protected void consumeRow(final DataRow dataRow) {
    int index = 0;
    for (int i : getIndices()) {
        DataCell cell = dataRow.getCell(i);
        if (!cell.isMissing()) {
            double val = ((DoubleValue) cell).getDoubleValue();
            if (Double.isInfinite(val)) {
                if (val > 0) {
                    m_numberPositiveInfiniteValues[index]++;
                } else {
                    m_numberNegativeInfiniteValues[index]++;
                }
            } else if (Double.isNaN(val)) {
                m_numberNaNValues[index]++;
            }
        }
        index++;
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 89 with DoubleValue

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

the class CovarianceMatrixCalculator method calculateCovarianceMatrix.

/**
 * Computes the covariance matrix and puts the result in the given (optional) data container and additionally
 * returns a in memory representation. The data container is expected to have the data table spec returned at
 * {@link #getResultSpec()}. The implementation traverses the data once.
 *
 * @param exec the execution container
 * @param inTable input data
 * @param tableSize the data table size
 * @param resultDataContainer optional result data container
 * @return the covariance matrix
 * @throws CanceledExecutionException if the user canceled the execution
 */
public RealMatrix calculateCovarianceMatrix(final ExecutionMonitor exec, final DataTable inTable, final long tableSize, final DataContainer resultDataContainer) throws CanceledExecutionException {
    checkArgument(m_targetSpec.equalStructure(inTable.getDataTableSpec()), "Target tables spec is different from the one given in the constructor!");
    if (resultDataContainer != null) {
        checkArgument(m_resultSpec.equalStructure(resultDataContainer.getTableSpec()), "Result tables spec is invalid!");
    }
    final ExecutionMonitor computingProgress = exec.createSubProgress(resultDataContainer != null ? 0.8 : 1);
    List<StorelessCovariance> covariancesList = new ArrayList<>();
    // create covariance pairs
    for (int i = 0; i < m_indexes.length; i++) {
        for (int j = i; j < m_indexes.length; j++) {
            covariancesList.add(new StorelessCovariance(2));
        }
    }
    // compute rest of co-variance matrix
    int rowCount = 0;
    double[] buffer = new double[2];
    for (DataRow dataRow : inTable) {
        for (int i = 0; i < m_indexes.length; i++) {
            final int outerIndex = m_indexes[i];
            final DataCell outerCell = dataRow.getCell(outerIndex);
            if (outerCell.isMissing()) {
                // skip missing values
                continue;
            }
            final double outerDouble = ((DoubleValue) outerCell).getDoubleValue();
            for (int j = i; j < m_indexes.length; j++) {
                final int innerIndex = m_indexes[j];
                final DataCell innerCell = dataRow.getCell(innerIndex);
                if (innerCell.isMissing()) {
                    // skip missing values
                    continue;
                }
                final double innerDouble = ((DoubleValue) innerCell).getDoubleValue();
                buffer[0] = outerDouble;
                buffer[1] = innerDouble;
                int covListIndex = index(m_indexes.length, i, j);
                covariancesList.get(covListIndex).increment(buffer);
            }
        }
        computingProgress.setProgress(rowCount++ / (double) tableSize, "Calculate covariance values, processing row: '" + dataRow.getKey() + "'");
        computingProgress.checkCanceled();
    }
    // Copy the storeless covariances to a real matrix
    RealMatrix covMatrix = new Array2DRowRealMatrix(m_indexes.length, m_indexes.length);
    for (int i = 0; i < m_indexes.length; i++) {
        for (int j = i; j < m_indexes.length; j++) {
            int covListIndex = index(m_indexes.length, i, j);
            double covValue;
            try {
                covValue = i == j ? covariancesList.get(covListIndex).getCovariance(1, 1) : covariancesList.get(covListIndex).getCovariance(0, 1);
            } catch (NumberIsTooSmallException e) {
                throw new IllegalArgumentException(String.format("There were not enough valid values to " + "compute covariance between columns: '%s' and '%s'.", inTable.getDataTableSpec().getColumnSpec(m_indexes[i]).getName(), inTable.getDataTableSpec().getColumnSpec(m_indexes[j]).getName()), e);
            }
            covMatrix.setEntry(i, j, covValue);
            covMatrix.setEntry(j, i, covValue);
        }
    }
    if (resultDataContainer != null) {
        exec.setProgress("Writing matrix to data table");
        final ExecutionMonitor writingProgress = exec.createSubProgress(0.2);
        for (int i = 0; i < covMatrix.getRowDimension(); i++) {
            resultDataContainer.addRowToTable(new DefaultRow(RowKey.toRowKeys(resultDataContainer.getTableSpec().getColumnSpec(i).getName())[0], covMatrix.getRow(i)));
            exec.checkCanceled();
            writingProgress.setProgress((double) i / covMatrix.getRowDimension(), "Writing row: " + resultDataContainer.getTableSpec().getColumnSpec(i).getName());
        }
    }
    return covMatrix;
}
Also used : ArrayList(java.util.ArrayList) NumberIsTooSmallException(org.apache.commons.math3.exception.NumberIsTooSmallException) StorelessCovariance(org.apache.commons.math3.stat.correlation.StorelessCovariance) DataRow(org.knime.core.data.DataRow) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) RealMatrix(org.apache.commons.math3.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math3.linear.Array2DRowRealMatrix) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 90 with DoubleValue

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

the class AffineTransTable method generateNewSpec.

/**
 * Creates a new DataTableSpec. The target column's type is set to
 * DoubleType, the domain is adjusted.
 */
private static DataTableSpec generateNewSpec(final DataTableSpec spec, final AffineTransConfiguration configuration) {
    String[] names = configuration.getNames();
    HashMap<String, Integer> hash = new HashMap<String, Integer>();
    for (int i = 0; i < names.length; i++) {
        hash.put(names[i], i);
    }
    for (int i = 0; i < spec.getNumColumns(); i++) {
        DataColumnSpec col = spec.getColumnSpec(i);
        Integer index = hash.get(col.getName());
        if (index != null) {
            DataType type = col.getType();
            // do we need to support IntValue also?
            if (!type.isCompatible(DoubleValue.class)) {
                throw new IllegalArgumentException("Not supported: " + type);
            }
        }
    }
    DataColumnSpec[] specs = new DataColumnSpec[spec.getNumColumns()];
    double[] newmin = configuration.getMin();
    double[] newmax = configuration.getMax();
    double[] scales = configuration.getScales();
    double[] translations = configuration.getTranslations();
    for (int i = 0; i < specs.length; i++) {
        DataColumnSpec colSpec = spec.getColumnSpec(i);
        DataColumnDomain colDomain = colSpec.getDomain();
        Integer indexObject = hash.get(colSpec.getName());
        if (indexObject == null) {
            specs[i] = colSpec;
        } else {
            int index = indexObject.intValue();
            assert !Double.isNaN(scales[index]);
            // determine domain
            double interval = newmax[index] - newmin[index];
            DataCell up = null;
            DataCell oldUp = colDomain.getUpperBound();
            if (oldUp != null && !oldUp.isMissing()) {
                double oldVal = ((DoubleValue) oldUp).getDoubleValue();
                double newVal = scales[index] * oldVal + translations[index];
                if (!Double.isNaN(newmax[index])) {
                    if (newVal > newmax[index] && ((newVal - newmax[index]) / interval) < VERY_SMALL) {
                        newVal = newmax[index];
                    }
                }
                up = new DoubleCell(newVal);
            }
            DataCell low = null;
            DataCell oldLow = colDomain.getLowerBound();
            if (oldLow != null && !oldLow.isMissing()) {
                double oldVal = ((DoubleValue) oldLow).getDoubleValue();
                double newVal = scales[index] * oldVal + translations[index];
                if (!Double.isNaN(newmin[index])) {
                    if (newVal < newmin[index] && ((newmin[index] - newVal) / interval) < VERY_SMALL) {
                        newVal = newmin[index];
                    }
                }
                low = new DoubleCell(newVal);
            }
            DataColumnDomain dom = new DataColumnDomainCreator(low, up).createDomain();
            DataType type = DoubleCell.TYPE;
            DataColumnSpecCreator c = new DataColumnSpecCreator(colSpec);
            // IntType must be converted to DoubleType!
            c.setType(type);
            c.setDomain(dom);
            specs[i] = c.createSpec();
        }
    }
    return new DataTableSpec(specs);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) HashMap(java.util.HashMap) DoubleCell(org.knime.core.data.def.DoubleCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell)

Aggregations

DoubleValue (org.knime.core.data.DoubleValue)154 DataCell (org.knime.core.data.DataCell)103 DataRow (org.knime.core.data.DataRow)71 DataColumnSpec (org.knime.core.data.DataColumnSpec)38 DataTableSpec (org.knime.core.data.DataTableSpec)38 DoubleCell (org.knime.core.data.def.DoubleCell)32 ArrayList (java.util.ArrayList)26 BufferedDataTable (org.knime.core.node.BufferedDataTable)26 DataType (org.knime.core.data.DataType)23 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)21 LinkedHashMap (java.util.LinkedHashMap)18 IntValue (org.knime.core.data.IntValue)15 HashMap (java.util.HashMap)14 RowIterator (org.knime.core.data.RowIterator)14 RowKey (org.knime.core.data.RowKey)13 DefaultRow (org.knime.core.data.def.DefaultRow)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 LongValue (org.knime.core.data.LongValue)10 StringValue (org.knime.core.data.StringValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10