comparison python/indicators.py @ 998:933670761a57

updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 27 May 2018 23:22:48 -0400
parents c6d4ea05a2d0
children
comparison
equal deleted inserted replaced
997:4f3387a242a1 998:933670761a57
4 import moving 4 import moving
5 #import matplotlib.nxutils as nx 5 #import matplotlib.nxutils as nx
6 from matplotlib.pyplot import plot, ylim 6 from matplotlib.pyplot import plot, ylim
7 from matplotlib.pylab import find 7 from matplotlib.pylab import find
8 from numpy import array, arange, mean, floor, mean 8 from numpy import array, arange, mean, floor, mean
9 9 from scipy import percentile
10 10
11 def multivariateName(indicatorNames): 11 def multivariateName(indicatorNames):
12 return '_'.join(indicatorNames) 12 return '_'.join(indicatorNames)
13 13
14 # need for a class representing the indicators, their units, how to print them in graphs... 14 # need for a class representing the indicators, their units, how to print them in graphs...
33 self.timeInterval = moving.TimeInterval() 33 self.timeInterval = moving.TimeInterval()
34 else: 34 else:
35 assert len(values) == timeInterval.length() 35 assert len(values) == timeInterval.length()
36 self.timeInterval = timeInterval 36 self.timeInterval = timeInterval
37 self.values = {} 37 self.values = {}
38 for i in xrange(int(round(self.timeInterval.length()))): 38 for i in range(int(round(self.timeInterval.length()))):
39 self.values[self.timeInterval[i]] = values[i] 39 self.values[self.timeInterval[i]] = values[i]
40 self.maxValue = maxValue 40 self.maxValue = maxValue
41 41
42 def __len__(self): 42 def __len__(self):
43 return len(self.values) 43 return len(self.values)
58 58
59 def __iter__(self): 59 def __iter__(self):
60 self.iterInstantNum = 0 # index in the interval or keys of the dict 60 self.iterInstantNum = 0 # index in the interval or keys of the dict
61 return self 61 return self
62 62
63 def next(self): 63 def __next__(self):
64 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ 64 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\
65 # or (self.iterInstantNum >= self.values) 65 # or (self.iterInstantNum >= self.values)
66 raise StopIteration 66 raise StopIteration
67 else: 67 else:
68 self.iterInstantNum += 1 68 self.iterInstantNum += 1
158 158
159 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None): 159 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None):
160 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) 160 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue)
161 self.mostSevereIsMax = mostSevereIsMax 161 self.mostSevereIsMax = mostSevereIsMax
162 162
163 def getMostSevereValue(self, minNInstants=1): # TODO use np.percentile 163 def getMostSevereValue(self, minNInstants=1, centile=15.):
164 values = array(self.values.values()) 164 '''if there are more than minNInstants observations,
165 indices = range(len(values)) 165 returns either the average of these maximum values
166 if len(indices) >= minNInstants: 166 or if centile is not None the n% centile from the most severe value
167 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values 167
168 return mean(values[:minNInstants]) 168 eg for TTC, 15 returns the 15th centile (value such that 15% of observations are lower)'''
169 else: 169 if self.__len__() < minNInstants:
170 return None 170 return None
171 else:
172 values = list(self.values.values())
173 if centile is not None:
174 if self.mostSevereIsMax:
175 c = 100-centile
176 else:
177 c = centile
178 return percentile(values, c)
179 else:
180 values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
181 return mean(values[:minNInstants])
171 182
172 def getInstantOfMostSevereValue(self): 183 def getInstantOfMostSevereValue(self):
173 '''Returns the instant at which the indicator reaches its most severe value''' 184 '''Returns the instant at which the indicator reaches its most severe value'''
174 if self.mostSevereIsMax: 185 if self.mostSevereIsMax:
175 return max(self.values, key=self.values.get) 186 return max(self.values, key=self.values.get)
187 198
188 ex: speeds and trajectory''' 199 ex: speeds and trajectory'''
189 200
190 assert len(indicatorValues) == trajectory.length() 201 assert len(indicatorValues) == trajectory.length()
191 indicatorMap = {} 202 indicatorMap = {}
192 for k in xrange(trajectory.length()): 203 for k in range(trajectory.length()):
193 p = trajectory[k] 204 p = trajectory[k]
194 i = floor(p.x/squareSize) 205 i = floor(p.x/squareSize)
195 j = floor(p.y/squareSize) 206 j = floor(p.y/squareSize)
196 if indicatorMap.has_key((i,j)): 207 if (i,j) in indicatorMap:
197 indicatorMap[(i,j)].append(indicatorValues[k]) 208 indicatorMap[(i,j)].append(indicatorValues[k])
198 else: 209 else:
199 indicatorMap[(i,j)] = [indicatorValues[k]] 210 indicatorMap[(i,j)] = [indicatorValues[k]]
200 for k in indicatorMap.keys(): 211 for k in indicatorMap:
201 indicatorMap[k] = mean(indicatorMap[k]) 212 indicatorMap[k] = mean(indicatorMap[k])
202 return indicatorMap 213 return indicatorMap
203 214
204 # def indicatorMapFromPolygon(value, polygon, squareSize): 215 # def indicatorMapFromPolygon(value, polygon, squareSize):
205 # '''Fills an indicator map with the value within the polygon 216 # '''Fills an indicator map with the value within the polygon
208 # for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): 219 # for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize):
209 # for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): 220 # for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize):
210 # points.append([x,y]) 221 # points.append([x,y])
211 # inside = nx.points_inside_poly(array(points), polygon) 222 # inside = nx.points_inside_poly(array(points), polygon)
212 # indicatorMap = {} 223 # indicatorMap = {}
213 # for i in xrange(len(inside)): 224 # for i in range(len(inside)):
214 # if inside[i]: 225 # if inside[i]:
215 # indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0 226 # indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0
216 # return indicatorMap 227 # return indicatorMap
217 228
218 def indicatorMapFromAxis(value, limits, squareSize): 229 def indicatorMapFromAxis(value, limits, squareSize):
227 '''Puts many indicator maps together 238 '''Puts many indicator maps together
228 (averaging the values in each cell 239 (averaging the values in each cell
229 if more than one maps has a value)''' 240 if more than one maps has a value)'''
230 indicatorMap = {} 241 indicatorMap = {}
231 for m in maps: 242 for m in maps:
232 for k,v in m.iteritems(): 243 for k,v in m.items():
233 if indicatorMap.has_key(k): 244 if k in indicatorMap:
234 indicatorMap[k].append(v) 245 indicatorMap[k].append(v)
235 else: 246 else:
236 indicatorMap[k] = [v] 247 indicatorMap[k] = [v]
237 for k in indicatorMap.keys(): 248 for k in indicatorMap:
238 indicatorMap[k] = combinationFunction(indicatorMap[k]) 249 indicatorMap[k] = combinationFunction(indicatorMap[k])
239 return indicatorMap 250 return indicatorMap
240 251
241 if __name__ == "__main__": 252 if __name__ == "__main__":
242 import doctest 253 import doctest