I have updated the script as you made it for me @melvingelbard also, yours contained a small mistake. The cellsArea needs to come first and then be divided by the totalArea 
// Script written in v0.2.0
import qupath.lib.gui.commands.SummaryMeasurementTableCommand;
import qupath.lib.gui.measure.ObservableMeasurementTableData;
import qupath.lib.objects.PathAnnotationObject;
// Get the total cell areas and add to corresponding cell area for the respective classes
cellsAreaPositive = 0
cellsAreaNegative = 0
getCellObjects().each {
if (it.getPathClass() == getPathClass('Positive'))
cellsAreaPositive += measurement(it, 'Cell: Area')
if (it.getPathClass() == getPathClass('Negative'))
cellsAreaNegative += measurement(it, 'Cell: Area')
}
def entry = getProjectEntry()
def imageData = entry.readImageData()
def model = new ObservableMeasurementTableData()
model.setImageData(imageData, imageData == null ? Collections.emptyList() : imageData.getHierarchy().getObjects(null, PathAnnotationObject.class))
def data = SummaryMeasurementTableCommand.getTableModelStrings(model, "\t", [])
// Considering you have only 1 annotation with your cells inside it,
// these lines will get the total area of the annotation and the total area of the classes together as earlier defined
def index = data[0].split('\t').findIndexOf { it.equals('Area µm^2')}
def totalAreaAnnotation = data[1].split('\t')[index] as double
def totalAreaPosNeg = cellsAreaPositive + cellsAreaNegative as double
// Calculate your measurements for different area
def areaPositive = cellsAreaPositive.div(totalAreaAnnotation)*100
def areaNegative = cellsAreaNegative.div(totalAreaAnnotation)*100
def areaPositiveFromPosNeg = cellsAreaPositive.div(totalAreaPosNeg)*100
def areaNegativeFromPosNeg = cellsAreaNegative.div(totalAreaPosNeg)*100
// Printing values
print cellsAreaPositive
print cellsAreaNegative
print totalAreaAnnotation
print totalAreaPosNeg
print areaPositive
print areaNegative
print areaPositiveFromPosNeg
print areaNegativeFromPosNeg
// Add measurements to annotation
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("cellsAreaPositive", cellsAreaPositive)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("cellsAreaNegative", cellsAreaNegative)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("totalAreaAnnotation", totalAreaAnnotation)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("totalAreaPosNeg", totalAreaPosNeg)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("areaPositive", areaPositive)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("areaNegative", areaNegative)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("areaPositiveFromPosNeg", areaPositiveFromPosNeg)
getAnnotationObjects().getAt(0).getMeasurementList().putMeasurement("areaNegativeFromPosNeg", areaNegativeFromPosNeg)
It now gives you:
- area of the cells that are classified positive
- area of the cells that are classified negative (I know this is a simple calculation, but nice if QuPath can run it instead of having to do it myself later)
- total area of the annotation
- calculated total area for all your detection together
- calculated % of your positive detection based on the annotation area
- calculated % of your negative detection based on the annotation area
- calculated % of your positive detection based on the total detection area
- calculated % of your negative detection based on the total detection area
It also prints all the above values to the measurement table.
Thanks again Melvin, I learned a lot from your script 