Merge: metrics: not a number fix
authorJean Privat <jean@pryen.org>
Fri, 16 Oct 2015 12:19:49 +0000 (08:19 -0400)
committerJean Privat <jean@pryen.org>
Fri, 16 Oct 2015 12:19:49 +0000 (08:19 -0400)
Skip `nan` values in metrics so the sum and std_div still have sense.

Also change `Metric::collect` to accept any kind of `Collection`.

Pull-Request: #1755
Reviewed-by: Jean Privat <jean@pryen.org>

src/metrics/metrics_base.nit

index 605f4d3..342d5c7 100644 (file)
@@ -166,7 +166,7 @@ interface Metric
        fun values: RES is abstract
 
        # Collect metric values on elements
-       fun collect(elements: Set[ELM]) is abstract
+       fun collect(elements: Collection[ELM]) is abstract
 
        # The value calculated for the element
        fun [](element: ELM): VAL do return values[element]
@@ -299,7 +299,10 @@ class FloatMetric
 
        redef fun sum do
                var sum = 0.0
-               for v in values.values do sum += v
+               for v in values.values do
+                       if v.is_nan then continue
+                       sum += v
+               end
                return sum
        end
 
@@ -337,6 +340,7 @@ class FloatMetric
        redef fun std_dev do
                var sum = 0.0
                for value in values.values do
+                       if value.is_nan then continue
                        sum += (value - avg).pow(2.to_f)
                end
                return (sum / values.length.to_f).sqrt