minccalc perform complex math operations on minc files

mincalc <options> <in1>.mnc <in2>.mnc <out>.mnc


Minccalc will perform complex, voxel-by-voxel math operations, on one or more minc files of the same shape and having the same coordinate sampling, producing a single output file. The operations to be performed are input using the -expression argument (see EXPRESSIONS). By default, the output file is the last non-option argument. However, if the -outfile option is used, then all non-option arguments are considered input files and the output file names come from the -outfile options, of which there can be more than one.


Note that options can be specified in abbreviated form (as long as they are unique) and can be given anywhere on the command line.

Create MINC 2.0 format output files.

Print summary of command-line options and exit.

Print the program’s version number and exit.

Overwrite an existing file.

Don’t overwrite an existing file (default).

Synonym for -noclobber.

Print out progress information for each chunk of data copied (default).

Do not print out progress information.

Print out debugging information.

Copy all of the header information from the first input file (default for one input file).

Do not copy all of the header from the first input file; copy only coordinate information (default for more than one input file).

Create an output file with the same type as the first input file (default).

Store output voxels in 8-bit integer format.

Store output voxels in 16-bit integer format.

Store output voxels in 32-bit integer format.

Superseded by -int.

Store output voxels in 32-bit floating point format.

Store output voxels in 64-bit floating point format.

Use signed, two’s complement integer format. Applies only if the output voxel type is specified to be an integer type (one of -byte, -short, -int or -long).

Use unsigned integer format. Applies only if the output voxel type is specified to be an integer type (one of -byte, -short, -int or -long).

-range min max
Restrict the valid range of integer data. Applies only if one of the -byte, -short, -int or -long options is specified.

-max_buffer_size_in_kb size
Specify the maximum size of the internal buffers (in kbytes). Default is 4096 (4MB).

-dimension dimname
Specify a dimension along which we wish to perform a cumulative operation.

Check that all input files have matching sampling in world dimensions (default).

Ignore any differences in world dimensions sampling for input files.

For cumulative vector operations (sum, prod and avg), invalid data (Not-A-Number or NaN) in any element of the vector will produce invalid data in the result (default).

For cumulative vector operations, invalid data (NaN) in the vector is ignored, ie. treated as though it is not present.

When an illegal operation is attempted at a voxel (such as divide by zero), the result is invalid data (NaN) (default). Having no valid input data for a cumulative operation is also considered an illegal operation when -ignore_nan is used.

When an illegal operation is attempted at a voxel (such as divide by zero), the result is value zero.

-illegal_value value
When an illegal operation is attempted at a voxel (such as divide by zero), the result is the value specified by this option.

-expression string
Specify the expression to evaluate at each voxel (see EXPRESSIONS).

-expfile filename
Specify a file containing an expression to evaluate at each voxel (see EXPRESSIONS). If filename - is given, then the expression is read from stdin. The only difference from command-line expressions is that comments can be given in the file. A comment line is specified by placing a # as the first non-whitespace character of the line. Minccalc scripts can be created by setting the first line to

#! /usr/local/mni/bin/minccalc -expfile

-outfile symbol output-file
Specify that output should be written to the specified file, taking values from the symbol which should be created in the expression (see the EXAMPLES section). If this option is given, then all non-option arguments are taken as input files. This option can be used multiple times for multiple output files.

-eval_width value
Specify the number of voxels to process in parallel. Default is 200.


The -expression argument is a single string that describes the function to evaluate. The function expression is typically written in terms of the vector A.

For example, the following expression will sum the first two input files together:A[0] + A[1]

Multiple expressions can be given separated by semicolons, in which case only the value of the last expression is used. These expression lists can be used with assignment expressions to make the syntax very C-like: ratio = A[0]/A[1]; A[2]*exp(-ratio)

An expression list in curly brackets is a valid expression and returns the value of last expression in the list. This is particularly useful in for and if expressions (see below).

There are two types of values in the language: vectors and scalars. Scalars literals are floating point numbers or may appear as symbols whose name starts with a lowercase letter.

Besides normal scalar operators such as +, -, * and /, the expression language also supports the infix exponentiation operator ^ , the usual relational operators <, <=, >, >=, ==, != as well as the boolean operators && (and), || (or) and ! (not). Note that the && and || boolean operators always evaluate both operands, unlike C. Scalar mathematical functions include abs, sqrt, exp, log, sin, cos, tan, asin, acos and atan. There are also some specialized functions:

The scalar constant NaN is defined such that isnan(NaN) return 1.

Vectors can be written in the following “extensional” form

or by using the following range-generating notations:

or be generated, by “intension”. The following intension expression generates the vector {3,2,1}:

{ i in [1:3] | 4 - i }

Vectors may also appear as symbols whose name starts with an uppercase letter.

In addition to the scalar operators, the following vector operators are supplied:

avg - the average value of the scalars in vector len - the length of sum - the sum of the elements of prod - the product of the elements of max - the maximum value of min - the minimum value of imax - the index of the maximum value of imin - the index of the minimum value of V[s] - the s’th element of vector V with origin 0.

Symbol names are introduced into a global symbol table by assignment expressions of the form

a = A[2] * log(2)

Symbols starting with a lowercase letter represent scalars while those starting with an uppercase letter represent vectors. Since = is an operator, its result can be used in an expression (as in C).

A few control constructs are provided:

For loops can be created to loop over a vector, assigning each value to a symbol and then evaluating an expression. This is done with expressions of the form

total=0; for{i in [0:len(A))} total=total+A[i]; total

which is equivalent to sum(A). Note that this is similar to using

since the for construct is actually an operator (although it is usually only used for changing symbol values). Note also that without the final “total”, the expression would not be very useful since it would only return the length of the vector.

As in C, a list of expressions can be specified in curlies:

There are also a few forms of the if-then-else construct:

The else is optional. Again, the if construct is an operator, and the then or else expressions can be expression lists in curlies, in which case the value of the last expression is returned. If the else expression is missing, then the value 0 is returned when the test expression is 0 (false).

The principal oddity with the for and if constructs is that unlike C statements, they must be separated from the next expression by a semicolon even when an expression list in curlies is used:

An alternative way to introduce symbol names is through let-expressions. For example, the following expression will always evaluate to 3:

These were originally designed to create variables only within the evaluated expression, but modifications have been made so that the global symbol table is changed.


Here is an expression for calculating standard deviation, taking into account the possibility of invalid input data, which is ignored:

The last if could be changed to return 0 if s0 is > 0 but <= 1. We also drop the curly brackets, but then there must not be a “;” between the if and the else

If we want both the mean and the standard deviation, we can use the -outfile option, invoking the command with

And using the expression file (with yet another form of if expression):


A few things you should remember…

Vector variables must start with an uppercase letter.

Vector variable names must not be one of the function keywords, sum, len, prod, … etc

For loops and if expressions always need to be separated from the next expression by a semicolon.

The symbol table is global.

Boolean operators && and || always evaluate both operands.

A note on parallelism: For efficiency reasons, evaluations are done on many voxels at once (the number of voxels is referred to as the width of the evaluation and is changed with the -eval_width option). An odd consequence of this is that both sides of an if-else statement are always evaluated (unless all voxels give the same test result), but statements within each consequent are only evaluated on the appropriate voxels. In particular, entries in the symbol table are only modified according to a voxel mask. A side-effect of this is that any vector symbol set in an if-else consequent must not change the length of the symbol (although it can create it) and both sides of the consequent must agree on the length of any vector symbols that they both modify. If this is not clear, just try it - the program will complain if it is not happy.


Andrew Janke - a.janke@gmail.com


Copyright © 2000 by Andrew Janke