Using python command line from Groovy/QuPath (CellPose)

Hi all! This is something I have been struggling with for a little while, and I suspect it is due to my lack of familiarity with how software in different environments is supposed to interact. I am hoping I am missing something obvious, or the answer is “you can’t get there from here.”

Goal: Use a command line function that works in an Anaconda virtual environment (to run CellPose on a batch of images) within QuPath/Groovy (Windows10).

TLDR: QuPath scripts can call Python scripts, but I am unable to run a command line function in an Anaconda virtual environment from within a QuPath script. And maybe I shouldn’t be?

What I can do:

  1. Call general Python scripts using the Windows command line from QuPath, as seen here:
    How to call python script from Groovy and the python script in turn calls another python script - #6 by Mike_Nelson
    The scripts work, and I have one that can copy a file from one location to another; that works and can output text results to the QuPath log.

  2. Run CellPose and batch process files with a command line like the following:
    python -m cellpose --dir J:/CellPoseProcess/Treated/ --pretrained_model cyto --use_gpu --cellprob_threshold -4.0 flow_threshold 1.4 --diameter 17.9 --chan 0 --save_png --no_npy
    This works within an Anaconda prompt while in the correct (with CellPose installed) virtual environment.

Less important, but my current workflow is:
Load base images into a QuPath project.
QP Script: Export images in a format that can be read by CellPose.
Target that folder with the command listed above in (2).
QP Script2: Import results of CellPose mask into QuPath as detections.

Being able call the that command line and adjust the variables within it would nicely condense everything into a single script that people could use to get CellPose results into QuPath.

I tried to set up CellPose in Python 3.7 rather than Anaconda according to the information here from @oburri
https://c4science.ch/w/bioimaging_and_optics_platform_biop/computers-servers/software/gpu-deep-learning/virtualenv/
Which was very helpful in terms of getting the Anaconda environment to work, since I had massive problems with getting mxnet to work. I might be able to revisit that if it turns out CellPose within an Anaconda venv is problematic.

Appreciate any suggestions, and can, of course, add more info as requested, I just wanted to keep the initial posting short enough to not scare everyone off :slight_smile: The end goal is a single script in QuPath that creates CellPose detections, but it might be that I have my environments set up in such a way that it cannot be done currently.

Cheers,
Mike

Example of CellPose results in QuPath:
https://forum.image.sc/t/does-anyone-have-an-importing-masks-script-corresponding-to-the-working-export-tiles-script/42411/14?u=mike_nelson

1 Like

Just wanted to bump this one more time in case anyone new could see a way to call specific Python environments. If not, I will just let it die :slight_smile:

1 Like

Hello @Mike_Nelson ,

We have been working on something along these lines here

We had used it to run Stardist before @petebankhead had implemented it in QuPath and have been successful using it for CellPose.

There are a couple of examples in that repo and I can try and answer any questions here. Please note it’s still in development.

Best

Oli

3 Likes

Dear Mike and Oli,

I want to thank you both for posting your work on the problem of using CellPose with QuPath. I am also interested in trying this out and your inputs here will surly help me with it.

Best wishes,
Paul

Thanks @oburri, I will take a look.

@anotherBunchOfAtoms, in case it helps (and you had not already found it :slight_smile: ), I currently batch import as shown here:

It is limited to smaller views, though, as it will not position tiles in a larger image.

2 Likes

Cool! Then we are complementary :slight_smile:

Our code tries to work in tiles, sends the tiles to python and places the rois back in the right position! I’d be very happy to collaborate on making something as general as possible, ideally as a Qupath command.

Let me know if we could work something out

A the best

Oli

2 Likes