Variables

Prerequisites

Before starting this lesson, you should be familiar with:

Learning Objectives

After completing this lesson, learners should be able to:
  • Understand difference between variable name, value, type, and storage

  • How to use variables in functions

Motivation

A variable is an essential concept in programming, it allows to structure and generalize a script/program. A variable stores in memory a value, e.g. a numeric or string value, that can be used and changed at several occasions in a script.

Concept map

graph TD V("Variable") --> |has|Name("Symbolic name") V --> |has|Type("Type") V --> |has|Value("Value") V --> |is| Stored("Stored in memory") Value --> |can| Change Type --> |can| Change



Figure


Variables are containers specific for an information type. Variable names do not contain spaces, should explain their purpose, should be consistent throughout your code, and should adhere to a naming convention.



Activities

Use a script editor for all these activities.

General usage of variables

String variables

Variable type


Show activity for:  

ImageJ Macro, general usage

// ImageJ Macro variables general concepts
// ImageJ Macro language is typeless
// Run these commands one by one


number1; // This will create an error as number1 without = is not defined.

// Define variable symbol and initiate/assign a value
number1 = 10;
number2 = 20;


// Use of variables: e.g. store result of sum in a new variable
sumNumbers = number1 + number2;

// Display results. We are using the variable in a macro
print(sumNumbers);

// Change value of number1
number1 = 20;
print(number1); // value is different
print(sumNumbers); // value is the same as before.


// Use variables in ImageProcessing function with & operator
radiusMedian = 15
open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__nuclei_very_noisy.tif");
run("Duplicate...", "title=Median");
run("Median...", "radius=&radiusMedian"); // The & operator does not always work

// Other method to use variables in string function arguments is with string concatenation. See appropriate module

ImageJ Macro, strings

// ImageJ Macro variables strings
// Preferred double quotes
welcomeMessage1 = "Hello world!";
print(welcomeMessage1);

// Can also use single quote. In standard JAVA this is for char only
welcomeMessage2 = 'Hello world again!';
print(welcomeMessage2);

ImageJ Macro, type

aVariable = 1;

print(aVariable);

aVariable = "Hello World!";

print(aVariable);

// ImageJ is type less and the variable type can change

ImageJ Groovy, type

// Print out is on the console
// Run the two chunks separately as you are not allow to define the variable twice
// Untyped variables
def aVariable = 1;
print(aVariable);
print("\n");
aVariable = "Hello World!";
print(aVariable);

// If you you use typed variables, type change is not allowed
int aVariable = 1;
print(aVariable);
print("\n");
aVariable = "Hello World!";
print(aVariable);

Exercises

Difference of Gaussians

The code performs two gaussian blurs of an image with fixed sigma. The code then computes the difference of the two filtered images.

  1. Modify the code and replace the hard-coded sigma value of the gaussian blur with 2 variables, sigma1 and sigma2 respectively
  2. Make sigma2 three times larger than sigma1 and run again the code

Fix it

Try to run the code and fix the error(s). Modify the variable names to camelCase in a way that their content/meaning is clear to the reader.

Show exercise/solution for:

ImageJ Macro, difference of gaussians

Copy the code to the FiJi script editor and solve the exercise.

Hint: You need to modify the code in line 5 and 7 and for example use the & operator to include the variables.

run("Close All")
open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif");
rename("raw");
run("Duplicate...", "title=sigma1");
run("Gaussian Blur...", "sigma = 1");
run("Duplicate...", "title=sigma2");
run("Gaussian Blur...", "sigma = 1.5");
// This subtracts the blurred image from the raw image, i.e. sigma1 - sigma2
imageCalculator("Subtract create", "sigma1","sigma2");

Solution

run("Close All")
sigma1 = 1.0;
sigma2 = 1.5;
// For part two
// sigma2 = 3*sigma1; 
open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif");
rename("raw");
run("Duplicate...", "title=sigma1");
run("Gaussian Blur...", "sigma=&sigma1");
run("Duplicate...", "title=sigma2");
run("Gaussian Blur...", "sigma=&sigma2");
// This subtracts the blurred image from the raw image, i.e. sigma1 - sigma2
imageCalculator("Subtract create", "sigma1","sigma2");

ImageJ Macro, fix it

Copy the macro code to the FiJi editor and press Run. This will create an error.

  1. Fix the variable naming for the file path. Use the camelCase naming convention.
  2. Change the name of the variables a and b so that it their names are meaningful and reflect their content. Use the camelCase naming convention.
run("Close All");
FilePath = "https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif";
a = 1;
b = 2;
open(filePath);
rename("input");
run("Duplicate...", "title=tophat");
run("Top Hat...", "radius=&b");
selectWindow("input");
run("Duplicate...", "title=");
run("Variance...", "radius=&a");

Solutions

run("Close All");
filePath = "https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_16bit__autophagosomes.tif";
varianceRadius = 1;
topHatRadius = 2;
open(filePath);
rename("input");
run("Duplicate...", "title=tophat");
run("Top Hat...", "radius=&topHatRadius");
selectWindow("input");
run("Duplicate...", "title=variance");
run("Variance...", "radius=&varianceRadius");



Assessment

True or False

Solution

  • False
  • True
  • False
  • True

Explanations




Follow-up material

Recommended follow-up modules:

Learn more: