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
Show how variables need to be assigned/declared before usage
Create two numeric variables and store results of addition in a new variable
Use a numeric variable in a simple function (e.g. print(variable)) and in an image processing function
String variables
Show an example of a string variable and how to declare it
Variable type
Discuss variable type and, for some languages, type declaration
Discuss possibility of type mismatch
Show activity for:
ImageJ Macro, general usage
// ImageJ Macro variables general concepts// ImageJ Macro language is typeless// Run these commands one by onenumber1;// This will create an error as number1 without = is not defined.// Define variable symbol and initiate/assign a valuenumber1=10;number2=20;// Use of variables: e.g. store result of sum in a new variablesumNumbers=number1+number2;// Display results. We are using the variable in a macroprint(sumNumbers);// Change value of number1number1=20;print(number1);// value is differentprint(sumNumbers);// value is the same as before.// Use variables in ImageProcessing function with & operatorradiusMedian=15open("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 quoteswelcomeMessage1="Hello world!";print(welcomeMessage1);// Can also use single quote. In standard JAVA this is for char onlywelcomeMessage2='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 variablesdefaVariable=1;print(aVariable);print("\n");aVariable="Hello World!";print(aVariable);// If you you use typed variables, type change is not allowedintaVariable=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.
Modify the code and replace the hard-coded sigma value of the gaussian blur with 2 variables,
sigma1 and sigma2 respectively
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 - sigma2imageCalculator("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 - sigma2imageCalculator("Subtract create","sigma1","sigma2");
ImageJ Macro, fix it
Copy the macro code to the FiJi editor and press Run. This will create an error.
Fix the variable naming for the file path. Use the camelCase naming convention.
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.