PythonArithmetic

MLModule
author MeVis Medical Solutions AG
package MeVisLab/Standard
dll MLPythonImageProcessing
definition PythonArithmetic.def
see also Arithmetic
keywords numpy

Purpose

The module PythonArithmetic allows to implement a paged ML image calculation using Python and NumPy.

Details

Calculation Panel

This defines the implementation of the paged ML image processing (aka calculateOutputSubImage).

The following variables are predefined:

  • out - the output sub image that should be filled with data (representing the ML output page)
  • in0, in1, etc. - the input sub images (or None if it was requested from a DummyImage)
  • numpy - NumPy is already imported and available via as “numpy” module
  • constants - the variables defined in the Constants panel are available by their given name

The out and in0, in1, etc. variables are PySubImage classes (see MeVisLab Scripting Reference for the full API) which is derived from numpy.ndarray and thus supports the whole ndarray API.

For details on numpy and ndarray, see numpy and ndarray.

In addition the the above variables, all variables that are set in calculateOutputImageProperties are directly available. The out object represents the ML output sub image (aka ML page) which should be filled with the result data.

Example for modifying the existing out:

out.fill(0)

As an alternative, you can as well assign a new ndarray to out. It is required that the assigned array is of the same extent as the ML page. The datatype may be different, the data will be converted and copied to the output ML page.

Example for assigning a new array:

out = (in0 + in1)/2.

NumPy is directly available via numpy. Example for using numpy:

out = numpy.minimum(in0, in1)

Note that for debugging your code, you can simple use print on any object or dir() / help() to get the API documentation for any object.

Function Declaration

You cannot call other global functions in global functions, but you can use closures.

Not working example:

def bar(value):
  return value + 1
def foo(value):
  return bar(value)

print foo(1)

--> error, bar is undefined

Instead, encapsulate all functions in a closure:

def myClosure(value)
  def bar(value):
    return value + 1
  def foo(value):
    return bar(value)
  return foo(value)

function = myClosure(1)
print function

Outputs Panel

This allows to set the output image properties via fields or by implementing the output image property setup in Python. The default is to use copy the properties of the input image 0.

The following variables are predefined:

  • outImage - the output image (with writeable properties)
  • inImage0, inImage1, … - the input images (or None if it was requested from a DummyImage)
  • numpy - NumPy is already imported and available via as “numpy” module
  • constants - the variables defined in the Constants panel are available by their given name

Example of a custom output properties calculation:

# set datatype explicitly
outImage.setDataType(outImage.MLuint16Type)

# set page extent to the whole slice
extent = outImage.imageExtent()
outImage.setPageExtent(extent[0], extent[1], 1,1,1,1)

# set the min/max value depending on min/ax of two input images
outImage.setMinVoxelValue(min(inImage0.minVoxelValue(), inImage1.minVoxelValue()))
outImage.setMaxVoxelValue(inImage0.maxVoxelValue() +  inImage1.maxVoxelValue())

# assign an additional variable for later use in calculation
inputExtent0 = inputImage0.imageExtent()

Note that all declared variables and imported modules are automatically available in calculateOutputSubImage and calculateInputSubImageBox.

Inputs Panel

Allows to configure the number of inputs that are available and which data type their sub images should have. If an input is allowed to be invalid, the input sub images in the calculation will be None. It is also possible to change the input sub image boxes that are requested using Python, but this is considered an advanced feature.

The following variables are predefined:

  • inIndex - the output image (with writeable properties)
  • outBox - the box of the output page for which the input box is needed
  • inBox - the input box that should be requested for image at inIndex (default: None)

Constants Panel

Allows to set named constants that are available in Python and can be modified via scripting or field connections. Currently 6 ints and 6 doubles are supported.

If your constants do not need to be modified via fields, you should better declare them in the calculation of the output image properties.

Input Fields

input0

name: input0, type: Image

input1

name: input1, type: Image

input2

name: input2, type: Image

input3

name: input3, type: Image

input4

name: input4, type: Image

Output Fields

output0

name: output0, type: Image

Parameter Fields

Field Index

Calculate Input Sub Image Box: String Min Voxel Value: Double
Calculate Output Image Properties: String setDataType: Bool
Calculate Output Sub Image: String setMinMaxValues: Bool
Data Type: Enum Update: Trigger
Handling: Enum  
Input Type 0: Enum  
Inputs: Integer  
Max Voxel Value: Double  

Visible Fields

Calculate Output Sub Image

name: calculateOutputSubImage, type: String

The python code for the calculation of the output sub image.

Calculate Input Sub Image Box

name: calculateInputSubImageBox, type: String

The python code for the calculation of the input sub image box. If not implemented, the input sub image box equals the output sub image box.

Calculate Output Image Properties

name: calculateOutputImageProperties, type: String

The python code for the calculation of the output image properties.

Update

name: update, type: Trigger

Updates the output image according to all changes that have been done on the module.

Inputs

name: numberOfInputs, type: Integer, default: 2, minimum: 0, maximum: 5

Select the number of input images that the module should use.

Min Voxel Value

name: minVoxelValue, type: Double, default: 0

The minimum voxel value of the output image.

Max Voxel Value

name: maxVoxelValue, type: Double, default: 0

The maximum voxel value of the output image.

Data Type

name: dataType, type: Enum, default: unsigned int8

The data type of the output image. If this is disabled, the type of the first input image is used.

Values:

Title Name
unsigned int8 unsigned int8
unsigned int16 unsigned int16
unsigned int32 unsigned int32
unsigned int64 unsigned int64
int8 int8
int16 int16
in32 in32
int64 int64
float float
double double
long double long double
complexf complexf
complexd complexd
complexld complexld

Input Type 0

name: inputDataType0, type: Enum, default: output type

Select the type of the input image 0. Default is the output type.

Values:

Title Name Description
output type output type The type of the output image.
input type input type The type of the input image.
unsigned int8 unsigned int8  
unsigned int16 unsigned int16  
unsigned int32 unsigned int32  
unsigned int64 unsigned int64  
int8 int8  
int16 int16  
in32 in32  
int64 int64  
float float  
double double  
long double long double  
complexf complexf  
complexd complexd  
complexld complexld  

Handling

name: inputHandling0, type: Enum, default: AllowAny

Select what the module should do when the input image is invalid.

Values:

Title Name Description
Invalidate Invalidate If the input image 0 is invalid or disconnected, just invalidate the output image.
Allow Any AllowAny Allow both an invalid or disconnected input image 0, make sure to check the in0 for None in your Python code, otherwise you will access the None object in case of an invalid input image.
Allow Invalid Image AllowInvalidImage Allow an invalid input image 0, make sure to check the in0 for None in your Python code, otherwise you will access the None object in case of an invalid input image.
Allow Disconnected Input AllowDisconnectedInput Allow a disconnected input image 0, make sure to check the in0 for None in your Python code, otherwise you will access the None object in case of an invalid input image.

Hidden Fields

setMinMaxValues

name: setMinMaxValues, type: Bool, default: FALSE

setDataType

name: setDataType, type: Bool, default: FALSE