Exporting data from a project to a table

Hi everyone,
a colleague and I have used this script for a long period of time, but with the recent updates it gives us the error “ERROR: It looks like you have tried to import a class ‘qupath.lib.objects.classes.PathClassFactory.PathClasses’ that doesn’t exist!” - In version 0.1.2 it is still functioning. With the most recent one, we are at a loss how to make it work again.
Cheers & thanks, Julia

import qupath.lib.gui.QuPathGUI
import qupath.lib.scripting.QP;
import qupath.lib.objects.classes.PathClassFactory.PathClasses
import qupath.lib.objects.classes.PathClass
import qupath.lib.objects.classes.PathClassFactory
import qupath.lib.projects.ProjectImageEntry;


String outputFilepath = "F:\\Projekte\\xyz\\outputData.tsv"

Map countDetectionObjects() {
    def countMap = [:]
    for (pathObject in QP.getDetectionObjects()) {
        def pathClass = pathObject.getPathClass()
        def count = countMap.get(pathClass)
        if (count == null)
            countMap.put(pathClass, 1)
        else
            countMap.put(pathClass, count + 1)
    }

    return countMap
}

void printData(String sampleName, List orderedHeader, Map countMap, File file) {

    file << "\n"

    file << sampleName
    orderedHeader.each {
        file << "\t" + countMap.get(it)
    }
}

File outputData = new File(outputFilepath)
PrintWriter writer = new PrintWriter(outputData);
writer.print("");

// Get QuPath & viewers
def qupath = getQuPath()

Map countMap = countDetectionObjects()
List orderedHeader = new ArrayList(countMap.keySet())

outputData << "filepath"
orderedHeader.each {
    outputData << "\t" + it
}

// Get the current project
def project = qupath.getProject()
List<ProjectImageEntry> myImages = project.imageList
def viewer = qupath.getViewer()
javafx.application.Platform.runLater({ ->
    myImages.each { ProjectImageEntry myImage ->
        qupath.openImageEntry(myImage)
        printData(myImage.serverPath, orderedHeader, countDetectionObjects(), outputData)
    }
})

Hi, it looks like you can simply remove the line

import qupath.lib.objects.classes.PathClassFactory.PathClasses

I’m not sure if it will be sufficient (I don’t think it’s a script I’ve written), but it should at least get past that specific error.

1 Like

Hi Pete, thanks for your answer!
I deleted the line and the script now runs and exports a table with “negative” and “positive”, but unfortunately doesn’t show the neg/pos values for that particular image.

If you want detection class counts “per image” (or per annotation, etc), I think that is taken care of on a project level with the Measurement Exporter. Might be worth looking into.

2 Likes

That’s a great tip, thanks! It looks like it’s exactly what we need; however, the export file for only 3 images is 800MB already. From the preview it looks like the export measurements function divvied up the detections in so many rows that LibreOffice cannot display them all. I thought just one row per image would suffice…?

Yes, it sounds like you want the image export but chose cell export.

I suppose it might help to say “Image data” rather than “Image” which looks like it might export the image itself.

1 Like

You’re right; I chose detections at first. However, with the image export, it still creates >300 rows even though the image only has one polygon annotation.

If you have 300+ images, that is what you should expect. Image creates one row per image, annotations have nothing to do with what it exports.

If you are doing something else, or tried to overwrite an already open CSV file (which should fail and keep the old CSV file), you will need to specify what steps you are taking.

Unfortunately, it creates this many rows for just the one image.
Overwriting shouldn’t be an issue; I opened a brand new test project, selected only one image and off it went. For me, I cannot choose a CSV file though, but a TSV instead.

It might help if you could show the problem. I am not sure if your TSV reader is reading the file incorrectly, or QuPath is having some edge case issue.

@petebankhead any reason someone could not choose CSV? That sounds weird, like a setup problem with the computer, but might just be something I am not familiar with.

I did it!
You just have to choose another Separator to be able to create a CSV file. With CSV, only one row per image as it should be.
Thank you for your patience and help!

1 Like