Skip to content

[Bug] grass.script.mapcalc: Pseudo-random number generator not seeded for raster calculator #5974

@baharmon

Description

@baharmon

Describe the bug

Running the raster calculator's with its rand() function in grass.jupyter without setting a seed throws the following error: ERROR: Pseudo-random number generator not seeded. This happens both with and without flag s which should generate a random seed. Setting a seed with the seed parameter works as expected. I think the grass.jupyter wrapper for r.mapcalc has a auto-seeding mechanism of its own introduced before flag s was implemented, but it seems to be broken and conflicts with flag s.

To reproduce

gs.mapcalc("random = rand(0.0,1.0)") throws an error.

Error message
ERROR: Pseudo-random number generator not seeded
---------------------------------------------------------------------------
CalledModuleError                         Traceback (most recent call last)
File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py:143](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py#line=142), in mapcalc(exp, quiet, superquiet, verbose, overwrite, seed, env, **kwargs)
    142 try:
--> 143     write_command(
    144         "r.mapcalc",
    145         file="-",
    146         stdin=e,
    147         env=env,
    148         seed=seed,
    149         quiet=quiet,
    150         superquiet=superquiet,
    151         verbose=verbose,
    152         overwrite=overwrite,
    153     )
    154 except CalledModuleError:

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:686](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py#line=685), in write_command(*args, **kwargs)
    685     sys.stderr.write(stderr)
--> 686 return handle_errors(returncode, None, args, kwargs)

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:372](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py#line=371), in handle_errors(returncode, result, args, kwargs)
    371 module, code = get_module_and_code(args, kwargs)
--> 372 raise CalledModuleError(module=module, code=code, returncode=returncode)

CalledModuleError: Module run `r.mapcalc file=-` ended with an error.
The subprocess ended with a non-zero return code: 1. See errors above the traceback or in the error output.

During handling of the above exception, another exception occurred:

ScriptError                               Traceback (most recent call last)
Cell In[12], line 1
----> 1 gs.mapcalc("random = rand(0.0,1.0)")

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py:155](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py#line=154), in mapcalc(exp, quiet, superquiet, verbose, overwrite, seed, env, **kwargs)
    143     write_command(
    144         "r.mapcalc",
    145         file="-",
   (...)    152         overwrite=overwrite,
    153     )
    154 except CalledModuleError:
--> 155     fatal(
    156         _("An error occurred while running r.mapcalc with expression: %s") % e,
    157         env=env,
    158     )

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:824](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py#line=823), in fatal(msg, env)
    822 global raise_on_error
    823 if raise_on_error:
--> 824     raise ScriptError(msg)
    826 error(msg, env=env)
    827 sys.exit(1)

ScriptError: An error occurred while running r.mapcalc with expression: random = rand(0.0,1.0)

gs.mapcalc("random = rand(0.0,1.0)", flags="s") throws an error.

Error message with flag s ``` ERROR: Pseudo-random number generator not seeded --------------------------------------------------------------------------- CalledModuleError Traceback (most recent call last) File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py:143](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py#line=142), in mapcalc(exp, quiet, superquiet, verbose, overwrite, seed, env, **kwargs) 142 try: --> 143 write_command( 144 "r.mapcalc", 145 file="-", 146 stdin=e, 147 env=env, 148 seed=seed, 149 quiet=quiet, 150 superquiet=superquiet, 151 verbose=verbose, 152 overwrite=overwrite, 153 ) 154 except CalledModuleError:

File /Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:686, in write_command(*args, **kwargs)
685 sys.stderr.write(stderr)
--> 686 return handle_errors(returncode, None, args, kwargs)


File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:372](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py#line=371), in handle_errors(returncode, result, args, kwargs)
    371 module, code = get_module_and_code(args, kwargs)
--> 372 raise CalledModuleError(module=module, code=code, returncode=returncode)

CalledModuleError: Module run `r.mapcalc file=-` ended with an error.
The subprocess ended with a non-zero return code: 1. See errors above the traceback or in the error output.

During handling of the above exception, another exception occurred:

ScriptError                               Traceback (most recent call last)
Cell In[10], line 1
----> 1 gs.mapcalc("random = rand(0.0,1.0)", flags="s")

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py:155](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/raster.py#line=154), in mapcalc(exp, quiet, superquiet, verbose, overwrite, seed, env, **kwargs)
    143     write_command(
    144         "r.mapcalc",
    145         file="-",
   (...)    152         overwrite=overwrite,
    153     )
    154 except CalledModuleError:
--> 155     fatal(
    156         _("An error occurred while running r.mapcalc with expression: %s") % e,
    157         env=env,
    158     )

File [/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py:824](http://localhost:8888/Applications/GRASS-8.5.app/Contents/Resources/etc/python/grass/script/core.py#line=823), in fatal(msg, env)
    822 global raise_on_error
    823 if raise_on_error:
--> 824     raise ScriptError(msg)
    826 error(msg, env=env)
    827 sys.exit(1)

ScriptError: An error occurred while running r.mapcalc with expression: random = rand(0.0,1.0)

gs.mapcalc("random = rand(0.0,1.0)", seed=12345) works as expected.

Expected behavior

I expect the rand() function for the raster calculator in grass.jupyter to have a random seed successfully generated by default. Flag s should still work even if it is redundant.

System description

  • GRASS GIS version: 8.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    PythonRelated code is in PythonbugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions