plantcv icon indicating copy to clipboard operation
plantcv copied to clipboard

An issue with importing plantcv.

Open lamvanbio opened this issue 2 years ago • 15 comments

Hi plantcv team, I am interested in plantcv for my research project. I installed the plantcv as instruction, however, it has error when I import the required packages as below. Could you please guide me how to solve this problem. Best regards, Lam

%matplotlib notebook

Import statements

import os import argparse import matplotlib import numpy as np from plantcv import plantcv as pcv

ModuleNotFoundError Traceback (most recent call last) Input In [3], in <cell line: 7>() 5 import matplotlib 6 import numpy as np ----> 7 from plantcv import plantcv as pcv

File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv_init_.py:20, in 18 from plantcv.plantcv.rgb2gray_cmyk import rgb2gray_cmyk 19 from plantcv.plantcv.gaussian_blur import gaussian_blur ---> 20 from plantcv.plantcv import transform 21 from plantcv.plantcv import hyperspectral 22 from plantcv.plantcv import spectral_index

File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\transform_init_.py:15, in 13 from plantcv.plantcv.transform.nonuniform_illumination import nonuniform_illumination 14 from plantcv.plantcv.transform.resize import resize, resize_factor ---> 15 from plantcv.plantcv.transform.warp import warp, warp_align 16 from plantcv.plantcv.transform.gamma_correct import gamma_correct 18 all = ["get_color_matrix", "get_matrix_m", "calc_transformation_matrix", "apply_transformation_matrix", 19 "save_matrix", "load_matrix", "correct_color", "create_color_card_mask", "quick_color_check", 20 "find_color_card", "rescale", "nonuniform_illumination", "resize", "resize_factor", 21 "warp", "rotate", "warp", "warp_align", "gamma_correct"]

File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\transform\warp.py:10, in 8 from plantcv.plantcv import fatal_error 9 from plantcv.plantcv import color_palette ---> 10 from plantcv.plantcv.visualize import overlay_two_imgs 11 from plantcv.plantcv.transform import rescale 12 from skimage import img_as_ubyte

File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\visualize_init_.py:3, in 1 from plantcv.plantcv.visualize.pseudocolor import pseudocolor 2 from plantcv.plantcv.visualize.colorize_masks import colorize_masks ----> 3 from plantcv.plantcv.visualize.histogram import histogram 4 from plantcv.plantcv.visualize.clustered_contours import clustered_contours 5 from plantcv.plantcv.visualize.colorspaces import colorspaces

File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\visualize\histogram.py:10, in 8 from plantcv.plantcv._debug import _debug 9 import pandas as pd ---> 10 from plotnine import ggplot, aes, geom_line, labels, scale_color_manual 13 def _hist_gray(gray_img, bins, lower_bound, upper_bound, mask=None): 14 """Prepare the ready to plot histogram data. 15 16 Inputs: (...) 35 :return hist_gray_data: numpy.ndarray 36 """

File ~.conda\envs\plantcv\lib\site-packages\plotnine_init_.py:9, in 7 from .coords import * # noqa: F401,F403,E261 8 from .geoms import * # noqa: F401,F403,E261 ----> 9 from .stats import * # noqa: F401,F403,E261 10 from .scales import * # noqa: F401,F403,E261 11 from .facets import * # noqa: F401,F403,E261

File ~.conda\envs\plantcv\lib\site-packages\plotnine\stats_init_.py:7, in 5 from .stat_boxplot import stat_boxplot 6 from .stat_density import stat_density ----> 7 from .stat_density_2d import stat_density_2d 8 from .stat_ecdf import stat_ecdf 9 from .stat_ellipse import stat_ellipse

File ~.conda\envs\plantcv\lib\site-packages\plotnine\stats\stat_density_2d.py:3, in 1 import numpy as np 2 import pandas as pd ----> 3 import matplotlib._contour as _contour 4 from mizani.breaks import extended_breaks 6 from ..doctools import document

ModuleNotFoundError: No module named 'matplotlib._contour'

lamvanbio avatar Sep 28 '22 03:09 lamvanbio

Hi @lamvanbio, we recently ran into this issue, it appears that the latest version of matplotlib breaks the latest version of plotnine. I think if you delete your current plantcv environment and reinstall it like the following it should fix the issue:

# Remove the plantcv environment
conda env remove -n plantcv

# Install and limit the upper version of matplotlib
conda create -n plantcv plantcv 'matplotlib<3.6'

nfahlgren avatar Sep 28 '22 15:09 nfahlgren

Hi Noah, Thank you for your reply. I have remove plantcv environment and reinstall it as you suggested. However, I do not see jupyter notebook in plantcv environment in Anaconda Navigator. I did try to run below codes in Anaconda Prompt, I got this message: (plantcv) C:>from plantcv import plantcv as pcv 'from' is not recognized as an internal or external command, operable program or batch file.

import os import argparse import matplotlib import numpy as np from plantcv import plantcv as pcv

Could you please suggest what I should do. Thank you. Best regards,

lamvanbio avatar Sep 29 '22 01:09 lamvanbio

To add jupyter to your environment, with Anaconda Prompt:

conda activate plantcv
conda install -c conda-forge nb_conda jupyterlab ipympl nodejs

This is also possible with the Anaconda Navigator program, I am just less familiar with it myself.

Once those packages install you can run with Anaconda Prompt:

jupyter lab

nfahlgren avatar Sep 29 '22 01:09 nfahlgren

I activated plantcv and run the second code you suggested on Anaconda Prompt. However, I still get this issue: Preparing transaction: done Verifying transaction: done Executing transaction: / Enabling nb_conda_kernels... CONDA_PREFIX: C:\Program Files\Anaconda3\envs\plantcv Status: enabled

  • Config option kernel_spec_manager_class not recognized by EnableNBExtensionApp. Enabling notebook extension nb_conda/main... - Validating: ok Enabling tree extension nb_conda/tree... - Validating: ok Config option kernel_spec_manager_class not recognized by EnableServerExtensionApp. Enabling: nb_conda
  • Writing config: C:\Program Files\Anaconda3\envs\plantcv\etc\jupyter
    • Validating... nb_conda 2.2.1 ok

done

(plantcv) C:>from plantcv import plantcv as pcv 'from' is not recognized as an internal or external command, operable program or batch file.

lamvanbio avatar Sep 29 '22 02:09 lamvanbio

You are getting that error because you are in Anaconda Prompt but not in Python. You can start a Python prompt in Anaconda by running python but I would recommend starting jupyter lab instead

nfahlgren avatar Sep 29 '22 02:09 nfahlgren

That's great. It works now. Thank you for your support.

lamvanbio avatar Sep 29 '22 02:09 lamvanbio

Hi Noah, I have a problem as below when reading the image using this code: img, path, filename=pcv.readimage(filename=args.image)

Please suggest me how to deal with this. Thank you.

RuntimeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_9476\800198661.py in <cell line: 1>() ----> 1 img, path, filename=pcv.readimage(filename=args.image)

C:\Program Files\Anaconda3\envs\plantcv\lib\site-packages\plantcv\plantcv\readimage.py in readimage(filename, mode) 49 50 if img is None: ---> 51 fatal_error("Failed to open " + filename) 52 53 # Split path from filename

C:\Program Files\Anaconda3\envs\plantcv\lib\site-packages\plantcv\plantcv\fatal_error.py in fatal_error(error) 11 :return: 12 """ ---> 13 raise RuntimeError(error)

RuntimeError: Failed to open imgs/10.9.1.29_pos-153-001-003_2019-10-26-19-05.jpg

lamvanbio avatar Sep 29 '22 03:09 lamvanbio

It is saying it cannot open the image - typically this would be caused by the path to the image being incorrect. Based on the path imgs/10.9.1.29_pos-153-001-003_2019-10-26-19-05.jpg the folder imgs would need to be in the same location as your Jupyter notebook, otherwise the path needs to be updated.

nfahlgren avatar Sep 29 '22 04:09 nfahlgren

I am trying to follow your tutorial on youtube (https://www.youtube.com/watch?v=zvn_05cE0L4&t=691s) and I want to use the picture you showed. I am trying to file the folder imgs in my computer but I could not find it. I am wondering when we install plantcv, we will have that folder with images in our computer? Or do I need to copy that folder to my computer?

Thank you.

lamvanbio avatar Sep 29 '22 05:09 lamvanbio

Hi @lamvanbio, the workshop materials are available separately from plantcv installation and can be downloaded from here (for that workshop): https://github.com/danforthcenter/plantcv-nappn2021-workshop

nfahlgren avatar Sep 29 '22 14:09 nfahlgren

Thanks Noah, I can load the image now. I am going to take the image of my plants soon. I am wondering what is background for the image?

Thank you. Lam

lamvanbio avatar Oct 04 '22 05:10 lamvanbio

Hi Noah, I got error when I try below codes. Could you please help. Thank you.

Plot histogram of grey scale

hist=pcv.visualize.histogram(gray_img=a)


TypeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_13620\4182240071.py in <cell line: 2>() 1 # Plot histogram of grey scale ----> 2 hist=pcv.visualize.histogram(gray_img=a)

TypeError: histogram() got an unexpected keyword argument 'gray_img'

lamvanbio avatar Oct 04 '22 06:10 lamvanbio

pcv.visualize.histogram was updated to support both RGB and grayscale input images, so the keyword was changed from gray_img to img: https://plantcv.readthedocs.io/en/stable/visualize_histogram/

This page in the documentation records all changes to function inputs and outputs by version: https://plantcv.readthedocs.io/en/stable/updating/#plantcvvisualizehistogram

nfahlgren avatar Oct 05 '22 19:10 nfahlgren

Thanks Noah,

I got it now. I am working on wheat and below are what I have processed. I attached two photos at the step of Set Threshold and obj. As I see, wheat leaves are not flat (they have some twists) and at the twists the "white color" is broken. I am wondering if you have a solution for this.

Thank you. Best regards, Lam

20221006 plantcv wheat pink 20221006 plantcv wheat black_white

lamvanbio avatar Oct 05 '22 23:10 lamvanbio

Hi @lamvanbio ,

I often find improved segmentation of tricky images by combining multiple masks from different channels. It's hard for me to say which channel might be best for picking up those parts of leaves without getting any background without having the original image to test on. I'd recommend running colorspace_img = pcv.visualize.colorspaces(rgb_img=img) and then use pcv.image_add or another method to combine. An example workflow where we use more than one colorspace can be found here.

Another approach that sometimes works is to be aggressive/inclusive with the first threshold to avoid major gaps, and then use clean-up functions (such as pcv.fill , pcv.fill_holes, or pcv.erode) to remove the noise that remains.

HaleySchuhl avatar Oct 07 '22 15:10 HaleySchuhl

Thanks Haley, I will try that. Best regards, Lam

lamvanbio avatar Oct 10 '22 02:10 lamvanbio

Hi Haley,

I have tried what you suggested. I am happy with that. However, I have two plants/image. Please suggest me how I can get the results of them separately? Thank you. Best regards, Here is what I get. image

lamvanbio avatar Oct 13 '22 04:10 lamvanbio

@lamvanbio awesome!

Next, to separate your objects I would usually define one ROI per object and then use a Python for loop to filter out each plant, do object_composition, and all measurements. We have a tutorial here that has an example of how to analyze multiple plants within a single workflow.

HaleySchuhl avatar Oct 13 '22 12:10 HaleySchuhl

Thanks Haley,

I will try that. I have a issue to open Jupyter Notebook after I install plantcv in my Macbook. It work well in my windows computers. However, I could not open Jupyter Notebook in plantcv environment. Could you please suggest me how to solve that.

Best regards, Lam

lamvanbio avatar Oct 14 '22 00:10 lamvanbio

Hi @lamvanbio, did you install Jupyter with your plantcv environment? We don't currently do that automatically because it's not strictly a dependency for using PlantCV. If you haven't yet, I posted the commands above: https://github.com/danforthcenter/plantcv/issues/961#issuecomment-1261646222

nfahlgren avatar Oct 14 '22 01:10 nfahlgren

Hi Noah,

I did install it using the code you sent me last time. However, when I launched the Jupyter Notebook, it did not open the Jupyter (web). Here is the image of plantcv with Jupyter Notebook.

Screen Shot 2022-10-14 at 10 44 22 pm

lamvanbio avatar Oct 14 '22 12:10 lamvanbio

Hi @lamvanbio, all I am seeing in the screenshot is Anaconda Navigator. Does clicking the Launch button on the Jupyter Notebook tile not work?

I only use Miniconda and launch Jupyter from the command-line so I'm not as familiar with using Anaconda

nfahlgren avatar Oct 14 '22 20:10 nfahlgren

Hi Noah, I could not open Jupyter Notebook but I can open JupyterLab. So, that's fine with me. At this stage, my plants are big so I take only one plant/photo. I am wondering if I can select by using "rectangular" instead of using circle as below code:

Define region of interest

roisl, roi_hierarchyl=pcv.roi.multi(img=rot_img, coord=(1600, 1200), radius=1100, spacing=(500, 415), nrows=1, ncols=1)

I did try to use below code and it works but then I cannot run following codes. Could you please suggest me to solve that?

Define the region of interest (ROI)

Inputs:

img - RGB or grayscale image to plot the ROI on

x - The x-coordinate of the upper left corner of the rectangle

y - The y-coordinate of the upper left corner of the rectangle

h - The height of the rectangle

w - The width of the rectangle

roisl, roi_hierarchy= pcv.roi.rectangle(img=rot_img, x=330, y=1000, h=1300, w=2900)

lamvanbio avatar Oct 16 '22 04:10 lamvanbio

We need the error you are seeing in order to help

nfahlgren avatar Oct 16 '22 16:10 nfahlgren

Hi Noah, If I use circle to select area of interest, it does not cover all. Can I use rectangular or other shape instead? I also have error as in the last image.

Screen Shot 2022-10-17 at 9 35 32 pm Screen Shot 2022-10-17 at 9 36 00 pm Screen Shot 2022-10-17 at 9 42 12 pm Screen Shot 2022-10-17 at 9 42 26 pm

lamvanbio avatar Oct 17 '22 11:10 lamvanbio

Can I use rectangular or other shape instead?

Yes, the functions are meant to be modular so you can use the ROI functions interchangeably (mostly).

Also note that the tutorials are meant to be example workflows, they can be modified any way you need to work with your specific data.

When you use the ROI with pcv.roi_objects and set roi_type="partial", the ROI does not need to fully enclose the entire plant - all objects that overlap with the ROI are kept. In your case, because the outer leaves are being detected as separate objects, they don't overlap with the ROI. But you are correct that a rectangular ROI would be easier to use in this case.

I also have error as in the last image.

The error is saying that args.filename is not defined. You could comment out that line, it's saving the plotted image to a file. Or, replace args.filename with a variable containing the input image filename. For example, if you have a line like this:

img, filepath, filename = pcv.readimage(filename="input_image.png")

Then filename is the name of the input image file and can be used instead of args.filename

nfahlgren avatar Oct 17 '22 16:10 nfahlgren

Thanks Noah,

Now I am trying to run analysis. I follow the workshop from youtube. I will try with the images you provide on github. As I understand, I need to code files: completed_multi_plant_notebook.py and multi-plant-analysis.config. Is that correct? Also, I think I need to download these files into my working directory. Is that right? I try to download this file into my working directory but I could not find the way to download them. Could you please let me know how to get it.

Thank you.

lamvanbio avatar Oct 20 '22 09:10 lamvanbio