From 75f815638e8a8a7d7c8070bf12fd559d7276bd25 Mon Sep 17 00:00:00 2001 From: Santiago Soler Date: Wed, 31 Jul 2024 08:21:03 -0300 Subject: [PATCH] Fix examples and tests to pass with Numpy 2 (#459) 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. --- verde/coordinates.py | 3 ++- verde/projections.py | 3 ++- verde/tests/test_coordinates.py | 31 +++++++++++++------------------ verde/utils.py | 16 +++++++++++----- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/verde/coordinates.py b/verde/coordinates.py index 9ca49955a..21acd08a2 100644 --- a/verde/coordinates.py +++ b/verde/coordinates.py @@ -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) """ diff --git a/verde/projections.py b/verde/projections.py index 3aad65bd3..e0715c3b8 100644 --- a/verde/projections.py +++ b/verde/projections.py @@ -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) """ diff --git a/verde/tests/test_coordinates.py b/verde/tests/test_coordinates.py index f7d7a7ef0..c2608064a 100644 --- a/verde/tests/test_coordinates.py +++ b/verde/tests/test_coordinates.py @@ -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(): diff --git a/verde/utils.py b/verde/utils.py index a12748d0d..0d7ee6c60 100644 --- a/verde/utils.py +++ b/verde/utils.py @@ -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 """