diff python/moving.py @ 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
line wrap: on
line diff
--- 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