ZeroValueDoubleTimeSeriesFilter extends TimeSeriesFilter { @Override public FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts) { ArgumentChecker.notNull(ts, "ts"); if (ts.isEmpty()) { s_logger.info("Time series was empty"); return new FilteredTimeSeries(EMPTY_SERIES, EMPTY_SERIES); } final int n = ts.size(); final int[] filteredDates = new int[n]; final double[] filteredData = new double[n]; final int[] rejectedDates = new int[n]; final double[] rejectedData = new double[n]; final LocalDateDoubleEntryIterator it = ts.iterator(); int i = 0, j = 0; while (it.hasNext()) { int date = it.nextTimeFast(); double value = it.currentValue(); if (Math.abs(value) < _zero) { rejectedDates[j] = date; rejectedData[j++] = value; } else { filteredDates[i] = date; filteredData[i++] = value; } } return getFilteredSeries(filteredDates, filteredData, i, rejectedDates, rejectedData, j); } ZeroValueDoubleTimeSeriesFilter();  ZeroValueDoubleTimeSeriesFilter(final double zero); void setZero(final double zero); @Override FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTS() { SMALL_ZERO_FILTER.evaluate((LocalDateDoubleTimeSeries) null); }
@Test public void testEmptyTS() { final FilteredTimeSeries filtered = SMALL_ZERO_FILTER.evaluate(EMPTY_SERIES); assertEquals(filtered.getFilteredTS(), EMPTY_SERIES); assertEquals(filtered.getRejectedTS(), EMPTY_SERIES); }
@Test public void test() { final int n = 100; final LocalDate[] dates = new LocalDate[n]; final double[] data = new double[n]; LocalDate[] smallZeroFilteredDates = new LocalDate[n]; double[] smallZeroFilteredData = new double[n]; LocalDate[] smallZeroRejectedDates = new LocalDate[n]; double[] smallZeroRejectedData = new double[n]; LocalDate[] largeZeroFilteredDates = new LocalDate[n]; double[] largeZeroFilteredData = new double[n]; LocalDate[] largeZeroRejectedDates = new LocalDate[n]; double[] largeZeroRejectedData = new double[n]; double d; final Double smallValue = 1e-4; int h, j = 0, k = 0, l = 0, m = 0; for (int i = 0; i < 100; i++) { d = RANDOM.nextDouble(); h = i + 1; dates[i] = LocalDate.ofEpochDay(h); if (d < 0.3) { if (d > 0.25) { data[i] = smallValue; largeZeroRejectedDates[j] = LocalDate.ofEpochDay(h); largeZeroRejectedData[j++] = smallValue; smallZeroFilteredDates[k] = LocalDate.ofEpochDay(h); smallZeroFilteredData[k++] = smallValue; } else { data[i] = 0; largeZeroRejectedDates[j] = LocalDate.ofEpochDay(h); largeZeroRejectedData[j++] = 0; smallZeroRejectedDates[l] = LocalDate.ofEpochDay(h); smallZeroRejectedData[l++] = 0; } } else { data[i] = d; smallZeroFilteredDates[k] = LocalDate.ofEpochDay(h); smallZeroFilteredData[k++] = d; largeZeroFilteredDates[m] = LocalDate.ofEpochDay(h); largeZeroFilteredData[m++] = d; } } smallZeroFilteredDates = trimToCapacity(smallZeroFilteredDates, k); smallZeroFilteredData = Arrays.trimToCapacity(smallZeroFilteredData, k); smallZeroRejectedDates = trimToCapacity(smallZeroRejectedDates, l); smallZeroRejectedData = Arrays.trimToCapacity(smallZeroRejectedData, l); largeZeroFilteredDates = trimToCapacity(largeZeroFilteredDates, m); largeZeroFilteredData = Arrays.trimToCapacity(largeZeroFilteredData, m); largeZeroRejectedDates = trimToCapacity(largeZeroRejectedDates, j); largeZeroRejectedData = Arrays.trimToCapacity(largeZeroRejectedData, j); final LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of(dates, data); FilteredTimeSeries result = SMALL_ZERO_FILTER.evaluate(ts); assertEquals(result, new FilteredTimeSeries(ImmutableLocalDateDoubleTimeSeries.of(smallZeroFilteredDates, smallZeroFilteredData), ImmutableLocalDateDoubleTimeSeries.of(smallZeroRejectedDates, smallZeroRejectedData))); result = LARGE_ZERO_FILTER.evaluate(ts); assertEquals(result, new FilteredTimeSeries(ImmutableLocalDateDoubleTimeSeries.of(largeZeroFilteredDates, largeZeroFilteredData), ImmutableLocalDateDoubleTimeSeries.of(largeZeroRejectedDates, largeZeroRejectedData))); }