[Insight-users] Problem with Python Wrapping my own class with WrapITK

Thomas Albrecht Thomas.Albrecht at unibas.ch
Wed Nov 7 05:52:28 EST 2012


Hi everyone,

this used to work with ITK 3.20 but does not work with ITK 4.2.

The itkLandmarkBasedTransformInitializer is not properly wrapped by 
WrapITK because it uses std::vectors. Therefore, we created our own 
class itkPythonEnabledLandmarkBasedTransformInitializer. This is wrapped 
using the following CMakeLists.txt:

    PROJECT(itkExtensions)
    FIND_PACKAGE(WrapITK REQUIRED)
    cmake_minimum_required(VERSION 2.8)

    SET(PROJECT_SOURCE_DIR "../../src")
    include_directories( ${PROJECT_SOURCE_DIR})

    itk_wrap_module("itkLandmarkAlignment")

    set(WRAPPER_LIBRARY_DEPENDS ITKCommon ITKTransform)
    SET(WRAPPER_LIBRARY_LINK_LIBRARIES ${ITK_LIBRARIES})

    itk_auto_load_submodules() # includes all *.wrap custom files
    itk_end_wrap_module()

The included .wrap file looks like this:

    ITK_WRAP_INCLUDE("itkSimilarity3DTransform.h")
    ITK_WRAP_CLASS("itk::PythonEnabledLandmarkBasedTransformInitializer"
    POINTER_WITH_SUPERCLASS)
       ITK_WRAP_TEMPLATE("VTIF3IF3"
    "itk::VersorRigid3DTransform<double>, itk::Image< float, 3>,
    itk::Image<float, 3>")
       ITK_WRAP_TEMPLATE("STIF3IF3" "itk::Similarity3DTransform<double>,
    itk::Image< float, 3>, itk::Image<float, 3>")
    ITK_END_WRAP_CLASS()



This compiles and can be imported into python. However, when I import 
both itk and this module itkLandmarkAlignment, the regular itk modules 
don't load properly anymore. I get this error:

      File
    "/home/tom/workspace/imageToImageRegistration/scripts/landmark_alignement.py",
    line 166, in <module>
         main(*args[1:]),
       File
    "/home/tom/workspace/imageToImageRegistration/scripts/landmark_alignement.py",
    line 46, in main
         fixed_image = ImageReader(fixed_image_name).get_image()
       File
    "/home/tom/workspace/tools/scripts/common/ImageReaderITK.py", line
    51, in __init__
         self.__itk_img_type = eval("itk.Image." + self.__datatype +
    str(self.__dimension))
       File "<string>", line 1, in <module>
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkLazy.py",
    line 37, in __getattribute__
         itkBase.LoadModule(module, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 95, in LoadModule
         LoadModule(dep, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 95, in LoadModule
         LoadModule(dep, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 95, in LoadModule
         LoadModule(dep, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 95, in LoadModule
         LoadModule(dep, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 95, in LoadModule
         LoadModule(dep, namespace)
       File
    "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
    line 118, in LoadModule
         for k, v in module.__dict__.items():
    UnboundLocalError: local variable 'module' referenced before assignment

It seems that itkBase cannot load the reader module anymore. This can be 
reproduced by hand in ipython. If and only if I have my own wrapped 
module imported before trying to use an ImageFileReader, I get the error 
messages:

    UnboundLocalError                         Traceback (most recent
    call last)
    /local/tom/data/tibia/Anatomie/right/ct/<ipython-input-3-e158c938807a>
    in <module>()
    ----> 1 reader = itk.ImageFileReader.ISS3.New()

    /export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkLazy.pyc
    in __getattribute__(self, attr)
          35       module = self.__lazy_attributes[attr]
          36       namespace = {}
    ---> 37       itkBase.LoadModule(module, namespace)
          38       # Load into 'namespace' first, then self.__dict__
    (via setattr) to

          39       # prevent the warnings about overwriting the
    'NotLoaded' values already


    /export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
    in LoadModule(name, namespace)
          93     deps.sort()
          94     for dep in deps:
    ---> 95       LoadModule(dep, namespace)
          96
          97   if itkConfig.ImportCallback:
    itkConfig.ImportCallback(name, 0)

    /export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
    in LoadModule(name, namespace)
          93     deps.sort()
          94     for dep in deps:
    ---> 95       LoadModule(dep, namespace)
          96
          97   if itkConfig.ImportCallback:
    itkConfig.ImportCallback(name, 0)

    /export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
    in LoadModule(name, namespace)
         116   this_module.swig = imp.new_module('swig')
         117   if namespace is not None: swig =
    namespace.setdefault('swig', imp.new_module('swig'))
    --> 118   for k, v in module.__dict__.items():
         119     if not k.startswith('__'): setattr(this_module.swig, k, v)
         120     if namespace is not None and not k.startswith('__'):
    setattr(swig, k, v)

    UnboundLocalError: local variable 'module' referenced before assignment


Any help would be greatly appreciated.

Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20121107/18150185/attachment.htm>


More information about the Insight-users mailing list