Script to merge result files for v020_milestone12 not working

Hi all,
I tried to merge my 300 txt files into one using the following code. It doesnt work with Qupath v020_12 (the latest one). Can anyone help me on this?

Many thanks!
Sincerely,
Sabrina

import qupath.lib.gui.QuPathGUI

// Some parameters you might want to change…
String ext = ‘.txt’ // File extension to search for
String delimiter = ‘\t’ // Use tab-delimiter (this is for the input, not the output)
String outputName = ‘Combined_results.txt’ // Name to use for output; use .csv if you really want comma separators

// Prompt for directory containing the results
def dirResults = QuPathGUI.getSharedDialogHelper().promptForDirectory()
if (dirResults == null)
return
def fileResults = new File(dirResults, outputName)

// Get a list of all the files to merge
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’

// Represent final results as a ‘list of maps’
def results = new ArrayList<Map<String, String>>()

// Store all column names that we see - not all files necessarily have all columns
def allColumns = new LinkedHashSet()
allColumns.add(‘File name’)

// Loop through the files
for (file in files) {
// Check if we have anything to read
def lines = file.readLines()
if (lines.size() <= 1) {
print 'No results found in ’ + file
continue
}
// Get the header columns
def iter = lines.iterator()
def columns = iter.next().split(delimiter)
allColumns.addAll(columns)
// Create the entries
while (iter.hasNext()) {
def line = iter.next()
if (line.isEmpty())
continue
def map = [‘File name’: file.getName()]
def values = line.split(delimiter)
// Check if we have the expected number of columns
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
}
// Store the results
for (int i = 0; i < columns.size(); i++)
map[columns[i]] = values[i]
results.add(map)
}
}

// Create a new results file - using a comma delimiter if the extension is csv
if (outputName.toLowerCase().endsWith(’.csv’))
delimiter = ‘,’
int count = 0
fileResults.withPrintWriter {
def header = String.join(delimiter, allColumns)
it.println(header)
// Add each of the results, with blank columns for missing values
for (result in results) {
for (column in allColumns) {
it.print(result.getOrDefault(column, ‘’))
it.print(delimiter)
}
it.println()
count++
}
}

// Success! Hopefully…
print 'Done! ’ + count + ’ result(s) written to ’ + fileResults.getAbsolutePath()

I’d really need more information than ‘doesn’t work’ (i.e. a specific error and description of what happens).

But in general the Measure → Export measurements command should replace most of the need for that script – since it provides a way to export results across a project.

2 Likes

Hi Pete,
Measure → Export measurements command solved the challenge. So simple and beautiful!
Many thanks!
Sabrina

2 Likes

I came across this problem too but solved it by changing this line:

def dirResults = QuPathGUI.getSharedDialogHelper().promptForDirectory()

to

def dirResults = Dialogs.promptForDirectory()
1 Like