Handling input parameters
Prerequisites
Before starting this lesson, you should be familiar with:
Learning Objectives
After completing this lesson, learners should be able to:
Organise script parameters in the code such that can be easliy adapted
Create dialog boxes for fetching script parameters
Motivation
Scripts typically have parameters that one would like to change while leaving the core of the code untouched. Examples for such changable parameteres are the input image file and image processing parameters such as filter sizes and thresholds. It is very important to learn how to “expose” such parameters in ways that do not require digging into and modifying the actual code too much.
Concept map
graph TD
S("Script") ---|has| P("Parameters")
CB("Code block") --> P
UI("UI elements") --> P
CF("Config file") --> P
A("Script arguments") --> P
Figure
Activities
- Exposing script parameters
- Open a script and carefully examine whether it contains parameters.
- Modify the script to implement different options for exposing these parameters (e.g, show in above figure and concept map).
- Interactive ROI placement
- Develop a script to interactively fetch a ROI drawn by the user and use it for a measurement.
Show activity for:
Exposing script parameters: ImageJ Macro
- Download median_filter.ijm and open in Fiji.
- Identify the input parameters to be fetched
- Create parameter code block at the top of the script
- Write code to create a GUI to fetch these parameters
Solution
Download median_filter_solution.ijm
Interactive ROIs: ImageJ Macro
Exercises
- Exposing script parameters
- Open a script and carefully examine whether it contains parameters.
- Modify the script to implement different options for exposing these parameters (e.g., as show in above figure and concept map).
Exposing script parameters: ImageJ Macro
- Download segment_nuclei.ijm and open in Fiji.
- Carefull examine the script and identify potential parameters.
- Tip: There are up to 5 parameters.
- Replace all potential parameters by variables and define them in a code block at the top of the script
- Write code to create a GUI to fetch these parameters
- Tip: In order not to define the varibales twice you need to remove (or better comment) the parameter code block that you just created.
Solution
Download segment_nuclei_solution.ijm
Assessment
Discussion
- Generally, what could be the pros and cons (considerations) of the different ways (see figure and concept map) in which scripting parameters can be handled?
- Specifically, what are the pros and cons of using UI elements for fetching parameters?
Solution
- This is a complex topic. Considerations could be: (i) How can I keep track which images were analyzed with which parameters? (ii) How can I ensure that users of my script use valid parameters? (iii) How experienced are the users of my script (e.g. would they be able to modify the script itself)? (iv) If the script itself is modified when changing a parameter, how do I keep track of the different “versions” of the script?
- Pro: UI elements typically allow you to restrict the input values to a valid range and it makes your script easy to use for people without programming experience. Con: Every time you want to run the script you have to interact with the UI and, unless you implement something special, you don’t keep track which parameters were used to run the script.
Explanations
Follow-up material
Recommended follow-up modules:
Learn more: