Skip to content

Commit

Permalink
refactoring to add show as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
maliagehan committed Apr 1, 2024
1 parent a3dc1b9 commit d0835aa
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
4 changes: 1 addition & 3 deletions docs/napari_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import plantcv.annotate as pcvan
# Create an instance of the Points class
img, path, name = pcv.readimage("./grayimg.png")

viewer= napari.Viewer()

viewer = pcvan.napari_label_classes(img=img, viewer= viewer, classes=['background', 'wing', 'seed'])
viewer = pcvan.napari_label_classes(img=img, classes=['background', 'wing', 'seed'])

classes = pcvan.napari_classes(viewer)

Expand Down
7 changes: 3 additions & 4 deletions docs/napari_label_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
This function opens an image in Napari and then defines a set of classes to label. A random shape label is assigned to each class.
Image can be annotate as long as viewer is open.

**plantcv.annotate.napari_label_classes*(*img, viewer, classes*)
**plantcv.annotate.napari_label_classes*(*img, classes, show=True*)

**returns** napari viewer object

- **Parameters:**
- img - image data (compatible with gray, RGB, and hyperspectral data. If data is hyperspecral it should be the array e.g. hyperspectral.array_data)
- viewer - Napari viewer object
- classes - list of classes to label. If no points are selected for a class,
data without labels will default to this class when napari_join_labels
is run. If all classes have points labeled, any clusters not labeled
will default to the last class in the list if napari_join_labels is
run.
- show - if show = True, viewer is launched. False setting is useful for test purposes.

- **Context:**
- Adding class labels to images. Works best on an image that has objects segmented/classified with contours/clusters labeled with values (e.g. labeled mask, output of kmeans clustering).
Expand All @@ -31,8 +31,7 @@ import napari
# Create an instance of the Points class
img, path, name = pcv.readimage("./grayimg.png")

viewer = napari.Viewer()
viewer = pcvan.napari_label_classes(img=img, viewer= viewer, classes=['background', 'wing','seed'])
viewer = pcvan.napari_label_classes(img=img, classes=['background', 'wing','seed'])

# Should open interactive napari viewer

Expand Down
8 changes: 3 additions & 5 deletions docs/napari_open.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Open image data (e.g. RGB, gray, hyperspectral) with an interactive Napari viewer. If a gray image is opened, the image will be pseudocolored for better visualization.

**plantcv.annotate.napari_open**(*img, viewer*)
**plantcv.annotate.napari_open**(*img, show=True*)

**returns** napari viewer object

- **Parameters:**
- img - image data (compatible with gray, RGB, and hyperspectral data. If data is hyperspecral it should be the array e.g. hyperspectral.array_data)
- viewer - Napari viewer object
- show - if show = True, viewer is launched. False setting is useful for test purposes.

- **Context:**
- Used to open image data with Napari.
Expand All @@ -24,9 +24,7 @@ import plantcv.annotate as pcvan
# Create an instance of the Points class
img, path, name = pcv.readimage("./grayimg.png")

viewer = napari.Viewer()

viewer = pcvan.napari_open(img=img, viewer=viewer)
viewer = pcvan.napari_open(img=img)

# Should open interactive napari viewer

Expand Down
7 changes: 5 additions & 2 deletions plantcv/annotate/napari_label_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from plantcv.annotate import napari_open


def napari_label_classes(img, classes):
def napari_label_classes(img, classes, show=True):
"""
open img in napari and label classes
Expand All @@ -19,6 +19,8 @@ def napari_label_classes(img, classes):
is run. If all classes have points labeled, any clusters not labeled
will default to the last class in the list when napari_join_labels is
run.
show = if show is True the viewer is launched. This opetion is useful for
running tests without triggering the viewer.
Returns:
viewer = Napari viewer object
Expand All @@ -27,7 +29,8 @@ def napari_label_classes(img, classes):
:return viewer: napari viewer object
"""
viewer = napari_open(img)
showcall = show
viewer = napari_open(img, show=showcall)

symbols = ['arrow', 'clobber', 'cross', 'diamond', 'disc', 'hbar', 'ring',
'square', 'star', 'tailed_arrow', 'triangle_down',
Expand Down
6 changes: 3 additions & 3 deletions plantcv/annotate/napari_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import napari


def napari_open(img):
def napari_open(img, show=True):
"""
open img in napari and label classes
Expand All @@ -33,7 +33,7 @@ def napari_open(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if shape[2] > 3:
img = img.transpose(2, 0, 1)
viewer = napari.Viewer()
showcall = show
viewer = napari.Viewer(show=showcall)
viewer.add_image(img)

return viewer
2 changes: 1 addition & 1 deletion tests/test_napari_label_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_napari_label_classes_gray(test_data):
"""Test for PlantCV.Annotate"""
# Read in test data
img, _, _ = readimage(test_data.kmeans_seed_gray_img)
viewer = napari_label_classes(img, ['seed'])
viewer = napari_label_classes(img, ['seed'], show=False)
coor = [(25, 25)]
viewer.add_points(np.array(coor), symbol="o", name='background',
face_color="red", size=1)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_napari_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_napari_open_rgb(test_data):
"""Test for PlantCV.Annotate"""
# Read in test data
img, _, _ = readimage(test_data.small_rgb_img)
viewer = napari_open(img)
viewer = napari_open(img, show=False)
coor = [(25, 25), (50, 50)]
viewer.add_points(np.array(coor), symbol="o", name="total",
face_color="red", size=1)
Expand All @@ -19,7 +19,7 @@ def test_napari_open_gray(test_data):
"""Test for PlantCV.Annotate"""
# Read in test data
img, _, _ = readimage(test_data.kmeans_seed_gray_img)
viewer = napari_open(img)
viewer = napari_open(img, show=False)
coor = [(25, 25), (50, 50)]
viewer.add_points(np.array(coor), symbol="o", name="total",
face_color="red", size=1)
Expand All @@ -32,7 +32,7 @@ def test_napari_open_envi(test_data):
# Read in test data
img = readimage(test_data.envi_sample_data, mode='envi')
img = img.array_data
viewer = napari_open(img)
viewer = napari_open(img, show=False)
coor = [(25, 25), (50, 50)]
viewer.add_points(np.array(coor), symbol="o", name="total",
face_color="red", size=1)
Expand Down

0 comments on commit d0835aa

Please sign in to comment.