Mercurial > hg > nsaunier > traffic-intelligence
diff python/indicators.py @ 708:a37c565f4b68
merged dev
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 22 Jul 2015 14:17:44 -0400 |
| parents | 5ee22bf7e4d5 |
| children | b75d0c258ca9 |
line wrap: on
line diff
--- a/python/indicators.py Wed Jul 22 14:17:19 2015 -0400 +++ b/python/indicators.py Wed Jul 22 14:17:44 2015 -0400 @@ -2,6 +2,10 @@ '''Class for indicators, temporal indicators, and safety indicators''' import moving +#import matplotlib.nxutils as nx +from matplotlib.pyplot import plot, ylim +from matplotlib.pylab import find +from numpy import array, arange, mean, floor, mean # need for a class representing the indicators, their units, how to print them in graphs... @@ -15,21 +19,21 @@ it should have more information like name, unit''' - def __init__(self, name, values, timeInterval=None, maxValue = None): + def __init__(self, name, values, timeInterval = None, maxValue = None): self.name = name - if timeInterval: + if timeInterval is None: + self.values = values + instants = sorted(self.values.keys()) + if len(instants) > 0: + self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) + else: + self.timeInterval = moving.TimeInterval() + else: assert len(values) == timeInterval.length() self.timeInterval = timeInterval self.values = {} for i in xrange(int(round(self.timeInterval.length()))): self.values[self.timeInterval[i]] = values[i] - else: - self.values = values - instants = sorted(self.values.keys()) - if instants: - self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) - else: - self.timeInterval = moving.TimeInterval() self.maxValue = maxValue def __len__(self): @@ -74,7 +78,6 @@ return [self.__getitem__(t) for t in self.timeInterval] def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs): - from matplotlib.pylab import plot,ylim if self.getTimeInterval().length() == 1: marker = 'o' else: @@ -141,9 +144,6 @@ self.mostSevereIsMax = mostSevereIsMax def getMostSevereValue(self, minNInstants=1): # TODO use np.percentile - from matplotlib.mlab import find - from numpy.core.multiarray import array - from numpy.core.fromnumeric import mean values = array(self.values.values()) indices = range(len(values)) if len(indices) >= minNInstants: @@ -152,6 +152,13 @@ else: return None + def getInstantOfMostSevereValue(self): + '''Returns the instant at which the indicator reaches its most severe value''' + if self.mostSevereIsMax: + return max(self.values, key=self.values.get) + else: + return min(self.values, key=self.values.get) + # functions to aggregate discretized maps of indicators # TODO add values in the cells between the positions (similar to discretizing vector graphics to bitmap) @@ -163,7 +170,6 @@ ex: speeds and trajectory''' - from numpy import floor, mean assert len(indicatorValues) == trajectory.length() indicatorMap = {} for k in xrange(trajectory.length()): @@ -178,28 +184,22 @@ indicatorMap[k] = mean(indicatorMap[k]) return indicatorMap -def indicatorMapFromPolygon(value, polygon, squareSize): - '''Fills an indicator map with the value within the polygon - (array of Nx2 coordinates of the polygon vertices)''' - import matplotlib.nxutils as nx - from numpy.core.multiarray import array, arange - from numpy import floor - - points = [] - for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): - for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): - points.append([x,y]) - inside = nx.points_inside_poly(array(points), polygon) - indicatorMap = {} - for i in xrange(len(inside)): - if inside[i]: - indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0 - return indicatorMap +# def indicatorMapFromPolygon(value, polygon, squareSize): +# '''Fills an indicator map with the value within the polygon +# (array of Nx2 coordinates of the polygon vertices)''' +# points = [] +# for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): +# for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): +# points.append([x,y]) +# inside = nx.points_inside_poly(array(points), polygon) +# indicatorMap = {} +# for i in xrange(len(inside)): +# if inside[i]: +# indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0 +# return indicatorMap def indicatorMapFromAxis(value, limits, squareSize): '''axis = [xmin, xmax, ymin, ymax] ''' - from numpy.core.multiarray import arange - from numpy import floor indicatorMap = {} for x in arange(limits[0], limits[1], squareSize): for y in arange(limits[2], limits[3], squareSize): @@ -210,7 +210,6 @@ '''Puts many indicator maps together (averaging the values in each cell if more than one maps has a value)''' - #from numpy import mean indicatorMap = {} for m in maps: for k,v in m.iteritems():
