How to get a reference to the results table?

Once I have got a results table (or a newly made image) from a plugin, how do I get a reference to it in jython? The code below works when in GUI mode the topmost window appears to be selected, but no csv-file is saved in headless mode.

image     = IJ.openImage(mask)
fileName  = path.splitext(path.basename(mask))[0]

savingFolder = path.join(path.dirname(mask), "morphometry")
if not path.exists(savingFolder):
    os.mkdir(savingFolder)

IJ.run(image, "Set Label Map", "colormap=[Golden angle] background=White shuffle")
IJ.run(image, "Label Size Filtering", "operation=Greater_Than size=5000")
IJ.saveAs(image, "Tiff", path.join(savingFolder, fileName))
IJ.run(image, "Analyze Regions", "area perimeter circularity equivalent_ellipse ellipse_elong. max._feret oriented_box oriented_box_elong.")
IJ.saveAs(IJ.getTextPanel(), path.join(savingFolder, fileName + ".csv"))

I expect the first parameter of the last line is not correct, but how should it be done, then?

Hi @iconoclastica

can you try

from ij.measure import ResultsTable

ResultsTable.getResultsTable().save(filename)

See also in the docs and here and let us know if this works!

Best,
Robert

Hi Robert,

It works in as much that a csv file is written to the specified path, But it is empty.
The cause is that ResultsTable.getResultsTable() comes with an empty table (title=null, size=0, hdr=). If I add the window title as an argument, it returns None.
Is this really the correct object to ask it from? Reading the dox I understand it would return the measurements’ result table.

thanks,

Wim

I usually get a results table this way:

from ij import WindowManager
rt_Window= WindowManager.getWindow("name of your results table")
rt  = rt_Window.getTextPanel().getOrCreateResultsTable()

hope it helps
Emanuele Martini

What is the window title of the results table you want to save? Can you enter it as parameter?

When I run the corresponding ijm-script from the gui, the title reads “ZM19-1_mask-Morphometry”; upto mask it is the input file name, and from there the added Morphometry by the plugin.
Neither getWindow() nor getResultsTable() returns other than None when I add this string as parameter.

This is what the plugin does (source, line 171):

        // Execute the plugin
        ResultsTable table = process(imagePlus);
        
        // show result
        String tableName = imagePlus.getShortTitle() + "-Morphometry"; 
        table.show(tableName);

To my knowledge the ResultsTable must be visible as noted in the sources:

However for the record in the ParticleAnalyzer class you can set and save the ResultsTable headless.

Here a reproducible ParticleAnalyzer Jython example (here with necessary imports explicitly shown, please change the path for the *.csv):

#For JavaScript see: https://forum.image.sc/t/solved-save-summary-in-headlessmode/26606/3
from ij.plugin.filter import ParticleAnalyzer;
from ij.measure import ResultsTable;
from ij import IJ;

img1 = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
IJ.setAutoThreshold(img1, "Default");
IJ.run("Set Measurements...", "area mean redirect=None decimal=3");
rt =  ResultsTable();
ParticleAnalyzer.setResultsTable(rt);
#ParticleAnalyzer.setSummaryTable(rt);
IJ.run(img1, "Analyze Particles...", "clear");
rt.save("/Applications/Bio7.app/Contents/MacOS/workspace/Jython/summary.csv");
´´´

The morpholib’s plugin unconditionally creates a new ResultTable, so creating one in advance won’t help.
I would try passing by it’s run-method though and call process() directly. Only it uses a number of option flags that it gets from a GenericDialog. The macro-processor seems to be able to bypass that (given the string argument ‘area perimeter … _elong’) but I wouldn’t know how to do that in code.

resultsTable = ResultsTable.getResultsTable()
always returns None, but with the windowTitle as argument it correcty returns the results when not in headless mode. So my jython script works when not running it headless.

Moreover, the original (recorded) macro neither works in headless mode. It still does in headed mode, but saves a shorter result list (3) than jython (7), a list from which small areas have been removed (I may have used or set that filter once).

1 Like

You could try ResultsTable.getActiveTable(). You may need to update ImageJ since it was added not so long ago.
I dont know if it works in headless. (EDIT: From the source code, I guess it wont work in headless sorry)
This function is a bit more flexible than getResultsTable() which when no title is passed expect the default title “Results”.

You could use a script parameter if you are in Fiji

#@ ResultsTable table
# the rest of your script
print table

but I just tested and it also seems to look for the table entitled Results, and not the active table.

Hmm, I thought my present install wasn’t a few weeks old. Anyway, after running the updates it still says type object ‘ij.measure.ResultsTable’ has no attribute ‘getActiveTable’. I can update further to 1.53i by the menu item Help|update ImageJ, but then no scripting works anymore. So I rolled back to 1.53c, which doesn’t know getActiveTable(). Apparently Fiji is lagging a bit behind with its imageJ version?

It seems I either have to go with running the script headed, or to go to Java and subclass the plugin to set the options programmatically.

Hmm ´getActiveTable` was added with 1.53h according to the release notes
ImageJ News (nih.gov)

Oh that’s concerning ! Feel free to open new threads if you spot specific bugs that were not appearing before.

The development of ImageJ1 and Fiji are actually separate. Changing the version shipped with Fiji sometimes requires some internal checks and reconfiguration of Fiji, therefore it is not systematically updated but only once in a while.