-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from danforthcenter/33-napari-extract-and-form…
…at-data-for-naive-bayes 33 napari extract and format data for naive bayes
- Loading branch information
Showing
10 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
""" | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.