Selecting newly expanded annotation

Hello! I am just getting into scripting with QuPath and I could not find a solution to select the newly created annotation after using ‘Expand annotation’ by script. The default behaviour after running ‘Expand annotation’ by script is to keep the original annotation selected.

As a bit of background, the original annotation (A; see visual aid below) has the class name ‘Epidermis’, and the expanded annotation inherits this class name. What I want to do is to select the newly created expanded annotation (B; see visual aid below) and rename/remove its class name for downstream manipulation.

In general, is there an easy method to select specific annotations by script?

Many thanks,

For some visual aid:

Here’s the Hierarchy:

Here’s my incomplete script:

//Select epidermis annotation
selectObjects { p -> p.getPathClass() == getPathClass('Epidermis') }

//Run 'Expand annotations'
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 300.0,  "lineCap": "Round",  "removeInterior": true,  "constrainToParent": true}');

//Select newly expanded annotation

//Shrink annotation by 50 um to remove tissue border
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": -50.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');

//Select shrunken annotation

//Restore shrunken annotation to original size without tissue border
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 50.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');

//Select and delete shrunken annotation

//Remove classification from restored annotation to avoid messing up downstream classifier

//Run positive cell detection (with 2 um cell expansion)
runPlugin('qupath.imagej.detect.cells.PositiveCellDetection', '{"detectionImageBrightfield": "Hematoxylin OD",  "requestedPixelSizeMicrons": 0.5,  "backgroundRadiusMicrons": 8.0,  "medianRadiusMicrons": 0.0,  "sigmaMicrons": 1.5,  "minAreaMicrons": 10.0,  "maxAreaMicrons": 400.0,  "threshold": 0.1,  "maxBackground": 2.0,  "watershedPostProcess": true,  "excludeDAB": false,  "cellExpansionMicrons": 2.0,  "includeNuclei": true,  "smoothBoundaries": true,  "makeMeasurements": true,  "thresholdCompartment": "Cell: DAB OD mean",  "thresholdPositive1": 0.2,  "thresholdPositive2": 0.4,  "thresholdPositive3": 0.6000000000000001,  "singleThreshold": true}');

Sounds like this:

I usually set the original annotation to the class I want the final annotation to be, and then alter the class of the original annotation. In Pete’s script, that would be something like:

Thank you so much for your help! Embarassingly, I have seen that post but it never clicked in my head to set the original annotation to the intended class first and then change it back after.

1 Like