Mercurial Hosting > traffic-intelligence
comparison python/indicators.py @ 282:abbd4bc13dac
modified indicator class (same interface)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 21 Dec 2012 17:50:10 -0500 |
parents | a9988971aac8 |
children | 5957aa1d69e1 8bafd054cda4 |
comparison
equal
deleted
inserted
replaced
281:4f012e3d881b | 282:abbd4bc13dac |
---|---|
17 it should have more information like name, unit''' | 17 it should have more information like name, unit''' |
18 | 18 |
19 def __init__(self, name, values, timeInterval=None, maxValue = None): | 19 def __init__(self, name, values, timeInterval=None, maxValue = None): |
20 self.name = name | 20 self.name = name |
21 self.isCosine = name.find('Cosine') | 21 self.isCosine = name.find('Cosine') |
22 self.values = values | |
23 self.timeInterval = timeInterval | |
24 if timeInterval: | 22 if timeInterval: |
25 assert len(values) == timeInterval.length() | 23 assert len(values) == timeInterval.length() |
24 self.timeInterval = timeInterval | |
25 self.values = {} | |
26 for i in xrange(int(round(self.timeInterval.length()))): | |
27 self.values[self.timeInterval[i]] = values[i] | |
28 else: | |
29 self.values = values | |
30 instants = sorted(self.values.keys()) | |
31 if instants: | |
32 self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) | |
33 else: | |
34 self.timeInterval = moving.TimeInterval() | |
26 self.maxValue = maxValue | 35 self.maxValue = maxValue |
27 | 36 |
28 def empty(self): | 37 def empty(self): |
29 return len(self.values) == 0 | 38 return len(self.values) == 0 |
30 | 39 |
31 def __getitem__(self, i): | 40 def __getitem__(self, i): |
32 if self.timeInterval: | 41 if i in self.values.keys(): |
33 if self.timeInterval.contains(i): | 42 return self.values[i] |
34 return self.values[i-self.timeInterval.first] | |
35 else: | 43 else: |
36 if i in self.values.keys(): | 44 return None |
37 return self.values[i] | |
38 return None # default | |
39 | 45 |
40 def __iter__(self): | 46 def __iter__(self): |
41 self.iterInstantNum = 0 # index in the interval or keys of the dict | 47 self.iterInstantNum = 0 # index in the interval or keys of the dict |
42 return self | 48 return self |
43 | 49 |
45 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ | 51 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ |
46 # or (self.iterInstantNum >= self.values) | 52 # or (self.iterInstantNum >= self.values) |
47 raise StopIteration | 53 raise StopIteration |
48 else: | 54 else: |
49 self.iterInstantNum += 1 | 55 self.iterInstantNum += 1 |
50 if self.timeInterval: | 56 return self.values[self.values.keys()[self.iterInstantNum-1]] |
51 return self.values[self.iterInstantNum-1] | |
52 else: | |
53 return self.values.values()[self.iterInstantNum-1] | |
54 | 57 |
55 def getTimeInterval(self): | 58 def getTimeInterval(self): |
56 if not self.timeInterval and type(self.values)==dict: | |
57 instants = self.values.keys() | |
58 if instants: | |
59 self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) | |
60 else: | |
61 self.timeInterval = moving.TimeInterval() | |
62 return self.timeInterval | 59 return self.timeInterval |
63 | 60 |
64 def getValues(self): | 61 def getValues(self): |
65 if self.timeInterval: | 62 return self.values.values() |
66 return self.values | |
67 else: | |
68 return self.values.values() | |
69 | 63 |
70 def getAngleValues(self): | 64 def getAngleValues(self): |
71 '''if the indicator is a function of an angle, | 65 '''if the indicator is a function of an angle, |
72 transform it to an angle (eg cos) | 66 transform it to an angle (eg cos) |
73 (no transformation otherwise)''' | 67 (no transformation otherwise)''' |
82 from matplotlib.pylab import plot,ylim | 76 from matplotlib.pylab import plot,ylim |
83 if self.getTimeInterval().length() == 1: | 77 if self.getTimeInterval().length() == 1: |
84 marker = 'o' | 78 marker = 'o' |
85 else: | 79 else: |
86 marker = '' | 80 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([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) |
89 plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) | |
90 else: | |
91 plot([x/xfactor for x in list(self.getTimeInterval())], self.values, options+marker, **kwargs) | |
92 if self.maxValue: | 83 if self.maxValue: |
93 ylim(ymax = self.maxValue) | 84 ylim(ymax = self.maxValue) |
94 | 85 |
95 class SeverityIndicator(TemporalIndicator): | 86 class SeverityIndicator(TemporalIndicator): |
96 '''Class for severity indicators | 87 '''Class for severity indicators |
97 field mostSevereIsMax is True | 88 field mostSevereIsMax is True |
98 if the most severe value taken by the indicator is the maximum''' | 89 if the most severe value taken by the indicator is the maximum''' |
99 | 90 |