Coding with Fiji

Dear ImageJ Community,

can anybody recommend in what order a beginner should work through the massive amount of tutorials and introductions on how to write advanced code with Fiji? I am already familiar with the Macro language and I am trying to advance into using Java or Python in Fiji. However, as I am a Biologist with only some basic coding background I am struggling on how to coherently work through all these tutorials and the flood of new information can easily get overwhelming.

I would be extremely thankful if anyone could recommend me how to best approach this problem.

Thanks in advance!

In my opinion, the natural evolution from coding in the Macro language is to use any of the available scripting languages. You can use BeanShell, which is like Java but without type declarations, or Python as you suggested.

@disputator1991, I’m in a similar situation, being a biologist with no formal training in coding (except for that nerd camp in high school :slight_smile: ).

I like Python over Java because the syntax and program structure are simpler and there’s no compilation step.

For a while, I was writing Java plugins regularly, and at that time I found the book “Thinking in Java” incredibly helpful.

For Python in ImageJ, I started with Albert Cardona’s tutorial, available in slightly different form here.

As a biologist you’re probably writing code to do some particular task, so IMO it can be more efficient to focus on a short-term goal, and learn what you need to do that task, rather than try to understand everything that the language can do.

I’ve rewritten a couple of my IJ1 macros in Python as an exercise, but usually time pressure dictates that I have to choose either Python or the macro language at the beginning of a project, and learn as I go.

When I get stuck, I’ll try the snippets from the BAR update site, or examples from the Help > Examples menu, or search the ImageJ mailing list archives, or of course this forum. StackOverflow is also helpful for finding general programming answers.

Good luck!

6 Likes

Yet another Biologist here:blush:. I was just going through the Python tutorials you mentioned, (there) and ran into an error when I tried to run this example.

Fiji’s macro editor gives an error for this line:

minimum = Float.MAX_VALUE

The following error:

    NameError: name 'Float' is not defined

        at org.python.core.Py.NameError(Py.java:260)
        at org.python.core.PyFrame.getname(PyFrame.java:257)
        at org.python.pycode._pyx0.f$0(Z:\Daan\FijiMacrosDaan\temp.py:37)
        at org.python.pycode._pyx0.call_function(Z:\Daan\FijiMacrosDaan\temp.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:1275)
        at org.scijava.plugins.scripting.jython.JythonScriptEngine.eval(JythonScriptEngine.java:76)
        at org.scijava.script.ScriptModule.run(ScriptModule.java:174)
        at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)
        at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)
        at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)
        at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:191)
        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)

as I said, I’m pretty new at python, so am probably missing something. But that shouldn’t happen, right?

The full example from the website (needs open image):

from ij import IJ  
  
imp = IJ.getImage()  
ip = imp.getProcessor().convertToFloat() # as a copy  
pixels = ip.getPixels()  
  
print "Image is", imp.title, "of type", imp.type  

minimum = Float.MAX_VALUE  
for i in xrange(len(pixels)):  
  if pixels[i] < minimum:  
    minimum = pixels[i]  
  
print "1. Minimum is:", minimum

ps.FIJI/ImageJ1.51d / java 1.8.0_66 on both OSX and Win7

1 Like

@daanb, I get the same error.

I guess that there is some other package that we need to import, or else the Float.MAX_VALUE is out of date.

try this

from ij import IJ

imp = IJ.getImage()
ip = imp.getProcessor().convertToFloat() # as a copy
pixels = ip.getPixels()

print "Image is", imp.title, "of type", imp.type

minimum = pixels[0]
for i in xrange(len(pixels)):
    if pixels[i] < minimum:
        minimum = pixels[i]

print "1. Minimum is:", minimum

the maximum minimum algorithm says that the first value of the array must be the comparator

@BishopWolf, thanks!

So you initialize the minimum value as the value of the 1st pixel in the image, rather than to the maximum possible floating-point value.

That seems at least as good as the original.

Off course you can just do

from ij import IJ

from org.apache.commons.math3.stat.descriptive.rank import Min, Max, Median, Percentile
from org.apache.commons.math3.stat.descriptive.moment import Mean, Variance, Skewness, Kurtosis

imp = IJ.getImage()
ip = imp.getProcessor().convertToFloat() # as a copy
pixels = ip.getPixels()
newpixels = []

print "Image is", imp.title, "of type", imp.type

for pixel in pixels:
    newpixels.append(pixel)
    
print "1. Minimum is:", Min().evaluate(newpixels)
print "2. Maximum is:", Max().evaluate(newpixels)
print "3. Median is:", Median().evaluate(newpixels)
print "4. Percentile is:", Percentile().evaluate(newpixels)
print "5. Mean is:", Mean().evaluate(newpixels)
print "6. Variance is:", Variance().evaluate(newpixels)
print "7. Skewness is:", Skewness().evaluate(newpixels)
print "8. Kurtosis is:", Kurtosis().evaluate(newpixels)
2 Likes

Thanks for all your suggestions, it works perfectly!

Did anyone try:

from java.lang import Float

?

1 Like

@BishopWolf
Cool, those Apache Commons Math 3 statistics functions are neat. We oughta wrap some Ops around them. I filed an issue: https://github.com/imagej/imagej-ops/issues/429

1 Like

Thank you, that solves it!

I tried import java.lang.Float and import java.lang but I see that my syntax was the problem, not the package.

1 Like