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
29 changes: 23 additions & 6 deletions deeptrack/optical/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.

"""
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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":
Expand All @@ -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)
]
Expand Down
42 changes: 42 additions & 0 deletions tests/test_augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down