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

Backport PR #7206 on branch 5.0 (Add a check for out-of-bounds sampling by sunpy.map.sample_at_coords()) #7225

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7206.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug with :func:`~sunpy.map.sample_at_coords()` where sampling outside the bounds of the map would sometimes not error and instead return strange pixel values.
11 changes: 11 additions & 0 deletions sunpy/map/maputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
Uses nearest-neighbor interpolation of coordinates in map, as
it effectively uses array indexing.

An error is raised if any of the coordinates fall outside the map bounds.

Parameters
----------
smap : `~sunpy.map.GenericMap`
Expand All @@ -156,6 +158,9 @@
--------
.. minigallery:: sunpy.map.sample_at_coords
"""
if not all(contains_coordinate(smap, coordinates)):
raise ValueError('At least one coordinate is not within the bounds of the map.')

Check warning on line 162 in sunpy/map/maputils.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/maputils.py#L161-L162

Added lines #L161 - L162 were not covered by tests

return u.Quantity(smap.data[smap.wcs.world_to_array_index(coordinates)], smap.unit)


Expand Down Expand Up @@ -581,6 +586,12 @@
If a pixel intersects the coordinate path at only its corner, it may not be
returned due to the limitations of floating-point comparisons.

If part of the coordinate path lies outside of the bounds of the map, this
function will still return pixel coordinates that are consistent with the WCS of
the map, but attempting to obtain the map data at these pixel coordinates
(e.g., using :func:`~sunpy.map.sample_at_coords`) will raise an error, as these
pixels are not "real" and have no corresponding data.

Returns
-------
`~astropy.coordinates.SkyCoord`
Expand Down
6 changes: 6 additions & 0 deletions sunpy/map/tests/test_maputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ def test_data_at_coordinates(aia171_test_map, aia_test_arc):
assert_quantity_allclose(data[-1], intensity_along_arc[-1])


def test_sample_out_of_bounds(aia171_test_map):
point = aia171_test_map.pixel_to_world([-1, 1]*u.pix, [-1, 1]*u.pix)
with pytest.raises(ValueError, match='At least one coordinate is not within the bounds of the map.'):
sample_at_coords(aia171_test_map, point)


def test_contains_solar_center(aia171_test_map, all_off_disk_map, all_on_disk_map, straddles_limb_map, sub_smap):
assert contains_solar_center(aia171_test_map)
assert not contains_solar_center(all_off_disk_map)
Expand Down