InsightSoftwareConsortium / ITKThickness3D

An ITK module to compute 3D thickness
Apache License 2.0
39 stars 18 forks source link

Running with AttributeError #33

Closed Jtwilight closed 3 years ago

Jtwilight commented 4 years ago

Hi, Thanks for your package and I'm trying to use it to get the skeleton of an airway. And I used the example code I found in a former issure


import itk
import numpy as np
import imageio

# create the original test array
array = np.zeros((256,256,256),dtype=np.int16)
array[50:100, :, :] = 1
# cast to itk image
image = itk.GetImageViewFromArray(array)
# extract the skeleton
skeleton = itk.BinaryThinningImageFilter3D(image)
# cast back the skeleton from itk to numpy
output = itk.GetArrayViewFromImage(skeleton)
# simple way to see if output is empty:
print("original object size : {} vx".format(np.sum(array)))
print("skeleton size : {} vx".format(np.sum(output)))
# export as tiff objects fot visualization
imageio.volwrite("in.tif", array)
imageio.volwrite("out.tif", output)```

and it reported an error like this: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
d:\personal\find_airway_centerline.py in 
      27 image = itk.GetImageViewFromArray(array)
     28 # extract the skeleton
---> 29 skeleton = itk.BinaryThinningImageFilter3D(image)
     30 # cast back the skeleton from itk to numpy
     31 output = itk.GetArrayViewFromImage(skeleton)

D:\softwares\ANACONDA\envs\torch\lib\site-packages\itkLazy.py in __getattribute__(self, attr)
     50             module = self.__belong_lazy_attributes[attr]
     51             namespace = {}
---> 52             itkBase.LoadModule(module, namespace)
     53             self.loaded_lazy_modules.add(module)
     54             for k, v in namespace.items():

D:\softwares\ANACONDA\envs\torch\lib\site-packages\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.Thickness3DPython' has no attribute 'swig'
---------------------------------------------------------------------------
It's strange because I didn't name any files with "itk" inside the name. 
Codes were running on windows, using Python 3.6.10, in VS Code. Do you have any suggestion? 
Thanks! 
fusentasticus commented 4 years ago

Me, too. Same problem. Do you have any specific suggestions for Anaconda? I'm new to itk, too. Thanks!

The main incantation was (to use the -m option inside a proper environment): subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'itk-thickness3d']) which I invoked in a Python shell from Ananconda Navigator. (This after installing 'conda install pip' in an Anaconda dispatched shell.)


Collecting itk-thickness3d
  Using cached itk_thickness3d-5.1-cp37-cp37m-win_amd64.whl (255 kB)
Collecting itk>=5.0.0.post1
  Using cached itk-5.1.1-cp37-cp37m-win_amd64.whl (7.6 kB)
Collecting itk-numerics==5.1.0.post3
  Downloading itk_numerics-5.1.0.post3-cp37-cp37m-win_amd64.whl (12.3 MB)
     |████████████████████████████████| 12.3 MB 1.1 MB/s
Collecting itk-filtering==5.1.0.post3
  Downloading itk_filtering-5.1.0.post3-cp37-cp37m-win_amd64.whl (23.7 MB)
     |████████████████████████████████| 23.7 MB 819 kB/s
Collecting itk-segmentation==5.1.0.post3
  Downloading itk_segmentation-5.1.0.post3-cp37-cp37m-win_amd64.whl (3.3 MB)
     |████████████████████████████████| 3.3 MB 1.1 MB/s
Collecting itk-registration==5.1.0.post3
  Downloading itk_registration-5.1.0.post3-cp37-cp37m-win_amd64.whl (4.5 MB)
     |████████████████████████████████| 4.5 MB 819 kB/s
Collecting itk-core==5.1.0.post3
  Downloading itk_core-5.1.0.post3-cp37-cp37m-win_amd64.whl (25.3 MB)
     |████████████████████████████████| 25.3 MB 544 kB/s
Requirement already satisfied, skipping upgrade: numpy in c:\users\nils\anaconda3\envs\myitk\lib\site-packages (from itk>=5.0.0.post1->itk-thickness3d) (1.19.1)
Collecting itk-io==5.1.0.post3
  Using cached itk_io-5.1.0.post3-cp37-cp37m-win_amd64.whl (5.7 MB)
Installing collected packages: itk-core, itk-numerics, itk-filtering, itk-segmentation, itk-registration, itk-io, itk, itk-thickness3d
Successfully installed itk-5.1.1 itk-core-5.1.0.post3 itk-filtering-5.1.0.post3 itk-io-5.1.0.post3 itk-numerics-5.1.0.post3 itk-registration-5.1.0.post3 itk-segmentation-5.1.0.post3 itk-thickness3d-5.1
0

AttributeError                            Traceback (most recent call last)
<ipython-input-4-e7c66b424812> in <module>
      1 import itk
      2 image = itk.imread(r'C:\Users\nils\Documents\Slicer2020\h30 - high\test high-Segment_1-label.nrrd')
----> 3 thickness_map = itk.MedialThicknessImageFilter3D.New(image)
      4 itk.imwrite(thickness_map, r'C:\Users\nils\Documents\Slicer2020\h30 - high\Test - thickness.nrrd')

~\anaconda3\envs\myitk\lib\site-packages\itkLazy.py in __getattribute__(self, attr)
     50             module = self.__belong_lazy_attributes[attr]
     51             namespace = {}
---> 52             itkBase.LoadModule(module, namespace)
     53             self.loaded_lazy_modules.add(module)
     54             for k, v in namespace.items():

~\anaconda3\envs\myitk\lib\site-packages\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.Thickness3DPython' has no attribute 'swig'
fusentasticus commented 4 years ago

And it didn't get better under Linux (wsl) and a in venv install. So I tried to bypass Anaconda. Again, the Swig code is not behaving. (I also attempted without venv)

I suspect ImportError: attempted relative import with no known parent package is a give-away about what's wrong? This happens somewhat deep in the call stack that's trying to load the 3D thicknesss code.


Trying in (3.7_itk) fst@frika-dell:~$ python /mnt/c/Users/fst/Documents/Slicer2020/thickness3.py
Traceback (most recent call last):
  File "/mnt/c/Users/fst/Documents/Slicer2020/thickness3.py", line 5, in <module>
    thickness_map = itk.MedialThicknessImageFilter3D.New(image)
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itkLazy.py", line 52, in __getattribute__
    itkBase.LoadModule(module, namespace)
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itkBase.py", line 99, in LoadModule
    module = loader.load(swig_module_name)
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itkBase.py", line 220, in load
    module.__loader__.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itk/Thickness3DPython.py", line 113, in <module>
    from itkThickness3DPython import *
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itk/itkThickness3DPython.py", line 106, in <module>
    import itkImageToImageFilterAPython
  File "/home/fst/venvs/3.7_itk/lib/python3.7/site-packages/itk/itkImageToImageFilterAPython.py", line 8, in <module>
    from . import _ITKCommonPython
ImportError: attempted relative import with no known parent package
fusentasticus commented 4 years ago

Perhaps this is related to https://pypi.org/project/itk-thickness3d/ last being updated June 19, 2019? There appears to be a few commits to master branch since then, but I don't know whether they address 5.1 issues.

T4mmi commented 4 years ago

Hi, Sorry for the delay, I'm working in a new field of area which leaves me very few time for old projects...

The problem is not related to the module itself, but to the Python wrapping (SWIG is the wrapping tool). Without looking into the error, I'd say that it is the wrapping template that need some tweaking for ITK 5.1

:/

fusentasticus commented 4 years ago

Thanks, I did manage to compile from source (5.1). The build took a few hours. Then from the generated code, I did cp Wrapping/Generators/Python/WrapITK.pth ~/.local/lib/python3.8/site-packages and I had a working installation.

Except it doesn't. The implementation appears broken as reported in issue #29. Please refer to my comment there.

thewtex commented 3 years ago

A new package is available, itk-thickness3d 5.1.1 -- please give it a try.

fusentasticus commented 3 years ago

Thanks so much for fixing this! It works.