Search in sources :

Example 61 with DataCell

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

the class DateShiftConfigure method getColumnbasedCellFactory.

/**
 * @param spec the  output column spec
 * @param col1Idx the column index of the numerical column to add
 * @param g the time field to modify (as defined by calendar constants)
 * @param conf the configuration object
 * @param col2Idx the time column
 * @return the cell factory
 */
public static SingleCellFactory getColumnbasedCellFactory(final DataColumnSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
    return new SingleCellFactory(spec) {

        /**
         * Value for the new column is based on the values of two column of the row (first and second date column),
         * the selected granularity, and the fraction digits for rounding.
         *
         * @param row the current row
         * @return the difference between the two date values with the given granularity and rounding
         */
        @Override
        public DataCell getCell(final DataRow row) {
            final int value;
            DataCell cell2 = row.getCell(col2Idx);
            if (cell2.isMissing()) {
                return DataType.getMissingCell();
            }
            String typeofshift = conf.gettypeofshift().getStringValue();
            if (typeofshift.equals(DateShiftNodeDialog.CFG_COLUMN_SHIFT)) {
                DataCell cell1 = row.getCell(col1Idx);
                if ((cell1.isMissing())) {
                    return DataType.getMissingCell();
                }
                value = ((IntValue) cell1).getIntValue();
            } else {
                value = conf.getvalueofshift().getIntValue();
            }
            Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
            c.add(g, value);
            return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
        }
    };
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 62 with DataCell

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

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

the class Statistics2Table method createNominalValueTable.

/**
 * Create nominal value table containing all possible values together with
 * their occurrences.
 * @param nominal value output table
 * @return data table with nominal values for each column
 */
public DataTable createNominalValueTable(final List<String> nominal) {
    DataTableSpec outSpec = createOutSpecNominal(m_spec, nominal);
    Iterator[] it = new Iterator[outSpec.getNumColumns() / 2];
    int idx = 0;
    for (int i = 0; i < m_nominalValues.length; i++) {
        if (m_nominalValues[i] != null) {
            it[idx++] = m_nominalValues[i].entrySet().iterator();
        }
    }
    DataContainer cont = new DataContainer(outSpec);
    int rowIndex = 0;
    do {
        boolean addEnd = true;
        DataCell[] cells = new DataCell[2 * it.length];
        for (int i = 0; i < it.length; i++) {
            if (it[i] != null && it[i].hasNext()) {
                Map.Entry<DataCell, Integer> e = (Map.Entry<DataCell, Integer>) it[i].next();
                cells[2 * i] = e.getKey();
                cells[2 * i + 1] = new IntCell(e.getValue());
                addEnd = false;
            } else {
                cells[2 * i] = DataType.getMissingCell();
                cells[2 * i + 1] = DataType.getMissingCell();
            }
        }
        if (addEnd) {
            break;
        }
        cont.addRowToTable(new DefaultRow(RowKey.createRowKey(rowIndex++), cells));
    } while (true);
    cont.close();
    return cont.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) IntCell(org.knime.core.data.def.IntCell) MutableInteger(org.knime.core.util.MutableInteger) DataContainer(org.knime.core.data.container.DataContainer) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 64 with DataCell

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

the class Statistics2Table method load.

/**
 * Load a new statistic table by the given settings object.
 * @param sett to load this table from
 * @return a new statistic table
 * @throws InvalidSettingsException if the settings are corrupt
 */
public static Statistics2Table load(final NodeSettingsRO sett) throws InvalidSettingsException {
    DataTableSpec spec = DataTableSpec.load(sett.getConfig("spec"));
    Map<DataCell, Integer>[] nominalValues = new Map[spec.getNumColumns()];
    for (int c = 0; c < nominalValues.length; c++) {
        String name = spec.getColumnSpec(c).getName();
        if (!sett.containsKey(name)) {
            nominalValues[c] = null;
        } else {
            nominalValues[c] = new LinkedHashMap<DataCell, Integer>();
            NodeSettingsRO subSett = sett.getNodeSettings(name);
            for (String key : subSett.keySet()) {
                NodeSettingsRO nomSett = subSett.getNodeSettings(key);
                nominalValues[c].put(nomSett.getDataCell("key"), nomSett.getInt("value"));
            }
        }
    }
    double[] min = sett.getDoubleArray("minimum");
    double[] max = sett.getDoubleArray("maximum");
    double[] mean = sett.getDoubleArray("mean");
    double[] var = sett.getDoubleArray("variance");
    double[] median = sett.getDoubleArray("median");
    double[] missings = sett.getDoubleArray("missings");
    double[] sums = sett.getDoubleArray("sums");
    // added with 2.7, fallback -1
    int rowCount = sett.getInt("row_count", -1);
    return new Statistics2Table(spec, min, max, mean, median, var, sums, missings, nominalValues, rowCount);
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 65 with DataCell

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

the class ReadPNGFromURLNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec in, final AtomicLong failCounter) throws InvalidSettingsException {
    String colName = m_config.getUrlColName();
    if (colName == null) {
        // throws ISE
        m_config.guessDefaults(in);
        colName = m_config.getUrlColName();
        setWarningMessage("Auto-configuration: Guessing column \"" + colName + "\" to contain locations");
    }
    final int colIndex = in.findColumnIndex(colName);
    if (colIndex < 0) {
        throw new InvalidSettingsException("No such column in input: " + colName);
    }
    DataColumnSpec colSpec = in.getColumnSpec(colIndex);
    if (!colSpec.getType().isCompatible(StringValue.class)) {
        throw new InvalidSettingsException("Selected column \"" + colName + "\" is not string-compatible");
    }
    final String newColName = m_config.getNewColumnName();
    DataColumnSpecCreator colSpecCreator;
    if (newColName != null) {
        String newName = DataTableSpec.getUniqueColumnName(in, newColName);
        colSpecCreator = new DataColumnSpecCreator(newName, PNGImageContent.TYPE);
    } else {
        colSpecCreator = new DataColumnSpecCreator(colSpec);
        colSpecCreator.setType(PNGImageContent.TYPE);
        colSpecCreator.removeAllHandlers();
        colSpecCreator.setDomain(null);
    }
    DataColumnSpec outColumnSpec = colSpecCreator.createSpec();
    ColumnRearranger rearranger = new ColumnRearranger(in);
    CellFactory fac = new SingleCellFactory(outColumnSpec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIndex);
            if (cell.isMissing()) {
                return DataType.getMissingCell();
            } else {
                String url = ((StringValue) cell).getStringValue();
                try {
                    return toPNGCell(url);
                } catch (Exception e) {
                    if (m_config.isFailOnInvalid()) {
                        if (e instanceof RuntimeException) {
                            throw (RuntimeException) e;
                        } else {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                    } else {
                        String message = "Failed to read png content from " + "\"" + url + "\": " + e.getMessage();
                        LOGGER.warn(message, e);
                        failCounter.incrementAndGet();
                        return DataType.getMissingCell();
                    }
                }
            }
        }
    };
    if (newColName == null) {
        rearranger.replace(fac, colIndex);
    } else {
        rearranger.append(fac);
    }
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Aggregations

DataCell (org.knime.core.data.DataCell)780 DataRow (org.knime.core.data.DataRow)268 DataTableSpec (org.knime.core.data.DataTableSpec)175 DataColumnSpec (org.knime.core.data.DataColumnSpec)170 DefaultRow (org.knime.core.data.def.DefaultRow)169 ArrayList (java.util.ArrayList)141 StringCell (org.knime.core.data.def.StringCell)131 DoubleCell (org.knime.core.data.def.DoubleCell)129 DoubleValue (org.knime.core.data.DoubleValue)111 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)109 DataType (org.knime.core.data.DataType)97 RowKey (org.knime.core.data.RowKey)94 BufferedDataTable (org.knime.core.node.BufferedDataTable)93 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)91 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)84 LinkedHashMap (java.util.LinkedHashMap)81 IntCell (org.knime.core.data.def.IntCell)79 HashMap (java.util.HashMap)60 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)57 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)56