diff --git a/docs/changelog.md b/docs/changelog.md index ba71ffd..7f8ac31 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -22,6 +22,10 @@ All notable changes to this project will be documented below. * v0.1dev: viewer = **annotate.napari_open**(*img, mode = 'native', show=True*) +#### annotate.napari_read_coor + +* v0.1dev: data = **annotate.napari_read_coor**(*coor, dataformat='yx'*) + #### annotate.napari_save_coor * v0.1dev: datadict = **annotate.napari_save_coor**(*viewer, filepath*) diff --git a/docs/napari_read_coor.md b/docs/napari_read_coor.md new file mode 100644 index 0000000..2d22fc9 --- /dev/null +++ b/docs/napari_read_coor.md @@ -0,0 +1,30 @@ +## Read point data into Napari Format + +Save Points Labeled in Napari to a File + +**plantcv.napari_read_coor**(*coor, dataformat = 'yx'*) + +**returns** dictionary of points labeled by class + +- **Parameters:** + - coor - dictionary object of coordinates, or a path to json datafile with dictionary of point coordinates + - dataformat - either 'yx' or 'xy', Napari takes data as y,x format. If data is 'xy' data is converted from x,y to y,x + +- **Context:** + - Import previously labeled points, or points from other functions (e.g. [`pcvan.napari_read_coor`](napari_read_coor.md)) + +- **Example use:** + - Below + + +```python +import plantcv.plantcv as pcv +import plantcv.annotate as pcvan + +# read in data + +data = pcvan.napari_read_points(coor ='coor.json', dataformat = 'xy') + +``` + +**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/napari_read_coor.py) diff --git a/mkdocs.yml b/mkdocs.yml index 24b2932..fb27379 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -21,7 +21,8 @@ nav: - Napari Join: napari_join_labels.md - Napari Label: napari_label_classes.md - Napari Open: napari_open.md - - Napari Save: napari_save_coor.md + - Napari Read Coor: napari_read_coor.md + - Napari Save Coor: napari_save_coor.md - Points: Points.md - Get Centroids: get_centroids.md markdown_extensions: diff --git a/plantcv/annotate/__init__.py b/plantcv/annotate/__init__.py index f9a5ca6..1a4548d 100644 --- a/plantcv/annotate/__init__.py +++ b/plantcv/annotate/__init__.py @@ -6,6 +6,7 @@ from plantcv.annotate.napari_label_classes import napari_label_classes from plantcv.annotate.napari_join_labels import napari_join_labels from plantcv.annotate.napari_save_coor import napari_save_coor +from plantcv.annotate.napari_read_coor import napari_read_coor # Auto versioning __version__ = version("plantcv-annotate") @@ -17,5 +18,6 @@ "napari_open", "napari_label_classes", "napari_join_labels", - "napari_save_coor" + "napari_save_coor", + "napari_read_coor" ] diff --git a/plantcv/annotate/napari_read_coor.py b/plantcv/annotate/napari_read_coor.py new file mode 100755 index 0000000..e571bac --- /dev/null +++ b/plantcv/annotate/napari_read_coor.py @@ -0,0 +1,37 @@ +# Import points from json file + +import json + + +def napari_read_coor(coor, dataformat='yx'): + """ + open img in napari and label classes + + Inputs: + coor = either a dictionary of data or a path to a json file + with dictionary of point coordinates + dataformat = either 'yx' or 'xy'. Output of points function is in + x,y format and Napari is in y,x format. + + Returns: + data = dictionary of data + + :param coor: dict or str + :param dataformat: str + :return data: dictionary of data in y,x format for napari + + """ + if isinstance(coor, dict): + data = coor + else: + with open(coor) as json_file: + data = json.load(json_file) + data1 = {} + if dataformat != 'yx': + keys = list(data.keys()) + for key in keys: + data2 = [(sub[1], sub[0]) for sub in data[key]] + data1.update({key: data2}) + data = data1 + + return data diff --git a/tests/conftest.py b/tests/conftest.py index 4023056..180c56a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,12 @@ def __init__(self): # Kmeans Clustered Gray image self.kmeans_seed_gray_img = os.path.join(self.datadir, "silphium_seed_labeled_example.png") # Small Hyperspectral image - self.envi_sample_data = os.path.join(self.datadir, "corn-kernel-hyperspectral.raw") + filename_hyper = "corn-kernel-hyperspectral.raw" + self.envi_sample_data = os.path.join(self.datadir, filename_hyper) + # Coordinates File + filename_coor = "germinated.txt" + self.coor_data = os.path.join(self.datadir, filename_coor) + @pytest.fixture(scope="session") diff --git a/tests/test_napari_read_coor.py b/tests/test_napari_read_coor.py new file mode 100644 index 0000000..55be5ac --- /dev/null +++ b/tests/test_napari_read_coor.py @@ -0,0 +1,26 @@ +from plantcv.annotate import napari_read_coor + + +def test_napari_read_coor_napari(test_data): + """Test for PlantCV.Annotate""" + # Read in test data + data = napari_read_coor(test_data.coor_data, 'yx') + + assert isinstance(data, dict) + + +def test_napari_read_coor_other(test_data): + """Test for PlantCV.Annotate""" + # Read in test data + data = napari_read_coor(test_data.coor_data, 'xy') + + assert data['germinated'][0] == (10, 25) + + +def test_napari_read_coor_flip(): + """Test for PlantCV.Annotate""" + # Read in test data + coor = {"germinated": [[25, 10]]} + data = napari_read_coor(coor, 'xy') + + assert data['germinated'][0] == (10, 25) diff --git a/tests/testdata/germinated.txt b/tests/testdata/germinated.txt new file mode 100644 index 0000000..d95c88e --- /dev/null +++ b/tests/testdata/germinated.txt @@ -0,0 +1 @@ +{"germinated": [[25, 10]]} \ No newline at end of file