[GRASS-SVN] r58453 - grass/trunk/gui/wxpython/iscatt

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Dec 12 02:09:49 PST 2013


Author: turek
Date: 2013-12-12 02:09:49 -0800 (Thu, 12 Dec 2013)
New Revision: 58453

Modified:
   grass/trunk/gui/wxpython/iscatt/core_c.py
   grass/trunk/gui/wxpython/iscatt/iscatt_core.py
   grass/trunk/gui/wxpython/iscatt/plots.py
Log:
wx.iscatt: workaround for non pickability of memmap in older numpy versions 

Modified: grass/trunk/gui/wxpython/iscatt/core_c.py
===================================================================
--- grass/trunk/gui/wxpython/iscatt/core_c.py	2013-12-12 09:45:52 UTC (rev 58452)
+++ grass/trunk/gui/wxpython/iscatt/core_c.py	2013-12-12 10:09:49 UTC (rev 58453)
@@ -69,17 +69,9 @@
 
     I_merge_arrays(merged_p, overlay_p, merged_arr.shape[0], merged_arr.shape[1], alpha)
 
-def MergeArrays(merged_arr, overlay_arr, alpha):
-    if merged_arr.shape != overlay_arr.shape:
-        GException("MergeArrays: merged_arr.shape != overlay_arr.shape")
-
-    c_uint8_p = POINTER(c_uint8)
-    merged_p = merged_arr.ctypes.data_as(c_uint8_p)
-    overlay_p = overlay_arr.ctypes.data_as(c_uint8_p)
-
-    I_merge_arrays(merged_p, overlay_p, merged_arr.shape[0], merged_arr.shape[1], alpha)
-
 def ComputeScatts(region, scatt_conds, bands, n_bands, scatts, cats_rasts_conds, cats_rasts):
+    _memmapToFileNames(scatts)
+    _memmapToFileNames(scatt_conds)
 
     q = Queue()
     p = Process(target=_computeScattsProcess, args=(region, scatt_conds, bands, 
@@ -90,6 +82,20 @@
     
     return ret[0], ret[1]
 
+#_memmapToFileNames and _fileNamesToMemmap are  workaround for older numpy version, 
+# where memmap objects are not pickable,
+# and therefore cannot be passed to process spawned by multiprocessing module
+def _memmapToFileNames(data):
+
+    for k, v in data.iteritems(): 
+        if v.has_key('np_vals'):
+            data[k]['np_vals'] = v['np_vals'].filename()
+
+def _fileNamesToMemmap(data):
+    for k, v in data.iteritems(): 
+        if v.has_key('np_vals'):
+            data[k]['np_vals'] = np.memmap(filename=v['np_vals'])
+
 def UpdateCatRast(patch_rast, region, cat_rast):
     q = Queue()
     p = Process(target=_updateCatRastProcess, args=(patch_rast, region, cat_rast, q))
@@ -106,7 +112,9 @@
 def _computeScattsProcess(region, scatt_conds, bands, n_bands, scatts, 
                           cats_rasts_conds, cats_rasts, output_queue):
 
-    #TODO names for types not 0 and 1?
+    _fileNamesToMemmap(scatts)
+    _fileNamesToMemmap(scatt_conds)
+
     sccats_c, cats_rasts_c, refs = _getComputationStruct(scatts, cats_rasts, 
                                                          SC_SCATT_DATA, n_bands)
     scatt_conds_c, cats_rasts_conds_c, refs2 = _getComputationStruct(scatt_conds, cats_rasts_conds, 

Modified: grass/trunk/gui/wxpython/iscatt/iscatt_core.py
===================================================================
--- grass/trunk/gui/wxpython/iscatt/iscatt_core.py	2013-12-12 09:45:52 UTC (rev 58452)
+++ grass/trunk/gui/wxpython/iscatt/iscatt_core.py	2013-12-12 10:09:49 UTC (rev 58453)
@@ -483,7 +483,7 @@
 
         shape = (b_i['b2']['max'] - b_i['b2']['min'] + 1, b_i['b1']['max'] - b_i['b1']['min'] + 1)
 
-        np_vals = np.memmap(grass.tempfile(), dtype=self.dtype, mode='w+', shape = shape)
+        np_vals = np.memmap(grass.tempfile(), dtype=self.dtype, mode='w+', shape=shape)
 
         self.cats[cat_id][scatt_id] = {'np_vals' : np_vals}
 

Modified: grass/trunk/gui/wxpython/iscatt/plots.py
===================================================================
--- grass/trunk/gui/wxpython/iscatt/plots.py	2013-12-12 09:45:52 UTC (rev 58452)
+++ grass/trunk/gui/wxpython/iscatt/plots.py	2013-12-12 10:09:49 UTC (rev 58453)
@@ -213,12 +213,17 @@
             c = None
 
         q = Queue()
+        _rendDtMemmapsToFiles(self.rend_dt)
         p = Process(target=MergeImg, args=(cats_order, scatts, styles, 
                                            self.rend_dt, q))
         p.start()
         merged_img, self.full_extend, self.rend_dt = q.get()
         p.join()
         
+
+        _rendDtFilesToMemmaps(self.rend_dt)
+        merged_img = np.memmap(filename=merged_img['dt'], shape=merged_img['sh'])
+
         #merged_img, self.full_extend = MergeImg(cats_order, scatts, styles, None)
         self.axes.clear()
         self.axes.axis('equal')
@@ -406,6 +411,8 @@
 
 def MergeImg(cats_order, scatts, styles, rend_dt, output_queue):
 
+        _rendDtFilesToMemmaps(rend_dt)
+
         init = True
         merged_img = None
         merge_tmp = grass.tempfile()
@@ -449,8 +456,10 @@
                 rend_dt[cat_id] = {}
                 if cat_id != 0:
                     rend_dt[cat_id]['color'] = styles[cat_id]['color']
+                
                 rend_dt[cat_id]['dt'] = np.memmap(grass.tempfile(), dtype='uint8', mode='w+', 
                                                                     shape=(sh[0], sh[1], 4))
+
                 #colored_cat = np.zeros(dtype='uint8', )
                 ApplyColormap(masked_cat, masked_cat.mask, cmap, rend_dt[cat_id]['dt'])
 
@@ -481,8 +490,27 @@
                 del c_img_a
             """
 
+        _rendDtMemmapsToFiles(rend_dt)
+
+        merged_img = {'dt' : merged_img.filename, 'sh' : merged_img.shape} 
         output_queue.put((merged_img, full_extend, rend_dt))
 
+#_rendDtMemmapsToFiles and _rendDtFilesToMemmaps are workarounds for older numpy versions,
+# where memmap objects are not pickable  
+def _rendDtMemmapsToFiles(rend_dt):
+
+    for k, v in rend_dt.iteritems():
+        if v.has_key('dt'):
+            rend_dt[k]['sh'] = v['dt'].shape
+            rend_dt[k]['dt'] = v['dt'].filename
+
+def _rendDtFilesToMemmaps(rend_dt):
+
+    for k, v in rend_dt.iteritems():
+        if v.has_key('dt'):
+            rend_dt[k]['dt'] = np.memmap(filename=v['dt'], shape=v['sh'])
+            del rend_dt[k]['sh']
+
 def _renderCat(cat_id, rend_dt, scatt, styles):
     return True
 



More information about the grass-commit mailing list