KitwareMedical / ITKContourInterpolation

An implementation of morphological contour interpolation
http://hdl.handle.net/10380/3563
Apache License 2.0
30 stars 13 forks source link

BUG: AttributeError: PyCapsule_Import "_ITKCommonPython._C_API" is not valid #107

Closed MLK97 closed 1 year ago

MLK97 commented 1 year ago

Encountered this Attribute Error on the line that calls the morphological contour interpolation. I am on version ITK 5.3.0 and Python 3.10.7 and assume that my itk version may be the source of the problem?

I have tried to downgrade to itk version v5.3rc03 via pip but that gives some compatibility issues with the itk-filtering version, so I am curious if there might be a simple solution to this?

Here is the code that I am running that causes the issue

scan_image_url =URL-TO-NIFTI-MR-SCAN-IMAGE
oar_url =URL-TO-NIFTI-CONTOUR-MASK

scan_image_itk = itk.imread(scan_image_url)
oar_itk = itk.imread(oar_url)

interpolated_contour = itk.morphological_contour_interpolator(oar_itk)

which gives the following traceback and error

AttributeError                            Traceback (most recent call last)
Cell In [4], line 15
     12 scan_image_itk = itk.imread(scan_image_url)
     13 oar_itk = itk.imread(oar_url)
---> 15 interpolated_contour = itk.morphological_contour_interpolator(scan_image_itk)

File c:\Users\UDA2UH\AppData\Local\Programs\Python\Python310\lib\site-packages\itk\support\lazy.py:138, in LazyITKModule.__getattribute__(self, attr)
    136 module = self.__belong_lazy_attributes[attr]
    137 namespace = {}
--> 138 base.itk_load_swig_module(module, namespace)
    139 self.loaded_lazy_modules.add(module)
    140 for k, v in namespace.items():

File c:\Users\UDA2UH\AppData\Local\Programs\Python\Python310\lib\site-packages\itk\support\base.py:132, in itk_load_swig_module(name, namespace)
    129 # SWIG-generated modules have 'Python' appended. Only load the SWIG module
    130 # if we haven't already.
    131 loader = LibraryLoader()
--> 132 l_module = loader.load(swig_module_name)
    134 # OK, now the modules on which this one depends are loaded and
    135 # template_feature-instantiated, and the SWIG module for this one is also loaded.
    136 # We're going to put the things we load and create in two places: the
   (...)
...
---> 13     from . import _MorphologicalContourInterpolationPython
     14 else:
     15     import _MorphologicalContourInterpolationPython

AttributeError: PyCapsule_Import "_ITKCommonPython._C_API" is not valid
dzenanz commented 1 year ago

I think we will need to release a new version of this remote module. @tbirdso could you do it?

dzenanz commented 1 year ago

The issue itself is probably a duplicate of https://github.com/InsightSoftwareConsortium/ITK/issues/3528.

tbirdso commented 1 year ago

Agreed, this is an issue with backwards compatibility between current PyPI wheels for this project and ITK v5.3.0.

Short-term workaround: uninstall all ITK modules before install ITK v5.3rc3 wheels

python -m pip uninstall itk itk-core itk-filtering ... <other ITK modules here>
python -m pip install itk==v5.3rc3 itk-morphologicalcontourinterpolation

Per @dzenanz 's request the longer-term solution is to bump and release new wheels for compatibility, I will take that on shortly.

MLK97 commented 1 year ago

Thank you @dzenanz and @tbirdso for the fast reply 🙂

The short-term solution you suggest is unfortunately not working anymore because of some very weird dependency conflicts with the version of the dependencies.

So when installing itk v5.3rc3, I get an issue because of itk-numerics:

ERROR: Could not find a version that satisfies the requirement itk-numerics==5.3rc3 (from itk) (from versions: 5.0.1, 5.1.0, 5.1.0.post3, 5.1.1, 5.1.1.post1, 5.1.2, 5.2.0, 5.2.0.post1, 5.2.0.post2, 5.2.1, 5.2.1.post1, 5.3rc4.post3, 5.3rc4.post4, 5.3.0)
ERROR: No matching distribution found for itk-numerics==5.3rc3

I went through every version from the current itk 5.3.0 down to itk 5.0.0 but the error is always similar. Sometimes itk-filters is causing the error as well, so unfortunately downgrading is not a working workaround anymore (at least for me)

dzenanz commented 1 year ago

python -m pip uninstall itk ...

This usually does not completely removes all mentions of ITK in site-pacakges. If you view site-pacakges in file browser and remove all itk-related files and folders, does the error still persist?

MLK97 commented 1 year ago

Yes, the error persists. I just tried it on another windows machine that did not have itk installed and I am getting the same error.

tbirdso commented 1 year ago

Hi all, unfortunately it looks like we are in the process of removing ITK release candidate wheels to make space on PyPI. v5.3rc3 is no longer available and v5.3rc04 candidates will be removed soon.

The previous stable release is v5.2.1.post1, if there is an ITKMorphologicalContourInterpolation package that is compatible with that release then that would be the one to downgrade to. Otherwise it shouldn't take too long to release a package compatible with the ITK v5.3.0 release, barring any unforeseen complications.

You can see the itk-numerics PyPI release history here: https://pypi.org/project/itk-numerics/#history