Skip to content

Commit

Permalink
Manual backport of astropy#14211 to v5.0.x
Browse files Browse the repository at this point in the history
Merge pull request astropy#14211 from ayshih/trunc

Fix WCSAxes sometimes emitting RuntimeWarning with NumPy 1.24

(cherry picked from commit 9ee4a46)
  • Loading branch information
pllim committed Dec 27, 2022
1 parent c253c74 commit 9d6a757
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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 @@ -548,3 +548,36 @@ def test_multiple_draws_grid_contours(tmpdir):
ax.grid(color="black", grid_type="contours")
fig.savefig(tmpdir / "plot.png")
fig.savefig(tmpdir / "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 the axes.

0 comments on commit 9d6a757

Please sign in to comment.