Skip to content

Commit

Permalink
Merge pull request #20 from danforthcenter/18-napari-import-data
Browse files Browse the repository at this point in the history
napari read coor
  • Loading branch information
HaleySchuhl authored Sep 12, 2024
2 parents 1dd5436 + 46e55f1 commit 4324cdd
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*)
Expand Down
30 changes: 30 additions & 0 deletions docs/napari_read_coor.md
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion plantcv/annotate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -17,5 +18,6 @@
"napari_open",
"napari_label_classes",
"napari_join_labels",
"napari_save_coor"
"napari_save_coor",
"napari_read_coor"
]
37 changes: 37 additions & 0 deletions plantcv/annotate/napari_read_coor.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_napari_read_coor.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/testdata/germinated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"germinated": [[25, 10]]}

0 comments on commit 4324cdd

Please sign in to comment.