use of org.diirt.util.Statistics in project yamcs-studio by yamcs.
the class ArrayOfNumberFormulaFunction method calculate.
@Override
public Object calculate(final List<Object> args) {
ListDouble data = new ListDouble() {
@Override
public double getDouble(int index) {
VNumber number = (VNumber) args.get(index);
if (number == null || number.getValue() == null)
return Double.NaN;
else
return number.getValue().doubleValue();
}
@Override
public int size() {
return args.size();
}
};
VNumber firstNonNull = null;
for (Object object : args) {
if (object != null) {
firstNonNull = (VNumber) object;
}
}
Display display = displayNone();
if (firstNonNull != null) {
if (ValueUtil.displayHasValidDisplayLimits(firstNonNull)) {
display = firstNonNull;
} else {
Statistics stats = StatisticsUtil.statisticsOf(data);
display = newDisplay(stats.getRange().getMinimum(), stats.getRange().getMinimum(), stats.getRange().getMinimum(), "", NumberFormats.toStringFormat(), stats.getRange().getMaximum(), stats.getRange().getMaximum(), stats.getRange().getMaximum(), stats.getRange().getMinimum(), stats.getRange().getMaximum());
}
}
return ValueFactory.newVNumberArray(data, highestSeverityOf(args, false), latestValidTimeOrNowOf(args), display);
}
use of org.diirt.util.Statistics 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;
}
use of org.diirt.util.Statistics 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;
}
Aggregations