Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/939.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The error raised by `~ndcube.NDCube.crop` when a point has the wrong number of components now lists the expected world objects and their order.
7 changes: 6 additions & 1 deletion ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,13 @@ def _get_crop_item(self, *points, wcs=None, keepdims=False):
classes = [wcs.world_axis_object_classes[c][0] for c in comp]
for i, point in enumerate(points):
if len(point) != len(comp):
component_names = ", ".join(
f"{name} ({cls.__name__})" for name, cls in zip(comp, classes))
raise ValueError(f"{len(point)} components in point {i} do not match "
f"WCS with {len(comp)} components.")
f"WCS with {len(comp)} components. Each point must "
"have one entry per world object (use None for a "
"component that should not be cropped), in order: "
f"{component_names}.")
for j, value in enumerate(point):
if not (value is None or isinstance(value, classes[j])):
raise TypeError(f"{type(value)} of component {j} in point {i} is "
Expand Down
7 changes: 7 additions & 0 deletions ndcube/tests/test_ndcube_slice_and_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,13 @@ def test_crop_all_points_beyond_cube_extent_error(points):
cube.crop(*points, keepdims=True)


def test_crop_length_error_names_world_components(ndcube_4d_ln_lt_l_t):
cube = ndcube_4d_ln_lt_l_t
interval0 = cube.wcs.array_index_to_world([1, 2], [0, 1], [0, 1], [0, 2])[0]
with pytest.raises(ValueError, match=r"one entry per world object"):
cube.crop([interval0[0], None], [interval0[-1], None])


def test_crop_by_values_quantity_table_coordinate():
# Regression: QuantityTableCoordinate-based WCS raised
# "High Level objects are not supported with the native API" because
Expand Down
Loading