@Test @Category(UnitTest.class) public void testDouble() { double[] values = {0.21, 0.32, 0.32, 0.54}; double nodata = Double.NaN; double result; Aggregator agg = new ModeAggregator(); result = agg.aggregate(values, nodata); assertEquals(0.32, result, epsilon); values[0] = nodata; result = agg.aggregate(values, nodata); assertEquals(0.32, result, epsilon); values[1] = nodata; result = agg.aggregate(values, nodata); assertThat((double) result, AnyOf.anyOf(IsCloseTo.closeTo(0.32f, epsilon), IsCloseTo.closeTo(0.54f, epsilon))); values[2] = nodata; result = agg.aggregate(values, nodata); assertEquals(0.54, result, epsilon); values[3] = nodata; result = agg.aggregate(values, nodata); assertEquals(nodata, result, epsilon); }
@Override public double aggregate(double[] values, double nodata) { HashMap<Double, Integer> freqs = new HashMap<>(); for (double val : values) { if (FloatUtils.isNotNodata(val, nodata)) { Integer freq = freqs.get(val); freqs.put(val, (freq == null ? 1 : freq + 1)); } } double mode = nodata; int maxFreq = 0; for (Entry<Double, Integer> entry : freqs.entrySet()) { int freq = entry.getValue(); if (freq > maxFreq) { maxFreq = freq; mode = entry.getKey(); } } return mode; }
ModeAggregator implements Aggregator { @Override public double aggregate(double[] values, double nodata) { HashMap<Double, Integer> freqs = new HashMap<>(); for (double val : values) { if (FloatUtils.isNotNodata(val, nodata)) { Integer freq = freqs.get(val); freqs.put(val, (freq == null ? 1 : freq + 1)); } } double mode = nodata; int maxFreq = 0; for (Entry<Double, Integer> entry : freqs.entrySet()) { int freq = entry.getValue(); if (freq > maxFreq) { maxFreq = freq; mode = entry.getKey(); } } return mode; } }
ModeAggregator implements Aggregator { @Override public double aggregate(double[] values, double nodata) { HashMap<Double, Integer> freqs = new HashMap<>(); for (double val : values) { if (FloatUtils.isNotNodata(val, nodata)) { Integer freq = freqs.get(val); freqs.put(val, (freq == null ? 1 : freq + 1)); } } double mode = nodata; int maxFreq = 0; for (Entry<Double, Integer> entry : freqs.entrySet()) { int freq = entry.getValue(); if (freq > maxFreq) { maxFreq = freq; mode = entry.getKey(); } } return mode; }  }
ModeAggregator implements Aggregator { @Override public double aggregate(double[] values, double nodata) { HashMap<Double, Integer> freqs = new HashMap<>(); for (double val : values) { if (FloatUtils.isNotNodata(val, nodata)) { Integer freq = freqs.get(val); freqs.put(val, (freq == null ? 1 : freq + 1)); } } double mode = nodata; int maxFreq = 0; for (Entry<Double, Integer> entry : freqs.entrySet()) { int freq = entry.getValue(); if (freq > maxFreq) { maxFreq = freq; mode = entry.getKey(); } } return mode; }  @Override double aggregate(double[] values, double nodata); @Override float aggregate(float[] values, float nodata); @Override int aggregate(int[] values, int nodata); @Override short aggregate(short[] values, short nodata); @Override byte aggregate(byte[] values, byte nodata); @Override double aggregate(double[][] values, double weightx, double weighty, double nodata); @Override float aggregate(float[][] values, double weightx, double weighty, float nodata); @Override byte aggregate(byte[][] values, double weightx, double weighty, byte nodata); @Override short aggregate(short[][] values, double weightx, double weighty, short nodata); @Override int aggregate(int[][] values, double weightx, double weighty, int nodata); }
ModeAggregator implements Aggregator { @Override public double aggregate(double[] values, double nodata) { HashMap<Double, Integer> freqs = new HashMap<>(); for (double val : values) { if (FloatUtils.isNotNodata(val, nodata)) { Integer freq = freqs.get(val); freqs.put(val, (freq == null ? 1 : freq + 1)); } } double mode = nodata; int maxFreq = 0; for (Entry<Double, Integer> entry : freqs.entrySet()) { int freq = entry.getValue(); if (freq > maxFreq) { maxFreq = freq; mode = entry.getKey(); } } return mode; }  @Override double aggregate(double[] values, double nodata); @Override float aggregate(float[] values, float nodata); @Override int aggregate(int[] values, int nodata); @Override short aggregate(short[] values, short nodata); @Override byte aggregate(byte[] values, byte nodata); @Override double aggregate(double[][] values, double weightx, double weighty, double nodata); @Override float aggregate(float[][] values, double weightx, double weighty, float nodata); @Override byte aggregate(byte[][] values, double weightx, double weighty, byte nodata); @Override short aggregate(short[][] values, double weightx, double weighty, short nodata); @Override int aggregate(int[][] values, double weightx, double weighty, int nodata);  }