Modifying Results Table with Jython/Python

fiji
analyze-particles
results
jython
imagej
results-table
biovoxxel

#1

I’m trying to modify an existing results table by adding an additional column, but running into problems. I’ve tried following the advice in this thread, but was unsuccessful.

I have a Results Window from the the Binary Feature Extractor of the bioVoxxel program, shown below:

res

I’d like to an additional column “DESCRIPTOR” that contains a unique string for each row, ie: transforming the table to something like this:

   DESCRIPTOR     Image      Objects    Selectors    Extracted
1  mutant A     ...1.tif      32         151          1
2  mutant B     ...2.tif      32           5          5
3  mutant C     ...3.tif      32           5          4 

I think one of the issues is that this is not a typical “results” window, as suggested by:
print type(IJ.selectWindow("BFE_Results"))
gives

<type ‘NoneType’>

I’ve tried this:

from ij.measure import ResultsTable
from ij import IJ

IJ.selectWindow("BFE_Results")
rt = ResultsTable.getResultsTable()
rt.setValue(1,1, "mutantA")
rt.show("BFE_Results")

But get the error below. … Any ideas? Thanks!

Traceback (most recent call last):
File “New_.py”, line 6, in
at ij.measure.ResultsTable.setValue(ResultsTable.java:456)
at ij.measure.ResultsTable.setValue(ResultsTable.java:490)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: row>counter
at org.python.core.Py.JavaError(Py.java:546)
at org.python.core.Py.JavaError(Py.java:537)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:188)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:204)
at org.python.core.PyObject.call(PyObject.java:515)
at org.python.core.PyObject.call(PyObject.java:521)
at org.python.core.PyMethod.call(PyMethod.java:171)
at org.python.pycode.pyx122.f$0(New.py:7)
at org.python.pycode.pyx122.call_function(New.py)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1386)
at org.python.core.builtin.eval(builtin.java:497)
at org.python.core.builtin.eval(builtin.java:501)
at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at org.scijava.script.ScriptModule.run(ScriptModule.java:160)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
at org.scijava.thread.DefaultThreadService$3.call(DefaultThreadService.java:238)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: row>counter
at ij.measure.ResultsTable.setValue(ResultsTable.java:456)
at ij.measure.ResultsTable.setValue(ResultsTable.java:490)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:186)
… 24 more


#2

I believe the issue you are experience is that getResultsTable() is looking for the “Results” window used by the measure command which does not exist and is returning NoneType to you. When you go to get a results table, you are then retrieving the empty table associated with the measure command and, since it is empty, the index you provide is out of bounds (java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: row>counter). What you need is the table that was created by the plugin with a different title specified. To retrieve the information from the table in your case, you probably need to grab it as follows:

from ij import IJ
from ij import WindowManager

rt = WindowManager.getWindow("BFE_Results").getTextPanel().getOrCreateResultsTable()

There is a related discussion you can view here. Within that discussion are some more robust suggestions for grabbing a results table not associated with the measure feature. I hope this helps!

Cheers,
Andrew


#3

Perfect!! Thank you!