changeset 1042:b1ba6d44fcb9

corrected bug in severity indicators
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 04 Jul 2018 16:21:09 -0400
parents fc7c0f38e8a6
children b735895c8815 1748c02f9ac3
files trafficintelligence/indicators.py trafficintelligence/tests/indicators.txt
diffstat 2 files changed, 28 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/indicators.py	Wed Jul 04 16:06:23 2018 -0400
+++ b/trafficintelligence/indicators.py	Wed Jul 04 16:21:09 2018 -0400
@@ -159,25 +159,24 @@
         TemporalIndicator.__init__(self, name, values, timeInterval, maxValue)
         self.mostSevereIsMax = mostSevereIsMax
 
-    def getMostSevereValue(self, minNInstants=1, centile=15.):
+    def getMostSevereValue(self, minNInstants=None, centile=None):
         '''if there are more than minNInstants observations, 
         returns either the average of these maximum values 
         or if centile is not None the n% centile from the most severe value
 
-        eg for TTC, 15 returns the 15th centile (value such that 15% of observations are lower)'''
-        if self.__len__() < minNInstants:
-            return None
+        eg for TTC, centile = 15 returns the 15th centile (value such that 15% of observations are lower)'''
+        values = list(self.values.values())
+        if centile is not None:
+            if self.mostSevereIsMax:
+                c = 100-centile
+            else:
+                c = centile
+            return percentile(values, c)
+        elif minNInstants is not None and minNInstants <= self.__len__():
+            values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
+            return mean(values[:minNInstants])
         else:
-            values = list(self.values.values())
-            if centile is not None:
-                if self.mostSevereIsMax:
-                    c = 100-centile
-                else:
-                    c = centile
-                return percentile(values, c)
-            else:
-                values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
-                return mean(values[:minNInstants])
+            return None
 
     def getInstantOfMostSevereValue(self):
         '''Returns the instant at which the indicator reaches its most severe value'''
--- a/trafficintelligence/tests/indicators.txt	Wed Jul 04 16:06:23 2018 -0400
+++ b/trafficintelligence/tests/indicators.txt	Wed Jul 04 16:21:09 2018 -0400
@@ -19,6 +19,21 @@
 >>> indic1[2]
 0
 
+>>> ttc = SeverityIndicator('TTC', list(range(11)), TimeInterval(1,11), mostSevereIsMax = False)
+>>> ttc.getMostSevereValue(1)
+0.0
+>>> ttc.getMostSevereValue(2)
+0.5
+>>> ttc.getMostSevereValue(centile = 10.)
+1.0
+>>> ttc.mostSevereIsMax = True
+>>> ttc.getMostSevereValue(1)
+10.0
+>>> ttc.getMostSevereValue(2)
+9.5
+>>> ttc.getMostSevereValue(centile = 10.)
+9.0
+
 >>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]])
 >>> m = indicatorMap([1,2,3], t1, 1)
 >>> m[(1.0, 3.0)]