Skip to content

Commit

Permalink
Fix examples and tests to pass with Numpy 2 (#459)
Browse files Browse the repository at this point in the history
Cast Numpy floats and ints to Python floats so doctests can pass using
both Numpy <2 and >=2. Update test that was checking floats with regex.
  • Loading branch information
santisoler committed Jul 31, 2024
1 parent 8d1c58e commit 75f8156
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
3 changes: 2 additions & 1 deletion verde/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def get_region(coordinates):
--------
>>> coords = grid_coordinates((0, 1, -10, -6), shape=(10, 10))
>>> print(get_region(coords))
>>> region = get_region(coords)
>>> print(tuple(float(c) for c in region))
(0.0, 1.0, -10.0, -6.0)
"""
Expand Down
3 changes: 2 additions & 1 deletion verde/projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def project_region(region, projection):
>>> def projection(x, y):
... return (2*x, -1*y)
>>> project_region((3, 5, -9, -4), projection)
>>> region_proj = project_region((3, 5, -9, -4), projection)
>>> print(tuple(float(c) for c in region_proj))
(6.0, 10.0, 4.0, 9.0)
"""
Expand Down
31 changes: 13 additions & 18 deletions verde/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,23 @@ def test_rolling_window_no_shape_or_spacing():
rolling_window(coords, size=2)


def test_rolling_window_oversized_window():
@pytest.mark.parametrize(
"region",
[
(-5, -1, 6, 20), # window larger than west-east
(-20, -1, 6, 10), # window larger than south-north
(-5, -1, 6, 10), # window larger than both dims
],
)
def test_rolling_window_oversized_window(region):
"""
Check if error is raised if size larger than region is passed
"""
oversize = 5
regions = [
(-5, -1, 6, 20), # window larger than west-east
(-20, -1, 6, 10), # window larger than south-north
(-5, -1, 6, 10), # window larger than both dims
]
for region in regions:
coords = grid_coordinates(region, spacing=1)
# The expected error message with regex
# (the long expression intends to capture floats and ints)
float_regex = r"[+-]?([0-9]*[.])?[0-9]+"
err_msg = (
r"Window size '{}' is larger ".format(float_regex)
+ r"than dimensions of the region "
+ r"'\({0}, {0}, {0}, {0}\)'.".format(float_regex)
)
with pytest.raises(ValueError, match=err_msg):
rolling_window(coords, size=oversize, spacing=2)
coords = grid_coordinates(region, spacing=1)
err_msg = f"Window size '{oversize}' is larger than dimensions of the region "
with pytest.raises(ValueError, match=err_msg):
rolling_window(coords, size=oversize, spacing=2)


def test_spacing_to_size():
Expand Down
16 changes: 11 additions & 5 deletions verde/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,24 @@ def maxabs(*args, nan=True):
Examples
--------
>>> maxabs((1, -10, 25, 2, 3))
25
>>> maxabs((1, -10.5, 25, 2), (0.1, 100, -500), (-200, -300, -0.1, -499))
>>> result = maxabs((1, -10, 25, 2, 3))
>>> float(result)
25.0
>>> result = maxabs(
... (1, -10.5, 25, 2), (0.1, 100, -500), (-200, -300, -0.1, -499)
... )
>>> float(result)
500.0
If the array contains NaNs, we'll use the ``nan`` version of of the numpy
functions by default. You can turn this off through the *nan* argument.
>>> import numpy as np
>>> maxabs((1, -10, 25, 2, 3, np.nan))
>>> result = maxabs((1, -10, 25, 2, 3, np.nan))
>>> float(result)
25.0
>>> maxabs((1, -10, 25, 2, 3, np.nan), nan=False)
>>> result = maxabs((1, -10, 25, 2, 3, np.nan), nan=False)
>>> float(result)
nan
"""
Expand Down

0 comments on commit 75f8156

Please sign in to comment.