Clarifying General Syntax When SimpleITK Documentation


(Brett) #1

Hi All,

With most SimpleITK modules the expected syntax is intuitive. But as a beginner I get occasionally stumped. For example, below I’m trying to load the SetChangeMap parameter for ChangeLabelLabelMapFilter but am not sure how to interpret the correct syntax from the documentation.

How am I supposed to pass the SetChangeMap parameter that defines old and new labels?

More generally, does anyone have suggestions for beginners on how to understand expected syntax as it’s described here.

Thank you!

filter = sitk.ChangeLabelLabelMapFilter()
filter.SetChangeMap((1,0))

TypeError Traceback (most recent call last)
<ipython-input-26-f55c87c7566e> in <module>()
----> 1 filter.SetChangeMap((1,1))
~/anaconda3/lib/python3.6/site-packages/SimpleITK/SimpleITK.py in SetChangeMap(self, ChangeMap)
_SimpleITK.ChangeLabelLabelMapFilter_SetChangeMap(self, ChangeMap)
TypeError: in method ‘ChangeLabelLabelMapFilter_SetChangeMap’, argument 2 of type ‘std::map< double,double,std::less< double >,std::allocator< std::pair< double const,double > > >’


(Bradley Lowekamp) #2

Hello,

The Doxgen link you have is old. Here is the link to the 1.1 release: https://itk.org/SimpleITKDoxygen110/html/
There is also the read the docs: https://simpleitk.readthedocs.io/en/master/

There is the “ChangeLabelImageFilter” and the “ChangeLabelLabelMapFilter”, the former has the “ImageFilter” while the later has the “LabelMapFilter” suffix. They work on scalar images and label map images respectfully. The label map images have pixel id’s such as sitk.sitkLabelUInt8 or sitk.sitkLabelUInt32 and are internally represented as run length encoded images for efficient storage and label operations. Make sure you have the proper image as input to the proper filer.

For this particular issue, the C++ SimpleITK filter is expecting std::map<double,double>. The wrapper creates as “DoubleDoubleMap” type:


In [5]: sitk.ChangeLabelLabelMap?
Signature: sitk.ChangeLabelLabelMap(*args, **kwargs)
Docstring:
ChangeLabelLabelMap(Image image1, DoubleDoubleMap changeMap) -> Image

The DoubleDoubleMap type is a wrapper for the std::map class:

sitk.DoubleDoubleMap?
Init signature: sitk.DoubleDoubleMap(self, *args)
Docstring:      Proxy of C++ std::map<(double,double)> class.
Init docstring:
__init__(std::map<(double,double)> self, std::less< double > const & arg2) -> DoubleDoubleMap
__init__(std::map<(double,double)> self) -> DoubleDoubleMap
__init__(std::map<(double,double)> self, DoubleDoubleMap arg2) -> DoubleDoubleMap
File:           /scratch/blowekamp/build/SimpleITK-ibex/SimpleITK-build/Wrapping/Python/SimpleITK.py
Type:           type

But the wrappers also allows conversion from a Python dictionary to this wrapped type. So you can just pass a dictionary to the function or class. So you can simple do the following:

In [1]: import SimpleITK as sitk

In [2]: img = sitk.Image([10,10], sitk.sitkLabelUInt16)

In [3]: img[1,2] = 1

In [4]: result = sitk.ChangeLabelLabelMap(img, {1:2,0:1})

In [5]: result[1,2]
Out[5]: 2

In [6]: result[0,0]
Out[6]: 1

Perhaps an example of for this class would help?