changeset 115:550556378466

added functionalities to indicator maps
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sat, 23 Jul 2011 02:18:16 -0400
parents 680d4c82886d
children 2bf5b76320c0
files python/moving.py python/utils.py
diffstat 2 files changed, 34 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Mon Jul 18 19:24:06 2011 -0400
+++ b/python/moving.py	Sat Jul 23 02:18:16 2011 -0400
@@ -563,6 +563,34 @@
         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 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):
+            indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value
+    return indicatorMap
+
 def combineIndicatorMaps(maps, squareSize, combinationFunction):
     '''Puts many indicator maps together 
     (averaging the values in each cell 
--- a/python/utils.py	Mon Jul 18 19:24:06 2011 -0400
+++ b/python/utils.py	Sat Jul 23 02:18:16 2011 -0400
@@ -174,7 +174,7 @@
 
 colors = PlottingPropertyValues('brgmyck') # 'w'
 
-def plotIndicatorMap(indicatorMap, squareSize):
+def plotIndicatorMap(indicatorMap, squareSize, masked = True, defaultValue=-1):
     from numpy import array, arange, ones, ma
     from matplotlib.pyplot import pcolor
     coords = array(indicatorMap.keys())
@@ -182,11 +182,13 @@
     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)))
+    C = defaultValue*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)
+    if masked:
+        pcolor(X, Y, ma.masked_where(C==defaultValue,C))
+    else:
+        pcolor(X, Y, C)
 
 #########################
 # file I/O section