Help writing script with QuPath

Hi!

I am using Qupath v2m4 to analyse some whole slide images. I have been able to automate some very basic analysis with the script editor on older version of QuPath: cell detection, load classifier and export annotation and detection results. Now, I wanted to add to the analysis also spot detection. I have no experience with groovy at all, so I am sure there may be something wrong with my script (I feel it may be in the export result part), but I can manage to fix it.

What I am doing is, cell detection first, then I apply the classifier and after that I perform subcellular detection. At the end of the script I export both the annotation and detection results:

setImageType('FLUORESCENCE');

// select annotations
selectAnnotations();

// cell detection
runPlugin('qupath.imagej.detect.cells.WatershedCellDetection', '{"detectionImage": "DAPI 100% - 345/455",  "requestedPixelSizeMicrons": 0.5,  "backgroundRadiusMicrons": 0.0,  "medianRadiusMicrons": 0.5,  "sigmaMicrons": 1.2,  "minAreaMicrons": 20.0,  "maxAreaMicrons": 150.0,  "threshold": 1000.0,  "watershedPostProcess": true,  "cellExpansionMicrons": 1.0,  "includeNuclei": true,  "smoothBoundaries": true,  "makeMeasurements": true}');

// Load and run a trained classifier
def pathClassifier = buildFilePath(PROJECT_BASE_DIR , 'classifiers', '190822_classifier.qpclassifier')
runClassifier(pathClassifier);

// Run the foci detection
runPlugin('qupath.imagej.detect.cells.SubcellularDetection', '{"detection[Channel 1]": -1.0,  "detection[Channel 2]": 7000.0,  "detection[Channel 3]": -1.0,  "doSmoothing": true,  "splitByIntensity": true,  "splitByShape": false,  "spotSizeMicrons": 1.0,  "minSpotSizeMicrons": 0.1,  "maxSpotSizeMicrons": 2.0,  "includeClusters": false}');

// Export the results - Annotations
def name = getProjectEntry().getImageName() + '.txt'
def pathAnnotation = buildFilePath(PROJECT_BASE_DIR, 'annotation results')
mkdirs(pathAnnotation)
pathAnnotation = buildFilePath(pathAnnotation, name)
saveAnnotationMeasurements(pathAnnotation)
print 'Results exported to ' + pathAnnotation

// Export the results - Detections
def pathDetection = buildFilePath(PROJECT_BASE_DIR, 'detection results')
mkdirs(pathDetection)
pathDetection = buildFilePath(pathDetection, name)
saveDetectionMeasurements(pathDetection)
print 'Results exported to ' + pathDetection

The script seemed to work when I ran it. No error popped and it was able to perform the 74 tasks. However, once done, it started 2 more tasks and at this point it freezed. I can get it to work with smaller annotations, but if I try to use annotations that cover the entire tissue it freezes as explained.

Any help with that? I thought there was a memory problem, so I tried on a more powerful computer but it didn’t solve the problem. Am I missing something in the script?

Any help is more that welcome!
Many thanks

I can’t be completely sure, but I would guess that the 74 is tiles for the cell detection, and the program is freezing for the subcellular detection part (2 annotations? That was the last thing your script selected). For some reason this can be very slow on large images. I should really add more of a warning to the guide. I had one whole slide 4 channel image take 3 days on a fairly decent computer. It seemed to freeze, but would eventually work through it. I have been looking into other options for large images, but haven’t had a project to test it on yet.

Consider scripting the selection of each cell in turn and running subcellular detection on that.
Or generate tiles prior to cell detection, and select tile by tile (in a script) and run subcellular detection on each in turn.

One last potential problem, you may be running into a size limit for the data file (.qpdata). I also ran into this way way back when looking at ISH staining in whole slide images, and had to start removing measurements in order to save the file successfully. I think I ran into issues around 4GB.

I haven’t really used the subcellular detection command much myself (despite writing it…); there is a lot of room for improvement when it comes to working on analyzing large areas of large images. It shouldn’t be necessary to wait 3 days for it… but I’m not sure when I’ll have time to look into why it is taking that long.

My guess (without checking) is that the biggest delay may be connected to updating the user interface. You may find that running a script through Run → Run for project is faster than running it interactively from the script editor, because when it runs interactively QuPath has to do a lot more work to update the interface. But be sure to backup your project in advance since then QuPath will automatically overwrite data files after the script completes (if it can).

Thanks both for replying. I tried what Pete suggested, to Run for project instead of running it interactively, but it didn’t work. Actually, it was slower and it didn’t even start the cell detection.

I am working on the scripting suggested, it will take me some time. I am not very ‘fluent’ with groovy but I will let you know if I manage to solve the problem.
In the meantime, I ran the script on a very small annotation and looked at the exported detection results. What I have noticed is that QuPath stored the information of the single cell (area, position, staining intensities etc, and the number of spots counted in the cell), called “PathCellObject” in the Name column. However, it also collected the information of each single spot (spot area, staining intensities etc), called “Subcellular spot: Channel 2 object” in the Name column.

Here is an example of the result I have obtained (let me know if you cannot access it):
Image_CDX4_IR_24HR_20190829.vsi - EFI 40x.txt (44.8 KB)

I don’t really need that information (“Subcellular spot: Channel 2 object”), I only want to know how many spot are in each cell. If I was able to remove the information about each single spot, I am sure the result file would be smaller and maybe I would be able to run the script on the entire tissue with no problem.

Do you know if it is possible to avoid recording the information related to each spots, without losing the information of the total number of spots per cell?

Thanks a lot for your help!

I generally copy the spot count to a new variable, then run an empty subcellular detection command to remove all of the spots. They also slow down the viewer over very large regions with very large spot counts. I only keep the actual spots if I need to do more calculations on them (R^2, colocalization), or for visualization during thresholding and classification.

If your cell detection isn’t running, it sounds like you might not have an annotation. You can’t just run a cell detection, though I had initially assumed you were creating annotations manually since that was not included in your script. There is an option to create a select all object that is handy if you are analyzing fields of view, or want to look at “everything.”

Thanks for your reply. I have annotations. I usually select the entire tissue and use it as annotation. The cell detection didn’t start when I used the Run → Run for project option suggested by Pete. I mean, the command started but never properly began to run the tasks. While if I ran the script from the editor it does the cell detection and then freeze for some reason and never generate a result file. I can only get the result file if my annotations are very small.

I am not sure I am clear enough. Here are some screenshots of what i consider a big annotation and a small annotation. I hope this can help.

50

22

I will see if I can manage to export the information I need to a new variable and remove the one that I don’t need.
Thanks for your suggestion.

Did it show any error, or a message under View → Show log?

Note that when using Run for Project it won’t show a dialog with a progress bar for each task - only a single dialog with a progress bar that increments after each image finished processing.

Also, saveDetectionMeasurements can be problematic if you have a large number of objects/measurements. Technically the amount of text being exported needs to be less than 2^31 characters in length… that could be where the problem lies and the results need to be exported some other way.

However before trying to solve that, I’d suggest removing that bit of a script to see if everything else works in case it isn’t the issue after all.

One last thing, Run for project (as I mention in the guide!), does not show any results on the image you are in. You would have to leave that image (without saving) and re-enter it in order to see any results. It can be run without any image open at all, in fact, and has no interaction with the currently open image, just its data file if you select it as one of the images to run the script on.

If I remove the saveDetectionMeasurements, nothing changes. I get the same problem. So it may not be the problem. Below is a screenshot with the message I get in the log window:

At this point the software stall or, as suggested by Research_Associate, would probably work but it needs a lot of time to run the 2 remaining tasks. From the log window it seems to freeze after the classifier, when the subcellular detection should start.

When it freeze like this, I have to quit from Qupath, because even Cancel takes ages to run. However, when I quit, the software asks me if I want to save the image I was working on and if I say yes, and then reopen that image, it looks like the software did the analysis at the end (cell detection, classifier and subcellular detection), as you can see here:

What I don’t have is the exported result file. Also, if I double click on one of the analysed cell to get the annotation information, it works but I also get this very long error (I am not sure it can be helpful). Otherwise I don’t get any other error. Maybe I just need to leave the script running overnight or longer and see if it works. The problem is that I have 30 slides to analyse, so it may be quite time consuming…

Here is the error:
ERROR.txt (86.1 KB)

Also, it is not true that the software does not start the cell detection when I run the script with Run → Run for project. Sorry for that. It does work, but then it stalls as it does when I ran the script from the editor. Always after the classifier:

I suspect that if you killed it when it seemed to freeze, you stopped it partway through the subcellular detection. It is writing those as it goes, so there is probably some part of the file where you stop getting subcellular detections.

Not 100% sure, but just my guess. Probably just needs more time. Try giving it 5, 10, 15 minutes before killing it when Run for project, and see if you get a greater percentage of cells with subcellular detections each time. Use Measurement Maps to see how many subcellular detections are where. I suspect they will fill down from the top.

The error message is consistent with my first suspicion, that it’s the GUI updating - specifically the tree view under the ‘Hierarchy’ tab.

Under the preferences in recent milestones you should be able to change a setting (using a drop down menu) to avoid showing all detections in the hierarchy - which can stop many freezes when you have a lot of detections.

Answering from my phone so unable to check the exact name of the preference… but if you search in the preferences for hierarchy it should appear.

The Cancel button is of rather limited use… it generally finished the current task - which is often precisely the one that is taking too long…

Actually it will only write then once they’ve all been detected… if you see them start and the top and then stop abruptly that sounds to me like it suffered an out of memory error while running the detection. If QuPath is quit early I think you’ll probably end up with nothing written at all or a corrupt .qpdata file (hopefully the former… since it’s meant to retain the old .qpdata file until everything has been written).

Oh weird, maybe I got that confused with something else. Or I was using a different selection method. I know you can either select Detections and run it, or select Annotations. Maybe I was thinking of something else entirely though! Been a hectic week :slight_smile:

1 Like

Hi,

At the end I tried to leave the script running overnight and it did work. Run → Run for project made it a bit faster (It took more or less 2hr for the bigger slides), so I could ran all the slides overnight. Thanks for your help, and sorry if I wasted your time. It was less complicated than I thought.

Cheers

1 Like