Search in sources :

Example 1 with ListNumber

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;
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ListByte(org.diirt.util.array.ListByte) ListInt(org.diirt.util.array.ListInt) ListLong(org.diirt.util.array.ListLong) ListShort(org.diirt.util.array.ListShort) NumberFormat(java.text.NumberFormat)

Example 2 with ListNumber

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;
        }
    };
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ListDouble(org.diirt.util.array.ListDouble)

Example 3 with ListNumber

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;
}
Also used : ListNumber(org.diirt.util.array.ListNumber) ArrayDouble(org.diirt.util.array.ArrayDouble) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with ListNumber

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));
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) ArrayDouble(org.diirt.util.array.ArrayDouble) ListDouble(org.diirt.util.array.ListDouble)

Example 5 with ListNumber

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;
}
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)

Aggregations

ListNumber (org.diirt.util.array.ListNumber)29 VNumberArray (org.diirt.vtype.VNumberArray)13 List (java.util.List)10 VString (org.diirt.vtype.VString)9 ArrayList (java.util.ArrayList)6 ArrayDouble (org.diirt.util.array.ArrayDouble)5 ArrayInt (org.diirt.util.array.ArrayInt)5 ListInt (org.diirt.util.array.ListInt)5 VEnumArray (org.diirt.vtype.VEnumArray)5 VNumber (org.diirt.vtype.VNumber)4 VTable (org.diirt.vtype.VTable)4 VType (org.diirt.vtype.VType)4 Test (org.junit.Test)4 AbstractList (java.util.AbstractList)3 ListDouble (org.diirt.util.array.ListDouble)3 VEnum (org.diirt.vtype.VEnum)3 VStringArray (org.diirt.vtype.VStringArray)3 BufferedImage (java.awt.image.BufferedImage)2 Background (javafx.scene.layout.Background)2 BackgroundFill (javafx.scene.layout.BackgroundFill)2