Renaming annotations

Here’s what I’m, trying to do:

Loop through each annotation, to -

  1. Expand for 25um and 75um
  2. Rename the newly created annotations; i.e. R1_Ext1, R1_Ext2
  3. Run cell detection in the outer-most layer (i.e. 75um) - I’m assuming the inner layers will have cells detected automatically.
    image

I managed to create the loop that does the extension for each annotation. However, could not update the names of the outer layers. Also, the cell detection runs only in the inner layer. How do I update the annotation selection?

hierarchy = getCurrentHierarchy()

for (annotation in getAnnotationObjects()) 
{
hierarchy.getSelectionModel().clearSelection();
selectObjects{p -> p == annotation}
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 25.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 75.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');

//rename outer layers
 //run cell detection in the outer-most layer


}

It is easiest to do this with classifications, given the new updates to classification selection. One thing to note is that, if you have annotations like the ones you drew, and you keep them separate, the resulting outlines WILL overlap. Depending on whether or not you want this behavior, you may want to handle things differently, like merging all of the base annotations.

hierarchy = getCurrentHierarchy()


getAnnotationObjects().each{it.setPathClass(getPathClass("Middle"))}
for (annotation in getAnnotationObjects()) 
{
hierarchy.getSelectionModel().clearSelection();
selectObjects{p -> p == annotation}
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 25.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');
getSelectedObjects().each{it.setPathClass(getPathClass("Outer"))}
runPlugin('qupath.lib.plugins.objects.DilateAnnotationPlugin', '{"radiusMicrons": 75.0,  "lineCap": "Round",  "removeInterior": false,  "constrainToParent": true}');
getSelectedObjects().each{it.setPathClass(getPathClass("Center"))}
//rename outer layers
 //run cell detection in the outer-most layer
selectObjectsByClassification("Outer")

}

image
Notice the overlap, which works as intended if you go about it this way.

If you want the cells to be placed within their respective locations in the hierarchy for other downstream “stuff” you may want to resolveHierarchy() at the end. Also, if you want to rename the other annotations, you may want to find another way to do that, possibly by applying the names later (cycling through all “outer” annotations, and naming those, for example).

1 Like

Thanks a lot. Yes, you are right. Most probably I will merge the initial circles.

Unfortunately, I’m having same difficulty with updating the name. I’m not familiar with Groovy - could you help me with the commented lines please? I am running a second loop after the rings are created to rename the rings:

hierarchy = getCurrentHierarchy()
for (annotation in getAnnotationObjects()) 
{
hierarchy.getSelectionModel().clearSelection();
selectObjects{p -> p == annotation}
className = getSelectedObjects().each{it.getPathClass()}
name = annotation.getName()
// if (className == "Outer")
    annotation.setName(name + '_75')
    
    //elseif (className == "Middle")
     annotation.setName(name + '_25')
     
}

Also, could you direct me to any list of all these functions? I usually go through example scripts to find a snippet that I can fit to my work, but it is really time consuming.

Checking for classes (not the name) is done by

if (annotation.getPathClass() == getPathClass("Outer")){
    //stuff
}
1 Like