EllipsoidFactorErrorTracking extends AbstractUnaryFunctionOp<Img<FloatType>, Map<String, Double>> { @Override public Map<String,Double> calculate(Img<FloatType> currentEllipsoidFactorImage) { final IterableInterval<FloatType> previousAverage; if(currentIteration==0) { previousAverage = ArrayImgs.floats(currentEllipsoidFactorImage.dimension(0),currentEllipsoidFactorImage.dimension(1),currentEllipsoidFactorImage.dimension(2)); currentAverage = currentEllipsoidFactorImage; } else { previousAverage = currentAverage; currentAverage = ops().math().multiply(previousAverage, new FloatType(currentIteration)); currentAverage = ops().math().add(currentEllipsoidFactorImage, currentAverage); currentAverage = ops().math().divide(currentAverage, new FloatType(currentIteration+1)); } currentIteration++; final IterableInterval<FloatType> error = ops().math().subtract(previousAverage, currentAverage); final Cursor<FloatType> cursor = error.cursor(); while (cursor.hasNext()) { final float next = cursor.next().get(); if (next < 0) { cursor.get().setReal(-next); } } cursor.reset(); List<Double> nonNanValues = new ArrayList<>(); while (cursor.hasNext()) { final double next = cursor.next().getRealDouble(); if (!Double.isNaN(next)) { nonNanValues.add(next); } } nonNanValues.sort(Double::compare); int count = nonNanValues.size(); double max = nonNanValues.get(count-1); double min = nonNanValues.get(0); double median = nonNanValues.get(count/2); double mean = nonNanValues.stream().reduce(Double::sum).get()/((double) count); final Map<String, Double> stats = new HashMap<>(); stats.put("Mean",mean); stats.put("Max",max); stats.put("Min",min); stats.put("Median",median); return stats; } EllipsoidFactorErrorTracking(OpService opService); @Override Map<String,Double> calculate(Img<FloatType> currentEllipsoidFactorImage);  }
@Test public void testMedian(){ final Img<FloatType> toyImg = getToyImg(); final EllipsoidFactorErrorTracking errorTracking = new EllipsoidFactorErrorTracking(ops); final Map<String,Double> errorStats = errorTracking.calculate(toyImg); Double median = errorStats.get("Median"); assertEquals("Median wrong", 3.0, median, 1e-12); }
@Test public void testIteration(){ final Img<FloatType> toyImg = getToyImg(); final EllipsoidFactorErrorTracking errorTracking = new EllipsoidFactorErrorTracking(ops); errorTracking.calculate(toyImg); Map<String,Double> errorStats = errorTracking.calculate((Img<FloatType>) toyImg); Double median = errorStats.get("Median"); assertEquals("Iteration get median wrong", 0.0, median, 1e-12); }
@Test public void testMean(){ final Img<FloatType> toyImg = getToyImg(); final EllipsoidFactorErrorTracking errorTracking = new EllipsoidFactorErrorTracking(ops); final Map<String,Double> errorStats = errorTracking.calculate(toyImg); Double mean = errorStats.get("Mean"); assertEquals("Mean wrong", 2.5, mean, 1e-12); }
@Test public void testMax(){ final Img<FloatType> toyImg = getToyImg(); final EllipsoidFactorErrorTracking errorTracking = new EllipsoidFactorErrorTracking(ops); Map<String,Double> errorStats = errorTracking.calculate(toyImg); Double max = errorStats.get("Max"); assertEquals("Max wrong", 3.5, max, 1e-12); }
@Test public void testMin(){ final Img<FloatType> toyImg = getToyImg(); final EllipsoidFactorErrorTracking errorTracking = new EllipsoidFactorErrorTracking(ops); Map<String,Double> errorStats = errorTracking.calculate(toyImg); Double min = errorStats.get("Min"); assertEquals("Min wrong", 1.0, min, 1e-12); }