[PATCH 6 of 7] '.filtered' handling: NaN treatment improved

Wald Commits scm-commit at wald.intevation.org
Wed Oct 9 15:45:08 CEST 2019


# HG changeset patch
# User dnt_bjoernsen <d.tironi at bjoernsen.de>
# Date 1568295550 -7200
#      Thu Sep 12 15:39:10 2019 +0200
# Node ID 96c41d4f4aba165f940df00d05c2bd1b7fcb1ff7
# Parent  68acd2f44609a647e8895aa1a58f4da04e63dcf5
'.filtered' handling: NaN treatment improved

diff -r 68acd2f44609 -r 96c41d4f4aba artifacts/src/main/java/org/dive4elements/river/artifacts/math/MovingAverage.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/math/MovingAverage.java	Thu Sep 12 15:36:01 2019 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/math/MovingAverage.java	Thu Sep 12 15:39:10 2019 +0200
@@ -12,21 +12,18 @@
 import java.util.SortedMap;
 import java.util.TreeMap;
 
-
-public class MovingAverage
-{
+public class MovingAverage {
 
-    public static double[][] simple(double[][] values, double radius) {
-        TreeMap<Double, Double> map = toMap(values);
-        int N = map.size();
-        double [] xs = new double[N];
-        double [] ys = new double[N];
+    public static double[][] simple(final double[][] values, final double radius) {
+        final TreeMap<Double, Double> map = toMap(values);
+        final int N = map.size();
+        final double[] xs = new double[N];
+        final double[] ys = new double[N];
         int ndx = 0;
-        for (double x: map.keySet()) {
-            SortedMap<Double, Double> range =
-                map.subMap(x-radius, true, x+radius, true);
+        for (final double x : map.keySet()) {
+            final SortedMap<Double, Double> range = map.subMap(x - radius, true, x + radius, true);
             double avg = 0d;
-            for (double v: range.values()) {
+            for (final double v : range.values()) {
                 avg += v;
             }
             avg /= range.size();
@@ -34,43 +31,43 @@
             ys[ndx] = avg;
             ndx++;
         }
-        return new double [][] { xs, ys };
+        return new double[][] { xs, ys };
     }
 
     /** Build moving average over values. Weight them. */
-    public static double[][] weighted(
-        double[][] values,
-        double radius
-    ) {
-        TreeMap<Double, Double> map = toMap(values);
-        int N = map.size();
-        double [] xs = new double[N];
-        double [] ys = new double[N];
+    public static double[][] weighted(final double[][] values, final double radius) {
+        final TreeMap<Double, Double> map = toMap(values);
+        final int N = map.size();
+        final double[] xs = new double[N];
+        final double[] ys = new double[N];
         int ndx = 0;
-        double _1radius = 1d/radius;
-        for (double x: map.keySet()) {
+        final double _1radius = 1d / radius;
+        for (final double x : map.keySet()) {
             double avg = 0d;
             double weights = 0d;
-            for (Map.Entry<Double, Double> e:
-                map.subMap(x-radius, false, x+radius, false).entrySet()
-            ) {
-                double weight = 1d - Math.abs(x - e.getKey())*_1radius;
-                avg += weight*e.getValue();
-                weights += weight;
+            for (final Map.Entry<Double, Double> e : map.subMap(x - radius, false, x + radius, false).entrySet()) {
+                final Double value = e.getValue();
+
+                if (!value.isNaN()) {
+                    final double weight = 1d - Math.abs(x - e.getKey()) * _1radius;
+                    weights += weight;
+                    avg += weight * value;
+                }
             }
+
             avg /= weights;
             xs[ndx] = x;
-            ys[ndx] = avg;
+            ys[ndx] = Double.isNaN(map.get(x)) ? Double.NaN : avg;
             ndx++;
         }
-        return new double [][] { xs, ys };
+        return new double[][] { xs, ys };
     }
 
     /** From [x1,x2][y1,y2] makes {x1:y1,x2:y2}. Sorted by x! */
-    private static TreeMap<Double, Double> toMap(double[][] values) {
-        TreeMap<Double, Double> map = new TreeMap<Double, Double>();
-        double [] xs = values[0];
-        double [] ys = values[1];
+    private static TreeMap<Double, Double> toMap(final double[][] values) {
+        final TreeMap<Double, Double> map = new TreeMap<>();
+        final double[] xs = values[0];
+        final double[] ys = values[1];
         for (int i = 0; i < xs.length; i++) {
             map.put(xs[i], ys[i]);
         }


More information about the Dive4Elements-commits mailing list