diff --git a/deeptrack/optical/augmentations.py b/deeptrack/optical/augmentations.py index 4e4927535..d98be77a8 100644 --- a/deeptrack/optical/augmentations.py +++ b/deeptrack/optical/augmentations.py @@ -1867,7 +1867,8 @@ class Crop(Augmentation): ---------- crop: int | tuple[int, ...] | list[int] | Callable Defines the cropping amount. If an integer, the same value is used - for all axes. If a tuple or list, values are interpreted per axis. + for all axes. If a tuple or list, values are interpreted per axis; + axes beyond its length are not cropped. If `crop_mode="retain"`, `crop` specifies the output size. If `crop_mode="remove"`, `crop` specifies the number of pixels removed from the borders. @@ -1880,7 +1881,8 @@ class Crop(Augmentation): corner: PropertyLike[str | tuple[int, int] | Callable], optional Top-left corner of the cropped region. - `"random"` selects a random valid corner. (default) - - A tuple specifies the corner explicitly. + - A tuple specifies the corner explicitly. Axes beyond its length + start at 0. - A callable receives the input array and returns a corner. Methods @@ -1923,7 +1925,8 @@ def __init__( ---------- crop: int | tuple[int, ...] | list[int] | Callable Defines the cropping amount. If an integer, the same value is used - for all axes. If a tuple or list, values are interpreted per axis. + for all axes. If a tuple or list, values are interpreted per axis; + axes beyond its length are not cropped. If `crop_mode="retain"`, `crop` specifies the output size. If `crop_mode="remove"`, `crop` specifies the number of pixels removed from the borders. @@ -1936,7 +1939,8 @@ def __init__( corner: PropertyLike[str | tuple[int, int] | Callable], optional Top-left corner of the cropped region. - `"random"` selects a random valid corner. (default) - - A tuple specifies the corner explicitly. + - A tuple specifies the corner explicitly. Axes beyond its length + start at 0. - A callable receives the input array and returns a corner. """ @@ -1979,7 +1983,8 @@ def _get_xp( Input array to be cropped. crop: int | tuple[int, ...] | list[int] | Callable Defines the cropping amount. If an integer, the same value is used - for all axes. If a tuple or list, values are interpreted per axis. + for all axes. If a tuple or list, values are interpreted per axis; + axes beyond its length are not cropped. If `crop_mode="retain"`, `crop` specifies the output size. If `crop_mode="remove"`, `crop` specifies the number of pixels removed from the borders. @@ -1992,7 +1997,8 @@ def _get_xp( corner: str | tuple[int] | Callable Top-left corner of the cropped region. - `"random"` selects a random valid corner. - - A tuple specifies the corner explicitly. + - A tuple specifies the corner explicitly. Axes beyond its length + start at 0. - A callable receives the input array and returns a corner. xp: module The array library (e.g., `numpy` or `torch`) to use for @@ -2015,6 +2021,13 @@ def _get_xp( c if c is not None else array.shape[i] for i, c in enumerate(crop) ] + # Axes not covered by `crop` are left uncropped. + missing_axes = range(len(crop), array.ndim) + if crop_mode == "retain": + crop += [array.shape[i] for i in missing_axes] + elif crop_mode == "remove": + crop += [0 for _ in missing_axes] + if crop_mode == "retain": crop_amount = np.array(array.shape) - np.array(crop) elif crop_mode == "remove": @@ -2033,6 +2046,10 @@ def _get_xp( else: slice_start = corner + # Axes not covered by `corner` start at 0. + slice_start = list(slice_start) + slice_start += [0] * (array.ndim - len(slice_start)) + slice_start = [ int(c) % (int(m) + 1) for c, m in zip(slice_start, crop_amount) ] diff --git a/tests/test_augmentations.py b/tests/test_augmentations.py index c96bda706..4bb7907aa 100644 --- a/tests/test_augmentations.py +++ b/tests/test_augmentations.py @@ -978,6 +978,48 @@ def test_Crop(self): expected_region, ) + def test_Crop_fewer_values_than_ndim(self): + # Axes not covered by `crop` or `corner` are left uncropped. + + backends = ["numpy"] + if TORCH_AVAILABLE: + backends.append("torch") + + for backend in backends: + + config.set_backend(backend) + + image_np = np.arange(20 * 30 * 2, dtype=np.float32) + image_np = image_np.reshape(20, 30, 2) + image = image_np if backend == "numpy" else torch.tensor(image_np) + + def check(out, expected): + if backend == "numpy": + np.testing.assert_array_equal(out, expected) + else: + self.assertTrue(torch.equal(out, torch.tensor(expected))) + + cropper = augmentations.Crop( + crop=(10, 12), crop_mode="retain", corner=(3, 5), + ) + check(cropper(image), image_np[3:13, 5:17, :]) + + cropper = augmentations.Crop( + crop=(4, 6), crop_mode="remove", corner=(1, 2), + ) + check(cropper(image), image_np[1:17, 2:26, :]) + + cropper = augmentations.Crop( + crop=(10, 12, 1), crop_mode="retain", corner=(3, 5), + ) + check(cropper(image), image_np[3:13, 5:17, 0:1]) + + cropper = augmentations.Crop(crop=(10, 12), crop_mode="retain") + out = cropper(image) + self.assertSequenceEqual(tuple(out.shape), (10, 12, 2)) + + config.set_backend("numpy") + def test_Crop_time_consistent_bind(self): backends = ["numpy"]