PythonArithmetic

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

Purpose

The module PythonArithmetic allows for implementing a page-based ML image calculation using Python and NumPy.

Details

Calculation Panel

This defines the implementation of the page-based ML image processing for the Python API, equivalent to the calculateOutputSubImage method in C++.

The following variables are predefined:

  • out - the output subimage that should be filled with data (representing the ML output page)
  • in0, in1, etc. - the input subimages (or None if it was requested from a DummyImage)
  • numpy - NumPy is already imported and available 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 are derived from numpy.ndarray and thus support the entire ndarray API.

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

In addition to the above variables, all variables that are set in calculateOutputImageProperties are directly available. The out object represents the ML output subimage (also known as an 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 data type 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 simply 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 for setting 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 writable properties)
  • inImage0, inImage1, … - the input images (or None if it was requested from a DummyImage)
  • numpy - NumPy is already imported and available 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 data type explicitly
outImage.setDataType(outImage.MLuint16Type)

# set page extent to the entire 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 configuration of the number of available inputs and the data type of their subimages. If an input is allowed to be invalid, the corresponding input subimages in the calculation will be None. It is also possible to change the requested input subimage boxes using Python, but this is considered an advanced feature.

The following variables are predefined:

  • inIndex - the output image (with writable 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 for setting named constants that are available in Python and can be modified via scripting or field connections. Currently, six integers and six 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

Sets the Python code for calculating the output subimage.

Calculate Input Sub Image Box

name: calculateInputSubImageBox, type: String

Sets the Python code for calculating the input subimage box. If not implemented, the input subimage box equals the output subimage box.

Calculate Output Image Properties

name: calculateOutputImageProperties, type: String

Sets the Python code for calculating the output image properties.

Update

name: update, type: Trigger

When pressed, the output image is updated.

Inputs

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

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

Min Voxel Value

name: minVoxelValue, type: Double, default: 0

Sets the minimum voxel value of the output image.

Max Voxel Value

name: maxVoxelValue, type: Double, default: 0

Sets the maximum voxel value of the output image.

Data Type

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

Defines 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

Defines the data type of the input image 0. Default is the output type.

Values:

Title Name Description
output type output type Uses the data type of the output image.
input type input type Uses the data 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

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

If you allow an invalid or disconnected image, ensure that you check this input image for None in your Python code; otherwise, you will access the None object if 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.
Allow Invalid Image AllowInvalidImage Allow an invalid input image 0.
Allow Disconnected Input AllowDisconnectedInput Allow a disconnected input image 0.

Hidden Fields

setMinMaxValues

name: setMinMaxValues, type: Bool, default: FALSE

setDataType

name: setDataType, type: Bool, default: FALSE