Pulling out detection data for specific annotations

The images that I import into QuPath have two tumour samples on each slide

To score my samples I create 20x ellipses (10 set as “Left tumour” class and 10 as “Right tumour” class) and place them on each sample, use cell detection, and export the data out via “measure > show detection measurements”. This works well but there is no way to determine which cell is derived from the right or left.

Is there a way to select a few annotations and export the cell data from just them, rather than the whole image?

Hi @Allie_F,

Here is a simple script that can do this for you:

import qupath.lib.gui.commands.SummaryMeasurementTableCommand;
import qupath.lib.gui.models.ObservableMeasurementTableData;
import qupath.lib.objects.PathCellObject;

def imageData = getCurrentImageData();
def excludeColumns = [];
def separator = ","

def model = new ObservableMeasurementTableData();
model.setImageData(imageData, imageData == null ? Collections.emptyList() : getDetectionObjects());
def data = SummaryMeasurementTableCommand.getTableModelStrings(model, separator, excludeColumns);


def pathOut = "C:/your/desired/path.csv";
def writer = new FileWriter(pathOut);
writer.write(data.get(0));
writer.write("\n");

def header = data.get(0).split(",");
indexOfClass = -1
for (int i = 0; i < header.size(); i++){
    if (header[i].equals("Class")){
        indexOfClass = i;
    }
}

def classWanted = "Right Tumour"; // Or "Left Tumour"
for (int index = 0; index < data.size(); index++){
    if (data.get(index).split(",")[indexOfClass].equals(classWanted)){
        writer.write(data.get(index));
        writer.write("\n");
    }
}

writer.flush();
writer.close();
print("Done!")

It should also allow you to try out different things and conditions if needed :slight_smile:
Hopefully in the future there will be an even simpler way to export measurements. But for now this should do!

1 Like

In v0.2.0-m9, the detection measurement table contains a ‘Parent’ class. Depending upon your exact workflow, this may be enough to let you identity the ellipses for each side.

Another way to separate them if you export all at once is to create a new measurement that is 1 or 0. Used that a bit in 1.2

This assumes that all of your cells are within the Left or Right tumour annotations.

//This should create a new Measurement column called Right tumour
//cells that are within an annotation of that class will have a 1
// left tumour OR ANY OTHER ANNOTATION TYPES would have a 0

testClass = "Right tumour"
getCellObjects().each{
    if (it.getParent().getPathClass() == getPathClass(testClass )){
        it.getMeasurementList().putMeasurement(testClass ,1)
    }else{ it.getMeasurementList().putMeasurement(testClass ,0)}
}

image

Alternatively, if you want to jump straight to the export, this script built off of Pete’s export summary scripts should work. It removes all cell objects with parents of one annotation class before export, and then adds them back in.


testClass = "Right tumour"

toRemove = getCellObjects().findAll{it.getParent().getPathClass() == getPathClass(testClass)}
removeObjects(toRemove,true)

/*
 * QuPath v0.1.2 has some bugs that make exporting annotations a bit annoying, specifically it doesn't include the 'dot' 
 * needed in the filename if you run it in batch, and it might put the 'slashes' the wrong way on Windows.
 * Manually fixing these afterwards is not very fun.
 * 
 * Anyhow, until this is fixed you could try the following script with Run -> Run for Project.
 * It should create a new subdirectory in the project, and write text files containing results there.
 *
 * @author Pete Bankhead
 */

def name = getProjectEntry().getImageName() + ' left tumour.txt'
def path = buildFilePath(PROJECT_BASE_DIR, 'detection results')
mkdirs(path)
path = buildFilePath(path, name)
saveDetectionMeasurements(path)
print 'Results exported to ' + path

addObjects(toRemove)
/////////////////////////////////////////////////////////
testClass = "Left tumour"

toRemove = getCellObjects().findAll{it.getParent().getPathClass() == getPathClass(testClass)}
removeObjects(toRemove,true)

/*
 * QuPath v0.1.2 has some bugs that make exporting annotations a bit annoying, specifically it doesn't include the 'dot' 
 * needed in the filename if you run it in batch, and it might put the 'slashes' the wrong way on Windows.
 * Manually fixing these afterwards is not very fun.
 * 
 * Anyhow, until this is fixed you could try the following script with Run -> Run for Project.
 * It should create a new subdirectory in the project, and write text files containing results there.
 *
 * @author Pete Bankhead
 */

name = getProjectEntry().getImageName() + ' right tumour.txt'
path = buildFilePath(PROJECT_BASE_DIR, 'detection results')
mkdirs(path)
path = buildFilePath(path, name)
saveDetectionMeasurements(path)
print 'Results exported to ' + path

addObjects(toRemove)

image

Thank you all - this has helped significantly!

2 Likes