Taking Values Directly From Results Table Pyimagej

Hello all. I’m currently working on a project that seems very simple but I keep hitting roadblocks and I only just started looking at pyimagej about a month ago, so apologies if this is a simple task that I’m just completely bad at.

Current issue

I can run a macro just fine to generate the data I want from imagej and haven’t had any issues there. The main goal right now is to take the results table generated by the image analysis in the macro (only basic nucleus counting for now to test this feature) and I have a quite nasty looking workaround that is able to give me generally what I want. Because it’s a clunky way to get it done, I’d like to know if there’s a more elegant solution that would allow direct access to the results table. I do not want to go through the motions of saving the data to a .csv file and then opening it, which would work just fine but defeat the purpose of doing everything in Python. Code with the workaround I’m using is below:

import imagej
ij = imagej.init('C:/[filepath]/Fiji.app', headless = False)
import numpy as np

macro = """
#@ String toAnalyze
#@ String output
#@output Object rt
setAutoThreshold("Yen dark");
setThreshold(223, 65535);
run("Analyze Particles...", "size=35-Infinity display add");
saveAs("Results", output + "testMacro.csv");
roiManager("Save", output + "testMacro.zip");
rt = String.paste;

args = {'toAnalyze':"C:/[filepath]/F02_110w2.TIF",

result = ij.py.run_macro(macro, args)
testcat = str(result.getOutput('rt'))  #(numpy won't recognize the string as a java object)
npa = np.fromstring(testcat, dtype = float, sep = "\t")
dum = testcat.split("\n")
res1 = []
for row in testcat.split("\n"):
    res1.append(np.fromstring(row, sep = "\t"))
resArray = np.asarray(res1[0:-1])

Is there a better way than this? My goal is to have the entire analysis process happen within the python environment and avoid having to save and open the results separately, instead being able to generate the data and interact with it directly using numpy in Python. Apologies for formatting as well, I’m very new to this site.