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

Fix geocentric_resolution compatibility with numpy 2.1.0 #614

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
- [ ] Closes #xxxx <!-- remove if there is no corresponding issue, which should only be the case for minor changes -->
- [ ] Tests added <!-- for all bug fixes or enhancements -->
- [ ] Tests passed <!-- for all non-documentation changes -->
- [ ] Passes ``git diff origin/main **/*py | flake8 --diff`` <!-- remove if you did not edit any Python files -->
- [ ] Fully documented <!-- remove if this change should not be visible to users, e.g., if it is an internal clean-up, or if this is part of a larger project that will be documented later -->
13 changes: 11 additions & 2 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,15 @@
edge averages.

"""
def _safe_bin_edges(arr):
try:
return np.histogram_bin_edges(arr, bins=10)[:2]
except ValueError:

Check warning on line 2702 in pyresample/geometry.py

View check run for this annotation

Codecov / codecov/patch

pyresample/geometry.py#L2702

Added line #L2702 was not covered by tests
# numpy 2.1.0+ produces a ValueError if it can't fill
# all bins due to a small data range
# we just arbitrarily use the first 2 elements as all elements
# should be within floating point precision for our use case
return arr[:2]

Check warning on line 2707 in pyresample/geometry.py

View check run for this annotation

Codecov / codecov/patch

pyresample/geometry.py#L2707

Added line #L2707 was not covered by tests
from pyproj.transformer import Transformer
rows, cols = self.shape
mid_row = rows // 2
Expand Down Expand Up @@ -2726,9 +2735,9 @@
# Very useful near edge of disk geostationary areas.
hor_res = vert_res = 0
if hor_dist.size:
hor_res = np.mean(np.histogram_bin_edges(hor_dist)[:2])
hor_res = np.mean(_safe_bin_edges(hor_dist))
if vert_dist.size:
vert_res = np.mean(np.histogram_bin_edges(vert_dist)[:2])
vert_res = np.mean(_safe_bin_edges(vert_dist))
# Use the maximum distance between the two midlines instead of
# binning both of them together. If we binned them together then
# we are highly dependent on the shape of the area (more rows in
Expand Down
15 changes: 15 additions & 0 deletions pyresample/test/test_geometry/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,21 @@ def test_area_def_geocentric_resolution(self, create_test_area):
geo_res = area_def.geocentric_resolution()
np.testing.assert_allclose(298.647232, geo_res, atol=1e-1)

def test_area_def_geocentric_resolution_close_dist(self, create_test_area):
"""Test geocentric resolution when distance range isn't big enough for histogram bins.

The method currently uses `np.histogram_bin_edges`. Starting in numpy
2.1.0, if the number of bins requested (10 in this default case) can't
be created because the range of the data is too small, it will raise an
exception. This test makes sure that geocentric_resolution doesn't
error out when this case is encountered.

"""
# this area is known to produce horizontal distances of ~999.9999989758
# and trigger the error in numpy 2.1.0
ar = create_test_area(4087, 5, 5, (-2500.0, -2500.0, 2500.0, 2500.0))
np.testing.assert_allclose(ar.geocentric_resolution(), 999.999999, atol=1e-2)

def test_area_def_geocentric_resolution_latlong(self, create_test_area):
"""Test the AreaDefinition.geocentric_resolution method on a latlong projection."""
area_extent = (-110.0, 45.0, -95.0, 55.0)
Expand Down
Loading