Skip to content

Commit 214924a

Browse files
authored
Merge pull request #2 from DanRyanIrish/cadair/fix_resampled
Writing tests and updated fix
2 parents 44c6d03 + 8530b22 commit 214924a

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

ndcube/wcs/wrappers/resampled_wcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def _top_to_underlying_pixels(self, top_pixels):
4343
# Convert user-facing pixel indices to the pixel grid of underlying WCS.
4444
factor = self._pad_dims(self._factor, top_pixels.ndim)
4545
offset = self._pad_dims(self._offset, top_pixels.ndim)
46-
return (top_pixels * factor) + 1 + offset
46+
return (top_pixels + 0.5) * factor - 0.5 + offset
4747

4848
def _underlying_to_top_pixels(self, underlying_pixels):
4949
# Convert pixel indices of underlying pixel grid to user-facing grid.
5050
factor = self._pad_dims(self._factor, underlying_pixels.ndim)
5151
offset = self._pad_dims(self._offset, underlying_pixels.ndim)
52-
return (underlying_pixels - 1 - offset) / factor
52+
return (underlying_pixels + 0.5 - offset) / factor - 0.5
5353

5454
def _pad_dims(self, arr, ndim):
5555
# Pad array with trailing degenerate dimensions.

ndcube/wcs/wrappers/tests/test_resampled_wcs.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_int_fraction_pixel_shape(celestial_wcs):
185185
assert isinstance(dim, numbers.Integral)
186186

187187
@pytest.fixture
188-
def nine_nine_wcs():
188+
def four_five_wcs():
189189
from astropy.wcs import WCS
190190

191191
wcs = WCS(naxis=2)
@@ -195,22 +195,21 @@ def nine_nine_wcs():
195195
wcs.wcs.crpix = [1, 1]
196196

197197
wcs.wcs.set()
198-
wcs.pixel_shape = [9, 9]
198+
wcs.pixel_shape = [4, 5]
199199
return wcs
200200

201-
def test_corner_pixels(nine_nine_wcs):
202-
owcs = nine_nine_wcs
203-
# original corner
204-
ocorner = owcs.pixel_to_world_values(-0.5, -0.5)
205-
# one super pixel along (3 original pixels)
206-
osone = owcs.pixel_to_world_values(2.5, 2.5)
207-
208-
wcs = ResampledLowLevelWCS(owcs, 3)
209-
210-
# resampled corner == original corner
211-
corner = wcs.pixel_to_world_values(-0.5, -0.5)
212-
# One super pixel along
213-
sone = wcs.pixel_to_world_values(0.5, 0.5)
214-
215-
assert np.allclose(ocorner, corner)
216-
assert np.allclose(osone, sone)
201+
@pytest.mark.parametrize(('factor', 'offset', 'expected_over_pixels'),
202+
[([2, 3], [0, 0], np.meshgrid(np.arange(-0.5, 1.75, 0.25), np.arange(-0.5, 1.2, 1/6))),
203+
([2, 3], [1, 2], np.meshgrid(np.arange(-1, 1.25, 0.25), np.arange(-7, 4) / 6)),
204+
])
205+
def test_resampled_pixel_to_world_values(four_five_wcs, factor, offset, expected_over_pixels):
206+
wcs = four_five_wcs
207+
# Get world values of original pixel grid.
208+
under_pixels = np.meshgrid(np.arange(-0.5, 4, 0.5), np.arange(-0.5, 5, 0.5))
209+
expected_world = wcs.pixel_to_world_values(*under_pixels)
210+
211+
# Resample WCS
212+
new_wcs = ResampledLowLevelWCS(wcs, factor, offset)
213+
# Get expected pixel coords in resampled WCS of same pixel locations as above.
214+
output_world = new_wcs.pixel_to_world_values(*expected_over_pixels)
215+
assert_allclose(np.asarray(output_world), np.asarray(expected_world), atol=1e-15)

0 commit comments

Comments
 (0)