Issues with processing script on all parent annotations and not only one

Hello!

I am trying something out with Qupath v0.2.3. I want to extract some objects which overlap smaller annotations in my main annotation. The script itself is already running nicely! However my only issue is that it only runs on one main annotation! Not on all! How can I let my script run on all my parent annotations?


import static qupath.lib.gui.scripting.QPEx.*

def islets=getDetectionObjects()
def isletsgeos=islets.collect{it.getROI().getGeometry()}
def outsidegeo=getAnnotationObjects().find{it.getPathClass()==getPathClass("Neuromelanin")}.getROI().getGeometry()

def intersections=[]
islets.eachWithIndex{entry,idx->
entrygeo = entry.getROI().getGeometry()
    if (!entrygeo.intersects(outsidegeo)){
        intersections<<idx
    }
}

print(intersections)
getCurrentHierarchy().getSelectionModel().selectObjects(islets[intersections])

removeObjects(islets[intersections], true)

selectObjectsByClassification("Neuromelanin")
clearSelectedObjects(true);

I think the issue lies here (correct me if I’m wrong)

getCurrentHierarchy().getSelectionModel().selectObjects(islets[intersections])

How can I fix this problem?

Best,
Lydian

The script, as written is only designed for one object. Could you merge your annotations?

2 Likes

Try this:

def islets=getDetectionObjects()
def outsideannots=getAnnotationObjects().findAll{it.getPathClass()==getPathClass("Neuromelanin")}

def intersections=[]
outsideannots.each {
     def outsidegeo=it.getROI().getGeometry()
     islets.eachWithIndex { entry, idx ->
         entrygeo = entry.getROI().getGeometry()
         if (!entrygeo.intersects(outsidegeo)) {
             intersections << idx
         }
     }
 }

If I’m reading this correctly, this will delete everything that does not intersect with a Neuromelanin annotation. If what you actually want is to delete the things that do intersect, remove the exclamation point in the line if (!entrygeo.intersects(outsidegeo)){

3 Likes

Yes I do in a previous step! That part actually works fine! If I look at my image when the script is running, that part works! Maybe with a screenshot of my image it will make things a bit better to understand.


2

So my big annotations (SNc, and SNr) have both an annotation classified as Neuromelanin, all the smaller annotations are merged as one. Both bigger annotations have objects inside (however I see now that the SNc does have objects inside, but it doesn’t display it in the annotation window??).

If I run the part of the script which I previously posted, it will only run on one big annotation. Now I am wondering if the issues is that it does now not recognize the objects in the SNc annotation? Why does it not recognize those? :cry:

Dear @smcardle ,

Yes! Actually I previously posted a question on this forum before with this issue! I first didn’t use the !, however that actually deleted the objects I wanted to keep haha! With the ! it actually does what it needs to do!

Thanks for noticing!
Best,
Lydian

1 Like

As shown, the original script was only designed to target one annotation. @smcardle adjusted the script to cycle through multiple annotations. Have you tried it?

1 Like

Yes, It works partly! However I have another issue now. All my objects are being selected/deleted now…

So now I tried removing the ! like @smcardle suggested. But this gives me the same issue I had before this new issue… I delete the objects I want to keep (all my objects which intersect with annotations)… . :thinking:

Can I make a command with something like this:

removeObjects : (all objects minus (islets[intersections]))?

So, that way I remove my objects I want to keep from the objects that are being removed.

I hope it’s clear for you!

best,
lydian

Ah, with multiple annotations it would, by default, delete all annotations. I made a slight change, but without the exact hierarchy it is a little bit hard to test.

It doesn’t seem to work! Now it won’t delete anything actually
3

In a previous step I removed the Neuromelanin annotation from it’s parent annotation! So the only objects inside the parents are the objects I still need to divide!

Um, wait, you have 2 levels of annotation and some detections, right?

Does this do anything, just on its own?

resolveHierarchy()

removeObjects( getDetectionObjects().findAll{it.getLevel() == 2}, true)

I also edited the post just above to fix what I thought that script was doing.

Thanks for your quick responses every time!
I tried it, and it didn’t work! Also tried it on the previous versions of the scripts we have in this thread!

All objects are being deleted…

I’m sorry, I’m getting lost in this thread. Could you describe again your hierarchy, the location of the objects, and what you want to happen?

2 Likes

Yeah, I was looking at this, and something is not what it appears to be. getLevel should always work if there is only one annotation above an object.

I also have a feeling the drawn annotations were not drawn properly, as there appear to be two lines very close to each other, which should not happen with CTRL+SHIFT.
image

2 Likes

One last try, as I am not sure you tried the original suggestion:

def islets=getDetectionObjects()
selectObjectsByClassification("Neuromelanin")
mergeSelectedAnnotations()

def outsidegeo=getAnnotationObjects().find{it.getPathClass()==getPathClass("Neuromelanin")}.getROI().getGeometry()

def intersections=[]
islets.eachWithIndex{entry,idx->
entrygeo = entry.getROI().getGeometry()
    if (!entrygeo.intersects(outsidegeo)){
        intersections<<idx
    }
}

print(intersections)
getCurrentHierarchy().getSelectionModel().selectObjects(islets[intersections])

removeObjects(islets[intersections], true)

selectObjectsByClassification("Neuromelanin")
clearSelectedObjects(true);

Whoops, thanks @smcardle for pointing out the islets line was missing.

2 Likes

I feel so stupid that I didn´t know the Cntrl+ Shift tool! This makes everything a lot easier, thanks!

1 Like

It works!! Thank you so much!

1 Like