Skip to content

Commit 63cc3cf

Browse files
committed
check p2w in wcs
1 parent ba1d208 commit 63cc3cf

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

ndcube/tests/helpers.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from numpy.testing import assert_equal
1414

1515
import astropy
16+
from astropy.wcs.wcsapi import SlicedLowLevelWCS
1617
from astropy.wcs.wcsapi.fitswcs import SlicedFITSWCS
1718
from astropy.wcs.wcsapi.low_level_api import BaseLowLevelWCS
1819
from astropy.wcs.wcsapi.wrappers.sliced_wcs import sanitize_slices
@@ -95,9 +96,11 @@ def assert_metas_equal(test_input, expected_output):
9596
assert test_input[key] == expected_output[key]
9697

9798

98-
def assert_cubes_equal(test_input, expected_cube):
99+
def assert_cubes_equal(test_input, expected_cube, check_data=False):
99100
assert isinstance(test_input, type(expected_cube))
100101
assert np.all(test_input.mask == expected_cube.mask)
102+
if check_data:
103+
np.testing.assert_array_equal(test_input.data, expected_cube.data)
101104
assert_wcs_are_equal(test_input.wcs, expected_cube.wcs)
102105
if test_input.uncertainty:
103106
assert test_input.uncertainty.array.shape == expected_cube.uncertainty.array.shape
@@ -110,12 +113,12 @@ def assert_cubes_equal(test_input, expected_cube):
110113
assert_extra_coords_equal(test_input.extra_coords, expected_cube.extra_coords)
111114

112115

113-
def assert_cubesequences_equal(test_input, expected_sequence):
116+
def assert_cubesequences_equal(test_input, expected_sequence, check_data=False):
114117
assert isinstance(test_input, type(expected_sequence))
115118
assert_metas_equal(test_input.meta, expected_sequence.meta)
116119
assert test_input._common_axis == expected_sequence._common_axis
117120
for i, cube in enumerate(test_input.data):
118-
assert_cubes_equal(cube, expected_sequence.data[i])
121+
assert_cubes_equal(cube, expected_sequence.data[i], check_data=check_data)
119122

120123

121124
def assert_wcs_are_equal(wcs1, wcs2):
@@ -140,7 +143,13 @@ def assert_wcs_are_equal(wcs1, wcs2):
140143
assert wcs1.world_axis_units == wcs2.world_axis_units
141144
assert_equal(wcs1.axis_correlation_matrix, wcs2.axis_correlation_matrix)
142145
assert wcs1.pixel_bounds == wcs2.pixel_bounds
143-
146+
if wcs1.pixel_shape is not None:
147+
random_idx = np.random.randint(wcs1.pixel_shape,size=[10,wcs1.pixel_n_dim])
148+
# SlicedLowLevelWCS vs HighLevelWCS don't have the same pixel_to_world method
149+
if isinstance(wcs1, SlicedLowLevelWCS):
150+
np.testing.assert_array_equal(wcs1.pixel_to_world_values(*random_idx.T), wcs2.pixel_to_world_values(*random_idx.T))
151+
else:
152+
np.testing.assert_array_equal(wcs1.pixel_to_world(*random_idx.T), wcs2.pixel_to_world(*random_idx.T))
144153

145154
def create_sliced_wcs(wcs, item, dim):
146155
"""
@@ -152,15 +161,15 @@ def create_sliced_wcs(wcs, item, dim):
152161
return SlicedFITSWCS(wcs, item)
153162

154163

155-
def assert_collections_equal(collection1, collection2):
164+
def assert_collections_equal(collection1, collection2, check_data=False):
156165
assert collection1.keys() == collection2.keys()
157166
assert collection1.aligned_axes == collection2.aligned_axes
158167
for cube1, cube2 in zip(collection1.values(), collection2.values()):
159168
# Check cubes are same type.
160169
assert type(cube1) is type(cube2)
161170
if isinstance(cube1, NDCube):
162-
assert_cubes_equal(cube1, cube2)
171+
assert_cubes_equal(cube1, cube2, check_data=check_data)
163172
elif isinstance(cube1, NDCubeSequence):
164-
assert_cubesequences_equal(cube1, cube2)
173+
assert_cubesequences_equal(cube1, cube2, check_data=check_data)
165174
else:
166175
raise TypeError(f"Unsupported Type in NDCollection: {type(cube1)}")

ndcube/tests/test_ndcube.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_crop(ndcube_4d_ln_lt_l_t):
410410
upper_corner = [coord[-1] for coord in intervals]
411411
expected = cube[1:3, 0:2, 0:2, 0:3]
412412
output = cube.crop(lower_corner, upper_corner)
413-
helpers.assert_cubes_equal(output, expected)
413+
helpers.assert_cubes_equal(output, expected, check_data=True)
414414

415415

416416
def test_crop_tuple_non_tuple_input(ndcube_2d_ln_lt):
@@ -420,7 +420,7 @@ def test_crop_tuple_non_tuple_input(ndcube_2d_ln_lt):
420420
upper_corner = SkyCoord(Tx=0.0044444444, Ty=0.0011111111, unit="deg", frame=frame)
421421
cropped_by_tuples = cube.crop((lower_corner,), (upper_corner,))
422422
cropped_by_coords = cube.crop(lower_corner, upper_corner)
423-
helpers.assert_cubes_equal(cropped_by_tuples, cropped_by_coords)
423+
helpers.assert_cubes_equal(cropped_by_tuples, cropped_by_coords, check_data=True)
424424

425425

426426
def test_crop_with_nones(ndcube_4d_ln_lt_l_t):
@@ -432,31 +432,31 @@ def test_crop_with_nones(ndcube_4d_ln_lt_l_t):
432432
upper_corner[0] = interval0[-1]
433433
expected = cube[:, :, :, 0:3]
434434
output = cube.crop(lower_corner, upper_corner)
435-
helpers.assert_cubes_equal(output, expected)
435+
helpers.assert_cubes_equal(output, expected, check_data=True)
436436

437437

438438
def test_crop_1d_independent(ndcube_4d_ln_lt_l_t):
439439
cube_1d = ndcube_4d_ln_lt_l_t[0, 0, :, 0]
440440
wl_range = SpectralCoord([3e-11, 4.5e-11], unit=u.m)
441441
expected = cube_1d[0:2]
442442
output = cube_1d.crop([wl_range[0]], [wl_range[-1]])
443-
helpers.assert_cubes_equal(output, expected)
443+
helpers.assert_cubes_equal(output, expected, check_data=True)
444444

445445

446446
def test_crop_1d_dependent(ndcube_4d_ln_lt_l_t):
447447
cube_1d = ndcube_4d_ln_lt_l_t[0, :, 0, 0]
448448
sky_range = cube_1d.wcs.array_index_to_world([0, 1])
449449
expected = cube_1d[0:2]
450450
output = cube_1d.crop([sky_range[0]], [sky_range[-1]])
451-
helpers.assert_cubes_equal(output, expected)
451+
helpers.assert_cubes_equal(output, expected, check_data=True)
452452

453453

454454
def test_crop_reduces_dimensionality(ndcube_4d_ln_lt_l_t):
455455
cube = ndcube_4d_ln_lt_l_t
456456
point = (None, SpectralCoord([3e-11], unit=u.m), None)
457457
expected = cube[:, :, 0, :]
458458
output = cube.crop(point)
459-
helpers.assert_cubes_equal(output, expected)
459+
helpers.assert_cubes_equal(output, expected, check_data=True)
460460

461461

462462
def test_crop_scalar_valuerror(ndcube_2d_ln_lt):
@@ -502,7 +502,7 @@ def test_crop_by_values(ndcube_4d_ln_lt_l_t):
502502
upper_corner[-1] = upper_corner[-1].to(units[-1])
503503
expected = cube[1:3, 0:2, 0:2, 0:3]
504504
output = cube.crop_by_values(lower_corner, upper_corner)
505-
helpers.assert_cubes_equal(output, expected)
505+
helpers.assert_cubes_equal(output, expected, check_data=True)
506506

507507

508508
def test_crop_by_values_with_units(ndcube_4d_ln_lt_l_t):
@@ -518,7 +518,7 @@ def test_crop_by_values_with_units(ndcube_4d_ln_lt_l_t):
518518
units[0] = None
519519
expected = ndcube_4d_ln_lt_l_t[1:3, 0:2, 0:2, 0:3]
520520
output = ndcube_4d_ln_lt_l_t.crop_by_values(lower_corner, upper_corner, units=units)
521-
helpers.assert_cubes_equal(output, expected)
521+
helpers.assert_cubes_equal(output, expected, check_data=True)
522522

523523

524524
def test_crop_by_values_with_equivalent_units(ndcube_2d_ln_lt):
@@ -528,7 +528,7 @@ def test_crop_by_values_with_equivalent_units(ndcube_2d_ln_lt):
528528
upper_corner = [(coord[-1]*u.deg).to(u.arcsec) for coord in intervals]
529529
expected = ndcube_2d_ln_lt[0:4, 1:7]
530530
output = ndcube_2d_ln_lt.crop_by_values(lower_corner, upper_corner)
531-
helpers.assert_cubes_equal(output, expected)
531+
helpers.assert_cubes_equal(output, expected, check_data=True)
532532

533533

534534
def test_crop_by_values_with_nones(ndcube_4d_ln_lt_l_t):
@@ -539,15 +539,15 @@ def test_crop_by_values_with_nones(ndcube_4d_ln_lt_l_t):
539539
upper_corner[0] = 1.1 * u.min
540540
expected = cube[:, :, :, 0:3]
541541
output = cube.crop_by_values(lower_corner, upper_corner)
542-
helpers.assert_cubes_equal(output, expected)
542+
helpers.assert_cubes_equal(output, expected, check_data=True)
543543

544544

545545
def test_crop_by_values_all_nones(ndcube_4d_ln_lt_l_t):
546546
cube = ndcube_4d_ln_lt_l_t
547547
lower_corner = [None] * 4
548548
upper_corner = [None] * 4
549549
output = cube.crop_by_values(lower_corner, upper_corner)
550-
helpers.assert_cubes_equal(output, cube)
550+
helpers.assert_cubes_equal(output, cube, check_data=True)
551551

552552

553553
def test_crop_by_values_valueerror1(ndcube_4d_ln_lt_l_t):
@@ -594,7 +594,7 @@ def test_crop_by_values_1d_dependent(ndcube_4d_ln_lt_l_t):
594594
upper_corner = [lat_range[-1] * u.deg, lon_range[-1] * u.deg]
595595
expected = cube_1d[0:2]
596596
output = cube_1d.crop_by_values(lower_corner, upper_corner)
597-
helpers.assert_cubes_equal(output, expected)
597+
helpers.assert_cubes_equal(output, expected, check_data=True)
598598

599599

600600
def test_crop_by_extra_coords(ndcube_3d_ln_lt_l_ec_time):
@@ -603,7 +603,7 @@ def test_crop_by_extra_coords(ndcube_3d_ln_lt_l_ec_time):
603603
upper_corner = (Time("2000-01-01T20:00:00", scale="utc", format="fits"), None)
604604
output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords)
605605
expected = cube[0]
606-
helpers.assert_cubes_equal(output, expected)
606+
helpers.assert_cubes_equal(output, expected, check_data=True)
607607

608608

609609
def test_crop_by_extra_coords_values(ndcube_3d_ln_lt_l_ec_time):
@@ -612,7 +612,7 @@ def test_crop_by_extra_coords_values(ndcube_3d_ln_lt_l_ec_time):
612612
upper_corner = (8 * 60 * 60 * u.s, 2 * u.pix)
613613
output = cube.crop_by_values(lower_corner, upper_corner, wcs=cube.extra_coords)
614614
expected = cube[0]
615-
helpers.assert_cubes_equal(output, expected)
615+
helpers.assert_cubes_equal(output, expected, check_data=True)
616616

617617

618618
def test_crop_by_extra_coords_all_axes_with_coord(ndcube_3d_ln_lt_l_ec_all_axes):
@@ -624,7 +624,7 @@ def test_crop_by_extra_coords_all_axes_with_coord(ndcube_3d_ln_lt_l_ec_all_axes)
624624
upper_corner = (interval0[1], interval1[1], interval2[1])
625625
output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords)
626626
expected = cube[0, 0:2, 1:4]
627-
helpers.assert_cubes_equal(output, expected)
627+
helpers.assert_cubes_equal(output, expected, check_data=True)
628628

629629

630630
def test_crop_by_extra_coords_values_all_axes_with_coord(ndcube_3d_ln_lt_l_ec_all_axes):
@@ -636,7 +636,7 @@ def test_crop_by_extra_coords_values_all_axes_with_coord(ndcube_3d_ln_lt_l_ec_al
636636
upper_corner = (interval0[1], interval1[1], interval2[1])
637637
output = cube.crop_by_values(lower_corner, upper_corner, wcs=cube.extra_coords)
638638
expected = cube[0, 0:2, 1:4]
639-
helpers.assert_cubes_equal(output, expected)
639+
helpers.assert_cubes_equal(output, expected, check_data=True)
640640

641641

642642
def test_crop_by_extra_coords_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis):
@@ -645,7 +645,7 @@ def test_crop_by_extra_coords_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis):
645645
upper_corner = (2 * u.m, 2 * u.keV)
646646
output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords)
647647
expected = cube[:, 1:3]
648-
helpers.assert_cubes_equal(output, expected)
648+
helpers.assert_cubes_equal(output, expected, check_data=True)
649649

650650

651651
def test_crop_by_extra_coords_values_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis):
@@ -654,7 +654,7 @@ def test_crop_by_extra_coords_values_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_ax
654654
upper_corner = (2 * u.m, 2 * u.keV)
655655
output = cube.crop_by_values(lower_corner, upper_corner, wcs=cube.extra_coords)
656656
expected = cube[:, 1:3]
657-
helpers.assert_cubes_equal(output, expected)
657+
helpers.assert_cubes_equal(output, expected, check_data=True)
658658

659659

660660
def test_crop_rotated_celestial(ndcube_4d_ln_lt_l_t):

0 commit comments

Comments
 (0)