Trying to get proper format for script

script-editor
scripting
Tags: #<Tag:0x00007fa307764e48> #<Tag:0x00007fa307764d08>

#1

I’m trying to run a script for ops and the format is unclear.
The starting point is https://nbviewer.jupyter.org/github/imagej/tutorials/blob/master/notebooks/1_-Using_ImageJ/ImageJ_Ops/01-_A_Very_Quick_Start_Guide.ipynb

I understand that the preferred language is jython which is chosen by using the python language under Fiji File -> New -> Script…
The very quick start, as written, doesn’t work, so I’m trying to get something which does work.

# @ImageJ ij
import net.imglib2.interpolation.randomaccess.NLinearInterpolatorFactory

clown = ij.io().open("http://imagej.net/images/clown.png")
ij.ui().show("clown",clown)
scaleFactors = [0.5, 0.5, 1]
interpolationStrategy = new NLinearInterpolatorFactory()

image = ij.op().run("scaleView", clown, scaleFactors, interpolationStrategy)
ij.ui().show("image",image)

I can get the clown to show but trying to scale it down, I’ve reached the end of my guessing game for the correct format. I get:

SyntaxError: no viable alternative at input 'NLinearInterpolatorFactory'

I don’t know if I have Jython, python or something else, but the language I chose is python (there is no jython).

Thanks,
Ilan


#2

Hi @ilan,

the imports must be reformulated a bit to match the pattern from <package> import <class>. Futhermore, there is no new in python/jython. The following script should run from within Fijis script editor:

@ImageJ ij
from net.imglib2.interpolation.randomaccess import NLinearInterpolatorFactory

clown = ij.io().open("http://imagej.net/images/clown.png")
ij.ui().show("clown",clown)
scaleFactors = [0.5, 0.5, 1]
interpolationStrategy = NLinearInterpolatorFactory()

image = ij.op().run("scaleView", clown, scaleFactors, interpolationStrategy)
ij.ui().show("image",image)

Cheers,
Robert


#3

Hi Robert,
Thanks for the reply. What you suggested works.
I received a suggestion to use Jython for ops with a reference to the site quoted above for a “A very quick start”. The name python I have known for years, but since I was very used to java, I never had to use it. Jython I had never heard, but it is supposed to work under ImageJ if you choose the python language.

The very first example didn’t work

from net.imglib2 import RandomAccessibleInterval
tile = { images ->
  int[] gridLayout = images[0] in List ?
    [images[0].size, images.size] : // 2D images list
    [images.size] // 1D images list
  RandomAccessibleInterval[] rais = images.flatten()
  ij.notebook().mosaic(gridLayout, rais)
}

where I translated the import statement to the format you showed me.
Something is wrong with “List ?” in this jython statement isn’t accepted in python. I also know that the comments // 2D images list are not accepted, but it has already crashed on the question mark.

It isn’t clear to me why the recommended jython doesn’t work. It seems it should be fixed or put into the junk bin. Perhaps making some special language ending in place of python should be used?

In any case what is the proper translation into python?

Thanks,
Ilan


#4

Please note that the notebooks at https://github.com/imagej/tutorials/tree/master/notebooks all use Groovy, not Python (although the file extension .ipynb stands for IPython notebook, which is confusing, I know – but they all use the polyglot SciJava Jupyter Kernel instead of a Python kernel).

I’d recommend to start learning with Groovy, as it runs natively in the JVM, whereas Jython (the Java implementation of Python) currently has some limitations, in particular when used in combination with the versatile ops signatures.

If you really want to stick to Jython/Python, have a look at the Jython scripting page:

But Ops are currently easier to use with Groovy, really.


#5

Thanks Jan,
The important sentence is:
“But Ops are currently easier to use with Groovy, really.”

If I have to learn something, I may as well make the best choice up front.

Ilan


#6

There is no universal best choice, unfortunately. People have different preferences, and the script languages are strong in different ways. E.g., learning Jython means learning Python, which is a valuable skill these days. And in the future, I expect Python libraries like numpy to become more accessible from Jython as well.