diff --git a/changelog/939.feature.rst b/changelog/939.feature.rst new file mode 100644 index 000000000..5c15dffef --- /dev/null +++ b/changelog/939.feature.rst @@ -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. diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index a9bcbb0ed..a181e7d2f 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -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 " diff --git a/ndcube/tests/test_ndcube_slice_and_crop.py b/ndcube/tests/test_ndcube_slice_and_crop.py index 56b7cd4a4..81ca3d745 100644 --- a/ndcube/tests/test_ndcube_slice_and_crop.py +++ b/ndcube/tests/test_ndcube_slice_and_crop.py @@ -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