Skip to content

Commit f823795

Browse files
authored
Merge pull request #863 from DanRyanIrish/crop_inputs
Allow length-1 inputs to NDCube.crop
2 parents 29f407b + dc3a05e commit f823795

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

changelog/863.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enable length-1 inputs to `ndcube.NDCube.crop`, not only scalars.

ndcube/tests/test_ndcube_reproject_and_rebin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import dask.array
55
import numpy as np
66
import pytest
7-
from specutils import Spectrum
7+
8+
try:
9+
from specutils import Spectrum
10+
except ImportError:
11+
from specutils import Spectrum1D as Spectrum
812

913
import astropy.units as u
1014
import astropy.wcs

ndcube/tests/test_ndcube_slice_and_crop.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ def test_crop_tuple_non_tuple_input(ndcube_2d_ln_lt):
218218
helpers.assert_cubes_equal(cropped_by_tuples, cropped_by_coords)
219219

220220

221+
def test_crop_length_1_input(ndcube_2d_ln_lt):
222+
cube = ndcube_2d_ln_lt
223+
frame = astropy.wcs.utils.wcs_to_celestial_frame(cube.wcs)
224+
lower_corner = SkyCoord(Tx=[0359.99667], Ty=[-0.0011111111], unit="deg", frame=frame)
225+
upper_corner = SkyCoord(Tx=[[0.0044444444]], Ty=[[0.0011111111]], unit="deg", frame=frame)
226+
cropped_by_shaped = cube.crop(lower_corner, upper_corner)
227+
cropped_by_scalars = cube.crop((lower_corner.squeeze(),), (upper_corner.squeeze(),))
228+
helpers.assert_cubes_equal(cropped_by_shaped, cropped_by_scalars)
229+
230+
221231
def test_crop_with_nones(ndcube_4d_ln_lt_l_t):
222232
cube = ndcube_4d_ln_lt_l_t
223233
lower_corner = [None] * 3

ndcube/utils/cube.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
import astropy.nddata
8-
from astropy.wcs.wcsapi import BaseHighLevelWCS, HighLevelWCSWrapper, SlicedLowLevelWCS
8+
from astropy.wcs.wcsapi import BaseHighLevelWCS, BaseLowLevelWCS, HighLevelWCSWrapper, SlicedLowLevelWCS
99

1010
from ndcube.utils import wcs as wcs_utils
1111

@@ -81,6 +81,8 @@ def sanitize_crop_inputs(points, wcs):
8181
# Confirm whether point contains at least one None entry.
8282
if all(coord is None for coord in points[i]):
8383
values_are_none[i] = True
84+
# Squeeze length-1 coordinate objects to scalars.
85+
points[i] = [coord.squeeze() if hasattr(coord, "squeeze") else coord for coord in points[i]]
8486
# If no points contain a coord, i.e. if all entries in all points are None,
8587
# set no-op flag to True and exit.
8688
if all(values_are_none):
@@ -138,6 +140,8 @@ def get_crop_item_from_points(points, wcs, crop_by_values, keepdims):
138140
# Define a list of lists to hold the array indices of the points
139141
# where each inner list gives the index of all points for that array axis.
140142
combined_points_array_idx = [[]] * wcs.pixel_n_dim
143+
high_level_wcs = HighLevelWCSWrapper(wcs) if isinstance(wcs, BaseLowLevelWCS) else wcs
144+
wcs = high_level_wcs.low_level_wcs
141145
# For each point compute the corresponding array indices.
142146
for point in points:
143147
# Get the arrays axes associated with each element in point.
@@ -150,8 +154,7 @@ def get_crop_item_from_points(points, wcs, crop_by_values, keepdims):
150154
wcs_utils.convert_between_array_and_pixel_axes(pix_axes, wcs.pixel_n_dim)))
151155
point_inputs_array_axes = tuple(point_inputs_array_axes)
152156
else:
153-
point_inputs_array_axes = wcs_utils.array_indices_for_world_objects(
154-
HighLevelWCSWrapper(wcs))
157+
point_inputs_array_axes = wcs_utils.array_indices_for_world_objects(high_level_wcs)
155158
# Get indices of array axes which correspond to only None inputs in point
156159
# as well as those that correspond to a coord.
157160
point_indices_with_inputs = []

0 commit comments

Comments
 (0)