[GRASS-SVN] r61414 - grass-addons/grass7/raster/r.random.weight

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jul 26 01:51:29 PDT 2014


Author: pvanbosgeo
Date: 2014-07-26 01:51:29 -0700 (Sat, 26 Jul 2014)
New Revision: 61414

Added:
   grass-addons/grass7/raster/r.random.weight/r.random.weight_legacy.py
Modified:
   grass-addons/grass7/raster/r.random.weight/r.random.weight.html
   grass-addons/grass7/raster/r.random.weight/r.random.weight.py
Log:
Added some extra functionality, and adapted script to work with latest update of  rand() function in r.mapcalc.

Modified: grass-addons/grass7/raster/r.random.weight/r.random.weight.html
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.html	2014-07-25 22:10:53 UTC (rev 61413)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.html	2014-07-26 08:51:29 UTC (rev 61414)
@@ -3,11 +3,16 @@
 
 <em>r.rand.weight</em> generates a binary raster layer with randomly assigned values of 1 or 0, weighted by raster cell values of an user-defined raster layer. I.e., the change for a raster cell to get assigned a 1 (to get selected) depends on the weight (value) of that cell in the input weight layer.
 
-<p>By default every run will give you a different layer. You can specify fixed seed value to ensure that model runs are reproducible under options.
+<p>By default the script is run setting a random seed every time. To ensure that your results are reproducible you can set the seed value under the 'Sample options' tab. See the 'Random number generator initialization' in the r.mapcalc helpfile for more details.
  
+<p>By default the minimum and maximum weight are set to be equal to the minimum and maximum values of the weight raster layer. Note that this means that the raster cells with the maximum values will all be selected. You have the option however to set define different minimum and maximum weights under the 'Sample options' tab. This effectively changes the probability of cells to be selected. For example if you have a weight raster layer with values between 0 and 100 and you want to ensure all cells with a value of 50 or more are selected, you set the maximum weight at 50.
+
+<p>You can also set the total number of sample points to be selected using the under the 'Sample options' tab. This can be done using an absolute number or as percentage (see the help file of the r.random function for more details). 
+ 
 <h2>Note</h2>
-The values of the weight raster layer should be within the range defined by the start and end value. Currently the script does not check this (on the todo list)
+This script uses the rand() function in r.mapcalc. Due to recent changes how this function, you will need the latest grass 7.1 version (revision 61413 or above). If you are running an earlier version, you can try r.random.weight_legacy.py
 
+
 <h2>AUTHOR</h2>
 
 Paulo van Breugel, paulo at ecodiv.org

Modified: grass-addons/grass7/raster/r.random.weight/r.random.weight.py
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight.py	2014-07-25 22:10:53 UTC (rev 61413)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.py	2014-07-26 08:51:29 UTC (rev 61414)
@@ -25,7 +25,7 @@
 #% type: string
 #% gisprompt: old,cell,raster
 #% description: layer with weight
-#% key_desc: weight
+#% key_desc: raster
 #% required: yes
 #% multiple: no
 #%end
@@ -35,7 +35,7 @@
 #% type: string
 #% gisprompt: new,cell,raster
 #% description: output layer
-#% key_desc: output
+#% key_desc: raster
 #% required: yes
 #% multiple: no
 #%end
@@ -44,35 +44,31 @@
 #% key: start
 #% type: double
 #% description: minimum weight
-#% key_desc: start
-#% answer: 0
-#% required: yes
+#% guisection: Sample options
 #%end
 
 #%option
 #% key: end
 #% type: double
 #% description: maximum weight
-#% key_desc: end
-#% answer: 100
-#% required: yes
+#% guisection: Sample options
 #%end
 
 #%option
 #% key: subsample
 #% type: string
-#% description: subsample - not implemented yet
-#% key_desc: ss
-#% answer: 0
+#% description: subsample
 #% required: no
+#% guisection: Sample options
 #%end
 
 #%option
 #% key: seed
-#% type: double
+#% type: string
 #% description: set seed for random number generation
-#% key_desc: seed
+#% answer: auto
 #% required: no
+#% guisection: Sample options
 #%end
 
 # import libraries
@@ -106,26 +102,52 @@
     subsample = options['subsample']
     seed = options['seed']
     
+    if (minval == '' or maxval == '') :
+        minmax = grass.parse_command('r.univar', 
+            map = weight,
+            flags='g',
+            sep = ':')
+        minval = minmax['min']
+        maxval = minmax['max']
+    
     # setup temporary files and seed
     tmp_map = 'r_w_rand_987654321'
-    if seed == "":
-        ticks = str(int(time.time()*1000)),
-        print("seed used is: " + str(ticks[0]))
-        os.environ['GRASS_RND_SEED'] = ticks[0]
-    else:
-        print("Seed used for random number generation is: " + str(seed))
-        
-    grass.mapcalc("$tmp_map = rand(${minval},${maxval})", 
-        minval = minval, 
-        maxval = maxval,
-        tmp_map = tmp_map)
+    tmp_map2 = 'r_w_rand_987654321a'
    
-    grass.mapcalc("${outmap} = if($tmp_map < ${weight},1,0)",
+    if seed == "auto":  
+        grass.mapcalc("$tmp_map = rand(${minval},${maxval})", 
+            seed='auto',
+            minval = minval, 
+            maxval = maxval,
+            tmp_map = tmp_map)
+    else:        
+        grass.mapcalc("$tmp_map = rand(${minval},${maxval})", 
+            seed=1,
+            minval = minval, 
+            maxval = maxval,
+            tmp_map = tmp_map)
+
+    grass.mapcalc("${outmap} = if($tmp_map <= ${weight},1,0)",
         weight = weight,
         outmap = outmap,
         tmp_map = tmp_map)
-    
+        
+    if not subsample == '': 
+        grass.run_command('r.random',
+            input = outmap,
+            n = subsample,
+            raster_output = tmp_map2)
+        grass.run_command('r.null',
+            map = tmp_map2, 
+            null = 0)
+        grass.mapcalc("${outmap} = if(${outmap}>=0,${tmp_map2},null())",
+            overwrite = True,
+            outmap = outmap,
+            tmp_map2 = tmp_map2)
+        grass.run_command('g.remove', rast=tmp_map2)
+
     print("Ready, name of raster created is " + outmap)
+    print("computed over value range " + minval + " - " + maxval)
     
 if __name__ == "__main__":
     options, flags = grass.parser()

Added: grass-addons/grass7/raster/r.random.weight/r.random.weight_legacy.py
===================================================================
--- grass-addons/grass7/raster/r.random.weight/r.random.weight_legacy.py	                        (rev 0)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight_legacy.py	2014-07-26 08:51:29 UTC (rev 61414)
@@ -0,0 +1,134 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+##############################################################################
+#
+# MODULE:       r.rand.weight
+# AUTHOR(S):    paulo van Breugel <paulo at ecodiv.org>         
+# PURPOSE:      Create a layer with weighted random sample
+# COPYRIGHT: (C) 2014 Paulo van Breugel
+#            http://ecodiv.org
+#            http://pvanb.wordpress.com/
+#
+#        This program is free software under the GNU General Public
+#        License (>=v2). Read the file COPYING that comes with GRASS
+#        for details.
+##############################################################################
+
+#%module
+#% description: Weighted sample
+#% keywords: raster, sample
+#%end
+
+#%option
+#% key: weights
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: layer with weight
+#% key_desc: weight
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: output layer
+#% key_desc: output
+#% required: yes
+#% multiple: no
+#%end
+
+#%option
+#% key: start
+#% type: double
+#% description: minimum weight
+#% key_desc: start
+#% answer: 0
+#% required: yes
+#%end
+
+#%option
+#% key: end
+#% type: double
+#% description: maximum weight
+#% key_desc: end
+#% answer: 100
+#% required: yes
+#%end
+
+#%option
+#% key: subsample
+#% type: string
+#% description: subsample - not implemented yet
+#% key_desc: ss
+#% answer: 0
+#% required: no
+#%end
+
+#%option
+#% key: seed
+#% type: double
+#% description: set seed for random number generation
+#% key_desc: seed
+#% required: no
+#%end
+
+# import libraries
+import os
+import sys
+import atexit
+import time
+import grass.script as grass
+
+# Runs modules silently
+# os.environ['GRASS_VERBOSE']='-1' 
+
+def cleanup():
+	grass.run_command('g.remove', 
+      rast = tmp_map, quiet = True)
+
+# main function
+def main():
+    global tmp_map
+    
+    # check if GISBASE is set
+    if "GISBASE" not in os.environ:
+    # return an error advice
+       grass.fatal(_("You must be in GRASS GIS to run this program"))
+       
+    # input raster map and parameters   
+    minval = options['start']
+    maxval = options['end']
+    weight = options['weights']
+    outmap = options['output']
+    subsample = options['subsample']
+    seed = options['seed']
+    
+    # setup temporary files and seed
+    tmp_map = 'r_w_rand_987654321'
+    if seed == "":
+        ticks = str(int(time.time()*1000)),
+        print("seed used is: " + str(ticks[0]))
+        os.environ['GRASS_RND_SEED'] = ticks[0]
+    else:
+        print("Seed used for random number generation is: " + str(seed))
+        
+    grass.mapcalc("$tmp_map = rand(${minval},${maxval})", 
+        minval = minval, 
+        maxval = maxval,
+        tmp_map = tmp_map)
+   
+    grass.mapcalc("${outmap} = if($tmp_map < ${weight},1,0)",
+        weight = weight,
+        outmap = outmap,
+        tmp_map = tmp_map)
+    
+    print("Ready, name of raster created is " + outmap)
+    
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    sys.exit(main())
+


Property changes on: grass-addons/grass7/raster/r.random.weight/r.random.weight_legacy.py
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list