Skip to content

Commit bbc3d8e

Browse files
authored
Merge pull request #24 from BiAPoL/unique_layer_names
Give cropped layers unique names. Merging this because it seems to have fixed #20 while keeping backwards compatibility.
2 parents 86b9670 + 814f583 commit bbc3d8e

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

.github/workflows/test_and_deploy.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
name: tests
55

6-
on:
6+
on:
77
push:
88
branches:
99
- master
@@ -46,7 +46,7 @@ jobs:
4646
- name: Install Windows OpenGL
4747
if: runner.os == 'Windows'
4848
run: |
49-
git clone --depth 1 git://github.com/pyvista/gl-ci-helpers.git
49+
git clone --depth 1 https://github.com/pyvista/gl-ci-helpers.git
5050
powershell gl-ci-helpers/appveyor/install_opengl.ps1
5151
5252
# note: if you need dependencies from conda, considering using
@@ -69,7 +69,7 @@ jobs:
6969

7070
deploy:
7171
# this will run when you have tagged a commit, starting with "v*"
72-
# and requires that you have put your twine API key in your
72+
# and requires that you have put your twine API key in your
7373
# github secrets (see readme for details)
7474
needs: [test]
7575
runs-on: ubuntu-latest

napari_crop/_function.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from napari.types import LayerDataTuple
88
from typing import List
99

10+
1011
# This is the actual plugin function, where we export our function
1112
# (The functions themselves are defined below)
1213
@napari_hook_implementation
@@ -18,6 +19,7 @@ def napari_experimental_provide_function():
1819
def crop_region(
1920
layer: napari.layers.Layer,
2021
shapes_layer: napari.layers.Shapes,
22+
viewer: 'napari.viewer.Viewer' = None,
2123
) -> List[LayerDataTuple]:
2224
"""Crop regions in napari defined by shapes."""
2325
if shapes_layer is None:
@@ -42,7 +44,14 @@ def crop_region(
4244
shape_types = shapes_layer.shape_type
4345
shapes = shapes_layer.data
4446
cropped_list = []
45-
for count, [shape, shape_type] in enumerate(zip(shapes, shape_types)):
47+
new_layer_index = 0
48+
new_name = layer_props["name"] + " cropped [0]"
49+
names_list = []
50+
if viewer is not None:
51+
# Get existing layer names in viewer
52+
names_list = [layer.name for layer in viewer.layers]
53+
for shape_count, [shape, shape_type] in enumerate(zip(shapes,
54+
shape_types)):
4655
# move shape vertices to within image coordinate limits
4756
layer_shape = np.array(layer_data.shape)
4857
if rgb:
@@ -101,6 +110,16 @@ def crop_region(
101110
cropped_data = cropped_data[tuple(indices)]
102111

103112
new_layer_props = layer_props.copy()
104-
new_layer_props["name"] = layer_props["name"] + f" (cropped {count+1})"
113+
# If layer name is in viewer or is about to be added,
114+
# give it a different name
115+
while True:
116+
new_name = layer_props["name"] \
117+
+ f" cropped [{shape_count+new_layer_index}]"
118+
if new_name not in names_list:
119+
break
120+
else:
121+
new_layer_index += 1
122+
new_layer_props["name"] = new_name
123+
names_list.append(new_name)
105124
cropped_list.append((cropped_data, new_layer_props, layer_type))
106125
return cropped_list

napari_crop/_tests/test_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_crop_multiple_shapes(make_napari_viewer):
6868
viewer = make_napari_viewer()
6969
img_layer = viewer.add_image(arr_2d)
7070
shapes_layer = viewer.add_shapes(shapes, shape_type=shape_types)
71-
cropped_actual = crop_region(img_layer, shapes_layer)
71+
cropped_actual = crop_region(img_layer, shapes_layer, viewer=viewer)
7272

7373
assert len(shapes) == len(cropped_actual)
7474

0 commit comments

Comments
 (0)