Export only certain columns and rows form detection measurements

Is there a way to script the export for detection measurements so that only certain columns are exported (for example name, class, TMA core, cytoplasm: AF488 mean, ect) to reduce the amount of data exported? And can it be done only for cells classified as CK by a detection classifier (Ck rows in class column)?

I am using the latest milestone v.0.2.0.m12 and the following script to export and merge detection measurements:
def name = getProjectEntry().getImageName() + ‘.txt’
def path = buildFilePath(PROJECT_BASE_DIR, ‘detection results’)
mkdirs(path)
path = buildFilePath(path, name)
saveDetectionMeasurements(path)

import qupath.lib.gui.QuPathGUI

String ext = ‘.txt’
String delimiter = ‘\t’
String outputName = ‘Combined_results.txt’

def dirResults = new File(buildFilePath(PROJECT_BASE_DIR, ‘detection results’))
if (dirResults == null)
return
def fileResults = new File(dirResults, outputName)

def files = dirResults.listFiles({
File f -> f.isFile() &&
f.getName().toLowerCase().endsWith(ext) &&
f.getName() != outputName} as FileFilter)
if (files.size() <= 1) {
print ‘At least two results files needed to merge!’
return
} else
print ‘Will try to merge ’ + files.size() + ’ files’

def results = new ArrayList<Map<String, String>>()

def allColumns = new LinkedHashSet()
allColumns.add(‘File name’)

for (file in files) {
def lines = file.readLines()
if (lines.size() <= 1) {
print 'No results found in ’ + file
continue
}
def iter = lines.iterator()
def columns = iter.next().split(delimiter)
allColumns.addAll(columns)
while (iter.hasNext()) {
def line = iter.next()
if (line.isEmpty())
continue
def map = [‘File name’: file.getName()]
def values = line.split(delimiter)
if (values.size() != columns.size()) {
print String.format(‘Number of entries (%d) does not match the number of columns (%d)!’, columns.size(), values.size())
print('I will stop processing ’ + file.getName())
break
}
for (int i = 0; i < columns.size(); i++)
map[columns[i]] = values[i]
results.add(map)
}
}

if (outputName.toLowerCase().endsWith(’.csv’))
delimiter = ‘,’
int count = 0
fileResults.withPrintWriter {
def header = String.join(delimiter, allColumns)
it.println(header)
for (result in results) {
for (column in allColumns) {
it.print(result.getOrDefault(column, ‘’))
it.print(delimiter)
}
it.println()
count++
}
}
print 'Done! ’ + count + ’ result(s) written to ’ + fileResults.getAbsolutePath()

Thanks!!!

Check out Measure → Export measurements. This intended to replace a lot of custom scripts. It already supports choosing custom columns for export.

After you’ve selected some images, press the Populate button and then select what you want to keep/remove from the drop-down menu. You can also right-click on it to select all/none.

3 Likes

Thanks!!!

2 Likes