-
Notifications
You must be signed in to change notification settings - Fork 0
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
HaleySchuhl
merged 15 commits into
main
from
33-napari-extract-and-format-data-for-naive-bayes
Sep 20, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7eeba94
added function and test
maliagehan 776abd6
documentation added
maliagehan b1df8e4
bgr to rgb
maliagehan fd049f4
Merge branch 'main' into 33-napari-extract-and-format-data-for-naive-…
HaleySchuhl bee8425
Update changelog.md
HaleySchuhl 148f421
docs syntax and link to upstream dir
HaleySchuhl 6a70615
reflect the plural function name
HaleySchuhl 9f67eaa
update to plural in changelog
HaleySchuhl 027c1f8
update python filename for consistency
HaleySchuhl b76a374
Update mkdocs.yml
HaleySchuhl 4b62b86
rename dir with function name
HaleySchuhl a809057
Update napari_naive_bayes_colors.md
HaleySchuhl 8d27f95
Update test_napari_naive_bayes.py
HaleySchuhl a7e8c0f
rename in init
HaleySchuhl b30f87a
Add context that this is a replacement for imageJ pixel collection
HaleySchuhl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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