Skip to content

Commit

Permalink
Merge pull request #26 from BiAPoL/fix_axes_swap
Browse files Browse the repository at this point in the history
I am merging this to have bug fixed. 
Fixed one of the tests as well.
If errors arise, please file new issues.
  • Loading branch information
zoccoler authored Jul 25, 2022
2 parents 814f583 + a7811a9 commit c87f01a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions napari_crop/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ def crop_region(
.squeeze()
)
# match cropped_data (x,y) shape with mask_2D shape
cropped_data_shape = cropped_data.shape
# Adjust cropped_data axes order in case axes were swapped in napari
if viewer is not None:
cropped_data_shape = np.moveaxis(cropped_data,
viewer.dims.order,
np.arange(len(cropped_data_shape))).shape
if rgb:
shape_dif_2D = np.array(cropped_data.shape[-3:-1]) \
shape_dif_2D = np.array(cropped_data_shape[-3:-1]) \
- np.array(mask_2D.shape)
else:
shape_dif_2D = np.array(cropped_data.shape[-2:]) \
shape_dif_2D = np.array(cropped_data_shape[-2:]) \
- np.array(mask_2D.shape)
shape_dif_2D = [None if i == 0 else i
for i in shape_dif_2D.tolist()]
Expand All @@ -111,7 +117,7 @@ def crop_region(

new_layer_props = layer_props.copy()
# If layer name is in viewer or is about to be added,
# give it a different name
# increment layer name until it has a different name
while True:
new_name = layer_props["name"] \
+ f" cropped [{shape_count+new_layer_index}]"
Expand Down
10 changes: 5 additions & 5 deletions napari_crop/_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
shapes = [
np.array([[1, 1], [1, 3], [4, 3], [4, 1]]),
np.array([[0.5, 0.5], [0.5, 3.5], [4.51, 3.5], [4.51, 0.5]]),
np.array([[0, 2], [4, 4], [4, 2], [2, 0]]),
np.array([[0, 2], [2, 0], [3, 2], [2, 4]]),
]
shape_types = ["rectangle", "ellipse", "polygon"]
crop_expected = [
np.array([[6, 7, 8], [11, 12, 13], [16, 17, 18], [21, 22, 23]]),
np.array([[0, 7, 0], [11, 12, 13], [16, 17, 18], [0, 22, 0]]),
np.array([[0, 2, 0],
[6, 7, 0],
[11, 12, 13],
[0, 17, 18]]), # fmt: skip
np.array([[ 0, 0, 2, 0, 0],
[ 0, 6, 7, 8, 0],
[10, 11, 12, 13, 14],
[ 0, 0, 17, 0, 0]]), # fmt: skip

]

Expand Down

0 comments on commit c87f01a

Please sign in to comment.