Discussion:
Help converting macro to jython for cluster computing headless?
Matthew
2012-06-04 23:00:49 UTC
Permalink
Hello!

I am trying to use FIJI/ImageJ on a cluster and I need to run
headless. I have to process a large number of images. I have a macro
that I made for my local computer and I was wondering if anyone could
help me adapt it to jython which seems to not need a GUI?

The Macro I have is this (fijimacro1.ijm):

input = "/test/fiji1in/";
output = "/test/fiji1out/";

setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
action(input, output, list[i]);
setBatchMode(false);

function action(input, output, filename) {
open(input + filename);
run("16-bit");
run("TransformJ Scale", "x-factor=4.0 y-factor=4.0 z-
factor=1.0 interpolation=[quintic B-spline]");
saveAs("TIFF", output + filename);
close();
}

setBatchMode(false);
//setTool("angle");


I am able to get this to run from the command line with ‘fiji-linux64 –
Dplugins.dir=. –macro fijimacro1.ijm –batch &’. But I am connected
through xwindows and the display is being exported. I have been
reading forum posts and the main suggestion is to write a jython
script that doesn’t interact with a GUI at all. I have very little
experience in programming, and from what I found at Albert Cardona’s
python and ImageJ tutorial this was what I came up with.

My first attempt is this (fiji1_.py):

from ij import IJ
from ij.io import FileSaver
import os

# folder to read all images from:
folderin = "/test/fiji1in"
folderout = "/test/fiji1out"

for filename in os.listdir(folderin):
if filename.endswith(".tif"):
print "Processing", filename
imp =IJ.openImage(folderin + "/" +
filename)
IJ.run(imp, "16-bit")
IJ.run(imp, "TransformJ Scale", "x-
factor=2.0 y-factor=2.0 z-factor=1.0 interpolation=[quintic B-
spline]")
filepath = folderout + "/" +filename
else:
print "Ignoring", filename

Run from the command line with: ‘fiji-linux64 --headless --jython ./
scripts/fiji1_.py -batch &’

The error message that I get is:

“Processing 6_FA3-120217_26_2_6_DAPI.tif Traceback (most recent call
last):
File "./scripts/fiji1_.py", line 13, in <module>
IJ.run(imp, "16-bit")
TypeError: run(): 1st arg can't be coerced to String”

Any advice or assistance is greatly appreciated!!

Matt
--
Please avoid top-posting, and please make sure to reply-to-all!

Mailing list web interface: http://groups.google.com/group/fiji-users
Albert Cardona
2012-06-05 02:36:09 UTC
Permalink
Post by Matthew
Hello!
I am trying to use FIJI/ImageJ on a cluster and I need to run
headless.  I have to process a large number of images.  I have a macro
that I made for my local computer and I was wondering if anyone could
help me adapt it to jython which seems to not need a GUI?
input = "/test/fiji1in/";
output = "/test/fiji1out/";
setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
       action(input, output, list[i]);
setBatchMode(false);
function action(input, output, filename) {
       open(input + filename);
               run("16-bit");
               run("TransformJ Scale", "x-factor=4.0 y-factor=4.0 z-
factor=1.0 interpolation=[quintic B-spline]");
       saveAs("TIFF", output + filename);
       close();
}
setBatchMode(false);
//setTool("angle");
I am able to get this to run from the command line with ‘fiji-linux64 –
Dplugins.dir=. –macro fijimacro1.ijm –batch &’.  But I am connected
through xwindows and the display is being exported. I have been
reading forum posts and the main suggestion is to write a jython
script that doesn’t interact with a GUI at all. I have very little
experience in programming, and from what I found at Albert Cardona’s
python and ImageJ tutorial this was what I came up with.
from ij import IJ
from ij.io import FileSaver
import os
folderin = "/test/fiji1in"
folderout = "/test/fiji1out"
                               print "Processing", filename
                               imp =IJ.openImage(folderin + "/" +
filename)
                               IJ.run(imp, "16-bit")
                               IJ.run(imp, "TransformJ Scale", "x-
factor=2.0 y-factor=2.0 z-factor=1.0 interpolation=[quintic B-
spline]")
                               filepath = folderout + "/" +filename
                               print "Ignoring", filename
Run from the command line with: ‘fiji-linux64 --headless --jython ./
scripts/fiji1_.py -batch &’
“Processing 6_FA3-120217_26_2_6_DAPI.tif Traceback (most recent call
 File "./scripts/fiji1_.py", line 13, in <module>
   IJ.run(imp, "16-bit")
TypeError: run(): 1st arg can't be coerced to String”
Notice that IJ.run takes:
A) either two String arguments and then runs on the currently active ImagePlus;
b) or one ImagePlus argument plus 2 String arguments, and runs on that
ImagePlus.

So what you would write is:

IJ.run(imp, "16-bit", "")

... where the third argument specifies the arguments (none) to the
"16-bit" command.


One way to write your script in jython:

####
from ij import IJ
from ij.io import FileSaver
import os

# folder to read all images from:
folderin = "/test/fiji1in"
folderout = "/test/fiji1out"

for filename in os.listdir(folderin):
if filename.endswith(".tif"):
print "Processing", filename
imp = IJ.openImage(os.path.join(folderin, filename))
# Same as IJ.run(imp, "16-bit", "")
imp.setProcessor(imp.title, imp.getProcessor().convertToShort(True))
IJ.run(imp, "TransformJ Scale", "x-factor=2.0 y-factor=2.0
z-factor=1.0 interpolation=[quintic B-
spline]")
IJ.save(imp, os.path.join(folderout, filename))
else:
print "Ignoring", filename
####
--
http://albert.rierol.net
http://www.ini.uzh.ch/~acardona/
--
Please avoid top-posting, and please make sure to reply-to-all!

Mailing list web interface: http://groups.google.com/group/fiji-users
Albert Cardona
2012-06-05 17:48:40 UTC
Permalink
Hi Albert,
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at ij.Command.runPlugIn(Command.java:146)
    at ij.Command.runCommand(Command.java:95)
    at ij.Executer.run(Executer.java:64)
    at ij.IJ.run(IJ.java:251)
    at ij.IJ.run(IJ.java:300)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:175)
    at
org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
    at org.python.core.PyObject.__call__(PyObject.java:432)
    at org.python.core.PyObject.__call__(PyObject.java:436)
    at org.python.pycode._pyx0.f$0(../Fiji.app/scripts/fiji1_.py:9)
    at org.python.pycode._pyx0.call_function(../Fiji.app/scripts/fiji1_.py)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyCode.call(PyCode.java:18)
    at org.python.core.Py.runCode(Py.java:1204)
    at
org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:200)
    at org.python.util.jython.run(jython.java:246)
    at org.python.util.jython.main(jython.java:129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at imagej.ClassLauncher.launch(ClassLauncher.java:220)
    at imagej.ClassLauncher.run(ClassLauncher.java:158)
    at imagej.ClassLauncher.main(ClassLauncher.java:71)
showHelp signature: ()V) Incompatible argument to function
    at TJ_Scale.run(TJ_Scale.java:39)
    at ij.IJ.runUserPlugIn(IJ.java:185)
    at ij.IJ.runPlugIn(IJ.java:152)
... 32 more
Fiji opens the image converts it to 16-bit, transformJ fails, and then it
saves it in the output directory.  Do you know how to overcome this problem?
Thank you very much or your help!!!
Matt
Does your macro work? The macro argument is the same as in the jython script?

Albert
--
http://albert.rierol.net
http://www.ini.uzh.ch/~acardona/
--
Please avoid top-posting, and please make sure to reply-to-all!

Mailing list web interface: http://groups.google.com/group/fiji-users
Loading...