Mercurial Hosting > traffic-intelligence
changeset 65:75cf537b8d88
moved and generalized map making functions to the library
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 07 Nov 2010 01:09:50 -0500 |
parents | c75bcdaed00f |
children | 56fe4ef1377e |
files | .hgignore python/moving.py python/utils.py |
diffstat | 3 files changed, 51 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Fri Nov 05 11:07:44 2010 -0400 +++ b/.hgignore Sun Nov 07 01:09:50 2010 -0500 @@ -6,6 +6,7 @@ *.o *.exe *.pyc +*.pyo *~ *.rej *.orig
--- a/python/moving.py Fri Nov 05 11:07:44 2010 -0400 +++ b/python/moving.py Sun Nov 07 01:09:50 2010 -0500 @@ -387,6 +387,42 @@ else: return None +def indicatorMap(indicatorValues, trajectory, squareSize): + '''Returns a dictionary + with keys for the indices of the cells (squares) + in which the trajectory positions are located + at which the indicator values are attached''' + from numpy import floor, mean + assert len(indicatorValues) == trajectory.length() + indicatorMap = {} + for k in xrange(trajectory.length()): + p = trajectory[k] + i = floor(p.x/squareSize) + j = floor(p.y/squareSize) + if indicatorMap.has_key((i,j)): + indicatorMap[(i,j)].append(indicatorValues[k]) + else: + indicatorMap[(i,j)] = [indicatorValues[k]] + for k in indicatorMap.keys(): + indicatorMap[k] = mean(indicatorMap[k]) + return indicatorMap + +def combineIndicatorMaps(maps, squareSize): + '''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(): + if indicatorMap.has_key(k): + indicatorMap[k].append(v) + else: + indicatorMap[k] = [v] + for k in indicatorMap.keys(): + indicatorMap[k] = mean(indicatorMap[k]) + return indicatorMap + if __name__ == "__main__": import doctest import unittest
--- a/python/utils.py Fri Nov 05 11:07:44 2010 -0400 +++ b/python/utils.py Sun Nov 07 01:09:50 2010 -0500 @@ -132,6 +132,20 @@ colors = PlottingPropertyValues('brgmyck') # 'w' +def plotIndicatorMap(indicatorMap, squareSize): + from numpy import array, arange, ones, ma + from matplotlib.pyplot import pcolor + coords = array(indicatorMap.keys()) + minX = min(coords[:,0]) + minY = min(coords[:,1]) + X = arange(minX, max(coords[:,0])+1.1)*squareSize + Y = arange(minY, max(coords[:,1])+1.1)*squareSize + C = -ones((len(Y), len(X))) + for k,v in indicatorMap.iteritems(): + C[k[1]-minY,k[0]-minX] = v + masked = ma.masked_where(C<0,C) + pcolor(X, Y, masked) + ######################### # file I/O section #########################