AggregatorPercentile extends AbstractAggregator { public AggregatorPercentile(VariableContext varCtx, String varName, String targetName, int percentage) { super(Descriptor.NAME, createFeatureNames(varName, "sum"), createFeatureNames(varName, "p" + percentage), createFeatureNames(targetName, "p" + percentage)); if (varCtx == null) { throw new NullPointerException("varCtx"); } if (varName == null) { throw new NullPointerException("varName"); } if (percentage < 0 || percentage > 100) { throw new IllegalArgumentException("percentage < 0 || percentage > 100"); } this.varIndex = varCtx.getVariableIndex(varName); this.percentage = percentage; this.mlName = "ml." + varName; this.icName = "ic." + varName; } AggregatorPercentile(VariableContext varCtx, String varName, String targetName, int percentage); @Override void initSpatial(BinContext ctx, WritableVector vector); @Override void aggregateSpatial(BinContext ctx, Observation observationVector, WritableVector spatialVector); @Override void completeSpatial(BinContext ctx, int numSpatialObs, WritableVector spatialVector); @Override void initTemporal(BinContext ctx, WritableVector vector); @Override void aggregateTemporal(BinContext ctx, Vector spatialVector, int numSpatialObs, WritableVector temporalVector); @Override void completeTemporal(BinContext ctx, int numTemporalObs, WritableVector temporalVector); @Override void computeOutput(Vector temporalVector, WritableVector outputVector); @Override String toString(); static float computePercentile(int p, float[] measurements);  }
@Test public void testAggregatorPercentile() { AggregatorPercentile agg = new AggregatorPercentile(new MyVariableContext("c"), "c", "c", 70); VectorImpl svec = vec(NaN); VectorImpl tvec = vec(NaN); VectorImpl out = vec(NaN); agg.initSpatial(ctx, svec); assertEquals(0.0f, svec.get(0), 0.0f); agg.aggregateSpatial(ctx, obsNT(1.5f), svec); agg.aggregateSpatial(ctx, obsNT(2.5f), svec); agg.aggregateSpatial(ctx, obsNT(0.5f), svec); float sumX = 1.5f + 2.5f + 0.5f; assertEquals(sumX, svec.get(0), 1e-5f); int numObs = 3; agg.completeSpatial(ctx, numObs, svec); assertEquals(sumX / numObs, svec.get(0), 1e-5f); agg.initTemporal(ctx, tvec); assertEquals(0.0f, tvec.get(0), 0.0f); agg.aggregateTemporal(ctx, vec(0.1f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.2f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.3f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.4f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.5f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.6f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.7f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.8f), 1, tvec); agg.aggregateTemporal(ctx, vec(0.9f), 1, tvec); agg.aggregateTemporal(ctx, vec(1.0f), 1, tvec); assertEquals(0.0f, tvec.get(0), 1e-5f); agg.completeTemporal(ctx, 10, tvec); assertEquals(0.77f, tvec.get(0), 1e-5f); agg.computeOutput(tvec, out); assertEquals(0.77f, out.get(0), 1e-5f); }