Strings and paths
Prerequisites
Before starting this lesson, you should be familiar with:
Learning Objectives
After completing this lesson, learners should be able to:
Construct complex strings, e.g. to produce log messages and create file paths
Automatically create paths for saving the results of the analysis of an input image
Motivation
Combining several strings into a larger string is a prevalent operation in scripting. This is useful, e.g., to create file paths and create log messages. Such concatenation of strings is surprisingly error prone and it is thus important to learn it properly and be aware of all the pitfalls. In addition, it is critical to be able to deal with and manipulate file paths, as this is crucial to write code that automatically saves resulting data at appropriate locations.
Concept map
Figure
Activities
Creating log messages
- Open a script editor
- Define two variables:
- index of the hypothetially currently processed image with value
11
- the total number of images to be processed with value
100
.
- index of the hypothetially currently processed image with value
- Using those two variables create the message:
"Analyzing image 11/100..."
.
Show activity for:
ImageJ Macro
python
Manipulate file paths
In bioimage analysis, one very often opens one image and then needs to save a segmentation (label mask file) and segmented object measurements (a table file). To this end, it is useful if the output images have similar names than the input image, such that one knows that they belong to each other. Sometimes it is useful for those output files to be placed in the same folder as the input image, but often you want to specify a dedicated output folder.
To manage all these scenarios it is critical to learn how to manipulate file and folder paths in various ways and thereby construct the output paths from the input path and a given output directory.
Show activity for:
ImageJ Macro
python
Exercises
Show exercise/solution for:ImageJ Macro: Concatenate variables
Open a new script editor
- From ImageJ GUI open a script [ File > New > Script…]
- Choose the language IJ1 Macro
- Define a string variable with the content
"nucleus"
- Define a numerical variable with the content
6
- Concatenate the variables to get the final string
"nucleus_6"
- Print the final string
Solution
// define variables str = "nucleus"; num = 6; // concatenate concat = str + "_" + num; // print the string to the log window print(concat);
ImageJ Macro: Create function arguments
Create a macro that applies a gaussian blur with a certain sigma value to an image.
- Open a sample image: [ File > Open samples > Blobs ]
- Open the script editor: [ File > New > Script…]
- Choose the language IJ1 Macro
- Add the line
run("Gaussian Blur...", "sigma=6");
- Tip: In programming copy and paste as much as possible!
- Run the macro to see the effect on the image
- Define a variable
blurSigma
with the value6
- Replace the
6
in therun(...)
command with this variable.Solution
blurSigma = 6; run("Gaussian Blur...", "sigma="+blurSigma ); // Below would also work and is very convenient, but a bit non-standard... // run("Gaussian Blur...", "sigma=&blurSigma");
ImageJ Macro: Create paths
Concatenatimg folder and file names
- [ File › Open Samples › Blobs ]
- Save the image onto the Desktop of your computer
- Open the script editor: [ File > New > Script…]
- Choose the language IJ1 Macro
- Define
folder
as a string variable pointing to your computer’s desktop folder (it should be something likeC:\Users\Username\Desktop
or/Users/Username/Deskop
)- If you are on Windows, watch out: In order to have
C:\Users\Username\Desktop
you will have to writeC:\\Users\\Username\\Desktop
, because\
is a special character that needs to be “escaped”- Define
fileName
as a string representing the file name of the image that you just saved (it should be something like"blobs.tiff"
)- Concatenate the variables to get a new string (
filePath
) representing the full file path (folder + File.Separator + fileName
).- Use the filePath variable to add code that prints something like “Opening C:\Users\Username\blobs.tiff”
- Add
open(filePath);
to open the image.- Run the macro
Solution
// define the variables folder = "/Users/Username/Desktop/"; // <= This must be replaced! fileName = "blobs.tiff"; // <= Be careful: There is ".tiff" and ".tif" // concatenating, adding the file separator in the middle filePath = folder + File.separator + fileName; // print log message print("Opening " + filePath); // open the file open(filePath);
Assessment
Fill in the blanks
- In MacOS and Linux sub-folders are separated by the
___
string, whereas on Windows they are separated by the___
string. - Concatenating
"Hello"
and"world"
one obtains___
. - Concatenation the variables
folder = "/Users/Images"
andfile = "MyImage.tif"
one obtains___
.
Solution
- MacOs
"/"
, Windows"\"
- One would get
"Helloworld"
; to fix this one needs to add a third" "
string in the middle to get"Hello world"
.- One would obetain
"/Users/ImagesMyImage.tif"
. There are several ways to fix this, depending on the scripting language. A good way is to use functions such as, e.g.,os.path.join( folder, file )
in python, because this will work for both cases:folder = "/Users/Images"
andfolder = "/Users/Images/"
.
Explanations
Creating paths
A frequent operation in bioimage analysis is to create paths to images by concatenating a folder and file name to a full path. Please note that when concatenating a folder and a file name into a full path, you might need to add a so-called file separator between the folder and the file name. This is a character that separates directory names within a path to a particular location on your computer. Different operating systems use different file separators: on Linux and MacOS, this is /
, while Windows uses \
. To make it worse, when you store a directory you are typically never sure whether the contained string ends on /
or \
or does not have the separator in the end, e.g. C:\Users\Data
, in which case you have to add it when concatenating a file name). To make it even worse, in some programming langauges the \
character have a special meaning within strings and is thus not simply interpreted as a character and to actually get a backslash you may have to write \\
.
If you want to have some “fun” you can read those discussions:
As all of this can quickly become a huge mess, fortunately, scripting languages typically offer special functions to help you write code to create file paths that will run on all operating systems.
String concatenation
String concatenation is the operation of joining multiple substrings.
For example concatenating “Hello “ and “world!” would result into “Hello world!”.
Follow-up material
Recommended follow-up modules:
Learn more: