Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 58:40e1508380ed
developed indicator classes
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 29 Oct 2010 01:54:11 -0400 |
parents | 61fe73df2d36 |
children | 290fceb125d2 |
comparison
equal
deleted
inserted
replaced
57:b8b3768f8d54 | 58:40e1508380ed |
---|---|
323 # need for a class representing the indicators, their units, how to print them in graphs... | 323 # need for a class representing the indicators, their units, how to print them in graphs... |
324 class TemporalIndicator: | 324 class TemporalIndicator: |
325 '''Class for temporal indicators | 325 '''Class for temporal indicators |
326 i.e. indicators that take a value at specific instants | 326 i.e. indicators that take a value at specific instants |
327 | 327 |
328 values should be | |
329 * a dict, for the values at specific time instants | |
330 * or a list with a time interval object if continuous measurements | |
331 | |
328 it should have more information like name, unit''' | 332 it should have more information like name, unit''' |
329 | 333 |
330 def __init__(self, name, values = {}): | 334 def __init__(self, name, values, timeInterval=None): |
331 self.name = name | 335 self.name = name |
332 self.values = values | 336 self.values = values |
337 self.timeInterval = timeInterval | |
338 | |
339 class SeverityIndicator(TemporalIndicator): | |
340 '''Class for severity indicators | |
341 field mostSevereIsMax is True | |
342 if the most severe value taken by the indicator is the maximum''' | |
343 | |
344 def __init__(self, name, values, mostSevereIsMax=True, ignoredValue = None): | |
345 # , timeInterval=None # implement later | |
346 TemporalIndicator.__init__(self, name, values, timeInterval=None) | |
347 self.mostSevereIsMax = mostSevereIsMax | |
348 self.ignoredValue = ignoredValue | |
349 | |
350 def getMostSevereValue(self, minNInstants=1): | |
351 from matplotlib.mlab import find | |
352 from numpy.core.multiarray import array | |
353 from numpy.core.fromnumeric import mean | |
354 values = array(self.values.values()) | |
355 if self.ignoredValue: | |
356 indices = find(values != self.ignoredValue) | |
357 else: | |
358 indices = range(len(values)) | |
359 if len(indices) >= minNInstants: | |
360 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values | |
361 return mean(values[:minNInstants]) | |
362 else: | |
363 return None | |
333 | 364 |
334 if __name__ == "__main__": | 365 if __name__ == "__main__": |
335 import doctest | 366 import doctest |
336 import unittest | 367 import unittest |
337 suite = doctest.DocFileSuite('tests/moving.txt') | 368 suite = doctest.DocFileSuite('tests/moving.txt') |