annotate python/indicators.py @ 322:28661c5887d3

Corrected a major bug for LCSS Added functions to test all alignments when computing the LCSS with a finite delta
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 07 May 2013 18:43:40 +0200
parents a5e40bd04cf4
children 1046b7346886
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 287
diff changeset
21 self.isCosine = (name.find('Cosine') >= 0)
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
22 if timeInterval:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
23 assert len(values) == timeInterval.length()
282
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
24 self.timeInterval = timeInterval
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
25 self.values = {}
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
26 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
27 self.values[self.timeInterval[i]] = values[i]
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
28 else:
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
29 self.values = values
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
30 instants = sorted(self.values.keys())
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
31 if instants:
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
32 self.timeInterval = moving.TimeInterval(instants[0], instants[-1])
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
33 else:
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
34 self.timeInterval = moving.TimeInterval()
269
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
35 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
36
287
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
37 def __len__(self):
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
38 return len(self.values)
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
39
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40 def empty(self):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
41 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
42
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
43 def __getitem__(self, i):
287
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
44 'Returns ith value in time interval'
282
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
45 if i in self.values.keys():
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
46 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
47 else:
282
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
48 return None
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
49
287
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
50 def getIthValue(self, i):
285
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
51 sortedKeys = sorted(self.values.keys())
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
52 if 0<=i<len(sortedKeys):
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
53 return self.values[sortedKeys[i]]
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
54 else:
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
55 return None
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
56
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 def __iter__(self):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 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
59 return self
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
60
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
61 def next(self):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
62 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
63 # 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
64 raise StopIteration
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
65 else:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66 self.iterInstantNum += 1
287
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
67 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
68
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
69 def getTimeInterval(self):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
70 return self.timeInterval
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72 def getValues(self):
287
66691c06928c first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 286
diff changeset
73 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
74
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
75 def getAngleValues(self):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
76 '''if the indicator is a function of an angle,
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
77 transform it to an angle (eg cos)
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
78 (no transformation otherwise)'''
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
79 from numpy import arccos
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
80 values = self.getValues()
306
93d851d0d21e bug correction, minor work on indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 287
diff changeset
81 if self.isCosine:
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
82 return [arccos(c) for c in values]
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
83 else:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
84 return values
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
85
269
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
86 def plot(self, options = '', xfactor = 1., **kwargs):
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
87 from matplotlib.pylab import plot,ylim
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
88 if self.getTimeInterval().length() == 1:
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
89 marker = 'o'
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
90 else:
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
91 marker = ''
282
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
92 time = sorted(self.values.keys())
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
93 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
94 if self.maxValue:
a9988971aac8 removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 265
diff changeset
95 ylim(ymax = self.maxValue)
308
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
96
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
97 def valueSorted(self):
312
6c068047edbf merged with Mohamed s work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 306 308
diff changeset
98 ''' 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
99 This should probably not be used: to delete'''
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
100 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
101 values=[]
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
102 keys = self.values.keys()
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
103 keys.sort()
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
104 for key in keys:
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
105 values.append(self.values[key])
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
106 return values
312
6c068047edbf merged with Mohamed s work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 306 308
diff changeset
107
308
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
108 @staticmethod
313
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
109 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
110 ''' 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
111 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
112 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
113 l1 = indic1.valueSorted
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
114 l2 = indic2.valueSorted
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
115 DLCSS = None
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
116 if method == 'min':
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
117 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
118 if method == 'mean':
43e62b9cb652 solved issues to make the files importable (caution with getDLCSS)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 312
diff changeset
119 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
120 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
121 return DLCSS
308
8bafd054cda4 Added a function to compute LCSS distance between two indcators
Mohamed Gomaa
parents: 282
diff changeset
122
282
abbd4bc13dac modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 269
diff changeset
123
322
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
124 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
125 if x == None or y == None:
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
126 return float('inf')
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
127 else:
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
128 return abs(x-y)
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
129
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
130 # non-aligned LCSS computations, ok for delta = inf
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
131 def computeLCSS(indicator1, indicator2, threshold, delta = float('inf')):
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
132 ''' 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
133 from utils import LCSS
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 LCSS(indicator1.getValues(), indicator2.getValues(), threshold, distanceForLCSS, delta)
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
136 else:
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 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
140 ''' compute the normalized LCSS between two indicators using LCSS
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
141 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
142 from utils import normalizedLCSS
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
143 if indicator1 and indicator2:
322
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
144 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
145 else:
321
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
146 return 0.
a5e40bd04cf4 rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 313
diff changeset
147
322
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
148 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
149 ''' 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
150 from utils import DLCSS
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
151 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
152
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
153 # aligned LCSS computations
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
154 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
155 ''' 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
156 from utils import alignedLCSS
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 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
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 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
163 ''' 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
164 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
165 from utils import normalizedAlignedLCSS
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
166 if indicator1 and indicator2:
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
167 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
168 else:
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
169 return 0.
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
170
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
171 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
172 ''' 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
173 from utils import alignedDLCSS
28661c5887d3 Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 321
diff changeset
174 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
175
244
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
176 class SeverityIndicator(TemporalIndicator):
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
177 '''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
178 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
179 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
180
286
fa95796a76b3 simplified indicators (only non-measured values, whether measurable or not)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 285
diff changeset
181 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
182 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
183 self.mostSevereIsMax = mostSevereIsMax
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
184
285
5957aa1d69e1 Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 282
diff changeset
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 else:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
195 return None
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 # 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
198 # 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
199
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
200 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
201 '''Returns a dictionary
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
202 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
203 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
204 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
205
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
206 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
207
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
208 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
209 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
210 indicatorMap = {}
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
211 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
212 p = trajectory[k]
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
213 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
214 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
215 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
216 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
217 else:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
218 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
219 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
220 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
221 return indicatorMap
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
222
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
223 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
224 '''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
225 (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
226 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
227 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
228 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
229
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
230 points = []
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
231 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
232 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
233 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
234 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
235 indicatorMap = {}
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
236 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
237 if inside[i]:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
238 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
239 return indicatorMap
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
240
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
241 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
242 '''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
243 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
244 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
245 indicatorMap = {}
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
246 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
247 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
248 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
249 return indicatorMap
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
250
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
251 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
252 '''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
253 (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
254 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
255 #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
256 indicatorMap = {}
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
257 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
258 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
259 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
260 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
261 else:
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
262 indicatorMap[k] = [v]
5027c174ab90 moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
263 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
264 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
265 return indicatorMap
247
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
266
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
267 if __name__ == "__main__":
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
268 import doctest
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
269 import unittest
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
270 suite = doctest.DocFileSuite('tests/indicators.txt')
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
271 unittest.TextTestRunner().run(suite)
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
272 # #doctest.testmod()
8f0ed138d373 moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 244
diff changeset
273 # #doctest.testfile("example.txt")