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

33 napari extract and format data for naive bayes #34

Merged
merged 15 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ All notable changes to this project will be documented below.

* v0.1dev: viewer = **annotate.napari_label_classes**(*img, classes, size=10, importdata=False, show=True*)

#### annotate.napari_naive_bayes_colors

* v0.1dev: viewer = **annotate.napari_naive_bayes_colors**(*img, maskdict, filename*)

#### annotate.napari_open

* v0.1dev: viewer = **annotate.napari_open**(*img, mode = 'native', show=True*)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions docs/napari_naive_bayes_colors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Extract Color Data from Dictionary of Masks for Naive Bayes

This function is to extract color information from a dictionary of masks in order
to get data for naive bayes functions. Collect pixel training data in Napari, rather than ImageJ for example.

**plantcv.annotate.napari_naive_bayes_colors**(*img, maskdict, filename*)

**returns** data frame

- **Parameters:**
- img - RGB image to extract color information from
- maskdict - dictionary of masks, output of [`napari_points_mask`](docs/napari_points_mask.md) for example
- filename - filename to save data, formatted to work with [Naive Bayes segmentation](https://plantcv.readthedocs.io/en/latest/tutorials/machine_learning_tutorial/)

- **Context:**
- This function is used to extract color information from an RGB image and a mask and convert data
to be compatible with Naive Bayes training functions.

- **Example use:**
- used in Napari Naive Bayes


```python
import plantcv.plantcv as pcv
import plantcv.annotate as pcvan
from plantcv import learn
import napari

# Create an instance of the Points class
img, path, name = pcv.readimage("./wheat.png")

# Should open interactive napari viewer
viewer = pcvan.napari_label_classes(img=img, classes=['background','healthy', 'rust', 'chlorosis'], size=4)

maskdict = pcvan.napari_points_mask(img, viewer)

nbdata = pcvan.napari_naive_bayes_colors(img=img, maskdict=maskdict, filename="./nbdata.txt")

learn.naive_bayes_multiclass(samples_file="./nbdata.txt", outfile="naive_bayes_test_pdfs.txt")

masks = pcv.naive_bayes_classifier(rgb_img=img,
pdf_file="./naive_bayes_test_pdfs.txt")

```

![Screenshot](img/documentation_images/napari_points_mask/viewer_labeled.png)

***Output Data***

![Screenshot](img/documentation_images/napari_naive_bayes_colors/nbdata.png)


**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/napari_naive_bayes_colors.py)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ nav:
- Napari Classes: napari_classes.md
- Napari Join: napari_join_labels.md
- Napari Label: napari_label_classes.md
- Napari Naive Bayes: napari_naive_bayes_colors.md
- Napari Open: napari_open.md
- Napari Points Mask: napari_points_mask.md
- Napari Read Coor: napari_read_coor.md
Expand Down
4 changes: 3 additions & 1 deletion plantcv/annotate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from plantcv.annotate.napari_save_coor import napari_save_coor
from plantcv.annotate.napari_read_coor import napari_read_coor
from plantcv.annotate.napari_points_mask import napari_points_mask
from plantcv.annotate.napari_naive_bayes_colors import napari_naive_bayes_colors

# Auto versioning
__version__ = version("plantcv-annotate")
Expand All @@ -21,5 +22,6 @@
"napari_join_labels",
"napari_save_coor",
"napari_read_coor",
"napari_points_mask"
"napari_points_mask",
"napari_naive_bayes_colors"
]
40 changes: 40 additions & 0 deletions plantcv/annotate/napari_naive_bayes_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Get Data from Dictionary of Masks and Format for Naive Bayes

import pandas as pd
import numpy as np


def napari_naive_bayes_colors(img, maskdict, filename):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in future iterations, rename the parameter to rgb_img

"""
get names of napari keys

Inputs:
img = rgb image
maskdict = dictionary of masks, output of napari_points_mask for example
filename = name/path of file to save data

Returns:
dataarray = pandas data array

:param img: RGB image
:param maskdict = dictionary of masks
:param filename = str
:return dataarray: pandas dataframe
"""
keys = list(maskdict.keys())
datadict = {}
for key in keys:
mask = maskdict[key]
nonzero = np.transpose(np.nonzero(mask))
keydata = []
for x, y in nonzero:
rgbdata = list(reversed(img[x][y]))
rgblist = ", ".join(repr(int(e)) for e in rgbdata)
keydata.append(rgblist)
dict1 = {key: keydata}
datadict.update(dict1)
dataarray = pd.DataFrame.from_dict(datadict, orient='index')
datatranspose = dataarray.transpose()
datatranspose.to_csv(filename, sep="\t", index=False)

return datatranspose
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self):
# Coordinates File
filename_coor = "germinated.txt"
self.coor_data = os.path.join(self.datadir, filename_coor)
# Naive Bayes Data
filename_nbrgb = "08_02_16-QTHJ-RIL-019_zoomed.png"
self.nb_rgb = os.path.join(self.datadir, filename_nbrgb)
filename_nbmask = "my_maskdict.npy"
self.nb_mask = os.path.join(self.datadir, filename_nbmask)



Expand Down
16 changes: 16 additions & 0 deletions tests/test_napari_naive_bayes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import numpy as np
from plantcv.plantcv import readimage
from plantcv.annotate import napari_naive_bayes_colors


def test_napari_naive_bayes_colors(test_data, tmpdir):
"""Test for PlantCV.Annotate"""
# Read in test data
cache_dir = tmpdir.mkdir("cache")
img, _, _ = readimage(test_data.nb_rgb)
maskdict = np.load(test_data.nb_mask, allow_pickle='TRUE').item()
filename = os.path.join(cache_dir, 'tempfile.txt')
data = napari_naive_bayes_colors(img, maskdict, filename)

assert data.shape == (16, 1)
Binary file added tests/testdata/08_02_16-QTHJ-RIL-019_zoomed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testdata/my_maskdict.npy
Binary file not shown.