AIM-Harvard / pyradiomics

Open-source python package for the extraction of Radiomics features from 2D and 3D images and binary masks. Support: https://discourse.slicer.org/c/community/radiomics
http://pyradiomics.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.15k stars 499 forks source link

[FEAT EXTRACTION] Error reading image Filepath or SimpleITK object #650

Closed slimyberry closed 4 years ago

slimyberry commented 4 years ago

I want to do a batch extraction but it throws a ValueError:Error reading image Filepath or SimpleITK object

However, if I supply these files and masks individually, they work fine. But when I put it in the csv and run it as a batch file, I get this error. How do I solve it?

(base) dr@DESKTOP-KJV2PE4:~/notebooks$ pyradiomics /mnt/d/1.Main\ Data/1.Annotated/testradiomics.csv
[2020-10-08 11:39:38] E: radiomics.script: Feature extraction failed!
Traceback (most recent call last):
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/scripts/segment.py", line 70, in _extractFeatures
    feature_vector.update(extractor.execute(imageFilepath, maskFilepath, label, label_channel))
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/featureextractor.py", line 269, in execute
    image, mask = self.loadImage(imageFilepath, maskFilepath, generalInfo, **_settings)
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/featureextractor.py", line 372, in loadImage
    raise ValueError('Error reading image Filepath or SimpleITK object')
ValueError: Error reading image Filepath or SimpleITK object
[2020-10-08 11:39:38] E: radiomics.script: Feature extraction failed!
Traceback (most recent call last):
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/scripts/segment.py", line 70, in _extractFeatures
    feature_vector.update(extractor.execute(imageFilepath, maskFilepath, label, label_channel))
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/featureextractor.py", line 269, in execute
    image, mask = self.loadImage(imageFilepath, maskFilepath, generalInfo, **_settings)
  File "/home/dr/anaconda3/lib/python3.7/site-packages/radiomics/featureextractor.py", line 372, in loadImage
    raise ValueError('Error reading image Filepath or SimpleITK object')
ValueError: Error reading image Filepath or SimpleITK object
Case-1_Image: /mnt/d/1.Main\ Data/1.Annotated/2/2_cna.nii
Case-1_Mask: /mnt/d/1.Main\ Data/1.Annotated/2/2_seg.nii.gz
Case-2_Image: /mnt/d/1.Main\ Data/1.Annotated/6/6_cna.nii
Case-2_Mask: /mnt/d/1.Main\ Data/1.Annotated/6/6_seg.nii.gz
(base)

NOTE: It did throw the same error even on a single file. But I changed the numpy version from 1.19.1 to 1.18 and the individual files work now.

JoostJM commented 4 years ago

@slimyberry, your image file is throwing the error. For some reason, it cannot be read by SimpleITK. It's strange that reverting the numpy array appears to fix your issue, as we are not using numpy to read in your image, but rather SimpleITK.

What happens if you pass --validate on the cmd line? In that case it only checks whether the paths you provided points to an existing file. Most likely there is an error somewhere in how you define your paths. Be aware that linux does not like the backslashes in the path. If that runs without an error, it is really the loading of the image itself, which does seem unlikely as your able to load when using in single-file mode.

Inside PyRadiomics, the only difference between batch mode and single file mode is the list of cases that iterates over your cases. In batch mode it is built from the csv file, in single mode a single entry is built from the command line arguments.

JoostJM commented 4 years ago

Can you also show the output in single-case mode (where it does complete successfully?)

slimyberry commented 4 years ago

@JoostJM Thankyou for your reply. It is unlikely that the image files have error because I have run the batch mode on the same files last year.

I solved it by installing an older version of pyradiomics. I did:

pip install pyradiomics==2.2.0

and ran the exact same command, it worked like a charm for my 160 files. I suspect this is a bug.

slimyberry commented 4 years ago

I am using the commands inside the WSL and the file path issue is possible. Does pyradiomics expect a windows style path?

JoostJM commented 4 years ago

It's quite possible it's failing over the backslah in your path. Be aware that paths entered in your csv are treated as if enclosed in quotes on the commandline. Therefore the backslash escape for the space is not necessary and most likely causes your path to fail. Does it work if you remove that backslash?

I'd not recommend using version 2.2.0, as there are some significant changes in the code

JoostJM commented 4 years ago

I am using the commands inside the WSL and the file path issue is possible. Does pyradiomics expect a windows style path?

Python generally accepts unix-style paths as well, though I don't know how space-escaped unix paths are handled. The --validate option allows you to check that easily though, as it only checks for the existence of the file, without trying to load or extract anything.

Alternatively, you can check the value seen in "case-1_Image" Which is the path as it is stored in PyRadiomics. In the batch case (shown above) you can see it preserved the backslash, I suspect you won't see it when running in single mode (as it has been "used" to escape the space, which is only necessary on the cmd line, not in the csv)

slimyberry commented 4 years ago

It's quite possible it's failing over the backslah in your path

I actually added the backslash as it wasn't working without it. I also changed the path ie renamed the folder to 1.MainData to not include any spaces in the file path and it still did not work.

Let me reinstall to the current version and try again

slimyberry commented 4 years ago

@JoostJM The uninstall and reinstall fixed it! I have not changed any other parameters so this is quite strange. Anyway, thankyou so much for your help and for telling me to not use version 2.2.0.

pcanomayoo commented 1 year ago

Hello, I have the same problem :/ I am trying to run "Example of using the PyRadiomics toolbox in Python"

import os  # needed navigate the system to get the input data
from radiomics import featureextractor  # This module is used for interaction with pyradiomics

# Define the testcase name
testCase = 'brain1'

# Obtiene la ruta relativa a pyradiomics\data
# os.cwd() devuelve el directorio de trabajo actual
# ".." apunta al directorio padre: \pyradiomics\bin\Notebooks\..\ es igual a \pyradiomics\bin\
# Subir 2 directorios (es decir, ir a \pyradiomics\) y luego pasar a \pyradiomics\data

dataDir = os.path.join(os.getcwd(), "..", "..", "data")
print ("dataDir, relative path:", dataDir)
print ("dataDir, absolute path:", os.path.abspath(dataDir))

# Store the file paths of our testing image and label map into two variables
imagePath = os.path.join(dataDir, testCase + "_image.nrrd")
#print ("Image, absolute path:", os.path.abspath(imagePath))
labelPath = os.path.join(dataDir, testCase + "_label.nrrd")
#print ("Label, absolute path:", os.path.abspath(labelPath))

# Additonally, store the location of the example parameter file, stored in \pyradiomics\bin
paramPath = os.path.join(os.getcwd(), "..", "Params.yaml")
#print ("Parameter file, absolute path:", os.path.abspath(paramPath))

# METODO 1
# Instantiate the extractor
extractor = featureextractor.RadiomicsFeatureExtractor()

#print ("Extraction parameters:\n\t", (extractor.settings))
#print ("Enabled filters:\n\t", (extractor.enabledImagetypes))
#print ("Enabled features:\n\t", (extractor.enabledFeatures))

result = extractor.execute(imagePath, labelPath)

PS C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\Radiomics_Ini\pyradiomics-master> & C:/Users/pauca/AppData/Local/Microsoft/WindowsApps/python3.7.exe "c:/Users/pauca/OneDrive - Universidad internacional de valencia/Escritorio/Máster/Prácticas/Radiomics_Ini/pyradiomics-master/Prueba.py" dataDir, relative path: C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\Radiomics_Ini\pyradiomics-master....\data dataDir, absolute path: C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\data Traceback (most recent call last): File "c:/Users/pauca/OneDrive - Universidad internacional de valencia/Escritorio/Máster/Prácticas/Radiomics_Ini/pyradiomics-master/Prueba.py", line 35, in result = extractor.execute(imagePath, labelPath) File "C:\Users\pauca\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\radiomics\featureextractor.py", line 272, in execute image, mask = self.loadImage(imageFilepath, maskFilepath, generalInfo, **_settings) File "C:\Users\pauca\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\radiomics\featureextractor.py", line 375, in loadImage raise ValueError('Error reading image Filepath or SimpleITK object') ValueError: Error reading image Filepath or SimpleITK object

colaquafina commented 9 months ago

Hey, I had the same problem. Did you figure out the solution?

colaquafina commented 9 months ago

Hello, I have the same problem :/ I am trying to run "Example of using the PyRadiomics toolbox in Python"

import os  # needed navigate the system to get the input data
from radiomics import featureextractor  # This module is used for interaction with pyradiomics

# Define the testcase name
testCase = 'brain1'

# Obtiene la ruta relativa a pyradiomics\data
# os.cwd() devuelve el directorio de trabajo actual
# ".." apunta al directorio padre: \pyradiomics\bin\Notebooks\..\ es igual a \pyradiomics\bin\
# Subir 2 directorios (es decir, ir a \pyradiomics\) y luego pasar a \pyradiomics\data

dataDir = os.path.join(os.getcwd(), "..", "..", "data")
print ("dataDir, relative path:", dataDir)
print ("dataDir, absolute path:", os.path.abspath(dataDir))

# Store the file paths of our testing image and label map into two variables
imagePath = os.path.join(dataDir, testCase + "_image.nrrd")
#print ("Image, absolute path:", os.path.abspath(imagePath))
labelPath = os.path.join(dataDir, testCase + "_label.nrrd")
#print ("Label, absolute path:", os.path.abspath(labelPath))

# Additonally, store the location of the example parameter file, stored in \pyradiomics\bin
paramPath = os.path.join(os.getcwd(), "..", "Params.yaml")
#print ("Parameter file, absolute path:", os.path.abspath(paramPath))

# METODO 1
# Instantiate the extractor
extractor = featureextractor.RadiomicsFeatureExtractor()

#print ("Extraction parameters:\n\t", (extractor.settings))
#print ("Enabled filters:\n\t", (extractor.enabledImagetypes))
#print ("Enabled features:\n\t", (extractor.enabledFeatures))

result = extractor.execute(imagePath, labelPath)

PS C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\Radiomics_Ini\pyradiomics-master> & C:/Users/pauca/AppData/Local/Microsoft/WindowsApps/python3.7.exe "c:/Users/pauca/OneDrive - Universidad internacional de valencia/Escritorio/Máster/Prácticas/Radiomics_Ini/pyradiomics-master/Prueba.py" dataDir, relative path: C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\Radiomics_Ini\pyradiomics-master....\data dataDir, absolute path: C:\Users\pauca\OneDrive - Universidad internacional de valencia\Escritorio\Máster\Prácticas\data Traceback (most recent call last): File "c:/Users/pauca/OneDrive - Universidad internacional de valencia/Escritorio/Máster/Prácticas/Radiomics_Ini/pyradiomics-master/Prueba.py", line 35, in result = extractor.execute(imagePath, labelPath) File "C:\Users\pauca\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\radiomics\featureextractor.py", line 272, in execute image, mask = self.loadImage(imageFilepath, maskFilepath, generalInfo, **_settings) File "C:\Users\pauca\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\radiomics\featureextractor.py", line 375, in loadImage raise ValueError('Error reading image Filepath or SimpleITK object') ValueError: Error reading image Filepath or SimpleITK object

Hey, I am not sure if my solution is correct. I reinstall the SimpleITK to version 2.2.0, and I think it works. Maybe you can try if it works for you.