Search in sources :

Example 16 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ModeOperator method getResultInternal.

/**
 * {@inheritDoc}
 */
@Override
protected DataCell getResultInternal() {
    if (m_valCounter.size() < 1) {
        return DataType.getMissingCell();
    }
    // get the cell with the most counts
    final Set<Entry<DataCell, MutableInteger>> entries = m_valCounter.entrySet();
    int max = Integer.MIN_VALUE;
    DataCell result = null;
    for (final Entry<DataCell, MutableInteger> entry : entries) {
        if (entry.getValue().intValue() > max) {
            max = entry.getValue().intValue();
            result = entry.getKey();
        }
    }
    return result;
}
Also used : Entry(java.util.Map.Entry) MutableInteger(org.knime.core.util.MutableInteger) DataCell(org.knime.core.data.DataCell)

Example 17 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ModeOperator method computeInternal.

/**
 * {@inheritDoc}
 */
@Override
protected boolean computeInternal(final DataCell cell) {
    MutableInteger counter = m_valCounter.get(cell);
    if (counter == null) {
        // before adding a new value
        if (m_valCounter.size() >= getMaxUniqueValues()) {
            setSkipMessage("Group contains too many unique values");
            return true;
        }
        counter = new MutableInteger(0);
        m_valCounter.put(cell, counter);
    }
    counter.inc();
    return false;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 18 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ColumnAggregator method saveColumnAggregators.

/**
 * @param settings the settings object to write to
 * @param key the unique settings key or <code>null</code> if the default should be used
 * @param aggregators the {@link ColumnAggregator} objects to save
 * @since 2.11
 */
public static void saveColumnAggregators(final NodeSettingsWO settings, final String key, final List<ColumnAggregator> aggregators) {
    if (settings == null) {
        throw new NullPointerException("settings must not be null");
    }
    if (aggregators == null) {
        throw new NullPointerException("aggregators must not be null");
    }
    final String[] colNames = new String[aggregators.size()];
    final String[] aggrMethods = new String[aggregators.size()];
    final boolean[] inclMissingVals = new boolean[aggregators.size()];
    final DataType[] types = new DataType[aggregators.size()];
    final Config cnfg;
    if (key == null || key.isEmpty()) {
        cnfg = settings.addConfig(CNFG_AGGR_COL_SECTION);
    } else {
        cnfg = settings.addConfig(key);
    }
    final NodeSettingsWO operatorSettings = settings.addNodeSettings(CNFG_OPERATOR_SETTINGS);
    final Map<String, MutableInteger> idMap = new HashMap<>();
    for (int i = 0, length = aggregators.size(); i < length; i++) {
        final ColumnAggregator colAggr = aggregators.get(i);
        colNames[i] = colAggr.getOriginalColName();
        final AggregationMethod method = colAggr.getMethodTemplate();
        aggrMethods[i] = colAggr.getId();
        types[i] = colAggr.getOriginalDataType();
        inclMissingVals[i] = colAggr.inclMissingCells();
        if (colAggr.hasOptionalSettings()) {
            try {
                final NodeSettingsWO operatorSetting = operatorSettings.addNodeSettings(createSettingsKey(idMap, colAggr));
                method.saveSettingsTo(operatorSetting);
            } catch (Exception e) {
                LOGGER.error("Exception while saving settings for aggreation operator '" + colAggr.getId() + "', reason: " + e.getMessage());
            }
        }
    }
    cnfg.addStringArray(CNFG_COL_NAMES, colNames);
    cnfg.addDataTypeArray(CNFG_COL_TYPES, types);
    cnfg.addStringArray(CNFG_AGGR_METHODS, aggrMethods);
    cnfg.addBooleanArray(CNFG_INCL_MISSING_VALS, inclMissingVals);
}
Also used : NodeSettingsWO(org.knime.core.node.NodeSettingsWO) HashMap(java.util.HashMap) Config(org.knime.core.node.config.Config) MutableInteger(org.knime.core.util.MutableInteger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType)

Example 19 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class ColumnAggregator method createSettingsKey.

/**
 * @param idMap
 * @param colAggr the {@link ColumnAggregator} to use
 * @return the unique settings key
 */
private static String createSettingsKey(final Map<String, MutableInteger> idMap, final ColumnAggregator colAggr) {
    // the method id and the original column name are unique since
    // both are used to generate the new column name in the result table
    // which must be unique as well
    final String id = colAggr.getId() + "_" + colAggr.getOriginalColName();
    final MutableInteger counter = idMap.get(id);
    final String uniqueId;
    if (counter == null) {
        idMap.put(id, new MutableInteger(0));
        uniqueId = id;
    } else {
        counter.inc();
        uniqueId = id + "_" + counter.intValue();
    }
    return uniqueId;
}
Also used : MutableInteger(org.knime.core.util.MutableInteger)

Example 20 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class EnrichmentPlotterModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final double rowCount = inData[0].size();
    final BufferedDataContainer areaOutCont = exec.createDataContainer(AREA_OUT_SPEC);
    final BufferedDataContainer discrateOutCont = exec.createDataContainer(getDiscrateOutSpec());
    final double[] fractionSizes = m_settings.getFractionSizes();
    for (int i = 0; i < m_settings.getCurveCount(); i++) {
        final ExecutionMonitor sexec = exec.createSubProgress(1.0 / m_settings.getCurveCount());
        exec.setMessage("Generating curve " + (i + 1));
        final Curve c = m_settings.getCurve(i);
        final Helper[] curve = new Helper[KnowsRowCountTable.checkRowCount(inData[0].size())];
        final int sortIndex = inData[0].getDataTableSpec().findColumnIndex(c.getSortColumn());
        final int actIndex = inData[0].getDataTableSpec().findColumnIndex(c.getActivityColumn());
        int k = 0, maxK = 0;
        for (DataRow row : inData[0]) {
            DataCell c1 = row.getCell(sortIndex);
            DataCell c2 = row.getCell(actIndex);
            if (k++ % 100 == 0) {
                sexec.checkCanceled();
                sexec.setProgress(k / rowCount);
            }
            if (c1.isMissing()) {
                continue;
            } else {
                curve[maxK] = new Helper(((DoubleValue) c1).getDoubleValue(), c2);
            }
            maxK++;
        }
        Arrays.sort(curve, 0, maxK);
        if (c.isSortDescending()) {
            for (int j = 0; j < maxK / 2; j++) {
                Helper h = curve[j];
                curve[j] = curve[maxK - j - 1];
                curve[maxK - j - 1] = h;
            }
        }
        // this is for down-sampling so that the view is faster;
        // plotting >100,000 points takes quite a long time
        final int size = Math.min(MAX_RESOLUTION, maxK);
        final double downSampleRate = maxK / (double) size;
        final double[] xValues = new double[size + 1];
        final double[] yValues = new double[size + 1];
        xValues[0] = 0;
        yValues[0] = 0;
        int lastK = 0;
        double y = 0, area = 0;
        int nextHitRatePoint = 0;
        final double[] hitRateValues = new double[fractionSizes.length];
        final HashMap<DataCell, MutableInteger> clusters = new HashMap<DataCell, MutableInteger>();
        // set hit rate values for fractions that are smaller than 1 row to 0
        while ((maxK * fractionSizes[nextHitRatePoint] / 100) < 1) {
            hitRateValues[nextHitRatePoint++] = 0;
        }
        for (k = 1; k <= maxK; k++) {
            final Helper h = curve[k - 1];
            if (m_settings.plotMode() == PlotMode.PlotSum) {
                y += ((DoubleValue) h.b).getDoubleValue();
            } else if (m_settings.plotMode() == PlotMode.PlotHits) {
                if (!h.b.isMissing() && (((DoubleValue) h.b).getDoubleValue() >= m_settings.hitThreshold())) {
                    y++;
                }
            } else if (!h.b.isMissing()) {
                MutableInteger count = clusters.get(h.b);
                if (count == null) {
                    count = new MutableInteger(0);
                    clusters.put(h.b, count);
                }
                if (count.inc() == m_settings.minClusterMembers()) {
                    y++;
                }
            }
            area += y / maxK;
            if ((int) (k / downSampleRate) >= lastK + 1) {
                lastK++;
                xValues[lastK] = k;
                yValues[lastK] = y;
            }
            // thats why this needs to be a while
            while ((nextHitRatePoint < fractionSizes.length) && (k == (int) Math.floor(maxK * fractionSizes[nextHitRatePoint] / 100))) {
                hitRateValues[nextHitRatePoint] = y;
                nextHitRatePoint++;
            }
        }
        xValues[xValues.length - 1] = maxK;
        yValues[yValues.length - 1] = y;
        area /= y;
        m_curves.add(new EnrichmentPlot(c.getSortColumn() + " vs " + c.getActivityColumn(), xValues, yValues, area));
        areaOutCont.addRowToTable(new DefaultRow(new RowKey(c.toString()), new DoubleCell(area)));
        for (int j = 0; j < hitRateValues.length; j++) {
            hitRateValues[j] /= y;
        }
        double[] enrichmentFactors = new double[hitRateValues.length];
        for (int j = 0; j < enrichmentFactors.length; j++) {
            enrichmentFactors[j] = calculateEnrichmentFactor(hitRateValues[j], fractionSizes[j]);
        }
        discrateOutCont.addRowToTable(new DefaultRow(new RowKey(c.toString()), ArrayUtils.addAll(hitRateValues, enrichmentFactors)));
    }
    areaOutCont.close();
    discrateOutCont.close();
    return new BufferedDataTable[] { areaOutCont.getTable(), discrateOutCont.getTable() };
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) HashMap(java.util.HashMap) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) MutableInteger(org.knime.core.util.MutableInteger) Curve(org.knime.base.node.viz.enrichment2.EnrichmentPlotterSettings.Curve) DataRow(org.knime.core.data.DataRow) DoubleValue(org.knime.core.data.DoubleValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DefaultRow(org.knime.core.data.def.DefaultRow)

Aggregations

MutableInteger (org.knime.core.util.MutableInteger)32 DataCell (org.knime.core.data.DataCell)12 HashMap (java.util.HashMap)11 DataRow (org.knime.core.data.DataRow)8 RowKey (org.knime.core.data.RowKey)7 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 HashSet (java.util.HashSet)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 DefaultRow (org.knime.core.data.def.DefaultRow)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 StringCell (org.knime.core.data.def.StringCell)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2