Skip to content

Commit

Permalink
Remove any use of legacy NumPy random number generator (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet authored May 6, 2024
1 parent 9f89428 commit 414dc5c
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 107 deletions.
6 changes: 3 additions & 3 deletions examples/analysis/point_extraction/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import numpy as np

# Replace by Raster function once done (valid coords)
np.random.seed(42)
x_coords = np.random.uniform(rast.bounds.left + 50, rast.bounds.right - 50, 50)
y_coords = np.random.uniform(rast.bounds.bottom + 50, rast.bounds.top - 50, 50)
rng = np.random.default_rng(42)
x_coords = rng.uniform(rast.bounds.left + 50, rast.bounds.right - 50, 50)
y_coords = rng.uniform(rast.bounds.bottom + 50, rast.bounds.top - 50, 50)

vals = rast.interp_points(points=list(zip(x_coords, y_coords)))

Expand Down
6 changes: 3 additions & 3 deletions examples/analysis/point_extraction/reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import numpy as np

# Replace by Raster function once done
np.random.seed(42)
x_coords = np.random.uniform(rast.bounds.left + 50, rast.bounds.right - 50, 50)
y_coords = np.random.uniform(rast.bounds.bottom + 50, rast.bounds.top - 50, 50)
rng = np.random.default_rng(42)
x_coords = rng.uniform(rast.bounds.left + 50, rast.bounds.right - 50, 50)
y_coords = rng.uniform(rast.bounds.bottom + 50, rast.bounds.top - 50, 50)

vals = rast.value_at_coords(x=x_coords, y=y_coords)

Expand Down
6 changes: 3 additions & 3 deletions examples/io/import_export/from_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import geoutils as gu

# A random 3 x 3 masked array
np.random.seed(42)
arr = np.random.normal(size=(5, 5))
rng = np.random.default_rng(42)
arr = rng.normal(size=(5, 5))
# Introduce a NaN value
arr[2, 2] = np.nan
# A transform with 3 x 3 pixels in a [0-1, 0-1] bound square
Expand All @@ -39,7 +39,7 @@
# We could also have created directly from a :class:`~numpy.ma.MaskedArray`.

# A random mask, that will mask one out of two values on average
mask = np.random.randint(0, 2, size=(5, 5), dtype="bool")
mask = rng.integers(0, 2, size=(5, 5), dtype="bool")
ma = np.ma.masked_array(data=arr, mask=mask)

# This time, we pass directly the masked array
Expand Down
16 changes: 8 additions & 8 deletions geoutils/raster/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3781,7 +3781,7 @@ def to_pointcloud(
subsample: float | int = 1,
*,
as_array: Literal[False] = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
force_pixel_offset: Literal["center", "ul", "ur", "ll", "lr"] = "ul",
) -> NDArrayNum:
...
Expand All @@ -3796,7 +3796,7 @@ def to_pointcloud(
subsample: float | int = 1,
*,
as_array: Literal[True],
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
force_pixel_offset: Literal["center", "ul", "ur", "ll", "lr"] = "ul",
) -> Vector:
...
Expand All @@ -3811,7 +3811,7 @@ def to_pointcloud(
subsample: float | int = 1,
*,
as_array: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
force_pixel_offset: Literal["center", "ul", "ur", "ll", "lr"] = "ul",
) -> NDArrayNum | Vector:
...
Expand All @@ -3824,7 +3824,7 @@ def to_pointcloud(
auxiliary_column_names: list[str] | None = None,
subsample: float | int = 1,
as_array: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
force_pixel_offset: Literal["center", "ul", "ur", "ll", "lr"] = "ul",
) -> NDArrayNum | Vector:
"""
Expand Down Expand Up @@ -4106,7 +4106,7 @@ def subsample(
subsample: int | float,
return_indices: Literal[False] = False,
*,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum:
...

Expand All @@ -4116,7 +4116,7 @@ def subsample(
subsample: int | float,
return_indices: Literal[True],
*,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> tuple[NDArrayNum, ...]:
...

Expand All @@ -4125,15 +4125,15 @@ def subsample(
self,
subsample: float | int,
return_indices: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum | tuple[NDArrayNum, ...]:
...

def subsample(
self,
subsample: float | int,
return_indices: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum | tuple[NDArrayNum, ...]:
"""
Randomly sample the raster. Only valid values are considered.
Expand Down
17 changes: 6 additions & 11 deletions geoutils/raster/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def subsample_array(
subsample: float | int,
return_indices: Literal[False] = False,
*,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum:
...

Expand All @@ -27,7 +27,7 @@ def subsample_array(
subsample: float | int,
return_indices: Literal[True],
*,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> tuple[NDArrayNum, ...]:
...

Expand All @@ -37,7 +37,7 @@ def subsample_array(
array: NDArrayNum | MArrayNum,
subsample: float | int,
return_indices: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum | tuple[NDArrayNum, ...]:
...

Expand All @@ -46,7 +46,7 @@ def subsample_array(
array: NDArrayNum | MArrayNum,
subsample: float | int,
return_indices: bool = False,
random_state: np.random.RandomState | int | None = None,
random_state: int | np.random.Generator | None = None,
) -> NDArrayNum | tuple[NDArrayNum, ...]:
"""
Randomly subsample a 1D or 2D array by a sampling factor, taking only non NaN/masked values.
Expand All @@ -60,12 +60,7 @@ def subsample_array(
:returns: The subsampled array (1D) or the indices to extract (same shape as input array)
"""
# Define state for random sampling (to fix results during testing)
if random_state is None:
rnd: np.random.RandomState | np.random.Generator = np.random.default_rng()
elif isinstance(random_state, np.random.RandomState):
rnd = random_state
else:
rnd = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(random_state)))
rng = np.random.default_rng(random_state)

# Remove invalid values and flatten array
mask = get_mask_from_array(array) # -> need to remove .squeeze in get_mask
Expand All @@ -92,7 +87,7 @@ def subsample_array(
npoints = np.size(valids)

# Randomly extract npoints without replacement
indices = rnd.choice(valids, npoints, replace=False)
indices = rng.choice(valids, npoints, replace=False)
unraveled_indices = np.unravel_index(indices, array.shape)

if return_indices:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ def test_get_xy_rotated(self) -> None:
"""Check the function to rotate array."""

# Create an artificial raster
rng = np.random.default_rng(42)
width = height = 5
transform = rio.transform.from_bounds(0, 0, 1, 1, width, height)
r1 = gu.Raster.from_array(
np.random.randint(1, 255, (height, width), dtype="uint8"), transform=transform, crs=None
)
r1 = gu.Raster.from_array(rng.integers(1, 255, (height, width), dtype="uint8"), transform=transform, crs=None)

# First, we get initial coords
xx, yy = r1.coords(grid=True, force_offset="ll")
Expand Down
5 changes: 3 additions & 2 deletions tests/test_projtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ def test_latlon_reproject(self, example: str) -> None:

# Test on random points
nsample = 100
randx = np.random.randint(low=img.bounds.left, high=img.bounds.right, size=(nsample,))
randy = np.random.randint(low=img.bounds.bottom, high=img.bounds.top, size=(nsample,))
rng = np.random.default_rng(42)
randx = rng.integers(low=img.bounds.left, high=img.bounds.right, size=(nsample,))
randy = rng.integers(low=img.bounds.bottom, high=img.bounds.top, size=(nsample,))

lat, lon = pt.reproject_to_latlon([list(randx), list(randy)], img.crs)
x, y = pt.reproject_from_latlon([lat, lon], img.crs)
Expand Down
Loading

0 comments on commit 414dc5c

Please sign in to comment.