Skip to content

Commit

Permalink
Update r.random.weight.py (#725)
Browse files Browse the repository at this point in the history
Bug fixes related to new requirement of r.random to set seed or -s flag.
Small format improvements.
  • Loading branch information
ecodiv committed Apr 1, 2022
1 parent 2a33209 commit 65e9a76
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions src/raster/r.random.weight/r.random.weight.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env python


##############################################################################
#
# MODULE: r.random.weight
# AUTHOR(S): paulo van Breugel <paulo ecodiv earth>
# PURPOSE: Create a layer with weighted random sample
# COPYRIGHT: (C) 2014-2019 Paulo van Breugel and GRASS DEVELOPMENT TEAM
# http://ecodiv.earth
# COPYRIGHT: (C) 2014-2022 Paulo van Breugel and GRASS DEVELOPMENT TEAM
# https://ecodiv.earth
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
Expand Down Expand Up @@ -67,13 +66,22 @@

# %option
# % key: seed
# % type: string
# % type: integer
# % description: set seed for random number generation
# % answer: auto
# % required: no
# % guisection: Sample options
# %end

# %flag
# % key: s
# % description: Generate random seed (result is non-deterministic)
# % guisection: Sample options
# %end

# %rules
# %exclusive: -s,seed
# %end

# %flag
# % key: n
# % description: set non-selected values to 0 (default to NULL)
Expand Down Expand Up @@ -103,7 +111,7 @@ def tmpname(name):
Store the name in the global list.
Use only for raster maps.
"""
tmpf = name + "_" + str(uuid.uuid4())
tmpf = "{}_{}".format(name, str(uuid.uuid4()))
tmpf = tmpf.replace("-", "_")
CLEAN_RAST.append(tmpf)
return tmpf
Expand All @@ -124,6 +132,7 @@ def main(options, flags):
subsample = options["subsample"]
seed = options["seed"]
flag_n = flags["n"]
flag_seed = flags["s"]

# Compute minimum and maximum value raster
minmax = gs.parse_command("r.univar", map=weight, flags="g", quiet=True)
Expand All @@ -135,23 +144,19 @@ def main(options, flags):
maxval = minmax["max"]
if minval > minmax["min"] or maxval < minmax["max"]:
ms = (
"\nYou defined the minimum and maximum weights\nas "
+ minval
+ " and "
+ maxval
+ " respectively. Note that the\nvalue range of weight raster is "
+ minmax["min"]
+ " - "
+ minmax["max"]
+ ".\nContinuing...\n\n"
"\nYou defined the minimum and maximum weights\nas {} and {} "
"respectively. Note that the\nvalue range of weight raster is"
" {} - {}.\nContinuing ...\n\n".format(
minval, maxval, minmax["min"], minmax["max"]
)
)
gs.message(ms)

# setup temporary files and seed
tmp_map1 = tmpname("r_w_rand1")
tmp_map2 = tmpname("r_w_rand2")

if seed == "auto":
if flag_seed:
gs.mapcalc(
"$tmp_map1 = rand(float(${minval}),float(${maxval}))",
seed="auto",
Expand Down Expand Up @@ -190,9 +195,24 @@ def main(options, flags):
gs.run_command("g.copy", raster=[tmp_map2, outmap], quiet=True)
else:
gs.run_command("r.null", map=tmp_map2, setnull=0, quiet=True)
gs.run_command(
"r.random", input=tmp_map2, n=subsample, raster=outmap, quiet=True
)
if flag_seed:
gs.run_command(
"r.random",
input=tmp_map2,
n=subsample,
raster=outmap,
flags="s",
quiet=True,
)
else:
gs.run_command(
"r.random",
input=tmp_map2,
n=subsample,
raster=outmap,
seed=seed,
quiet=True,
)
if flag_n:
gs.run_command("r.null", map=outmap, null=0, quiet=True)

Expand All @@ -201,11 +221,15 @@ def main(options, flags):
nflag = "\n\t-n"
else:
nflag = ""
if flag_seed:
seednumber = "random"
else:
seednumber = seed
desctxt = (
"\n\nr.random.weight \n weight={} \n output={}"
" start={} \n end={} \n subsample={}"
"\n seed={}{}\n"
).format(weight, outmap, minval, maxval, subsample, seed, nflag)
).format(weight, outmap, minval, maxval, subsample, seednumber, nflag)
if flag_n:
bso = "selected: 1/0"
else:
Expand All @@ -223,7 +247,7 @@ def main(options, flags):

gs.message("\n")
gs.message("Ready!")
gs.message("The name of the output raster is " + outmap + "\n")
gs.message("The name of the output raster is {}\n".format(outmap))
gs.message("\n")


Expand Down

0 comments on commit 65e9a76

Please sign in to comment.