@Test public void readList1() throws Exception { String inputText = "10\n" + "8\n" + "6\n" + "4\n" + "2\n" + "0\n"; VType value = TextIO.readList(new StringReader(inputText)); assertThat(value, instanceOf(VNumberArray.class)); VNumberArray array = (VNumberArray) value; assertThat(array.getData(), equalTo((ListNumber) new ArrayDouble(10, 8, 6, 4, 2, 0))); }
/** * Format just the value of a {@link VType} into a buffer (not timestamp, ..) * * @param value Value * @param buf {@link StringBuilder} * @return {@link StringBuilder} */ public StringBuilder format(final VType value, final StringBuilder buf) { // After the time this is implemented, VEnum may change into a class // that also implements VNumber and/or VString. // Handle it first to assert that VEnum is always identified as such // and not handled as Number. if (value instanceof VEnum) { final VEnum enumerated = (VEnum) value; try { buf.append(enumerated.getValue()); buf.append(" (").append(enumerated.getIndex()).append(")"); } catch (ArrayIndexOutOfBoundsException ex) { // Error getting label for invalid index? buf.append("<enum ").append(enumerated.getIndex()).append(">"); } } else if (value instanceof VNumber) { final VNumber number = (VNumber) value; final Display display = (Display) number; format(number.getValue().doubleValue(), display, buf); } else if (value instanceof VNumberArray) { final VNumberArray array = (VNumberArray) value; final Display display = (Display) array; final ListNumber list = array.getData(); final int N = list.size(); if (N <= MAX_ARRAY_ELEMENTS) { if (N > 0) format(list.getDouble(0), display, buf); for (int i = 1; i < N; ++i) { buf.append(", "); format(list.getDouble(i), display, buf); } } else { format(list.getDouble(0), display, buf); for (int i = 1; i < MAX_ARRAY_ELEMENTS / 2; ++i) { buf.append(", "); format(list.getDouble(i), display, buf); } buf.append(", ... (total ").append(N).append(" elements) ..."); for (int i = N - MAX_ARRAY_ELEMENTS / 2; i < N; ++i) { buf.append(", "); format(list.getDouble(i), display, buf); } } } else if (value instanceof VStatistics) { final VStatistics stats = (VStatistics) value; final Display display = (Display) stats; format(stats.getAverage(), display, buf); buf.append(" [").append(stats.getMin()).append(" ... ").append(stats.getMax()); buf.append(", ").append(stats.getNSamples()).append(" samples"); final Double dev = stats.getStdDev(); if (dev > 0) buf.append(", dev ").append(dev); buf.append("]"); } else if (value instanceof VString) buf.append(((VString) value).getValue()); else if (value == null) buf.append("null"); else // TODO: VEnumArray, other types? buf.append(value.toString()); return buf; }
/** * Default toString implementation for VNumberArray. * * @param vNumberArray the object * @return the string representation */ public static String toString(VNumberArray vNumberArray) { StringBuilder builder = new StringBuilder(); Class type = ValueUtil.typeOf(vNumberArray); builder.append(type.getSimpleName()).append("["); builder.append(format.format(vNumberArray)); builder.append(", size ").append(vNumberArray.getData().size()); appendAlarm(builder, vNumberArray); appendTime(builder, vNumberArray); builder.append(']'); return builder.toString(); }
@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, previousXRange); Range xRange; if (Ranges.overlap(stats, aggregatedRange) >= 0.75) { xRange = aggregatedRange; } else { xRange = stats; } IteratorNumber newValues = numberArray.getData().iterator(); double minValueRange = xRange.getMinimum().doubleValue(); double maxValueRange = xRange.getMaximum().doubleValue(); 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 (Ranges.contains(xRange, value)) { int bin = (int) Math.floor(Ranges.normalize(xRange, 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; }