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 WCSAxes sometimes emitting RuntimeWarning with NumPy 1.24 #14211

Merged
merged 3 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions astropy/visualization/wcsaxes/coordinate_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def find_coordinate_range(transform, extent, coord_types, coord_units, coord_wra
reset = np.abs(wjump) > 180.0
if np.any(reset):
wjump = wjump + np.sign(wjump) * 180.0
wjump = 360.0 * (wjump / 360.0).astype(int)
wjump = 360.0 * np.trunc(wjump / 360.0)
xw[0, 1:][reset] -= wjump[reset]

# Now iron out coordinates along all columns, starting with first row.
Expand All @@ -80,7 +80,7 @@ def find_coordinate_range(transform, extent, coord_types, coord_units, coord_wra
reset = np.abs(wjump) > 180.0
if np.any(reset):
wjump = wjump + np.sign(wjump) * 180.0
wjump = 360.0 * (wjump / 360.0).astype(int)
wjump = 360.0 * np.trunc(wjump / 360.0)
xw[1:][reset] -= wjump[reset]

with warnings.catch_warnings():
Expand Down
33 changes: 33 additions & 0 deletions astropy/visualization/wcsaxes/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,36 @@ def test_multiple_draws_grid_contours(tmp_path):
ax.grid(color="black", grid_type="contours")
fig.savefig(tmp_path / "plot.png")
fig.savefig(tmp_path / "plot.png")


def test_get_coord_range_nan_regression():
# Test to make sure there is no internal casting of NaN to integers
# NumPy 1.24 raises a RuntimeWarning if a NaN is cast to an integer

wcs = WCS(TARGET_HEADER)
wcs.wcs.crval[0] = 0 # Re-position the longitude wrap to the middle
ax = plt.subplot(1, 1, 1, projection=wcs)

# Set the Y limits within valid latitudes/declinations
ax.set_ylim(300, 500)

# Set the X limits within valid longitudes/RAs, so the world coordinates have no NaNs
ax.set_xlim(300, 700)
assert np.allclose(
ax.coords.get_coord_range(),
np.array(
[
(-123.5219272110385, 122.49684897692201),
(-44.02289164685554, 44.80732766607591),
]
),
)

# Extend the X limits to include invalid longitudes/RAs, so the world coordinates have NaNs
ax.set_xlim(0, 700)
assert np.allclose(
ax.coords.get_coord_range(),
np.array(
[(-131.3193386797236, 180.0), (-44.02289164685554, 44.80732766607591)]
),
)
1 change: 1 addition & 0 deletions docs/changes/visualization/14211.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed WCSAxes sometimes triggering a NumPy RuntimeWarning when determining the coordinate range of an axes.
pllim marked this conversation as resolved.
Show resolved Hide resolved