Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] RasterRow / Buffer boolean comparison fails (AttributeError: module 'numpy' has no attribute 'bool') #2948

Closed
torsti opened this issue May 5, 2023 · 0 comments · Fixed by #2949
Labels
bug Something isn't working Python Related code is in Python

Comments

@torsti
Copy link

torsti commented May 5, 2023

Describe the bug

Boolean comparisons and filtering of raster rows using eg. RasterRow fail (probably) due to use of deprecated numpy.bool type in buffer.py.

An example error message:

/usr/lib/grass82/etc/python/grass/pygrass/raster/buffer.py:51: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar.
  if out_arr.dtype == np.bool:
Traceback (most recent call last):
  File "/tmp/grassbug/test_script.py", line 19, in <module>
    print(row > 0)
          ^^^^^^^
  File "/usr/lib/grass82/etc/python/grass/pygrass/raster/buffer.py", line 51, in __array_wrap__
    if out_arr.dtype == np.bool:
                        ^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 305, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'bool_'?

To Reproduce

#!/usr/bin/python3

import numpy as np

import grass.script as gscript
from grass.pygrass import raster

# create dummy raster
gscript.run_command("g.region", res = 1, s = 0, w = 0, e = 5, n = 5)
gscript.mapcalc("dummy = 1")

# read row from raster
with raster.RasterRow("dummy", mode = "r") as rast:
    row = rast[0]
    arr = np.asarray(row)

    print(row)
    print(arr > 0)
    print(row > 0)

Test with eg. grass --tmp-location XY --exec test_script.py

Expected behavior

print(row > 0) and all other boolean operations should work, e.g in this case return the same as print(arr > 0).

System description (please complete the following information):

  • Operating System: Debian 12 (bookworm)
  • numpy: 1.24.2
  • python: 3.11.2
  • GRASS GIS version: 8.2.1 (debian.org package)
version=8.2.1
date=2023
revision=exported
build_date=2023-01-22
build_platform=x86_64-pc-linux-gnu
build_off_t_size=8
libgis_revision=8.2.1
libgis_date=2023-01-22T07:33:16+00:00
proj=9.1.1
gdal=3.6.2
geos=3.11.1
sqlite=3.40.1
@torsti torsti added the bug Something isn't working label May 5, 2023
@veroandreo veroandreo added the Python Related code is in Python label May 5, 2023
@petrasovaa petrasovaa linked a pull request May 5, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Python Related code is in Python
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants