QuPath - for-loop through Images of Project - Issue

Hello everybody,

I m trying to scritp a automated workflow in Qupath and i m a bit confused about this for-loop:

import qupath.lib.gui.QuPathGUI
import qupath.lib.roi.RectangleROI
import qupath.lib.objects.PathAnnotationObject
import qupath.tensorflow.stardist.StarDist2D

def project = QuPathGUI.getInstance().getProject().getBaseDirectory()
project = project.toString()+"\\detectionMeasurements\\"

// ------------------------------   OUTPUT definition
mkdirs(project)
def outputFolder = project.toString()+"\\detectionMeasurements\\"
mkdirs(outputFolder)

// **In the first Part of the script i'm just deleting older annotations from the images in the project:**

def rotatio = getProject()
for (entry in rotatio.getImageList()) {

        // ---------------------------------------------------Annoatiation Deletion
        
    hierarchy = getCurrentHierarchy()
    def annotations = getAnnotationObjects()
    int i = 1
    clearDetections()  

    for (annotation in annotations)
    {
        hierarchy.getSelectionModel().clearSelection();
        selectObjects{p -> p == annotation}
    }

        // ---------------------------------------------------Annoatiation Creation
    def imageData = getCurrentImageData()
//  **Here i want to know if i'm in the right picture**
    print imageData
    def server = imageData.getServer()
    def xdist = server.getWidth()
    def ydist = server.getHeight()
    clearAllObjects()
    if (server.nZSlices() >0){
       10.upto(server.nZSlices()-1){
           frame = PathObjects.createAnnotationObject(ROIs.createRectangleROI(0,0,xdist,ydist,ImagePlane.getPlane(it,0)));
           addObject(frame);
       }
    }

    println 'Done Copying Annotation!'
}

As you see in the pic, there are 2 images in the project, and the for loop runs 2 times, but every time with the same picture. I just can not explain that to me.

Looking forward for your answer

Max

Edited to format the code as code.image

    for (annotation in annotations)
    {
        hierarchy.getSelectionModel().clearSelection();
        selectObjects{p -> p == annotation}
    }

Not sure about this part, is there a reason for it? If so, is it different from selectAnnotations() ?

As for the project loop, it looks like you are continuously using getCurrentImageSomething and not accessing the project entry that you are looping through. In other words, your loop is “entry”, but you never use entry.

Also, when working with project entries like this you need to save your data - this does not happen automatically like a normal script.

Note that this example also uses it’s “entry” variable.

1 Like

To slightly drill down, imageData should always be the currently open image in the viewer with these two lines of code.
The code in the link above (not mine, but the other user’s), shows how to interact with the entry’s hierarchy - not the currently open one.

Hi @Vogt_without_i

Usually you shouldn’t have to code the for loop in QuPath, you can use Run → Run for project instead.

1 Like

Thx to all of you! that makes a lot of sense!