Search in sources :

Example 1 with Range

use of org.diirt.util.Range in project yamcs-studio by yamcs.

the class Histogram2DOfFormulaFunction method calculate.

@Override
public Object calculate(List<Object> args) {
    if (NullUtils.containsNull(args)) {
        return null;
    }
    VTable table = (VTable) args.get(0);
    VString yColumnName = (VString) args.get(1);
    VString xColumnName = (VString) args.get(2);
    ListNumber yData = ValueUtil.numericColumnOf(table, yColumnName.getValue());
    ListNumber xData = ValueUtil.numericColumnOf(table, xColumnName.getValue());
    int nPoints = Math.min(yData.size(), xData.size());
    Statistics xStats = StatisticsUtil.statisticsOf(xData);
    Statistics yStats = StatisticsUtil.statisticsOf(yData);
    int nXBins = 20;
    int nYBins = 20;
    Range aggregatedXRange = Ranges.aggregateRange(xStats.getRange(), previousXRange);
    Range aggregatedYRange = Ranges.aggregateRange(yStats.getRange(), previousYRange);
    Range xRange;
    Range yRange;
    if (Ranges.overlap(aggregatedXRange, xStats.getRange()) >= 0.90) {
        xRange = aggregatedXRange;
    } else {
        xRange = xStats.getRange();
    }
    if (Ranges.overlap(aggregatedYRange, yStats.getRange()) >= 0.90) {
        yRange = aggregatedYRange;
    } else {
        yRange = yStats.getRange();
    }
    double minXValueRange = xRange.getMinimum();
    double maxXValueRange = xRange.getMaximum();
    double minYValueRange = yRange.getMinimum();
    double maxYValueRange = yRange.getMaximum();
    ListNumber xBoundaries = ListNumbers.linearListFromRange(minXValueRange, maxXValueRange, nXBins + 1);
    ListNumber yBoundaries = ListNumbers.linearListFromRange(minYValueRange, maxYValueRange, nYBins + 1);
    int[] binData = new int[nXBins * nYBins];
    double maxCount = 0;
    for (int i = 0; i < nPoints; i++) {
        double xValue = xData.getDouble(i);
        double yValue = yData.getDouble(i);
        // Check value in range
        if (xRange.contains(xValue)) {
            if (yRange.contains(yValue)) {
                int xBin = (int) Math.floor(xRange.normalize(xValue) * nXBins);
                int yBin = (int) Math.floor(yRange.normalize(yValue) * nYBins);
                if (xBin == nXBins) {
                    xBin--;
                }
                if (yBin == nYBins) {
                    yBin--;
                }
                int binIndex = yBin * nXBins + xBin;
                binData[binIndex]++;
                if (binData[binIndex] > maxCount) {
                    maxCount = binData[binIndex];
                }
            }
        }
    }
    // time based "forget"
    if (previousMaxCount > maxCount && (previousMaxCount < maxCount * 2.0 || maxCount < 9)) {
        maxCount = previousMaxCount;
    }
    previousMaxCount = maxCount;
    previousXRange = xRange;
    previousXData = xData;
    previousYData = yData;
    previousResult = newVNumberArray(new ArrayInt(binData), new ArrayInt(nYBins, nXBins), Arrays.asList(newDisplay(yBoundaries, ""), newDisplay(xBoundaries, "")), alarmNone(), timeNow(), newDisplay(0.0, 0.0, 0.0, "count", NumberFormats.format(0), maxCount, maxCount, maxCount, Double.NaN, Double.NaN));
    return previousResult;
}
Also used : ListNumber(org.diirt.util.array.ListNumber) VString(org.diirt.vtype.VString) VTable(org.diirt.vtype.VTable) Range(org.diirt.util.Range) ArrayInt(org.diirt.util.array.ArrayInt) Statistics(org.diirt.util.Statistics)

Example 2 with Range

use of org.diirt.util.Range in project yamcs-studio by yamcs.

the class HistogramOfFormulaFunction method calculate.

@Override
public Object calculate(List<Object> args) {
    VNumberArray numberArray = (VNumberArray) args.get(0);
    if (numberArray == null) {
        return null;
    }
    // If no change, return previous
    if (previousValue == numberArray) {
        return previousResult;
    }
    Statistics stats = StatisticsUtil.statisticsOf(numberArray.getData());
    int nBins = 100;
    Range aggregatedRange = Ranges.aggregateRange(stats.getRange(), previousXRange);
    Range xRange;
    if (Ranges.overlap(aggregatedRange, stats.getRange()) >= 0.75) {
        xRange = aggregatedRange;
    } else {
        xRange = stats.getRange();
    }
    IteratorNumber newValues = numberArray.getData().iterator();
    double minValueRange = xRange.getMinimum();
    double maxValueRange = xRange.getMaximum();
    ListNumber xBoundaries = ListNumbers.linearListFromRange(minValueRange, maxValueRange, nBins + 1);
    String unit = numberArray.getUnits();
    int[] binData = new int[nBins];
    double maxCount = 0;
    while (newValues.hasNext()) {
        double value = newValues.nextDouble();
        // Check value in range
        if (xRange.contains(value)) {
            int bin = (int) Math.floor(xRange.normalize(value) * nBins);
            if (bin == nBins) {
                bin--;
            }
            binData[bin]++;
            if (binData[bin] > maxCount) {
                maxCount = binData[bin];
            }
        }
    }
    if (previousMaxCount > maxCount && previousMaxCount < maxCount * 2.0) {
        maxCount = previousMaxCount;
    }
    previousMaxCount = maxCount;
    previousXRange = xRange;
    previousValue = numberArray;
    previousResult = newVNumberArray(new ArrayInt(binData), new ArrayInt(nBins), Arrays.asList(newDisplay(xBoundaries, unit)), numberArray, numberArray, newDisplay(0.0, 0.0, 0.0, "count", NumberFormats.format(0), maxCount, maxCount, maxCount, Double.NaN, Double.NaN));
    return previousResult;
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) Range(org.diirt.util.Range) ArrayInt(org.diirt.util.array.ArrayInt) Statistics(org.diirt.util.Statistics) IteratorNumber(org.diirt.util.array.IteratorNumber)

Aggregations

Range (org.diirt.util.Range)2 Statistics (org.diirt.util.Statistics)2 ArrayInt (org.diirt.util.array.ArrayInt)2 ListNumber (org.diirt.util.array.ListNumber)2 IteratorNumber (org.diirt.util.array.IteratorNumber)1 VNumberArray (org.diirt.vtype.VNumberArray)1 VString (org.diirt.vtype.VString)1 VTable (org.diirt.vtype.VTable)1