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

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 4 05:46:09 PDT 2014


Author: pvanbosgeo
Date: 2014-07-04 05:46:09 -0700 (Fri, 04 Jul 2014)
New Revision: 61154

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 the option to set a seed for random number generation. By default the seed is set to depend on the time (no reproducibility), using time.time() function

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-04 11:23:59 UTC (rev 61153)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.html	2014-07-04 12:46:09 UTC (rev 61154)
@@ -2,6 +2,8 @@
 <h2>DESCRIPTION</h2>
 
 <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.
  
 <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)

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-04 11:23:59 UTC (rev 61153)
+++ grass-addons/grass7/raster/r.random.weight/r.random.weight.py	2014-07-04 12:46:09 UTC (rev 61154)
@@ -67,12 +67,23 @@
 #% 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', 
@@ -93,10 +104,17 @@
     weight = options['weights']
     outmap = options['output']
     subsample = options['subsample']
+    seed = options['seed']
     
-    # setup temporary files
+    # 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,
@@ -107,6 +125,8 @@
         outmap = outmap,
         tmp_map = tmp_map)
     
+    print("Ready, name of raster created is " + outmap)
+    
 if __name__ == "__main__":
     options, flags = grass.parser()
     atexit.register(cleanup)



More information about the grass-commit mailing list