Script Parameters

Revision as of 06:13, 29 August 2017 by Eglinger (talk | contribs) (Basic syntax: Embed source code from github)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Learn
Topics
Introduction
Getting Started
User Guides
Tutorials
Tips and Tricks
Presentations
Plugins
Techniques
All Techniques
Colocalization
Deconvolution
Registration
Segmentation
Stitching
Tracking
Visualization
Scripting
Overview
Parameters
Basics of script writing
Batch processing
Script Editor
Auto Imports
Templates
Running headlessly
Comparisons
Toolbox
Multithreading in Clojure
Multithreading in JavaScript
Chess in Jython
Languages
BeanShell
Groovy
ImageJ Macro
JavaScript
Lisp (Clojure)
MATLAB
Python (Jython)
R (Renjin)
Ruby (JRuby)
Scala

All scripting languages have access to a universal #@parameter notation for declaring inputs and outputs. This approach is preferred to using ImageJ 1.x GenericDialog because it is totally agnostic of the user interface, allowing such scripts to run in a variety of contexts. As with ImageJ2 plugins, script parameterization is based on the SciJava @Parameter annotation—so experience with plugin writing directly translates to scripting, and vice versa.

Basic syntax

The rules for #@parameter use are as follows:

  1. Parameter declarations begin with #@. Each such line contains a single parameter declaration and nothing else.
  2. #@type variableName will declare an input of the indicated type, assigned to the specified name.
  3. #@output type outputName will declare the variable of the specified name as an output parameter with the given type.
“zomg UIs are so easy now
done by lunchtime”
Kyle Harrington,
Clojure developer
[ source ]

For example, if we look at the Greeting.py template supplied with Fiji:

src/main/resources/script_templates/Intro/Greeting.py

# @String(label="Please enter your name",description="Name field") name
# @OUTPUT String greeting

# A Jython script with parameters.
# It is the duty of the scripting framework to harvest
# the 'name' parameter from the user, and then display
# the 'greeting' output parameter, based on its type.

greeting = "Hello, " + name + "!"

We see that an input parameter name of type String is declared. @Parameters are handled automatically by the framework; if we run this script when the User Interface is available (e.g. from the script editor), the name parameter will automatically be harvested via a pop-up dialog:


ScriptParams.png


We could also run this script headlessly, thanks to the general nature of @parameters.

When the script is completed, any #@output variables are handled by the framework, based on their type. In this case we expect the greeting variable to be printed, since it is a string.

Parameter types

A list of possible data types and the corresponding widgets is provided:

Data type Widget type Available styles
boolean | Boolean checkbox
byte | short | int | long numeric field slider | spinner | scroll bar
Byte | Short | Integer | Long numeric field slider | spinner | scroll bar
BigInteger | BigDecimal numeric field slider | spinner | scroll bar
char | Character | String text field text field | text area | password
Dataset | ImagePlus (>=2 images) triggers a dropdown list
ColorRGB color chooser
Date date chooser
File file chooser open | save | directory

By implementing InputWidget it is possible to extend this list.

Parameter properties

If you look at the @Parameter annotation, you will notice it has many properties—for example, name and description.

Script parameters can set these properties, following these guidelines:

  1. All properties are defined in a single parenthetical expression immediately following the #@type declaration.
  2. Properties are set by a comma-separated list of key=value pairs

Properties are your way to customize how an #@parameter should be handled by the framework.

Widget labels

Widgets are the User Interface elements shown to users to collect input information. For example, instead of just displaying "Name" to the user, we can add a custom label to the field of our Greeting.py script as follows:

#@String(label="Please enter your name") name
#@output String greeting

greeting = "Hello, " + name + "!"

Widget mouseover

We can add a description property to provide mouse-over text for our field:

#@String(label="Please enter your name", description="Your name") name
#@output String greeting

greeting = "Hello, " + name + "!"

Default values

Default values are also supported as parameter properties:

#@Integer(label="An integer!",value=15) someInt

Multiple Choice

Any parameter can be turned into a multiple-choice selector by adding a choices = {...} property:

#@String(label="What mythical monster would you like to unleash?",choices={"Kraken","Cyclops","Medusa","Fluffy bunny"}) monsterChoice

Files and Folders

By default, a #@File parameter will create a chooser for a single file. Here is an example in python:

#@File(label="Select a file") myFile

print(myFile)

The exact same code works for the ImageJ1 Macro language, too.

If you want to select a directory instead, use a style property:

#@File(label="Select a directory", style="directory") myDir

print(myDir)

Styles

You can influence the visual style of some of the input widgets. See previous paragraph for an example for how to switch from file to folder selection. You can also switch

#@String(choices={"Option 1", "Option 2"}, style="listBox") myChoice123
#@String(choices={"Option A", "Option B"}, style="radioButtonHorizontal") myChoiceABC

print(myChoice123)
print(myChoiceABC)

Input-styles.png