]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/commitdiff
Add compute statistics function
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Tue, 8 Oct 2019 07:31:50 +0000 (09:31 +0200)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Tue, 8 Oct 2019 08:18:00 +0000 (10:18 +0200)
scripts/print_json_objects_scenaris_statistics.py

index 27c5ffbca93dbaf8fc2e1b0534ad5a9efffe2981..4eb9c007a5505c62f17409aceeef7e38631ea6d6 100644 (file)
@@ -54,6 +54,34 @@ def get_scenarios(dname):
             })
     return s
 
+def compute_stats(sl={}, what="time"):
+    """Return ``mean``, ``std``, ``max``, and ``min`` of ``what``.
+
+    Keyword arguments:
+    sl -- Scenarios list.
+    what -- The variable in scenario stats to compute with.
+    """
+    assert len(sl) > 0
+    wl = [s["sc"][what] for s in sl if "sc" in s and what in s["sc"]]
+    if len(wl) == 0:
+        wl_mean = inf
+        wl_std = inf
+        wl_max = inf
+        wl_min = -inf
+    else:
+        wl_mean = sum(wl) / len(wl)
+        wl_tmp = [(x - wl_mean)**2 for x in wl]
+        wl_std = (sum(wl_tmp) / len(wl_tmp))**0.5
+        wl_max = max(wl)
+        wl_min = min(wl)
+    return {
+        "err": 100 * (len(sl) - len(wl)) / len(sl),
+        "mean": wl_mean,
+        "std": wl_std,
+        "max": wl_max,
+        "min": wl_min,
+    }
+
 if __name__ == "__main__":
     if (len(argv) == 2):
         SCEN_DIR = argv[1]