QuPath scripting - annotations deleted when running cell detection


I’m doing my first steps in QuPath scripting, trying to automate a workflow
I encounter the following problem:

I have a hierarchy of objects as seen here

When I run the following script to detect cells, the “Tumor” annotation and all it’s derived annotations are deleted.
How can I avoid this ?
I’m running version 0.2.2 compiled locally with TensorFlow

// Detect Cells
runPlugin('qupath.imagej.detect.cells.WatershedCellDetection', '{"detectionImage": "Blue", "requestedPixelSizeMicrons": 0.5, "backgroundRadiusMicrons": 6.0, "medianRadiusMicrons": 1.0, "sigmaMicrons": 2.0, "minAreaMicrons": 10.0, "maxAreaMicrons": 400.0, "threshold": 3.0, "watershedPostProcess": true, "cellExpansionMicrons": 5.0, "includeNuclei": true, "smoothBoundaries": true, "makeMeasurements": true}');


Could you run the cell detection prior to the steps where you manipulate the annotations?

Otherwise you could store them as a variable and re-add them later.


I’m also having the same problem and hopefully @petebankhead can have a look at this?

It seems like a bug or an unintended behaviour of the Cell Detection tool. This is still present on QuPath v0.2.3.

The workaround above should work but it sometimes it is more intuitive in the workflow to prepare all the annotations first before running cell detection.

@ym.lim it is intended behavior that existing objects ‘within’ an annotation will be deleted whenever cell detection is called. I’m not aware of any bug here.

Not doing this would be very risky, potentially resulting in subtle errors (e.g. cells being counted twice, a lot of objects piling upon top of one another).

However, deleting should only happen for ‘child’ objects as described in the hierarchy documentation.
By default, if you manually draw overlapping annotations in v0.2 these aren’t automatically treated as having a parent/child relationship in the way that they would be in v0.1.2. But this relationship can be established by calling Resolve hierarchy, or if you generate annotations some other way.

This means that, in most normal workflows, things should work fine without needing to bother about the details of how the hierarchy feature works. If this default behavior isn’t what you want, I’d need to know the exact details of your workflow.

Thanks for the explanation @petebankhead!

Unfortunately for my workflow, I require some annotations to be a child of a bigger one as I want to expand an annotation A by 400 um within annotation Tissue without going outside Tissue, using the constrainToParent option.

Ah ok. In that case you’ll need to find a way to break that relationship. This script might work:

def annotations = getAnnotationObjects()
removeObjects(annotations, false)

Although I think detecting cells first may be preferable, so the script can be avoided.


Thank you for the suggestion!

My workflow requires some manual intervention and having to do the cell detection first would require two instances of the manual intervention instead of one. I will test out your script!

EDIT: The script works to break the parent/child relationship of the annotations and they are no longer deleted by the Cell Detection tool. Thanks @petebankhead!

1 Like