use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class SimpleValueFormat method format.
/**
* Formats a numeric array. This method can be overridden to change
* the way numeric arrays are formatted.
*
* @param array the array to format
* @param toAppendTo the buffer to append to
* @param pos the position of the field
* @return the string buffer
*/
protected StringBuffer format(VNumberArray array, StringBuffer toAppendTo, FieldPosition pos) {
NumberFormat f = nf(array);
toAppendTo.append("[");
boolean hasMore = false;
ListNumber data = array.getData();
if (data.size() > maxElements) {
hasMore = true;
}
for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
if (i != 0) {
toAppendTo.append(", ");
}
if (data instanceof ListByte || data instanceof ListShort || data instanceof ListInt || data instanceof ListLong) {
toAppendTo.append(f.format(data.getLong(i)));
} else {
toAppendTo.append(f.format(data.getDouble(i)));
}
}
if (hasMore) {
toAppendTo.append(", ...");
}
toAppendTo.append("]");
return toAppendTo;
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class VColumn method combineDoubleData.
private static Object combineDoubleData(final int size, final ListInt offsets, final List<VColumn> columns) {
return new ListDouble() {
@Override
public double getDouble(int index) {
int tableIndex = ListNumbers.binarySearchValueOrLower(offsets, index);
if (columns.get(tableIndex) == null) {
return Double.NaN;
}
int rowIndex = index - offsets.getInt(tableIndex);
// TODO: mismatched type should be handled better
if (!ListNumber.class.isInstance(columns.get(tableIndex).getData())) {
return Double.NaN;
}
@SuppressWarnings("unchecked") ListNumber values = (ListNumber) columns.get(tableIndex).getData();
if (rowIndex < values.size()) {
return values.getDouble(rowIndex);
} else {
return Double.NaN;
}
}
@Override
public int size() {
return size;
}
};
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class VTableFactory method extractColumnData.
private static Object extractColumnData(Object columnData, int... rows) {
if (columnData instanceof List) {
List<Object> data = new ArrayList<>(rows.length);
for (int i = 0; i < rows.length; i++) {
int j = rows[i];
data.add(((List<?>) columnData).get(j));
}
return data;
} else if (columnData instanceof ListNumber) {
double[] data = new double[rows.length];
for (int i = 0; i < rows.length; i++) {
int j = rows[i];
data[i] = ((ListNumber) columnData).getDouble(j);
}
return new ArrayDouble(data);
}
return null;
}
use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.
the class DftFormulaFunction method calculate.
@Override
public Object calculate(final List<Object> args) {
VNumberArray array = (VNumberArray) args.get(0);
if (array == null) {
return null;
}
if (array.getSizes().size() != 1) {
throw new IllegalArgumentException("Only 1D array supported for DFT");
}
// TODO: no need to allocate empty array
List<ListNumber> fft = ListMath.dft(array.getData(), new ArrayDouble(new double[array.getData().size()]));
final ListNumber real = fft.get(0);
final ListNumber imaginary = fft.get(1);
ListNumber modulus = new ListDouble() {
@Override
public double getDouble(int index) {
double x = real.getDouble(index);
double y = imaginary.getDouble(index);
if (x != 0 || y != 0) {
return Math.sqrt(x * x + y * y);
} else {
return 0.0;
}
}
@Override
public int size() {
return real.size();
}
};
ListNumber phase = new ListDouble() {
@Override
public double getDouble(int index) {
double x = real.getDouble(index);
double y = imaginary.getDouble(index);
return Math.atan2(y, x);
}
@Override
public int size() {
return real.size();
}
};
return ValueFactory.newVTable(Arrays.<Class<?>>asList(double.class, double.class, double.class, double.class), Arrays.asList("x", "y", "mod", "phase"), Arrays.<Object>asList(real, imaginary, modulus, phase));
}
use of org.diirt.util.array.ListNumber 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;
}
Aggregations