STIR  6.2.0
Functions
stir_math.cxx File Reference

add or multiply data, with some other basic math manipulations More...

#include "stir/ArrayFunction.h"
#include "stir/DiscretisedDensity.h"
#include "stir/SegmentByView.h"
#include "stir/IO/OutputFileFormat.h"
#include "stir/IO/read_from_file.h"
#include "stir/Succeeded.h"
#include "stir/ProjDataInterfile.h"
#include "stir/ExamInfo.h"
#include "stir/utilities.h"
#include "stir/NumericInfo.h"
#include "stir/KeyParser.h"
#include "stir/is_null_ptr.h"
#include "stir/modelling/ParametricDiscretisedDensity.h"
#include "stir/DynamicDiscretisedDensity.h"
#include "stir/stir_math.h"
#include "stir/warning.h"
#include "stir/error.h"
#include <fstream>
#include <iostream>
#include <functional>
#include <algorithm>
#include <memory>

Functions

template<class DataT , class FunctionObjectT >
void process_data (const string &output_file_name, const int num_files, char **argv, const bool no_math_on_data, const bool except_first, const bool verbose, const bool do_add, const FunctionObjectT &pow_times_add_object, const OutputFileFormat< DataT > &output_format)
 
template<class FunctionObjectT >
void process_data (const string &output_file_name, const int num_files, char **argv, const bool no_math_on_data, const bool except_first, const bool verbose, const bool do_add, const FunctionObjectT &pow_times_add_object, const OutputFileFormat< DynamicDiscretisedDensity > &output_format)
 
template<class DataT >
shared_ptr< OutputFileFormat< DataT > > find_output_format (const std::string &filename)
 
int main (int argc, char **argv)
 

Detailed Description

add or multiply data, with some other basic math manipulations

This is a command line utility for adding, multiplying, thresholding ... data, with a somewhat awkward syntax. The command line arguments are as follows (but everything has to fit on 1 line):

[--output-format parameter-filename ]
[--parametric || --dynamic]
[-s [--max_segment_num_to_process number] ]
[--add | --mult]
[--power power_float]
[--times-scalar mult_scalar_float]
[--divide-scalar divide_scalar_float]
[--add-scalar add_scalar_float]
[--min-threshold min_threshold]
[--max-threshold max_threshold]
[--including-first]
[--verbose]
output_filename_with_extension in_data1 [in_data2 [in_data3...]]

or

[--output-format parameter-filename ]
--accumulate
[-s]
[--parametric || --dynamic]
[--add | --mult]
[--power power_float]
[--times-scalar mult_scalar_float]
[--divide-scalar divide_scalar_float]
[--add-scalar add_scalar_float]
[--min-threshold min_threshold]
[--max-threshold max_threshold]
[--including-first]
[--verbose]
out_and_input_filename in_data2 [in_data3 [in_data4...]]

'–add' is default, and outputs the sum of the result of processed data. '–mult' outputs the multiplication of the result of processed data.
The '–include-first' option can be used such that power and scalar multiplication are done on the first input argument as well. Otherwise these manipulations are done only on the 2nd, 3rd,.. argument.
The '–accumulate' option can be used to say that the first filename given will be used for input AND output. Note that when using this option together with '–including-first', the data in the first filename will first be manipulated according to '–power' and '–times-scalar'.
The '-s' option is necessary if the arguments are projection data. Otherwise, it is assumed the data are images. '–parametric' and '–dynamic' can be used to read parametric/dynamic images.
For projection data, you can restrict the number of segments read/written using the –max_segment_num_to_process option (unless –accumulate is used). For example, using 2 as an argument of this option, will read/write segments -2,-1,0,1,2.
Multiple occurences of '–times-scalar' and '–divide-scalar' are allowed and will just result in accumulation of the factors.

The order of the manipulations is as follows:
(1) thresholding (2) power (3) scalar multiplication (4) scalar addition.

The '–output-format' option can be used to write the output in a different file format then the default (although this currently only works for images). The parameter file should have the following format:

output file format parameters:=
output file format type:= <sometype>
END:=

See the stir::OutputFileFormat hierarchy for possible values.

Examples
  • subtracting 2 files
    stir_math --times-scalar -1 output in1 in2
  • sum the square of each file
    stir_math --power 2 --including-first output in1 in2
  • Dividing 2 files after thresholding the 2nd file
    stir_math --mult --power -1 --min-threshold .1 output in1 in2
  • Dividing 2 files, with first file set to the quotient

    stir_math --accumulate --mult --power -1 in1 in2

Warning
There is no check that the data sizes and other info are compatible and the output will have the largest data size in the input, and the characteristics (like voxel-size or so) are taken from the first input data. Hence, lots of funny effects can happen if data are not compatible.
When '–accumulate' is not used, the output file HAS to be different from all the input files.
The result of using non-integral powers on negative numbers is probably system-dependent.
For future compatibility, it is recommended to put the command line arguments in the order that they will be executed (i.e. as listed above). It might be that we take the order into account in a future release.
Author
Kris Thielemans