Help getting started with python wrapping

Hello,

I’m having some trouble using the ITK python wrapping functionality. I’m trying to work through the basic python examples but I’m unable to import ITK even after copying the WrapITK.pth file to my site-packages directory. I can import ITK if I manually add “/Wrapping/Generators/Python/” to the path. If I do that I receive an error when I try to use basic modules (I’ve tested itk.imread and itk.Image so far and receive the same error for both):

C:\research\itk\Wrapping\Generators\Python\Release\itkBase.py in LoadModule (name, namespace)
59 if namespace is not None :
60 swig = namespace . setdefault ( ‘swig’ , { } ) —>
61
swig . update ( this_module . swig )
62
63 # don’t worry about overwriting the symbols in namespace – any **

AttributeError** : module ‘itk.ITKIOImageBasePython’ has no attribute ‘swig’

Incase it’s helpful, here’s some background:

I compiled ITK 5.1.1 from source following the instructions in the software guide. ITK_WRAP_PYTHON was enabled, but I did not have the other variables that the guide mentioned after configuring multiple times: PYTHON INCLUDE DIR, PYTHON LIBRARY, and PYTHON EXECUTABLE. Even so, everything compiled without error. I’m using Windows 10 and Visual Studio 2019.

When I went to copy the WrapITK.pth to my site-packages directory, I found that it wasn’t in the Wrapping\Generators\Python\ folder as specified in the install guide. I did find it in \Wrapping\Generators\Python\Release and copied it to the site-packages directory.

I’ve tried installing swig separately and enabling the use system swig option, but that didn’t help. I’ve also tried compiling with and without building the shared libraries.

I’m very stumped on this one, and I would appreciate any suggestions on what to try next.

Some bugfixes were done to Python wrapping recently. The last in the series is PR 2081, so make sure to use commit e9fdf14 or newer.

2 Likes

Success! I recompiled with the latest version of the source and it’s working. Thank you!

2 Likes

Unfortunately I ran into some more trouble. ITK and Python wrapping are working to the best of my knowledge, so I’ve gone on to attempting to compile RTK.

First, despite ITK appearing to be fully functional, I received these errors when compiling the latest code from Github. Adding them here just in case they are related to my RTK issues:
Severity Code Description Project File Line Suppression State
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUCommonPython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUFiniteDifferencePython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUAnisotropicSmoothingPython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUImageFilterBasePython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209

After ITK, I’ve tried to compile RTK 2.1.0 as well as the latest copy of from github, and got multiple errors, starting with this one:

Severity Code Description Project File Line Suppression State
Error MSB8066 Custom build for ‘C:\Users\dylan\RTK-2.1.0\src\rtkCudaAverageOutOfROIImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConjugateGradientImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConstantVolumeSeriesSource.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConstantVolumeSource.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaCropImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaCyclicDeformationImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaDisplacedDetectorImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFDKBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFDKWeightProjectionFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFFTProjectionsConvolutionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFirstOrderKernels.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaForwardProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaForwardWarpImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaInterpolateImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLagCorrectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLaplacianImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLastDimensionTVDenoisingImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaParkerShortScanImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaPolynomialGainCorrectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaRayCastBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaSplatImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaTotalVariationDenoisingBPDQImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaUtilities.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpForwardProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWeidingerForwardModelImageFilter.cu’ exited with code 1. RTK C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 238

There were also several errors, all with the same code cannot use ‘dynamic_cast’ to convert from ‘const itk::DataObject *’ to ‘itk::ImageBase<3> *’. Here’s an example:

Severity Code Description Project File Line Suppression State
Error C2682 cannot use ‘dynamic_cast’ to convert from ‘const itk::DataObject *’ to ‘itk::ImageBase<3> *’ rtkvarianobigeometry C:\Users\dylan\RTK-2.1.0\applications\rtkvarianobigeometry\rtkvarianobigeometry.cxx 36

And several more similar to this one:
Severity Code Description Project File Line Suppression State
Error LNK1181 cannot open input file ‘lib\Release\itkRTK-5.2.lib’ RTKHeaderTest1 C:\research\rtk\LINK 1

I also tried completely uninstalling ITK, and then recompiling ITK with the RTK module turned on. In that case, I got the LNK1181 cannot open input file ‘lib\Release\itkRTK-5.2.lib’ errors as well.

Once again any help or advice would be much appreciated.

@simon.rit or @LucasGandel might be able to help. Or ask this question on the RTK mailing list.

1 Like

Hi,
Do you get the same errors with ITK and RTK master? The last time I tried to compile the python wrappings, it compiled but failed to load, see issue #2064… I don’t think it has been solved but I’ll relaunch a build to check now (on Windows though).

For your initial error, note that the RTK pypi package is incompatible with the latest ITK release. I haven’t had time to prepare a release to fix this but you can download CI packages here compatible with the latest ITK release. Note that you need to be logged in github to access the artifacts.
Simon

2 Likes

On Linux, I can successfully compile but something seems wrong. I get

>>> import itk
>>> itk.Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'itk' has no attribute 'Image'

with or without RTK. I have no idea if it’s a known problem, maybe @hjmjohnson knows…

1 Like

Thank you both for your replies!

@simon.rit, Just to clarify: when I try to install the latest version of ITK master with the Module_RTK option enabled, I receive an MSB8066 error for project RTK in file Microsoft.Cpp.Common.targets, line 238, which is the beginning of this block:

<CustomBuild Condition ="'@(_CustomBuild)' != ''"
  Sources                     ="@(_CustomBuild)"
  BuildSuffix                 ="$(_BuildSuffix)"

  MinimalRebuildFromTracking  ="false"
  AcceptableNonZeroExitCodes  ="%(_CustomBuild.AcceptableNonZeroExitCodes)"
  StdOutEncoding = "%(_CustomBuild.StdOutEncoding)"
  StdErrEncoding = "%(_CustomBuild.StdErrEncoding)"
>

Is this the error that you’re referring to being due to the RTK pypi package incompatibility? Or is this error a separate problem? Either way, I receive this both when compiling ITK with the RTK module, or when compiling RTK after having compiled ITK without the RTK option.

The subsequent errors are linker related (LINK1181), and all are stating that itkRTK-2.lib can’t be opened. I get these in both cases as well.

**Severity	Code	Description	Project	File	Line	Suppression State**
Error	LNK1181	cannot open input file '..\..\..\..\lib\Release\itkRTK-5.2.lib'	 
ITKPyUtilsPython	C:\research\itk\Wrapping\Generators\Python\PyUtils\LINK	1	

I can’t find itkRTK-5.2.lib anywhere so I’m guessing it isn’t being built successfully.

Here’s the third type of error. I only receive this when compiling RTK after ITK:

Severity Code Description Project File Line Suppression State
Error C2682 cannot use ‘dynamic_cast’ to convert from ‘const itk::DataObject *’ to 
‘itk::ImageBase<3> *’ rtkvarianobigeometry C:\Users\dylan\RTK-2.1.0\applications\rtkvarianobigeometry\rtkvarianobigeometry.cxx 36

Thank you for the CI packages. I installed these, but I’m unable to use the GPUImageType:

AttributeError: module 'RTK' has no attribute 'CudaImage' 

I have the same behavior when installing the RTK binaries through pip. Ultimately I’d like to be able to use the RTK GPU functionality, so that’s what led me to trying to compile from source.

Once again, thank you very much for your time.

I think I misunderstood the first post, I though you were trying to use compiled packages from pypi. So this is a separate problem. On my side, I am not able to compile ITK without RTK so I won’t be able to help you… I’ll try to work on this next week.

1 Like

@doconnell: have you been able to solve the errors in this post. I just downloaded ITK from GIT and tried to build it with RTK and python wrapping, and get the same list of errors.