Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/indicators.py @ 1042:b1ba6d44fcb9
corrected bug in severity indicators
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 04 Jul 2018 16:21:09 -0400 |
parents | c6cf75a2ed08 |
children | 956a66096e91 |
comparison
equal
deleted
inserted
replaced
1041:fc7c0f38e8a6 | 1042:b1ba6d44fcb9 |
---|---|
157 | 157 |
158 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None): | 158 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None): |
159 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) | 159 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) |
160 self.mostSevereIsMax = mostSevereIsMax | 160 self.mostSevereIsMax = mostSevereIsMax |
161 | 161 |
162 def getMostSevereValue(self, minNInstants=1, centile=15.): | 162 def getMostSevereValue(self, minNInstants=None, centile=None): |
163 '''if there are more than minNInstants observations, | 163 '''if there are more than minNInstants observations, |
164 returns either the average of these maximum values | 164 returns either the average of these maximum values |
165 or if centile is not None the n% centile from the most severe value | 165 or if centile is not None the n% centile from the most severe value |
166 | 166 |
167 eg for TTC, 15 returns the 15th centile (value such that 15% of observations are lower)''' | 167 eg for TTC, centile = 15 returns the 15th centile (value such that 15% of observations are lower)''' |
168 if self.__len__() < minNInstants: | 168 values = list(self.values.values()) |
169 if centile is not None: | |
170 if self.mostSevereIsMax: | |
171 c = 100-centile | |
172 else: | |
173 c = centile | |
174 return percentile(values, c) | |
175 elif minNInstants is not None and minNInstants <= self.__len__(): | |
176 values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values | |
177 return mean(values[:minNInstants]) | |
178 else: | |
169 return None | 179 return None |
170 else: | |
171 values = list(self.values.values()) | |
172 if centile is not None: | |
173 if self.mostSevereIsMax: | |
174 c = 100-centile | |
175 else: | |
176 c = centile | |
177 return percentile(values, c) | |
178 else: | |
179 values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values | |
180 return mean(values[:minNInstants]) | |
181 | 180 |
182 def getInstantOfMostSevereValue(self): | 181 def getInstantOfMostSevereValue(self): |
183 '''Returns the instant at which the indicator reaches its most severe value''' | 182 '''Returns the instant at which the indicator reaches its most severe value''' |
184 if self.mostSevereIsMax: | 183 if self.mostSevereIsMax: |
185 return max(self.values, key=self.values.get) | 184 return max(self.values, key=self.values.get) |