Mercurial > hg > nsaunier > traffic-intelligence
comparison python/indicators.py @ 269:a9988971aac8
removed legacy code + tweaks
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Sun, 29 Jul 2012 04:09:43 -0400 |
| parents | 7a3bf04cf016 |
| children | abbd4bc13dac |
comparison
equal
deleted
inserted
replaced
| 268:0c0b92f621f6 | 269:a9988971aac8 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Class for indicators, temporal indicators, and safety indicators''' | 2 '''Class for indicators, temporal indicators, and safety indicators''' |
| 3 | 3 |
| 4 __metaclass__ = type | 4 __metaclass__ = type |
| 5 | |
| 6 import moving | |
| 5 | 7 |
| 6 # need for a class representing the indicators, their units, how to print them in graphs... | 8 # need for a class representing the indicators, their units, how to print them in graphs... |
| 7 class TemporalIndicator: | 9 class TemporalIndicator: |
| 8 '''Class for temporal indicators | 10 '''Class for temporal indicators |
| 9 i.e. indicators that take a value at specific instants | 11 i.e. indicators that take a value at specific instants |
| 12 * a dict, for the values at specific time instants | 14 * a dict, for the values at specific time instants |
| 13 * or a list with a time interval object if continuous measurements | 15 * or a list with a time interval object if continuous measurements |
| 14 | 16 |
| 15 it should have more information like name, unit''' | 17 it should have more information like name, unit''' |
| 16 | 18 |
| 17 def __init__(self, name, values, timeInterval=None): | 19 def __init__(self, name, values, timeInterval=None, maxValue = None): |
| 18 self.name = name | 20 self.name = name |
| 19 self.isCosine = name.find('Cosine') | 21 self.isCosine = name.find('Cosine') |
| 20 self.values = values | 22 self.values = values |
| 21 self.timeInterval = timeInterval | 23 self.timeInterval = timeInterval |
| 22 if timeInterval: | 24 if timeInterval: |
| 23 assert len(values) == timeInterval.length() | 25 assert len(values) == timeInterval.length() |
| 26 self.maxValue = maxValue | |
| 24 | 27 |
| 25 def empty(self): | 28 def empty(self): |
| 26 return len(self.values) == 0 | 29 return len(self.values) == 0 |
| 27 | 30 |
| 28 def __getitem__(self, i): | 31 def __getitem__(self, i): |
| 51 | 54 |
| 52 def getTimeInterval(self): | 55 def getTimeInterval(self): |
| 53 if not self.timeInterval and type(self.values)==dict: | 56 if not self.timeInterval and type(self.values)==dict: |
| 54 instants = self.values.keys() | 57 instants = self.values.keys() |
| 55 if instants: | 58 if instants: |
| 56 self.timeInterval = TimeInterval(instants[0], instants[-1]) | 59 self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) |
| 57 else: | 60 else: |
| 58 self.timeInterval = TimeInterval() | 61 self.timeInterval = moving.TimeInterval() |
| 59 return self.timeInterval | 62 return self.timeInterval |
| 60 | 63 |
| 61 def getValues(self): | 64 def getValues(self): |
| 62 if self.timeInterval: | 65 if self.timeInterval: |
| 63 return self.values | 66 return self.values |
| 73 if self.isCosine >= 0: | 76 if self.isCosine >= 0: |
| 74 return [arccos(c) for c in values] | 77 return [arccos(c) for c in values] |
| 75 else: | 78 else: |
| 76 return values | 79 return values |
| 77 | 80 |
| 78 def plot(self, options = '', **kwargs): | 81 def plot(self, options = '', xfactor = 1., **kwargs): |
| 79 from matplotlib.pylab import plot | 82 from matplotlib.pylab import plot,ylim |
| 80 if not self.timeInterval and type(self.values)==dict: | 83 if self.getTimeInterval().length() == 1: |
| 84 marker = 'o' | |
| 85 else: | |
| 86 marker = '' | |
| 87 if not self.timeInterval or type(self.values)==dict: | |
| 81 time = sorted(self.values.keys()) | 88 time = sorted(self.values.keys()) |
| 82 plot(time, [self.values[i] for i in time], options, **kwargs) | 89 plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) |
| 83 else: | 90 else: |
| 84 plot(list(getTimeInterval()), self.values, options, **kwargs) | 91 plot([x/xfactor for x in list(self.getTimeInterval())], self.values, options+marker, **kwargs) |
| 85 | 92 if self.maxValue: |
| 93 ylim(ymax = self.maxValue) | |
| 94 | |
| 86 class SeverityIndicator(TemporalIndicator): | 95 class SeverityIndicator(TemporalIndicator): |
| 87 '''Class for severity indicators | 96 '''Class for severity indicators |
| 88 field mostSevereIsMax is True | 97 field mostSevereIsMax is True |
| 89 if the most severe value taken by the indicator is the maximum''' | 98 if the most severe value taken by the indicator is the maximum''' |
| 90 | 99 |
| 91 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, ignoredValue = None): | 100 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, ignoredValue = None, maxValue = None): |
| 92 TemporalIndicator.__init__(self, name, values, timeInterval) | 101 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) |
| 93 self.mostSevereIsMax = mostSevereIsMax | 102 self.mostSevereIsMax = mostSevereIsMax |
| 94 self.ignoredValue = ignoredValue | 103 self.ignoredValue = ignoredValue |
| 95 | 104 |
| 96 def getMostSevereValue(self, minNInstants=1): | 105 def getMostSevereValue(self, minNInstants=1): |
| 97 from matplotlib.mlab import find | 106 from matplotlib.mlab import find |
