Fiji - Jython Interpreter, how does it work?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Fiji - Jython Interpreter, how does it work?

maringa

I am familiar with ImageJ, I can write macros and I know the basics of Java. To make my software more userfriendly I wanted to write plugins instead, and I am now using Fiji (much more extensive!). I read that Jython was a very easy language to use, and that Fiji had a Jython Interpreter. My problem is, that I have no idea how to use it and I haven't found any tutorials. I tried to write MacroJ in the Interpreter, didn't work, I tried to write Java in the Interpreter, didn't work. And how do I save the code, seems like the interpreter just runs and compiles at once?

I would really appreciate some basic introduction, my main questions:
- what language can I use?
- where can I save the code? copy-paste into a textdocument and save as .java?
- tutorials?
- how does the interpreter work

Thanks in advance!

Tina
 

   
Reply | Threaded
Open this post in threaded view
|

Re: Fiji - Jython Interpreter, how does it work?

Albert Cardona
maringa wrote:
>  I am familiar with ImageJ, I can write macros and I know the basics of
Java. To make my software more userfriendly I wanted to write plugins
instead, and I am now using Fiji (much more extensive!). I read that
Jython was a very easy language to use, and that Fiji had a Jython
Interpreter. My problem is, that I have no idea how to use it and I
haven't found any tutorials. I tried to write MacroJ in the Interpreter,
didn't work, I tried to write Java in the Interpreter, didn't work. And
how do I save the code, seems like the interpreter just runs and
compiles at once?
>
>  I would really appreciate some basic introduction, my main questions:
>  - what language can I use?


The language is python. The documentation is that for python 2.5, as
available in the python.org website:

http://www.python.org/doc/2.5.4/

In addition to python's modules, classes and built-in functions, you may
access java classes directly. Hence all classes as described in the java
documentation and the ImageJ documentation are valid:

Java standard library documentation:
http://java.sun.com/j2se/1.5.0/docs/api/

ImageJ Java classes documentation:
http://rsb.info.nih.gov/ij/developer/api/index.html


To see how it compares to Java code, see the scripting comparisons page,
which will give you a reasonable idea:

http://pacific.mpi-cbg.de/wiki/index.php/Scripting_comparisons


>  - where can I save the code? copy-paste into a textdocument and save
as .java?

The interpreter is meant for dynamic interaction with ImageJ.
Keybindings for the interpreter are listed here:
http://pacific.mpi-cbg.de/wiki/index.php/Scripting_Help

To save the code, you may do either of:
 * Select the text on the interpreter running screen and choose "Copy"
   from the popup menu.

 * Write jython code directly to a text file, save it as a file with an
   underscore in its name and with extension .py, and drop it into fiji
   plugins folder or subfolder. Then run "Plugins > Scripting > Refresh
   Jython Scripts" and it will appear as new menu item, like any other
   plugin.

Read this section in detail:
http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting#Workflow_for_creating_Jython_scripts


>  - tutorials?


There is a brief jython for ImageJ tutorial here:
http://albert.rierol.net/jython_imagej_examples.html

The Jython Scripting page also contains some examples:
http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting

The tutorials above assumes you know a bit of python the language. If
you need a deep introduction to python the language, see the python
tutorial:
http://www.python.org/doc/2.5.4/tut/tut.html


There are numerous python scripts in the plugins/Examples folder of
fiji. You will identify them by their '.py' file name extension.


Also, the Jython developers have a page with documentation and examples:

http://www.jython.org/Project/

... particularly here:
http://www.jython.org/Project/userguide.html#interaction-with-java-packages
and here on accessing properties of an object instance:
http://www.jython.org/Project/userguide.html#javabean-properties
and here on java arrays:
http://www.jython.org/Project/userguide.html#java-arrays
and here on creating your own classes derived from java classes:
http://www.jython.org/Project/userguide.html#subclassing-java-classes-in-jython
and here on accessing databases:
http://www.jython.org/Project/userguide.html#database-connectivity-in-jython



>  - how does the interpreter work

Details on key bindings are here:
http://pacific.mpi-cbg.de/wiki/index.php/Scripting_Help

Basically: type text at the prompt (bottom text area), then push enter
to execute it.
To recall previously executed text, use the up/down arrows.
To type in multilines, use shift+enter to open a new line within the
prompt text area.
To move up and down multilines, use shift+up arrow and shift+down arrow.

If you mean how does it work internally, it's just a python language
engine written in pure Java, which gets compiled on the fly to bytecode.
But you shouldn't worry about that.


As I said, the interpreter is for dynamic interaction with ImageJ. Here
is an example.

First open an image, then, from the Jython interpreter:

  >>> imp = IJ.getImage()
  >>> print imp
  imp[AuPbSn40.jpg 600x412x1]
  >>> print imp.width, imp.height
  600 412
  >>> radius = 100
  >>> roi = OvalRoi( imp.width/2 - radius/2, imp.height/2 -radius/2,
radius, radius )
  >>> imp.setRoi(roi)


... and observe how now your image has an oval ROI centered on it.

If something goes wrong, you'll get a printout of an Exception.
The first and second lines of the Exception stack trace are usually the
most useful ones.


>  Thanks in advance!


You are welcome.

Albert
--
Albert Cardona
http://albert.rierol.net
Reply | Threaded
Open this post in threaded view
|

Re: Fiji - Jython Interpreter, how does it work?

maringa


Thanks a lot Albert!
Now I have a lot of tutorials and pages to read through!
Do you think it's a good idea to use Jython when developing plugins? It
feels like the MacroJ is too week to create anything userfriendly, at
least that is my impression of it. I also found some information about
Beanshell, but that is perhaps very similar (when it comes to user
friendlyness and structure) to Jython?


Thanks again for your extensive answer!

Tina

>
>
>
> maringa wrote:
>>  I am familiar with ImageJ, I can write macros and I know the basics of
> Java. To make my software more userfriendly I wanted to write plugins
> instead, and I am now using Fiji (much more extensive!). I read that
> Jython was a very easy language to use, and that Fiji had a Jython
> Interpreter. My problem is, that I have no idea how to use it and I
> haven't found any tutorials. I tried to write MacroJ in the Interpreter,
> didn't work, I tried to write Java in the Interpreter, didn't work. And
> how do I save the code, seems like the interpreter just runs and
> compiles at once?
>>
>>  I would really appreciate some basic introduction, my main questions:
>>  - what language can I use?
>
>
> The language is python. The documentation is that for python 2.5, as
> available in the python.org website:
>
> http://www.python.org/doc/2.5.4/
>
> In addition to python's modules, classes and built-in functions, you may
> access java classes directly. Hence all classes as described in the java
> documentation and the ImageJ documentation are valid:
>
> Java standard library documentation:
> http://java.sun.com/j2se/1.5.0/docs/api/
>
> ImageJ Java classes documentation:
> http://rsb.info.nih.gov/ij/developer/api/index.html
>
>
> To see how it compares to Java code, see the scripting comparisons page,
> which will give you a reasonable idea:
>
> http://pacific.mpi-cbg.de/wiki/index.php/Scripting_comparisons
>
>
>>  - where can I save the code? copy-paste into a textdocument and save
> as .java?
>
> The interpreter is meant for dynamic interaction with ImageJ.
> Keybindings for the interpreter are listed here:
> http://pacific.mpi-cbg.de/wiki/index.php/Scripting_Help
>
> To save the code, you may do either of:
>  * Select the text on the interpreter running screen and choose "Copy"
>    from the popup menu.
>
>  * Write jython code directly to a text file, save it as a file with an
>    underscore in its name and with extension .py, and drop it into fiji
>    plugins folder or subfolder. Then run "Plugins > Scripting > Refresh
>    Jython Scripts" and it will appear as new menu item, like any other
>    plugin.
>
> Read this section in detail:
> http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting#Workflow_for_creating_Jython_scripts
>
>
>>  - tutorials?
>
>
> There is a brief jython for ImageJ tutorial here:
> http://albert.rierol.net/jython_imagej_examples.html
>
> The Jython Scripting page also contains some examples:
> http://pacific.mpi-cbg.de/wiki/index.php/Jython_Scripting
>
> The tutorials above assumes you know a bit of python the language. If
> you need a deep introduction to python the language, see the python
> tutorial:
> http://www.python.org/doc/2.5.4/tut/tut.html
>
>
> There are numerous python scripts in the plugins/Examples folder of
> fiji. You will identify them by their '.py' file name extension.
>
>
> Also, the Jython developers have a page with documentation and examples:
>
> http://www.jython.org/Project/
>
> ... particularly here:
> http://www.jython.org/Project/userguide.html#interaction-with-java-packages
> and here on accessing properties of an object instance:
> http://www.jython.org/Project/userguide.html#javabean-properties
> and here on java arrays:
> http://www.jython.org/Project/userguide.html#java-arrays
> and here on creating your own classes derived from java classes:
> http://www.jython.org/Project/userguide.html#subclassing-java-classes-in-jython
> and here on accessing databases:
> http://www.jython.org/Project/userguide.html#database-connectivity-in-jython
>
>
>
>>  - how does the interpreter work
>
> Details on key bindings are here:
> http://pacific.mpi-cbg.de/wiki/index.php/Scripting_Help
>
> Basically: type text at the prompt (bottom text area), then push enter
> to execute it.
> To recall previously executed text, use the up/down arrows.
> To type in multilines, use shift+enter to open a new line within the
> prompt text area.
> To move up and down multilines, use shift+up arrow and shift+down arrow.
>
> If you mean how does it work internally, it's just a python language
> engine written in pure Java, which gets compiled on the fly to bytecode.
> But you shouldn't worry about that.
>
>
> As I said, the interpreter is for dynamic interaction with ImageJ. Here
> is an example.
>
> First open an image, then, from the Jython interpreter:
>
>   >>> imp = IJ.getImage()
>   >>> print imp
>   imp[AuPbSn40.jpg 600x412x1]
>   >>> print imp.width, imp.height
>   600 412
>   >>> radius = 100
>   >>> roi = OvalRoi( imp.width/2 - radius/2, imp.height/2 -radius/2,
> radius, radius )
>   >>> imp.setRoi(roi)
>
>
> ... and observe how now your image has an oval ROI centered on it.
>
> If something goes wrong, you'll get a printout of an Exception.
> The first and second lines of the Exception stack trace are usually the
> most useful ones.
>
>
>>  Thanks in advance!
>
>
> You are welcome.
>
> Albert
> --
> Albert Cardona
> http://albert.rierol.net
>
>
> ______________________________________
>
> This email is a reply to your post @
> http://n2.nabble.com/Fiji---Jython-Interpreter%2C-how-does-it-work--tp2614733p2615111.html
> You can reply by email or by visting the link above.
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Fiji - Jython Interpreter, how does it work?

Albert Cardona
maringa wrote:
>  Thanks a lot Albert!
>  Now I have a lot of tutorials and pages to read through!
>  Do you think it's a good idea to use Jython when developing plugins? It
>  feels like the MacroJ is too week to create anything userfriendly, at
>  least that is my impression of it. I also found some information about
>  Beanshell, but that is perhaps very similar (when it comes to user
>  friendlyness and structure) to Jython?


By "MacroJ" I guess you mean the ImageJ Macro Language.
The Macro language is great to record a dozen of commands and then
execute that in batch mode using the Multiple Image Processor plugin,
for example.
Beyond a dozen lines, ImageJ Macros become very difficult to understand
a few weeks after being written. I would discourage you to write Macros
other than by using the recorder and editing its output a bit.

Jython is orders of magnitude cleaner to read, and furthermore, way more
powerful: you may use abstractions like classes, collections,
dictionaries, sets, tuples, and even do multithreading (see for example
the plugins/Examples/Delayed_Snapshot.py script for how to launch a
separate thread). Also functions are first-class citizens, so you may
pass functions as arguments to other functions, which is very useful. In
short, Jython is a complete, proper language for writing plugins.

An example of a dictionary containing two new images:

 >>> images = {}
 >>> images["first one"] = ImagePlus("one", ByteProcessor(100, 100))
 >>> images["other"] = ImagePlus("other", FloatProcessor(200, 200))


 >>> print len(images)
 2


Now loop your dictionary and print its data:

 >>> for name, image in images.iteritems():
       print "Name is", name, "and the image is", image

 Name is other and the image is imp[other 200x200x1]
 Name is first one and the image is imp[one 100x100x1


Now query one image by the name key:

 >>> imp = images["other"]
 >>> print imp
 imp[other 200x200x1]


Now define a generic function that applies a function to each
value in the dictionary:

 >>> def apply_to_all(table, fn):
       for value in table.values():
         fn(value)


Then use it to apply an anonymous function to each image:

 >>> apply_to_all(images, lambda x: x.show())

... which results in all images in the dictionary being shown.


Be careful that python specifies it's code blocks not by curly brackets
like java does, but by indentation of its lines., Emails are famous for
not respecting line indentation.


>  Thanks again for your extensive answer!

You are welcome.

Albert
--
Albert Cardona
http://albert.rierol.net