Running a script

Learning Objectives

After completing this lesson, learners should be able to:
  • Understand that a script is a single text file that is written in a specific scripting language

  • Understand the basic building blocks of a script, i.e. what happens in each line

  • Run a bioimage analysis script

  • Modify a bioimage analysis script

Motivation

Scripts are a very good and common way of sharing and publishing bioimage analysis workflows. It is thus very important to know how to run such scripts, e.g. when you find one in a publication or when someone in your bioimage analysis support develops such a script for you. Many of the common bioimage analysis platforms support scripting, e.g. Fiji, QuPath, napari.

Concept map

graph TD S("Script") --> R("Run") S --> X("Share, Publish, Reproduce") R --> Z("Batch") S --> T("Text") T --> P("Programming language") P --> OFP("Variables, functions, parameters, comments, ...")



Figure


Running a python script in the napari script editor plugin.



Activities


Show activity for:  

ImageJ Macro in Fiji

  • Download the local background correction ImageJ macro script.
  • Drag and drop the script onto Fiji; Fiji’s script editor will open.
  • Click Run
  • Try to roughly understand what is happening in each line of the code
    • Note the you can type the command and see the help of the autocompletion.
  • Consult the ImageJ Macro Functions website to find out what certain commands do.
  • Appreciate that run(command, options) is very important.
    • command: Fjij menu entry
    • options: the options of the commands as key value pairs
  • Set the medianFilterRadius to 1 and run the script again

(Draft) Python script in napari console

  • Download the binarisation_napari python script
  • Open the script in a script editor of your choice (e.g.: ???)
  • Open Napari’s IPython console by clicking on the icon [>_]
  • Copy and paste the script into the console and hit enter
  • Discuss the content of the script in order to roughly understand what is happening in each line of the code

(Draft) Python script in napari script editor plugin

  • Download binarisation_napari python script
  • [ Load ] the script into napari’s script editor
  • [ Run ] the script
  • Observer that two images have been added to the napari viewer
  • Discuss the content of the script in order to roughly understand what is happening in each line of the code

Exercises

Show exercise/solution for:

ImageJ Macro in Fiji

  • Download thresholding.ijm
  • Open the script in Fiji’s script editor and click Run.
  • Try to roughly understand what is happening in each line of the code.
  • Add segmentation with a third threshold, e.g. with the threshold so low that the two cells merge into one object.
    • Tip: You can do this mainly using copy and paste

Solution

Download three_thresholds.ijm




Assessment

True or False

  1. Python is a scripting language.
  2. Anaconda is a scripting language.
  3. A comment is a line of code that will be executed.
  4. You can run scripts in Excel and Word.
  5. You can run Python scripts in Fiji.

Solution

  1. True. In fact, python has very powerful image analysis capabilities.
  2. False. Anaconda is something that will help you with dependency management (e.g., of the things you need to run a script).
  3. False. Comments are just for humans to read.
  4. True. Excel and Word in fact do have their own scripting capabilities
  5. True and also False. In fact, you can run Jython scripts in Fiji. Jython is a scripting language that looks like python but it actually runs Java code.

Explanations

Programming script content

A programming script is a text file where each line is code that can be executed by the platform (the compiler) in which you are running the script. There are different types of content that a line can represent. Sometimes one line can even contain multiple of such contents. In the following sections some of the very common types of content are very briefly discussed (check out the follow-up modules for much more details).

Comments

It is good practice to add some human readable comments to explain what the code is doing. To tell the compiler that a part of a script is a comment, one prepends the comment section special symbol, such as // or #.

Examples:

  • ImageJ-Macro, Java, Groovy: // binarise image
  • Python: # binarise image
  • Python: binary_image = image > 49 # binarise image
    • In this example the comment is on the same line as the actual code
Import statements

In some cases one needs to tell the executing environment which libraries are needed to run the code. This is done via so-called import statements.

Examples:

  • Groovy: import ij.plugin.Scaler
  • Python: from os import open
Functions and parameter

Functions are the heart of a program, they do stuff, depending on the paramteres that you give.

Examples:

  • IJ-Macro: run("Duplicate...", "title=duplicateImage");
  • Python: viewer.add_image(image)
Variables

Very often you want to store the results of some computation. In most languages this is achieved by the = sign operator, where you assign the right of the = sign to the varaible on the left.

Examples:

  • IJ-Macro: lengthOfString = getStringWidth("hello world");
  • Python: binary_image = threshold(image, 10)



Follow-up material

Recommended follow-up modules:

Learn more: