<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Hi everyone,<br>
    <br>
    this used to work with ITK 3.20 but does not work with ITK 4.2. <br>
    <br>
    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:<br>
    <br>
    <blockquote><tt>PROJECT(itkExtensions)</tt><tt><br>
      </tt><tt>FIND_PACKAGE(WrapITK REQUIRED)</tt><tt><br>
      </tt><tt>cmake_minimum_required(VERSION 2.8) </tt><tt><br>
      </tt><tt><br>
      </tt><tt>SET(PROJECT_SOURCE_DIR "../../src")</tt><tt><br>
      </tt><tt>include_directories( ${PROJECT_SOURCE_DIR})</tt><tt><br>
      </tt><tt><br>
      </tt><tt>itk_wrap_module("itkLandmarkAlignment")</tt><tt><br>
      </tt><tt><br>
      </tt><tt>set(WRAPPER_LIBRARY_DEPENDS ITKCommon ITKTransform) </tt><tt><br>
      </tt><tt>SET(WRAPPER_LIBRARY_LINK_LIBRARIES ${ITK_LIBRARIES})</tt><tt><br>
      </tt><tt><br>
      </tt><tt>itk_auto_load_submodules() # includes all *.wrap custom
        files</tt><tt><br>
      </tt><tt>itk_end_wrap_module()</tt><br>
    </blockquote>
    The included .wrap file looks like this:<br>
    <tt><br>
    </tt>
    <blockquote><tt>ITK_WRAP_INCLUDE("itkSimilarity3DTransform.h")</tt><br>
      <tt>ITK_WRAP_CLASS("itk::PythonEnabledLandmarkBasedTransformInitializer"
        POINTER_WITH_SUPERCLASS)</tt><br>
      <tt>&nbsp; ITK_WRAP_TEMPLATE("VTIF3IF3"
        "itk::VersorRigid3DTransform&lt;double&gt;, itk::Image&lt;
        float, 3&gt;, itk::Image&lt;float, 3&gt;")</tt><br>
      <tt>&nbsp; ITK_WRAP_TEMPLATE("STIF3IF3"
        "itk::Similarity3DTransform&lt;double&gt;, itk::Image&lt; float,
        3&gt;, itk::Image&lt;float, 3&gt;")&nbsp; </tt><br>
      <tt>ITK_END_WRAP_CLASS()</tt><br>
    </blockquote>
    <br>
    <br>
    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:<br>
    <br>
    <blockquote><tt>&nbsp;File
        "/home/tom/workspace/imageToImageRegistration/scripts/landmark_alignement.py",
        line 166, in &lt;module&gt;</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; main(*args[1:]), </tt><br>
      <tt>&nbsp; File
        "/home/tom/workspace/imageToImageRegistration/scripts/landmark_alignement.py",
        line 46, in main</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; fixed_image = ImageReader(fixed_image_name).get_image()&nbsp; </tt><br>
      <tt>&nbsp; File
        "/home/tom/workspace/tools/scripts/common/ImageReaderITK.py",
        line 51, in __init__</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; self.__itk_img_type = eval("itk.Image." + self.__datatype
        + str(self.__dimension))</tt><br>
      <tt>&nbsp; File "&lt;string&gt;", line 1, in &lt;module&gt;</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkLazy.py",
        line 37, in __getattribute__</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; itkBase.LoadModule(module, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 95, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 95, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 95, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 95, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 95, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp; File
        "/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.py",
        line 118, in LoadModule</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; for k, v in module.__dict__.items():</tt><br>
      <tt>UnboundLocalError: local variable 'module' referenced before
        assignment</tt><br>
    </blockquote>
    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:<br>
    <br>
    <blockquote><tt>UnboundLocalError&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Traceback
        (most recent call last)</tt><br>
      <tt>/local/tom/data/tibia/Anatomie/right/ct/&lt;ipython-input-3-e158c938807a&gt;
        in &lt;module&gt;()</tt><br>
      <tt>----&gt; 1 reader = itk.ImageFileReader.ISS3.New()</tt><br>
      <br>
      <tt>/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkLazy.pyc
        in __getattribute__(self, attr)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; module = self.__lazy_attributes[attr]</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; namespace = {}</tt><br>
      <tt>---&gt; 37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itkBase.LoadModule(module, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Load into 'namespace' first, then
        self.__dict__ (via setattr) to</tt><br>
      <br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # prevent the warnings about overwriting the
        'NotLoaded' values already</tt><br>
      <br>
      <br>
      <tt>/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
        in LoadModule(name, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 93&nbsp;&nbsp;&nbsp;&nbsp; deps.sort()</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 94&nbsp;&nbsp;&nbsp;&nbsp; for dep in deps:</tt><br>
      <tt>---&gt; 95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 96 </tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 97&nbsp;&nbsp; if itkConfig.ImportCallback:
        itkConfig.ImportCallback(name, 0)</tt><br>
      <br>
      <tt>/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
        in LoadModule(name, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 93&nbsp;&nbsp;&nbsp;&nbsp; deps.sort()</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 94&nbsp;&nbsp;&nbsp;&nbsp; for dep in deps:</tt><br>
      <tt>---&gt; 95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LoadModule(dep, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 96 </tt><br>
      <tt>&nbsp;&nbsp;&nbsp;&nbsp; 97&nbsp;&nbsp; if itkConfig.ImportCallback:
        itkConfig.ImportCallback(name, 0)</tt><br>
      <br>
      <tt>/export/contrib/InsightToolkit-4.20/linux64/u12_04_release/lib/ITK-4.2/Python/itkBase.pyc
        in LoadModule(name, namespace)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; 116&nbsp;&nbsp; this_module.swig = imp.new_module('swig')</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; 117&nbsp;&nbsp; if namespace is not None: swig =
        namespace.setdefault('swig', imp.new_module('swig'))</tt><br>
      <tt>--&gt; 118&nbsp;&nbsp; for k, v in module.__dict__.items():</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; 119&nbsp;&nbsp;&nbsp;&nbsp; if not k.startswith('__'):
        setattr(this_module.swig, k, v)</tt><br>
      <tt>&nbsp;&nbsp;&nbsp; 120&nbsp;&nbsp;&nbsp;&nbsp; if namespace is not None and not
        k.startswith('__'): setattr(swig, k, v)</tt><br>
      <br>
      <tt>UnboundLocalError: local variable 'module' referenced before
        assignment</tt><br>
    </blockquote>
    <br>
    Any help would be greatly appreciated.<br>
    <br>
    Tom<br>
  </body>
</html>