Skip to content

Commit

Permalink
style: fix numpy-deprecation-rules (NPY) (OSGeo#4062)
Browse files Browse the repository at this point in the history
Fix the last cases of ruff rules:

* NPY001

* NPY002
  • Loading branch information
ninsbl authored Jul 16, 2024
1 parent f3681c5 commit f0497c5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gui/wxpython/wxplot/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def OnStats(self, event):
statstr = "Profile of %s\n\n" % rast

iterable = (i[1] for i in self.raster[r]["datalist"])
a = np.fromiter(iterable, np.float)
a = np.fromiter(iterable, float)

statstr += "n: %f\n" % a.size
statstr += "minimum: %f\n" % np.amin(a)
Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ ignore = [
"gui/wxpython/web_services/dialogs.py" = ["INT003"]
"gui/wxpython/web_services/widgets.py" = ["INT003"]
"gui/wxpython/wxgui.py" = ["INT002"]
"gui/wxpython/wxplot/profile.py" = ["NPY001"]
"lib/imagery/testsuite/test_imagery_sigsetfile.py" = ["FURB152"]
"lib/init/grass.py" = ["INT003"]
"python/grass/__init__.py" = ["PYI056"]
Expand All @@ -365,14 +364,11 @@ ignore = [
"python/grass/jupyter/tests/reprojection_renderer_test.py" = ["PT013"]
"python/grass/jupyter/testsuite/interactivemap_test.py" = ["PGH004"]
"python/grass/jupyter/testsuite/map_test.py" = ["PGH004"]
"python/grass/pygrass/raster/__init__.py" = ["NPY002"]
"python/grass/pygrass/raster/category.py" = ["INT002"]
"python/grass/pygrass/raster/testsuite/test_numpy.py" = ["NPY002"]
"python/grass/pygrass/vector/__init__.py" = ["INT003"]
"python/grass/pygrass/vector/geometry.py" = ["PYI024"]
"python/grass/pygrass/vector/sql.py" = ["FLY002"]
"python/grass/pygrass/vector/testsuite/test_table.py" = ["NPY002", "PLW0108"]
"python/grass/pygrass/vector/testsuite/test_vector3d.py" = ["NPY002"]
"python/grass/pygrass/vector/testsuite/test_table.py" = ["PLW0108"]
"python/grass/script/raster.py" = ["INT003"]
"python/grass/temporal/abstract_space_time_dataset.py" = ["INT003"]
"python/grass/temporal/aggregation.py" = ["INT003"]
Expand Down
16 changes: 4 additions & 12 deletions python/grass/pygrass/raster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,11 @@ def close(self, rm_temp_files=True):
def random_map_only_columns(mapname, mtype, overwrite=True, factor=100):
region = Region()
random_map = RasterRow(mapname)
rng = np.random.default_rng()
row_buf = Buffer(
(region.cols,),
mtype,
buffer=(
np.random.random(
region.cols,
)
* factor
).data,
buffer=(rng.random(region.cols) * factor).data,
)
random_map.open("w", mtype, overwrite)
for _ in range(region.rows):
Expand All @@ -601,16 +597,12 @@ def random_map(mapname, mtype, overwrite=True, factor=100):
region = Region()
random_map = RasterRow(mapname)
random_map.open("w", mtype, overwrite)
rng = np.random.default_rng()
for _ in range(region.rows):
row_buf = Buffer(
(region.cols,),
mtype,
buffer=(
np.random.random(
region.cols,
)
* factor
).data,
buffer=(rng.random(region.cols) * factor).data,
)
random_map.put_row(row_buf)
random_map.close()
Expand Down
6 changes: 3 additions & 3 deletions python/grass/pygrass/raster/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from numpy.random import random
from numpy.random import default_rng
from grass.pygrass.raster import raster2numpy, numpy2raster, RasterRow


Expand Down Expand Up @@ -49,8 +49,8 @@ def test_len(self):
self.assertTrue(len(self.numpy_obj[0]), 60)

def test_write(self):
ran = random([40, 60])
numpy2raster(ran, "FCELL", self.name, True)
rng = default_rng()
numpy2raster(rng.random([40, 60]), "FCELL", self.name, True)
self.assertTrue(check_raster(self.name))


Expand Down
7 changes: 4 additions & 3 deletions python/grass/pygrass/vector/testsuite/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@


# dictionary that generate random data
RNG = np.random.default_rng()
COL2VALS = {
"INT": lambda n: np.random.randint(9, size=n),
"INTEGER": lambda n: np.random.randint(9, size=n),
"INT": lambda n: RNG.integers(low=0, high=9, size=n),
"INTEGER": lambda n: RNG.integers(low=0, high=9, size=n),
"INTEGER PRIMARY KEY": lambda n: np.arange(1, n + 1, dtype=int),
"REAL": lambda n: np.random.rand(n),
"REAL": lambda n: RNG.random(n),
"TEXT": lambda n: np.array([randstr() for _ in range(n)]),
}

Expand Down
7 changes: 4 additions & 3 deletions python/grass/pygrass/vector/testsuite/test_vector3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

def generate_coordinates(number, bbox=None, with_z=False):
"""Return 2 or 3 random arrays of coordinates"""
rng = np.random.default_rng()
bbox = Region() if bbox is None else bbox
x = bbox.south + (bbox.north - bbox.south) * np.random.random(number)
y = bbox.west + (bbox.east - bbox.west) * np.random.random(number)
x = bbox.south + (bbox.north - bbox.south) * rng.random(number)
y = bbox.west + (bbox.east - bbox.west) * rng.random(number)
if with_z:
z = np.random.random(number) * 1000
z = rng.random(number) * 1000
return x, y, z
return x, y

Expand Down

0 comments on commit f0497c5

Please sign in to comment.