Tutorials for Python for Fiji

Hi,

I’m a very beginner in the programmation area. I began macros 3 months ago, and I read advices for the python learning. I’ve a few questions about it :
-Is Python really usefull for the scientific imagery analysis ?
-What is the difference between Jython and Python ? (I have no computational vocabulary so it’s hard for me to understand what i find on google).
-Do you have tutorials or online free courses for the use of python (or jython if better…) in image analysis ?

Thanks a lot !
Virgile.

Hi Syra,

Oh yes, see scikit-image, OpenCV and related projects.

The syntax is the same ie you would make a function of for-loop the same way but in Jython the code is run on the Java virtual machine, which in practice allows you to call Java libraries just like if there were Python libraries.

Jython is mostly used as a scripting language in Fiji. Out of Fiji, Jython is not really used “as is”.

Yes we do on the ImageJ wiki.
This should help you to get started. also explore the other sections (scripting in general…)

4 Likes

Hello Virgile (and Laurent) -

To answer your original question: Jython is python. It’s a
specific implementation of python. (But more details, below.)

You should be aware of two significant limitations of jython.

(But limitations aside, let me note that I like jython a lot for
scripting Fiji. Having direct access to the Fiji java classes
and their methods in jython is really what makes it work well
for this use case.)

First, jython is python 2, rather than the current python 3. You
may not care – I don’t care much myself – but it’s something
to be aware of. (As I understand it, the jython project is not
that active anymore, and they haven’t chosen to upgrade to
python 3.)

Second, jython can’t use python packages that incorporate
native code (that is, that have part of their functionality written
in something like c that is compiled into a platform-specific
library and shipped with the package). This rules out, in
particular, numpy, which is used a lot in scientific python
processing, including image analysis.

(Jython is a python interpreter written in java that uses java
reflection (and some type mapping) to automatically provide
the very useful python–java bridge. This makes it easy to
incorporate python as a scripting language into an application
written in java, such as Fiji / ImageJ.)

One last comment: Learning python is unlikely to be wasted
effort. Python is pretty good (It’s not my favorite language, but
it’s pretty good.), and it’s commonly used for lots of different
things with a healthy community and lots of different (useful)
packages. So independent of Fiji (and jython), learning python
is likely to pay off in the future.

Thanks, mm

2 Likes

Mountain Man’s comment about support for Python 2 is important. Python 2 will be unsupported after January 1, 2020. As someone who has many Jython scripts for FIJI and for the NIST DTSA-2 X-Ray microanalysis software this causes me some concern. It is something I will continue to watch…

1 Like

Thanks a lot to the three of you ! I begin today my learning.
For future people : I also found this website that provides a very descriptive tutorial for Python & Fiji :
https://www.ini.uzh.ch/~acardona/fiji-tutorial/

2 Likes

also my favourite one! thanks @albertcardona

Glad it’s useful, @Syra & @emartini. Most of the “Fiji Scripting Tutorials” are, so to speak, notes to my future self.

If you are curious about real-world scripts written in Jython for the analysis of multi-terabyte data sets, both electron microscopy and light microscopy, see: https://github.com/acardona/scripts/tree/master/python/imagej

In particular, look into the IsoView-GCaMP/lib and the programs that use it and related folders (e.g. tests).

Recently I created scripts for fast registration of FIBSEM series of images which apply equally to any series of sections that mostly already registered and merely require some translation and contrast adjustment.

1 Like

Hi @John (and @mountain_man),

While jython is currently in development, it is true that development is rather slow. A recent conversation with developers highlighted that they would welcome funding, and I am set on raising said funding, as well as identifying a suitable developer to carry on the transition from 2.7 to 3.6+.

Regarding compatibility with python 3, note that jython supports the __future__ package, to bring in useful built-ins such as python 3’s with statement and python 3’s print function with parentheses and its properties. For example:

from __future__ import with_statement, print_function
import csv

with open("/tmp/table.csv") as csvfile:
  reader =  csv.reader(csvfile, delimiter=',', quotechar='\"') 
  reader.next() # skip first line with headers
  for name, ID, size in reader:
    print(name, ID, size, sep=", ")

See also this example on the usage of with for writing and reading files, automatically taking care of closing the file handles.

1 Like

Another promising development for bridging Java and Python, R, etc. is GraalVM.:

https://www.graalvm.org/

https://www.graalvm.org/docs/reference-manual/languages/python/

https://www.graalvm.org/docs/reference-manual/polyglot/

1 Like

That is great news, Albert. Thank you for taking the initiative to make this happen.

The best part of working with Jython in Fiji is the ability to make a file of Jython functions (a library, maintained under version control) and place it in Fiji.app/jars/Lib and then import the functions into my scripts. My main library is named jmFijiGen.py and I import the functions into my scripts with this line:

import jmFijiGen as jmg

and call a function from a script with a call like the one shown below to do a background correction, and return the background corrected image. (The blur size is 0.3*image width defined in the file’s doc string)…

imp_bks = jmg.gaussian_blur_8_bks(imp, size)

Haven’t yet figured out the correct syntax to do that with groovy (my backup scripting language…)

Hello Albert -

I looked at the jython-development conversation you linked to.

Does that mean that you and/or the jython developers have
scoped porting cpython 3 to java / upgrading jython 2 to jython
3 as a two-man-year project? Or is that viewed as an add-on
to a larger development effort?

Thanks, mm

Hi @mountain_man,
The team of developers that volunteer their free time to continue developing jython should be considered part of that project. Also, there is already a port of python 3 to the JVM, that they started, but is currently in alpha stage; the last commit is from 2 years ago https://github.com/jython/jython3
Starting from this tree, a 2 man/year project could move the ball a lot further, perhaps to a mature beta or even a release for some form of python 3 (perhaps not 3.7, but e.g. 3.5).
Are you interested in this project?