Skip to content

Commit c87f01a

Browse files
authored
Merge pull request #26 from BiAPoL/fix_axes_swap
I am merging this to have bug fixed. Fixed one of the tests as well. If errors arise, please file new issues.
2 parents 814f583 + a7811a9 commit c87f01a

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

napari_crop/_function.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ def crop_region(
8383
.squeeze()
8484
)
8585
# match cropped_data (x,y) shape with mask_2D shape
86+
cropped_data_shape = cropped_data.shape
87+
# Adjust cropped_data axes order in case axes were swapped in napari
88+
if viewer is not None:
89+
cropped_data_shape = np.moveaxis(cropped_data,
90+
viewer.dims.order,
91+
np.arange(len(cropped_data_shape))).shape
8692
if rgb:
87-
shape_dif_2D = np.array(cropped_data.shape[-3:-1]) \
93+
shape_dif_2D = np.array(cropped_data_shape[-3:-1]) \
8894
- np.array(mask_2D.shape)
8995
else:
90-
shape_dif_2D = np.array(cropped_data.shape[-2:]) \
96+
shape_dif_2D = np.array(cropped_data_shape[-2:]) \
9197
- np.array(mask_2D.shape)
9298
shape_dif_2D = [None if i == 0 else i
9399
for i in shape_dif_2D.tolist()]
@@ -111,7 +117,7 @@ def crop_region(
111117

112118
new_layer_props = layer_props.copy()
113119
# If layer name is in viewer or is about to be added,
114-
# give it a different name
120+
# increment layer name until it has a different name
115121
while True:
116122
new_name = layer_props["name"] \
117123
+ f" cropped [{shape_count+new_layer_index}]"

napari_crop/_tests/test_function.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
shapes = [
1414
np.array([[1, 1], [1, 3], [4, 3], [4, 1]]),
1515
np.array([[0.5, 0.5], [0.5, 3.5], [4.51, 3.5], [4.51, 0.5]]),
16-
np.array([[0, 2], [4, 4], [4, 2], [2, 0]]),
16+
np.array([[0, 2], [2, 0], [3, 2], [2, 4]]),
1717
]
1818
shape_types = ["rectangle", "ellipse", "polygon"]
1919
crop_expected = [
2020
np.array([[6, 7, 8], [11, 12, 13], [16, 17, 18], [21, 22, 23]]),
2121
np.array([[0, 7, 0], [11, 12, 13], [16, 17, 18], [0, 22, 0]]),
22-
np.array([[0, 2, 0],
23-
[6, 7, 0],
24-
[11, 12, 13],
25-
[0, 17, 18]]), # fmt: skip
22+
np.array([[ 0, 0, 2, 0, 0],
23+
[ 0, 6, 7, 8, 0],
24+
[10, 11, 12, 13, 14],
25+
[ 0, 0, 17, 0, 0]]), # fmt: skip
2626

2727
]
2828

0 commit comments

Comments
 (0)