The MEDx ImageScript Reference Page

The goal of ImageScript is to provide users with a simple, easy to use scripting language interface to the MEDx image processing engine. The primary goal is to provide script writers with access to the complete functionality of MEDx that is available through the user interface. To that end we have chosen Tcl as the basis for the scripting language since the style and nature of programming in Tcl is very similar to that of existing shell scripting languages which many scientists currently use.

MEDx Extensions to Tcl/Tk


Alphabetical Listing

Data Management

Object Management

External Program Interfacing

General Image Processing


Transformation

Filtering

Image Measurement

Functional Image Analysis

Medical Image Processing

Important Tcl functions

Not only does Tcl provide generic programming constructs such as control loops and procedures, it is also easily extended and can be embedded in other applications. It provides an interpreter-based programming environment which allows for rapid prototyping and development.

Learning to script is a combination of becoming familiar with the basic Tcl functions, understanding the main Tcl and ImageScript data types, and learning how to develop a script. One of the advantages of Tcl is that it is an interpreted language which allows a user to enter a command and see the affect the command.

The main Tcl functions that you will want to be come familiar are set, expr, and various control functions. The set function assigns a value to a variable. The expr function computes a mathematical expression. For example, the following command will compute which slice is the middle slice of a volume:

 
 
set MiddleSlice [expr $NumSlices / 2]

In this case, the expr function divides the variable NumSlices by 2 and this result is assigned to another variable called MiddleSlice. Any command enclosed in brackets is executed first and the result of that command is used in the evaluation of the outer command. Also notice that the value of a variable is de-referenced by preceding it with a dollar sign.

The various Tcl control functions allow you to write scripts that loop over a set of functions as well as make decisions based on a test-condition. This category of functions includes: for, foreach, while, and if-else. For example, to open a set of 10 images you can use a for loop:

 

for {set i 0} {$i < 10} {incr i 1} {
    set n [expr $i + 1]
    MxOpenImage $Folder nec$n.hdr Volume($i)
}

The for loop has four components: an initialization command, a test-condition, a loop completion command, and a loop body. Upon entering a for loop, the initialization command is executed. The test-condition is then evaluated. If the test-condition is not true then the command in the the loop body are executed. After executing the loop body, the loop completion command is executed. The loop body will continue to be evaluated until the test-condition is true.

In the above example, the variable i is used as a counter to keep track of the number of times the loop is executed. On each iteration of the loop, the variable n is set equal to the value of i plus one. The incr command is used in the loop completion command to increment i by one in preparation of the next iteration of the loop. On the first iteration of the loop i is equal to zero and n is set equal to 1. In this iteration the named nec1.hdr is opened into a folder referenced by the variable Folder. The result of opening this image is assigned to the 0th element of the array called Volume.

The other looping functions (foreach and while) work in a similar fashion.

Tcl and ImageScript Data Types

Tcl supports four main data types: strings, numbers, arrays, and lists. The fundamental data type in Tcl is a string. A string is simply an arbitrary set of characters. The string nees to be enclosed in quotes if it contains any white space characters such as a space, tab, or return. For example, the following command will create a variable whose value is Registered Scans:

 
 
set ImageName "Registered Scans"

Numbers can be either traditional integers or floats. It is important to remember that calculations performed on numbers which do not include a decimal point are performed using integer arithmetic. Thus, in the following example i will have a value of 0 and x will have a value of 0.5:

 
 
set i [expr 1/2]
set x [expr 1.0/2.0]

Tcl also support arrays. An array is a variable which is indexed by some value. Typically, the value used is an integer. But since Tcl supports associative arrays the index can be any arbitrary string. In the following example, the indices to the array Volume are integers:

 
 
MxOpenImage $Folder nec1.hdr Volume(1)
MxOpenImage $Folder nec2.hdr Volume(2)

While in the next example the indices are strings:

 
 
MxOpenImage $Folder nec1.hdr Volume(Scan 1)
MxOpenImage $Folder nec2.hdr Volume(Scan 2)

Multidimensional arrays are also supported in Tcl. It is important to use a consistent format for the indices. For example, the extra spaces in the second use of the MxOpenImage will result in two different elements to the array Volume:

 

MxOpenImage $Folder nec1.hdr Volume(Subj1,Scan1)

vs.

 

MxOpenImage $Folder nec2.hdr Volume(Subj 1, Scan 1)

Another important Tcl data type is the list. A list is an ordered collection of elements where each element is any Tcl data type. The elements of a list can also be of mixed data types:

 
 
set list {"Element 1" 2 {third element}}

Keyed lists are a special type of list that provide a structured data type similar to structs in the C programming language. A keyed list is a list in which each element contains a key and value pair. For example the ImageScript function MxGetPageProperties will return the properties of a particular page in a keyed list:

 
 
MxGetPageProperties $Page Properties
put $Properties

results in:

 
 
{{Name MRI.img} {Color Black} {Skip Off} {Type Volume} \ 
    {ShowGraphic On} {ShowImage On}}

Using the keyed list functions, keylget, keylset, and keylkeys it is easy to manipulate keyed lists. For example, to the value of the Name key in the above example one can use the keylget function:

 

keylget Properties Name

which will return MRI.img.

The ImageScript functions use keyed lists are used to pass parameters to functions which have a large number of optional parameters. For example, the function MxReslice has 15 different parameters many of which will not be changed from their defaults on any given call. By using a keyed list to pass only those parameters for which a non-default value will be specified, the user does not have the burden of specifying all 15 parameters on every call. In addition, keyed lists are used to pass back large amounts of data such as property lists. In the example above, MxGetPageProperties returns a keyed list with all of the properties of a page. Other keyed list manipulation functions include:

 

keylset Properties Name "My MRI"
keyldel Properties ShowGraphic 
keylkeys Properties

The result of keylkeys is a list of the keys in the keyed list:

 

Name Type ShowImage Color

The final set of properties after performing the above keyed list functions are:

 

{{Name "My MRI"} {Color Black} {Skip Off} {Type Volume} \ 
    {ShowImage On}}

Magic Cookies

ImageScript adds an additional data type called "magic cookies" to the native set of Tcl data types. A magic cookie is a reference to a MEDx object such as an image, folder, page, or graphic. The basis of magic cookies comes from the X server model of object naming. In the X model of object naming, whenever a program creates an object the X server passes back a magic cookie which the program uses to refer to that object. In the server there is a table mapping magic cookies to objects.

A similar model has been implemented in ImageScript. MEDx maintains a mapping of magic cookies to their corresponding windows, folders, pages, images, volumes, and graphics. Every time a script creates a new object MEDx passes back to the script the magic cookie which refers to that object. For example, when a new folder is created two new objects are created: a folder and a window on that folder.

 
 
MxNewFolder Folder Window

In the above example, the variables Folder and Window are assigned the magic cookies for the folder and the window that are created respectively. The variable Folder can be used later in the script to tell MEDx into which folder an image should be opened. For example,

 
 
MxOpenImage $Folder nec1.hdr Volume

The image nec1.hdr is opened into the flder created above. At the same time a new magic cookie for this image is returned in Volume. Magic cookies provide scripts with references or handles to all of the objects in MEDx.

How to Develop a Script

Developing a script is simply a matter of determining which functions in ImageScript will perform the same operations that you normally perform interactively. Your script will consist of a set of ImageScript functions with perhaps additional Tcl commands to repeatedly perform a set of functions.

There are a number of ways to find the functions for your script. First, the documentation for ImageScript can be found from the help page in the ImageScript dialog box. In this documenation, there is an alphabetical list of all of the ImageScript functions. In addition, the functions are broken down by category. Second, our web-site also contains a search engine. The names of ImageScript functions all begin with Mx and are related to the operation they perform. Probably most convient way to find a function is to use the ImageScript Logging facility described below.

When testing your script, it is best to use an interactive shell (this is one of the options in the ImageScript dialog box). The shell will allow you to enter your script a line at a time. Since each function will be performed immediately after you have entered it, you will be able to get instant feedback as to whether the function worked as you had expected.

Logging - Automatic Script Generation

The MEDx User Interface provides an automatic Tcl script file generation facility using the Tcl Logger. The Tcl Logger records all the actions performed by the user via the user interface in the Tcl scripting language. The script generated by the Logger is written in Tcl using calls to functions in the MEDx extensions to Tcl. The script file generated by the Logger reproduces the actions performed the user when interacting through the user interface.

This feature provides script writers with a very easy method for writing scripts. As an example, suppose a script is need to perform a single subject PET analysis for 10 subjects. Suppose further the steps of the analysis is to load the data into a new folder, group it and apply a paradigm file followed by intensity normalization, a t-test, application of a LUT, and running the local min/max finder saving the folder in the subjects directory when done. The script writer can perform these steps interactively and the following script would be automatically generated by the Logger:

Sample Log

 

MxNewFolder F0 W0
MxOpenImage $F0 { "/home/motor/S1/S1-1.img" } I0
MxOpenImage $F0 { "/home/motor/S1/S1-2.img" } I1
MxOpenImage $F0 { "/home/motor/S1/S1-3.img" } I2
MxOpenImage $F0 { "/home/motor/S1/S1-4.img" } I3
MxOpenImage $F0 { "/home/motor/S1/S1-5.img" } I4
MxOpenImage $F0 { "/home/motor/S1/S1-6.img" } I5
MxOpenImage $F0 { "/home/motor/S1/S1-7.img" } I6
MxOpenImage $F0 { "/home/motor/S1/S1-8.img" } I7
MxGroupImages { $I0 $I1 $I2 $I3 $I4 $I5 $I6 $I7 } \
    I "New Group" P6
MxApplyParadigmGrouping $P6 "/home/motor/fingertap.pdm" \
    resultgroups
MxGetPageByName $F0 "All" P7
MxIntensityNormalization $P7 R 1000 10
MxGetPageByName $F0 "FingerTapping" P8
MxGetPageByName $F0 "Rest" P9
MxBetweenGrpStatistics $P8 $P9 U I2 I3 I4 I5 I6
MxSetCurrentPage $I5
MxSetLUT $I5 {{LUT "MirageHotBody"} {Min 0.50} {Max 1.00} \
    {MinCtrl Low} {MaxCtrl High} {Percent true}}
MxLocalMinMax $I5 Max 20 1 "/home/motor/S1/localminmax.txt"
MxSaveFolderAs $F0 "/home/motor/S1/Analysis.fldr"
MxCloseFolder $F0

By simply adding a for loop to the automatically generated script and replacing the references to subject S1 with the loop variable Subj, the following script can be used to analyze the all 10 subjects at once.

Final Script

 

foreach Subj { S1 S2 S3 S4 S5 S6 S7 S8 S9 } {
    MxNewFolder F0 W0
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-1.img" } I0
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-2.img" } I1
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-3.img" } I2
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-4.img" } I3
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-5.img" } I4
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-6.img" } I5
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-7.img" } I6
    MxOpenImage $F0 { "/home/motor/$Subj/$Subj-8.img" } I7
    MxGroupImages { $I0 $I1 $I2 $I3 $I4 $I5 $I6 $I7 } \
        I "New Group" P6
    MxApplyParadigmGrouping $P6 "/home/motor/fingertap.pdm" \
        resultgroups
    MxGetPageByName $F0 "All" P7
    MxIntensityNormalization $P7 R 1000 10
    MxGetPageByName $F0 "FingerTapping" P8
    MxGetPageByName $F0 "Rest" P9
    MxBetweenGrpStatistics $P8 $P9 U I2 I3 I4 I5 I6
    MxSetCurrentPage $I5
    MxSetLUT $I5 {{LUT "MirageHotBody"} {Min 0.50} {Max 1.00}\
        {MinCtrl Low} {MaxCtrl High} {Percent true}}
    
    MxLocalMinMax  $I5 Max 20 1 \
        "/home/motor/$Subj/localminmax.txt"
    MxSaveFolderAs $F0 "/home/motor/$Subj/Analysis.fldr"
    MxCloseFolder $F0
}

Error Handling

There are two levels of error handling in ImageScript. Syntax errors are treated as unrecoverable and as such the Tcl error handling mechanism is invoked. In these instances, Tcl will halt execution of the script and dump the Tcl stack when executing a script. If the script is executed without an interactive shell the dump is displayed in the window in which MEDx was started. All other errors are considered recoverable and the Mx functions return a success or failure status ("0" and "1" respectively). The cause of the most recent recoverable MEDx error can be determined by calling MxReportError which will return a description of the error and the function which caused the error. The example in the description of MxReportError shows how this function can be used to test for and respond to errors.

Some Example Scripts

The example scripts listed below illustrate the wide range of tasks that can be easily scripted.

  • Functional Image Analysis Script. This script performs a single subject analysis of PET data using a t-test.

  • Movie and Statistics Script. This short script scales the display range of each slice of a volume based on the minimum and maximum values in a region of interest and then plays a movie of the result.

  • Time Activity Curve Script. This script computes the area under a time activity curve in a region of interest drawn on a volume derived from a subset of dynamic volumes.

  • Rotating Rendered Heart Script. This script produces a movie loop consisting of a beating heart rendered at different angles and time points.

  • Skew Correction Script. This script demonstrates the ease with which external programs can be executed from Image Script and how image data can be exported from and imported into MEDx.

  • Surface Registration Script. This script uses contours to perform surface registration of two PET images.

  • Useful Widgets. This is a collection of easy to use input widgets.

 

MEDx Extensions to Tcl/Tk


Alphabetical Listing

Data Management

Object Management

External Program Interfacing

General Image Processing


Transformation

Filtering

Image Measurement

Functional Image Analysis

Medical Image Processing

Tcl/Tk References

  1. Donald Barnes (Editor). Tcl and Tk: Reference Manual. Red Hat Inc., 1995.
  2. Eric F. Johnson. Graphical Applications with Tcl and Tk. M&T Books, 1996.
  3. John K. Ousterhout. TCL and the TK Toolkit. Addison-Wesley, 1993.
  4. Brent Welch. Practical Programming in TCL and TK. Prentice Hall, 1995.

Top