comparison python/indicators.py @ 691:fa9aa5f08210 dev

cleaned imports in indicators.py
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 29 Jun 2015 16:15:04 -0400
parents 15e244d2a1b5
children 9a258687af4c
comparison
equal deleted inserted replaced
690:463150a8e129 691:fa9aa5f08210
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Class for indicators, temporal indicators, and safety indicators''' 2 '''Class for indicators, temporal indicators, and safety indicators'''
3 3
4 import moving 4 import moving
5 #import matplotlib.nxutils as nx
6 from matplotlib.pyplot import plot, ylim
7 from matplotlib.pylab import find
8 from numpy import array, arange, mean, floor, mean
5 9
6 10
7 # need for a class representing the indicators, their units, how to print them in graphs... 11 # need for a class representing the indicators, their units, how to print them in graphs...
8 class TemporalIndicator(object): 12 class TemporalIndicator(object):
9 '''Class for temporal indicators 13 '''Class for temporal indicators
72 76
73 def getValues(self): 77 def getValues(self):
74 return [self.__getitem__(t) for t in self.timeInterval] 78 return [self.__getitem__(t) for t in self.timeInterval]
75 79
76 def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs): 80 def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs):
77 from matplotlib.pylab import plot,ylim
78 if self.getTimeInterval().length() == 1: 81 if self.getTimeInterval().length() == 1:
79 marker = 'o' 82 marker = 'o'
80 else: 83 else:
81 marker = '' 84 marker = ''
82 time = sorted(self.values.keys()) 85 time = sorted(self.values.keys())
139 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None): 142 def __init__(self, name, values, timeInterval=None, mostSevereIsMax=True, maxValue = None):
140 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue) 143 TemporalIndicator.__init__(self, name, values, timeInterval, maxValue)
141 self.mostSevereIsMax = mostSevereIsMax 144 self.mostSevereIsMax = mostSevereIsMax
142 145
143 def getMostSevereValue(self, minNInstants=1): # TODO use np.percentile 146 def getMostSevereValue(self, minNInstants=1): # TODO use np.percentile
144 from matplotlib.mlab import find
145 from numpy.core.multiarray import array
146 from numpy.core.fromnumeric import mean
147 values = array(self.values.values()) 147 values = array(self.values.values())
148 indices = range(len(values)) 148 indices = range(len(values))
149 if len(indices) >= minNInstants: 149 if len(indices) >= minNInstants:
150 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values 150 values = sorted(values[indices], reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values
151 return mean(values[:minNInstants]) 151 return mean(values[:minNInstants])
161 in which the trajectory positions are located 161 in which the trajectory positions are located
162 at which the indicator values are attached 162 at which the indicator values are attached
163 163
164 ex: speeds and trajectory''' 164 ex: speeds and trajectory'''
165 165
166 from numpy import floor, mean
167 assert len(indicatorValues) == trajectory.length() 166 assert len(indicatorValues) == trajectory.length()
168 indicatorMap = {} 167 indicatorMap = {}
169 for k in xrange(trajectory.length()): 168 for k in xrange(trajectory.length()):
170 p = trajectory[k] 169 p = trajectory[k]
171 i = floor(p.x/squareSize) 170 i = floor(p.x/squareSize)
176 indicatorMap[(i,j)] = [indicatorValues[k]] 175 indicatorMap[(i,j)] = [indicatorValues[k]]
177 for k in indicatorMap.keys(): 176 for k in indicatorMap.keys():
178 indicatorMap[k] = mean(indicatorMap[k]) 177 indicatorMap[k] = mean(indicatorMap[k])
179 return indicatorMap 178 return indicatorMap
180 179
181 def indicatorMapFromPolygon(value, polygon, squareSize): 180 # def indicatorMapFromPolygon(value, polygon, squareSize):
182 '''Fills an indicator map with the value within the polygon 181 # '''Fills an indicator map with the value within the polygon
183 (array of Nx2 coordinates of the polygon vertices)''' 182 # (array of Nx2 coordinates of the polygon vertices)'''
184 import matplotlib.nxutils as nx 183 # points = []
185 from numpy.core.multiarray import array, arange 184 # for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize):
186 from numpy import floor 185 # for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize):
187 186 # points.append([x,y])
188 points = [] 187 # inside = nx.points_inside_poly(array(points), polygon)
189 for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): 188 # indicatorMap = {}
190 for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): 189 # for i in xrange(len(inside)):
191 points.append([x,y]) 190 # if inside[i]:
192 inside = nx.points_inside_poly(array(points), polygon) 191 # indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0
193 indicatorMap = {} 192 # return indicatorMap
194 for i in xrange(len(inside)):
195 if inside[i]:
196 indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0
197 return indicatorMap
198 193
199 def indicatorMapFromAxis(value, limits, squareSize): 194 def indicatorMapFromAxis(value, limits, squareSize):
200 '''axis = [xmin, xmax, ymin, ymax] ''' 195 '''axis = [xmin, xmax, ymin, ymax] '''
201 from numpy.core.multiarray import arange
202 from numpy import floor
203 indicatorMap = {} 196 indicatorMap = {}
204 for x in arange(limits[0], limits[1], squareSize): 197 for x in arange(limits[0], limits[1], squareSize):
205 for y in arange(limits[2], limits[3], squareSize): 198 for y in arange(limits[2], limits[3], squareSize):
206 indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value 199 indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value
207 return indicatorMap 200 return indicatorMap
208 201
209 def combineIndicatorMaps(maps, squareSize, combinationFunction): 202 def combineIndicatorMaps(maps, squareSize, combinationFunction):
210 '''Puts many indicator maps together 203 '''Puts many indicator maps together
211 (averaging the values in each cell 204 (averaging the values in each cell
212 if more than one maps has a value)''' 205 if more than one maps has a value)'''
213 #from numpy import mean
214 indicatorMap = {} 206 indicatorMap = {}
215 for m in maps: 207 for m in maps:
216 for k,v in m.iteritems(): 208 for k,v in m.iteritems():
217 if indicatorMap.has_key(k): 209 if indicatorMap.has_key(k):
218 indicatorMap[k].append(v) 210 indicatorMap[k].append(v)