use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class QuantityIoProcessChain method getData.
@Override
public DataCollection<Data<QuantityValue>> getData() {
boolean generalize = parameters.isGeneralize();
DataService<Data<QuantityValue>> service = generalize ? new GeneralizingQuantityService(dataService) : dataService;
return service.getData(parameters);
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class DouglasPeuckerGeneralizer method recursiveGeneralize.
private QuantityValue[] recursiveGeneralize(Data<QuantityValue> timeseries) {
QuantityValue[] values = getValueArray(timeseries);
QuantityValue startValue = getFirstValue(timeseries);
QuantityValue endValue = getLastValue(timeseries);
Line2D.Double line = createTendencyLine(startValue, endValue);
// find the point of maximum distance to the line
int index = 0;
double maxDist = 0;
double distance;
// start and end value are not mentioned
for (int i = 1; i < (values.length - 1); i++) {
QuantityValue timeseriesValue = values[i];
distance = calculateDistance(line, timeseriesValue);
if (distance > maxDist) {
index = i;
maxDist = distance;
}
}
if (maxDist < toleranceValue) {
return getValueArray(timeseries);
} else {
// split and handle both parts separately
Data<QuantityValue> generalizedData = new Data<>();
Data<QuantityValue> firstPartToBeGeneralized = new Data<>();
Data<QuantityValue> restPartToBeGeneralized = new Data<>();
firstPartToBeGeneralized.addValues(Arrays.copyOfRange(values, 0, index));
restPartToBeGeneralized.addValues(Arrays.copyOfRange(values, index + 1, values.length));
generalizedData.addValues(recursiveGeneralize(firstPartToBeGeneralized));
generalizedData.addValues(recursiveGeneralize(restPartToBeGeneralized));
return getValueArray(generalizedData);
}
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class LargestTriangleThreeBucketsGeneralizerTest method getData.
private Data<QuantityValue> getData(int maxValues) {
BigDecimal startValue = BigDecimal.valueOf(0);
QuantityValue current = createQuantityValue(DateTime.now(), startValue);
Data<QuantityValue> data = new Data<>();
for (int i = 0; i < maxValues; i++) {
data.addNewValue(current);
current = getNextDataValue(current);
}
return data;
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class LargestTriangleThreeBucketsGeneralizerTest method when_quotientHasNonterminatingDecimals_then_noArithmeticExceptionIsThrown.
@Test
public void when_quotientHasNonterminatingDecimals_then_noArithmeticExceptionIsThrown() throws GeneralizerException {
// https://github.com/52North/series-rest-api/issues/446
TvpDataCollection<Data<QuantityValue>> collection = new TvpDataCollection<>();
collection.addNewSeries("test", getData(10000));
long threshold = 100L;
IoParameters defaults = IoParameters.createDefaults().extendWith("threshold", Long.toString(threshold));
Generalizer<Data<QuantityValue>> generalizer = new LargestTriangleThreeBucketsGeneralizer(defaults);
DataCollection<Data<QuantityValue>> generalizedData = generalizer.generalize(collection);
assertThat(generalizedData.getSeries("test").size(), Is.is(threshold));
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class LargestTriangleThreeBucketsGeneralizer method calculateBucketAverage.
private BucketAverage calculateBucketAverage(int bucketIndex, double bucketSize, QuantityValue[] data) {
int dataLength = data.length;
int avgRangeStart = (int) Math.floor((bucketIndex + 0) * bucketSize) + 1;
int avgRangeEnd = (int) Math.floor((bucketIndex + 1) * bucketSize) + 1;
avgRangeEnd = avgRangeEnd < dataLength ? avgRangeEnd : dataLength;
double avgRangeLength = avgRangeEnd - avgRangeStart;
double avgTimestamp = 0d;
BigDecimal avgValue = BigDecimal.ZERO;
int amountOfNodataValues = 0;
boolean noDataThresholdExceeded = false;
boolean unixTime = false;
for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
final QuantityValue current = data[avgRangeStart];
avgTimestamp += current.getTimestamp().getMillis();
unixTime = current.getTimestamp().isUnixTime();
if (noDataThresholdExceeded) {
// keep on calc avg timestamp
continue;
}
if (current.isNoDataValue()) {
amountOfNodataValues++;
if (amountOfNodataValues == noDataGapThreshold) {
noDataThresholdExceeded = true;
}
} else {
avgValue = avgValue.add(current.getValue());
}
}
avgTimestamp /= avgRangeLength;
avgValue = avgValue.divide(BigDecimal.valueOf(avgRangeLength), MathContext.DECIMAL128);
return new BucketAverage(avgTimestamp, avgValue, unixTime);
}
Aggregations