| status | stable |
| author | Florian Link |
| package | MeVisLab/Standard |
| dll | MLPythonImageProcessing |
| definition | PythonArithmetic.def |
| see also | Arithmetic |
| keywords | numpy, python, arithmetic |
The module PythonArithmetic allows to implement a paged ML image calculation using Python and NumPy.
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, ... - 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,... 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.
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.
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)
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.
| cancelTask: Trigger | taskFinished: Trigger |
| progress: Float | taskRunning: Bool |
| setDataType: Bool | taskVerboseLogging: Bool |
| setMinMaxValues: Bool | |
| startTask: Trigger | |
| startTaskSynchronous: Trigger | |
| status: String | |
| taskCanceled: Trigger |