loli / medpy

Medical image processing in Python
http://loli.github.io/medpy/
GNU General Public License v3.0
578 stars 138 forks source link

Error importing medpy.io's load #6

Closed plooney closed 7 years ago

plooney commented 10 years ago

I have installed using pip3

from medpy.io import load Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.4/dist-packages/medpy/io/init.py", line 59, in from .load import load File "/usr/local/lib/python3.4/dist-packages/medpy/io/load.py", line 28, in from . import header File "/usr/local/lib/python3.4/dist-packages/medpy/io/header.py", line 27, in from ..core import Logger File "/usr/local/lib/python3.4/dist-packages/medpy/core/init.py", line 55, in from .logger import Logger File "/usr/local/lib/python3.4/dist-packages/medpy/core/logger.py", line 94 raise RuntimeError, 'Only one instance of Logger is allowed!'

loli commented 10 years ago

Thanks for the information. Since I can't reproduce the error using Python 2, it seems to be an issue with Python 3 and singleton classes (using the factory method).

Can you reproduce the error using Python 2?

plooney commented 10 years ago

I get a little further in Python2 but I get this error when loading

medpy.core.exceptions.ImageLoadingError: Failes to load image 1/1_Prim.mha as Itk/Vtk MetaImage (.mhd, .mha/.raw). Reason signaled by third-party module: 'LazyITKModule' object has no attribute 'AnalyzeImageIO

loli commented 10 years ago

This problem stems from way ITK has been wrapped, not from MedPy.

When wrapping ITK for Python using WrapITK, each class desired has to be included explicitly. WrapITK does not provide a complete interface to ITK by itself.

Your version does not seem to include the itkAnalyzeImageIO class required by MedPy to load images. This probably means that the optional ITK module "IO" has not been included during compilation. You will have to compile it again.

Could you tell me, how you've acquired the ITK Python wrappings? And what configuration (with cmake) you've used? Best post a copy of your ccmake config.

For now, try to stick to image formats supported by NiBabel (see http://pythonhosted.org//MedPy/information/imageformats.html).

MMRohe commented 8 years ago

Hello, I have exactly the same problem with medpy : medpy.core.exceptions.ImageLoadingError: Failes to load image .../image.mha as Itk/Vtk MetaImage (.mhd, .mha/.raw). Reason signaled by third-party module: 'LazyITKModule' object has no attribute 'AnalyzeImageIO'

The problem is that the said module "AnalyzeImageIO" is deprecated under ITKv4 (which has already been running for quite a long time) so I don't really udnerstand how is it that medpy still needs it (http://www.itk.org/Doxygen42/html/classitk_1_1AnalyzeImageIO.html) ? Does it mean that I need to install ITKv3 to get medpy working with python?

Am I missing something here ?

Here is my CMake configuration for ITK v 4.7.2:

CMakeCache.txt

loli commented 8 years ago

Dear MMRohe,

thank you for reporting this and attaching your CMake config. As far as I can see, everything seems to be in order. MedPy has actually been testen with ITK 4.7 (http://pythonhosted.org/MedPy/installation/itkwrapper4.7.html), so I can't find the error immediately. You even have ITKV3_COMPATIBILITY=ON set, hence the AnalyzeImageIO should be available.

Today I am not a machine with ITK. I will look into the issue tomorrow and come back to you.

To tell the truth, keeping up with the changes in ITK and the very buggy behaviour of its Python Wrapper is very tedious and I often miss recent changes. I tend to use Convert3D (http://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md) to convert all images to NIfTI format and work with them, which improves stability and avoids loading the very large itk python module.

Best, loli

MMRohe commented 8 years ago

Hello loli,

thanks for your answer. I also use Nifti format and using it to make it work with Python could be a solution but with the data I am working on at the moment it would not be very convenient.

Indeed, the problem is quite weird. One thing I could think of, is that either the medpy version is outdated or the version of ITK that medPy is linked to is not the correct one (because I have several ITK versions on my computer) but I have double checked the link and the other ITK do not have the python wrapper built in anyway. Do you know if there is command inside python that could show exactly which version of the module it is using (I tried print medpy.version or print itk.version but these attributes dont exist).

Anyway, thanks a lot for your help, if you manage to get a solution or have any idea don't hesitate to contact me :)

Best,

Marc-Michel

On 01/11/2016 02:21 PM, loli wrote:

Dear MMRohe,

thank you for reporting this and attaching your CMake config. As far as I can see, everything seems to be in order. MedPy has actually been testen with ITK 4.7 (http://pythonhosted.org/MedPy/installation/itkwrapper4.7.html), so I can't find the error immediately. You even have ITKV3_COMPATIBILITY=ON set, hence the AnalyzeImageIO should be available.

Today I am not a machine with ITK. I will look into the issue tomorrow and come back to you.

To tell the truth, keeping up with the changes in ITK and the very buggy behaviour of its Python Wrapper is very tedious and I often miss recent changes. I tend to use Convert3D (http://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md) to convert all images to NIfTI format and work with them, which improves stability and avoids loading the very large itk python module.

Best, loli

— Reply to this email directly or view it on GitHub https://github.com/loli/medpy/issues/6#issuecomment-170546516.

loli commented 8 years ago

The ITK wrappers are fragile things. Trying

import inspect
import itk
inspect.getfile(itk)

tells me that it is an build-in module, which we know to be false. Well, nothing to be done about it. I could simply look at my PYTHONPATH to figure out, which one I am using.

Currently, I am still compiling ITK 4.7, I will come back to you later. It might take till next week, I fear :/

Best, loli

loli commented 8 years ago

Fixed with the newest commit to https://github.com/loli/medpy/

Enjoy! loli

MMRohe commented 8 years ago

thx for your answer ! (and sorry I did not reply back earlier, had some problem with my comp so could not try it)

so it seems the update has fixed the previous issue but not I get the following message. It seems weird that it calls the load_nibabel function for a .mha image ?

ImageLoadingError Traceback (most recent call last)

in() 20 pathImage='image.mha'21 ---> 22image_data, image_header = load(pathImage)23 #print image_data.shape()/medpy-master/medpy/io/load.py in load(image) 199 logger.debug('Module {} signaled error: {}.'.format(loader, e))200 --> 201raise err202 203 def __load_nibabel(image):ImageLoadingError: Failes to load image image.mha as Itk/Vtk MetaImage (.mhd, .mha/.raw). Reason signaled by third-party module: 'module' object has no attribute 'swig' On 01/13/2016 04:08 PM, loli wrote: > Fixed with the newest commit to > https://github.com/loli/medpy/ > > Enjoy! > loli > > — > Reply to this email directly or view it on GitHub > https://github.com/loli/medpy/issues/6#issuecomment-171323363.
loli commented 8 years ago

Mhh... my version is loading MHA images just fine. Could you attached a zipped version of your image for me for testing?

The __load_nibabel error results from the way MedPy loads images: First, it tries to find the best module depending on the image type. If that loader fails, it tries all available ones on a brute-force basis and returns the error of the last one tested.

raghuramdr commented 8 years ago

I don't know if this issue is still being tracked, but I am also having similar problems. When I try to load .mha files, I get a bunch of warnings, followed by the error: _raise err medpy.core.exceptions.ImageLoadingError: Failes to load image Registered-1808820224_Classification.mha as Itk/Vtk MetaImage (.mhd, .mha/.raw). Reason signaled by third-party module: 'LazyITKModule' object has no attribute 'AnalyzeImageIO' _

I set the ITK V3_COMPATIBILITY flag = ON during make. I am using ITK 4.9 and I am using Python 2.7

loli commented 8 years ago

Did you install the PYPI 2.2 or the GitHub develoment version?

sajjo79 commented 7 years ago

Hi loli, I am getting same error as of raghuramdr. How can i correct it .

Best

rcasero commented 7 years ago

Same problem as @plooney.

Conda virtual environment with python 3.6.1, numpy 1.12.1 and scipy 0.19.0.

I install medpy with

$ pip install medpy
Collecting medpy
Requirement already satisfied: numpy>=1.6.1 in /home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages (from medpy)
Requirement already satisfied: scipy>=0.9.0 in /home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages (from medpy)
Installing collected packages: medpy
Successfully installed medpy-0.2.2

Then, importing load() fails

$ python -c 'from medpy.io import load'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages/medpy/io/__init__.py", line 59, in <module>
    from .load import load
  File "/home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages/medpy/io/load.py", line 28, in <module>
    from . import header
  File "/home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages/medpy/io/header.py", line 27, in <module>
    from ..core import Logger
  File "/home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages/medpy/core/__init__.py", line 55, in <module>
    from .logger import Logger
  File "/home/rcasero/.conda/envs/cytometer/lib/python3.6/site-packages/medpy/core/logger.py", line 94
    raise RuntimeError, 'Only one instance of Logger is allowed!'
                      ^
SyntaxError: invalid syntax
kiranvaidhya commented 7 years ago

Same error as @rcasero

rcasero commented 7 years ago

@loli , details on how to reproduce the error

  1. Install conda

    1. Download the Miniconda bash installer (e.g. for linux 64-bit, Miniconda2-latest-Linux-x86_64.sh).
    2. Make the script executable and run it as root

      cd ~/Downloads
      chmod u+x Miniconda2-latest-Linux-x86_64.sh
      sudo ./Miniconda2-latest-Linux-x86_64.sh
    3. When it asks for the install destination, select /opt/miniconda2, rather than the default /home/rcasero/miniconda2.
    4. "Do you wish the installer to prepend the Miniconda2 install location to PATH in your /home/rcasero/.bashrc ? [yes|no]". Select yes.
  2. Create a conda environment for the test (python 3.6 at the time of this writing)

    conda create --name test python=3
  3. Activate the conda environment

    source activate test
  4. Install medpy

    pip install medpy
  5. test medpy

    python -c 'from medpy.io import load'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/rcasero/.conda/envs/test/lib/python3.6/site-packages/medpy/io/__init__.py", line 59, in <module>
        from .load import load
      File "/home/rcasero/.conda/envs/test/lib/python3.6/site-packages/medpy/io/load.py", line 28, in <module>
        from . import header
      File "/home/rcasero/.conda/envs/test/lib/python3.6/site-packages/medpy/io/header.py", line 27, in <module>
        from ..core import Logger
      File "/home/rcasero/.conda/envs/test/lib/python3.6/site-packages/medpy/core/__init__.py", line 55, in <module>
        from .logger import Logger
      File "/home/rcasero/.conda/envs/test/lib/python3.6/site-packages/medpy/core/logger.py", line 94
        raise RuntimeError, 'Only one instance of Logger is allowed!'
                          ^
    SyntaxError: invalid syntax
loli commented 7 years ago

This thread addresses two separate problems which are both solved.

Problem 1 raise RuntimeError, 'Only one instance of Logger is allowed!' If you are encountering this problem, you are using Python 3. Officially, MedPy only supports Python 2. But, thanks for an effort from @mamrehn, the current master branch might just be Python 3 compatible. I.e., get the newest MedPy version from https://github.com/loli/medpy. (@rcasero and @kvrd18)

Problem 2 Reason signaled by third-party module: 'LazyITKModule' object has no attribute 'AnalyzeImageIO' See Issue #15. Solved in the master current master branch. I.e., get the newest MedPy version from https://github.com/loli/medpy. (@raghuramdr and @sajjo79)