[ImageJ-devel] Scripting examples

Curtis Rueden ctrueden at wisc.edu
Wed Feb 19 16:48:15 CST 2014


Hi Brian,

> So far in ImageJ2 I have been writing java applications that start by
> instantiating an ImageJ app and then use that to access services and
> run commands. Basically following the tutorials.
>
> So how are things intended to work in a script??  Is there a simpler
> api somewhere like the IJ API??  Or does one still need to use the
> service APIs in a script??

The intent is to use the ImageJ gateway in your scripts (but see my P.S.
below). So that way, what you've learned in the tutorials is equally
applicable to scripting. From my perspective, writing:

  ij.ui().showDialog("Hello world!");

is just as easy to read and write as:

  IJ.showDialog("Hello world!");

The advantages of IJ2's service-based approach are many:

1) Routines are naturally grouped according to which service they are in,
so you can use IDE autocompletion to first browse the available services,
and then browse the methods available for a particular service. Whereas if
you browse IJ.<ctrl+space> you get one massive list of methods that I
personally find somewhat overwhelming.

2) The ImageJ application context can be extended with additional services,
which are also available via compile-safe ij.get(...).fooBar() calls.

3) Core service implementations can be overridden to behave differently,
unlike the static IJ.* methods which can never be overridden (unless you
use byte-code manipulation, which is serious voodoo).

4) All scripting languages use one unified API. There is no separate list
of "macro language functions" in IJ2.

> I'm wondering if there is a more concise way to run the command and
> get the output?? Something more like this....
>
> output=command.run("somecommand");
>
> Basically a way to run the command, have it automatically block, and
> return the output(s) (if they exist).  I could write a wrapper but
> first I want to make sure I know what all ready exists.

Nope, we don't have that. I think the ambiguity of returning an Object[] in
some unspecified order is not very friendly. I guess we could return the
output map, but then it's hardly shorter than:

outputs = command.run("somecommand").get().getOutputs();

There is already an unfortunate proliferation of run method signatures in
ModuleService and CommandService, so I am honestly loath to add any more.
But if you have an idea for a method to make the API more convenient, let
us know!

Regards,
Curtis

P.S. ImageJ master now has a working Script Editor with parameter
annotations, thanks to Johannes's efforts. I also made a branch to support
"@ImageJ" auto-filling [1], although it won't merge to master until after
the next release of SciJava Common.

[1] https://github.com/imagej/imagej/compare/gateway-autofill


On Wed, Feb 19, 2014 at 11:22 AM, Brian Northan <bnorthan at gmail.com> wrote:

> Hi Curtis, Johannes and list
>
> The scripting is working well.
>
> One quick question.  To run a command and retrieve the output I basically
> have this
>
> --snip--
> # @CommandService command
>
> module=command.run("somecommand").get();
> output=module.getOutputs().get("output");
>
> (or)
> output = module.getOutputs().values().toArray()[0];
> --snap--
>
> I'm wondering if there is a more concise way to run the command and get
> the output?? Something more like this....
>
> output=command.run("somecommand");
>
> Basically a way to run the command, have it automatically block, and
> return the output(s) (if they exist).  I could write a wrapper but first I
> want to make sure I know what all ready exists.
>
>
> On Tue, Feb 18, 2014 at 4:14 PM, Brian Northan <bnorthan at gmail.com> wrote:
>
>> Thanks very much Johannes and Curtis
>>
>> Johannes' code snippet works perfectly.  That's exactly what I needed to
>> know.  How to access the services from a script.  Now that I know how to do
>> that I can try some more complicated stuff (use the datasetservice and
>> commandservice to open images and perform operations).  I will let you know
>> if any questions come up.  Please let me know any additional information or
>> tips that will be helpful.
>>
>> Thanks again
>>
>> Brian
>>
>>
>> On Tue, Feb 18, 2014 at 2:26 PM, Curtis Rueden <ctrueden at wisc.edu> wrote:
>>
>>> Hi Brian,
>>>
>>> I am out of the office today, and will reply in more detail later, but
>>> just wanted to quickly add: the example Johannes wrote should work with the
>>> current master branch as well as the recent 2.0.0-beta-7.7 release.
>>> Specifically, it will work if you "Run Script" since the parameter parsing
>>> mechanism etc. is all already in place. It just doesn't work from the
>>> Script Editor yet since that tool needs some TLC still. As Johannes says, I
>>> am still actively hacking on scripting support.
>>>
>>> Also note that you can inject a Context parameter then make a new ImageJ
>>> gateway object wrapping it to easily access services the same way the
>>> tutorials do. (I forget whether ImageJ gateways are injectable ATM but if
>>> not, they will be.)
>>>
>>> Anyway, please let us know if you are unable to run such scripts.
>>>
>>> Regards,
>>> Curtis
>>> On Feb 18, 2014 12:41 PM, "Johannes Schindelin" <
>>> Johannes.Schindelin at gmx.de> wrote:
>>>
>>>> Hi Brian,
>>>>
>>>> On Tue, 18 Feb 2014, Brian Northan wrote:
>>>>
>>>> > Now I am trying to understand how one should be accessing ImageJ2
>>>> > functionality from the scripts.  In ImageJ1 I use the static
>>>> functions in
>>>> > the IJ class, IJ.run, IJ.openImage etc.
>>>>
>>>> Yeah, in IJ2 you have to use a context. The static functions of ImageJ
>>>> 1.x
>>>> do not allow you to insulate separate processes from each other. That
>>>> leads to surprising results, e.g. when you try to put more than one
>>>> ImageJ 1.x applet into the same web page: it simply won't work, ever.
>>>>
>>>> > So far in ImageJ2 I have been writing java applications that start by
>>>> > instantiating an ImageJ app and then use that to access services and
>>>> run
>>>> > commands. Basically following the tutorials.
>>>> >
>>>> > So how are things intended to work in a script??  Is there a simpler
>>>> api
>>>> > somewhere like the IJ API??  Or does one still need to use the
>>>> service APIs
>>>> > in a script??
>>>>
>>>> This is a work in progress right now, under heavy construction.
>>>> Eventually, something like this will work:
>>>>
>>>> -- snip --
>>>> # @StatusService status
>>>> # @OUTPUT String greeting
>>>>
>>>> status.showStatus(1, 2, "In progress...")
>>>> status.warn("This is a gentle warning")
>>>> status.showStatus(2, 2)
>>>>
>>>> greeting = "Hello, world!"
>>>> -- snap --
>>>>
>>>> In other words, your input and output parameters will be specified
>>>> using a
>>>> syntax similar to ImageJ2 commands' at the top of the script.
>>>>
>>>> Ciao,
>>>> Johannes
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20140219/d5284aaf/attachment.html>


More information about the ImageJ-devel mailing list