From ce5a3c483c443a80ec06eaccdffb3f7e8969ced1 Mon Sep 17 00:00:00 2001 From: Noah Fahlgren Date: Thu, 28 Mar 2024 21:25:15 -0500 Subject: [PATCH] Test tests --- plantcv/annotate/__init__.py | 3 +++ plantcv/annotate/napari_open.py | 36 +++++++++++++++++++++++++++++++++ tests/test_annotate.py | 18 +++++++++++------ 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 plantcv/annotate/napari_open.py diff --git a/plantcv/annotate/__init__.py b/plantcv/annotate/__init__.py index e69de29..d18cd53 100644 --- a/plantcv/annotate/__init__.py +++ b/plantcv/annotate/__init__.py @@ -0,0 +1,3 @@ +from plantcv.annotate.napari_open import napari_open + +__all__ = ["napari_open"] diff --git a/plantcv/annotate/napari_open.py b/plantcv/annotate/napari_open.py new file mode 100644 index 0000000..6b7c490 --- /dev/null +++ b/plantcv/annotate/napari_open.py @@ -0,0 +1,36 @@ +# Use Napari to Label + +import napari +import cv2 +import numpy as np +from skimage.color import label2rgb + + +def napari_open(img, show=True): + """ + open img in napari and label classes + Inputs: + img = img (grayimg, rgbimg, or hyperspectral image array data e.g. + hyperspectraldata.array_data) + 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 + :param img: numpy.ndarray + :return viewer: napari viewer object + """ + shape = np.shape(img) + showcall = show + if len(shape) == 2: + colorful = label2rgb(img) + img = (255*colorful).astype(np.uint8) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + if len(shape) == 3: + if shape[2] == 3: + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + if shape[2] > 3: + img = img.transpose(2, 0, 1) + + viewer = napari.view_image(img, show=showcall) + + return viewer diff --git a/tests/test_annotate.py b/tests/test_annotate.py index ab22976..8f99900 100644 --- a/tests/test_annotate.py +++ b/tests/test_annotate.py @@ -1,11 +1,17 @@ -from plantcv import annotate as an +import numpy as np +from plantcv.annotate import napari_open -def test_annotate(): +def test_napari(qtbot): """PlantCV Test""" - assert an.__name__ == "plantcv.annotate" + img = np.zeros((100, 100, 3), dtype=np.uint8) + viewer = napari_open(img, show=False) + coor = [(25, 25), (75, 75)] + viewer.add_points(np.array(coor), symbol="o", name="total", face_color="red", size=1) + def check_open(): + assert np.shape(viewer.layers['total'].data) != (0, 2) -def test_napari(make_napari_viewer): - """PlantCV Test""" - viewer = make_napari_viewer(show=False) + qtbot.waitUntil(check_open, timeout=60_000) + + assert len(viewer.layers["total"].data) == 2