Mercurial Hosting > traffic-intelligence
annotate python/indicators.py @ 343:74e437ab5f11
first version of indicator loading code
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 21 Jun 2013 15:28:59 -0400 |
parents | 2f39c4ed0b62 |
children | 2db4e76599a1 |
rev | line source |
---|---|
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 '''Class for indicators, temporal indicators, and safety indicators''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
4 __metaclass__ = type |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
5 |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
6 import moving |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
7 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 # need for a class representing the indicators, their units, how to print them in graphs... |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 class TemporalIndicator: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
10 '''Class for temporal indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
11 i.e. indicators that take a value at specific instants |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
12 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 values should be |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 * a dict, for the values at specific time instants |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 * or a list with a time interval object if continuous measurements |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 it should have more information like name, unit''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
19 def __init__(self, name, values, timeInterval=None, maxValue = None): |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
20 self.name = name |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
21 if timeInterval: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 assert len(values) == timeInterval.length() |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
23 self.timeInterval = timeInterval |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
24 self.values = {} |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
25 for i in xrange(int(round(self.timeInterval.length()))): |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
26 self.values[self.timeInterval[i]] = values[i] |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
27 else: |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
28 self.values = values |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
29 instants = sorted(self.values.keys()) |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
30 if instants: |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
31 self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
32 else: |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
33 self.timeInterval = moving.TimeInterval() |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
34 self.maxValue = maxValue |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
35 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
36 def __len__(self): |
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
37 return len(self.values) |
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
38 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
39 def empty(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
40 return len(self.values) == 0 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
41 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
42 def __getitem__(self, i): |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
43 'Returns ith value in time interval' |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
44 if i in self.values.keys(): |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
45 return self.values[i] |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
46 else: |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
47 return None |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
48 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
49 def getIthValue(self, i): |
285
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
50 sortedKeys = sorted(self.values.keys()) |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
51 if 0<=i<len(sortedKeys): |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
52 return self.values[sortedKeys[i]] |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
53 else: |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
54 return None |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
55 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 def __iter__(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
57 self.iterInstantNum = 0 # index in the interval or keys of the dict |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
58 return self |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
59 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
60 def next(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
61 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
62 # or (self.iterInstantNum >= self.values) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
63 raise StopIteration |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
64 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
65 self.iterInstantNum += 1 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
66 return self.getIthValue(self.iterInstantNum-1) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
67 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
68 def getTimeInterval(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
69 return self.timeInterval |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
70 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
71 def getName(self): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
72 return self.name |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
73 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
74 def getValues(self): |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
75 return [self.__getitem__(t) for t in self.timeInterval] |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
76 |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
77 def plot(self, options = '', xfactor = 1., **kwargs): |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
78 from matplotlib.pylab import plot,ylim |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
79 if self.getTimeInterval().length() == 1: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
80 marker = 'o' |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
81 else: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
82 marker = '' |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
83 time = sorted(self.values.keys()) |
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
84 plot([x/xfactor for x in time], [self.values[i] for i in time], options+marker, **kwargs) |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
85 if self.maxValue: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
86 ylim(ymax = self.maxValue) |
308
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
87 |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
88 def valueSorted(self): |
312 | 89 ''' return the values after sort the keys in the indicator |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
90 This should probably not be used: to delete''' |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
91 print('Deprecated: values should not be accessed in this way') |
308
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
92 values=[] |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
93 keys = self.values.keys() |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
94 keys.sort() |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
95 for key in keys: |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
96 values.append(self.values[key]) |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
97 return values |
312 | 98 |
308
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
99 @staticmethod |
313
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
100 def getDLCSS(indic1, indic2, threshold, delta = float('inf') , method ='min' ): |
308
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
101 ''' compute the distance between two indicators using LCSS |
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
102 two common methods are used: min or mean of the indicators length''' |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
103 print('Deprecated: this is not appropriate method for indicator comparison') |
313
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
104 l1 = indic1.valueSorted |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
105 l2 = indic2.valueSorted |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
106 DLCSS = None |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
107 if method == 'min': |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
108 DLCSS = 1- (LCSS(l1,l2, threshold, delta, distance))/min(len(l1),len(l2)) |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
109 if method == 'mean': |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
110 average = len(l1)+len(l2)/2 |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
111 DLCSS = 1- ((LCSS(l1,l2, threshold, delta, distance))/average) |
43e62b9cb652
solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
312
diff
changeset
|
112 return DLCSS |
308
8bafd054cda4
Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents:
282
diff
changeset
|
113 |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
114 |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
115 def distanceForLCSS(x, y): # lambda x,y:abs(x-y) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
116 if x == None or y == None: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
117 return float('inf') |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
118 else: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
119 return abs(x-y) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
120 |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
121 # non-aligned LCSS computations, ok for delta = inf |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
122 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')): |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
123 ''' compute the LCSS between two indicators using LCSS''' |
286
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
285
diff
changeset
|
124 from utils import LCSS |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
125 if indicator1 and indicator2: |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
126 return LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
127 else: |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
128 return 0 |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
129 |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
130 def computeNormalizedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
131 ''' compute the normalized LCSS between two indicators using LCSS |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
132 ie, the LCSS divided by the min or mean of the indicator lengths''' |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
133 from utils import normalizedLCSS |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
134 if indicator1 and indicator2: |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
135 return normalizedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) |
286
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
285
diff
changeset
|
136 else: |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
137 return 0. |
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
138 |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
139 def computeDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
140 ''' compute the LCSS distance between two indicators using LCSS''' |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
141 from utils import DLCSS |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
142 return DLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
143 |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
144 # aligned LCSS computations |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
145 def computeAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf')): |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
146 ''' compute the aligned LCSS between two indicators using LCSS''' |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
147 from utils import alignedLCSS |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
148 if indicator1 and indicator2: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
149 return alignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
150 else: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
151 return 0 |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
152 |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
153 def computeNormalizedAlignedLCSS(indicator1, indicator2, threshold, delta = float('inf'), method= min): |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
154 ''' compute the normalized aligned LCSS between two indicators using LCSS |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
155 ie, the LCSS divided by the min or mean of the indicator lengths''' |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
156 from utils import normalizedAlignedLCSS |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
157 if indicator1 and indicator2: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
158 return normalizedAlignedLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
159 else: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
160 return 0. |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
161 |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
162 def computeAlignedDLCSS(indicator1, indicator2, threshold, delta = float('inf'), method = min): |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
163 ''' compute the aligned LCSS distance between two indicators using LCSS''' |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
164 from utils import alignedDLCSS |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
165 return alignedDLCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta, method) |
285
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
166 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
167 class SeverityIndicator(TemporalIndicator): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
168 '''Class for severity indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
169 field mostSevereIsMax is True |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
170 if the most severe value taken by the indicator is the maximum''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
171 |
286
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
285
diff
changeset
|
172 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None): |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
173 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
174 self.mostSevereIsMax = mostSevereIsMax |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
175 |
285
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
176 def getMostSevereValue(self, minNInstants=1): # TODO use scoreatpercentile |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
177 from matplotlib.mlab import find |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
178 from numpy.core.multiarray import array |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
179 from numpy.core.fromnumeric import mean |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
180 values = array(self.values.values()) |
286
fa95796a76b3
simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
285
diff
changeset
|
181 indices = range(len(values)) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
182 if len(indices) >= minNInstants: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
183 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
184 return mean(values[:minNInstants]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
185 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
186 return None |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
187 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
188 # functions to aggregate discretized maps of indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
189 # TODO add values in the cells between the positions (similar to discretizing vector graphics to bitmap) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
190 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
191 def indicatorMap(indicatorValues, trajectory, squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
192 '''Returns a dictionary |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
193 with keys for the indices of the cells (squares) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
194 in which the trajectory positions are located |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
195 at which the indicator values are attached |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
196 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
197 ex: speeds and trajectory''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
198 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
199 from numpy import floor, mean |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
200 assert len(indicatorValues) == trajectory.length() |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
201 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
202 for k in xrange(trajectory.length()): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
203 p = trajectory[k] |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
204 i = floor(p.x/squareSize) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
205 j = floor(p.y/squareSize) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
206 if indicatorMap.has_key((i,j)): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
207 indicatorMap[(i,j)].append(indicatorValues[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
208 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
209 indicatorMap[(i,j)] = [indicatorValues[k]] |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
210 for k in indicatorMap.keys(): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
211 indicatorMap[k] = mean(indicatorMap[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
212 return indicatorMap |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
213 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
214 def indicatorMapFromPolygon(value, polygon, squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
215 '''Fills an indicator map with the value within the polygon |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
216 (array of Nx2 coordinates of the polygon vertices)''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
217 import matplotlib.nxutils as nx |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
218 from numpy.core.multiarray import array, arange |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
219 from numpy import floor |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
220 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
221 points = [] |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
222 for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
223 for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
224 points.append([x,y]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
225 inside = nx.points_inside_poly(array(points), polygon) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
226 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
227 for i in xrange(len(inside)): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
228 if inside[i]: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
229 indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
230 return indicatorMap |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
231 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
232 def indicatorMapFromAxis(value, limits, squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
233 '''axis = [xmin, xmax, ymin, ymax] ''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
234 from numpy.core.multiarray import arange |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
235 from numpy import floor |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
236 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
237 for x in arange(limits[0], limits[1], squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
238 for y in arange(limits[2], limits[3], squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
239 indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
240 return indicatorMap |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
241 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
242 def combineIndicatorMaps(maps, squareSize, combinationFunction): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
243 '''Puts many indicator maps together |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
244 (averaging the values in each cell |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
245 if more than one maps has a value)''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
246 #from numpy import mean |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
247 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
248 for m in maps: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
249 for k,v in m.iteritems(): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
250 if indicatorMap.has_key(k): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
251 indicatorMap[k].append(v) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
252 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
253 indicatorMap[k] = [v] |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
254 for k in indicatorMap.keys(): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
255 indicatorMap[k] = combinationFunction(indicatorMap[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
256 return indicatorMap |
247
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
257 |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
258 if __name__ == "__main__": |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
259 import doctest |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
260 import unittest |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
261 suite = doctest.DocFileSuite('tests/indicators.txt') |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
262 unittest.TextTestRunner().run(suite) |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
263 # #doctest.testmod() |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
264 # #doctest.testfile("example.txt") |