Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

napari save #17

Merged
merged 14 commits into from
Sep 12, 2024
Merged
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ See [this page](https://plantcv.readthedocs.io/en/latest/pr_review_process/) for
- [ ] Test coverage remains 100%
- [ ] Documentation tested
- [ ] New documentation pages added to `plantcv/mkdocs.yml`
- [ ] Changes to function input/output signatures added to `updating.md`
- [ ] Changes to function input/output signatures added to `changelog.md`
- [ ] Code reviewed
- [ ] PR approved
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_save_coor

* v0.1dev: datadict = **annotate.napari_save_coor**(*viewer, filepath*)

#### annotate.Points

* v0.1dev: viewer = **annotate.Points**(*img, figsize=(12,6), label="dafault"*)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions docs/napari_save_coor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Save Napari Labels to File

Save Points Labeled in Napari to a File

**plantcv.annotate.napari_save_coor**(*viewer, filepath*)

**returns** dictionary of points labeled by class

- **Parameters:**
- viewer - Napari Viewer Object
- Filepath - File to save data. If the file exits an extension will be added.

- **Context:**
- Save points labeled in Napari to a file to checkpoint annotation progress or reuse.

- **Example use:**
- Save points labeled to a file


```python
import plantcv.plantcv as pcv
import plantcv.annotate as pcvan

# Create an instance of the Points class
img, path, name = pcv.readimage("./grayimg.png")
# Should open interactive napari viewer
viewer = pcvan.napari_label_classes(img=img, classes=['background', 'wing','seed'])

dictobj = pcvan.napari_save_coor(viewer, 'testdata.txt')

```

![Screenshot](img/documentation_images/napari_label_classes/napari_label_classes.png)

![Screenshot](img/documentation_images/napari_save_coor/napari_save.png)



**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/napari_save_coor.py)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ nav:
- Napari Join: napari_join_labels.md
- Napari Label: napari_label_classes.md
- Napari Open: napari_open.md
- Napari Save: napari_save_coor.md
- Points: Points.md
- Get Centroids: get_centroids.md
markdown_extensions:
Expand Down
6 changes: 4 additions & 2 deletions plantcv/annotate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from plantcv.annotate.napari_open import napari_open
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

# Auto versioning
__version__ = version("plantcv-annotate")
Expand All @@ -15,5 +16,6 @@
"napari_classes",
"napari_open",
"napari_label_classes",
"napari_join_labels"
]
"napari_join_labels",
"napari_save_coor"
]
40 changes: 40 additions & 0 deletions plantcv/annotate/napari_save_coor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Save Napari Data to a File

import json
import os
from plantcv.annotate import napari_classes


def napari_save_coor(viewer, filepath):
"""
save napari labeled points to a file

Inputs:
viewer = Napari viewer object
filepath = path to file to save data to

Returns:
dictionary = dictionary of saved data points

:param viewer: napari viewer object
:param filepath: str
:return dictionary: dictionary

"""
classes = napari_classes(viewer)
datadict = {}

for label in classes:
coordict = []
for _, (x, y) in enumerate(viewer.layers[label].data):
x = int(x)
y = int(y)
coordict.append((x, y))
datadict.update({label: coordict})

if os.path.exists(filepath):
filepath = str(filepath)+"_1.txt"
with open(filepath, 'w') as fp:
json.dump(datadict, fp)

return datadict
25 changes: 25 additions & 0 deletions tests/test_napari_save_coor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np
import os
from plantcv.annotate import napari_label_classes
from plantcv.plantcv import readimage
from plantcv.annotate import napari_save_coor


def test_napari_save_coor(test_data, tmpdir):
"""Test for PlantCV.Annotate"""
# Read in test data
cache_dir = tmpdir.mkdir("cache")
img, _, _ = readimage(test_data.kmeans_seed_gray_img)
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)

filename = os.path.join(cache_dir, 'tempfile.txt')
# Assert that the file was created
_ = napari_save_coor(viewer, filename)
# Triggers addtion of _1 since filename previously exists
_ = napari_save_coor(viewer, filename)

assert os.path.exists(filename)
viewer.close()