Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip patient/Series processing if corrupted DICOM cannot be read by pydicom's dcmread() #20

Open
denbonte opened this issue Sep 1, 2021 · 2 comments

Comments

@denbonte
Copy link

denbonte commented Sep 1, 2021

Steve,

As we discussed, I think it could be beneficial to have the possibility to skip the processing of a patient/specific Series if one of the DICOM files in the Series/folder is corrupted or, in general, if it cannot be read by pydicom's dcmread()†.

as I reported at the end of the issue, this is already done in some cases - so I'm sorry if the title is a bit misleading but I didn't know how to name this properly!

For the sake of completeness, I'm pasting here the example I already shared privately:

Considering file XXXXXXXXXXXXXXXXXXXXXXXXX.dcm
ERROR, UNEXPECTED EXCEPTION
'bytes' object has no attribute 'name'
Traceback (most recent call last):
  File "dicomsort.py", line 493, in <module>
    if not sorter.renameFiles():
  File "dicomsort.py", line 214, in renameFiles
    if self.renameFile(file):
  File "dicomsort.py", line 232, in renameFile
    ds = dicom.read_file(file,stop_before_pixels=True)
  File "/home/dennis/.local/lib/python3.6/site-packages/pydicom/filereader.py", line 1034, in dcmread
    specific_tags=specific_tags,
  File "/home/dennis/.local/lib/python3.6/site-packages/pydicom/filereader.py", line 874, in read_partial
    if class_uid and class_uid.name == "Media Storage Directory Storage":
AttributeError: 'bytes' object has no attribute 'name'

If it can help in any way, apparently another .dcm file in the dataset was not readable - and was put under the folder UnknownPatientID/UnknownStudyInstanceUID/UnknownSeriesInstanceUID.

When I tried to open that single .dcm file, pydicom wasn't happy again and raised an exception (unfortunately, I removed the file without thinking this through, but it was something almost identical to this problem here - except the tags were different).

That exception was somehow handled correctly by DICOMSort (even if the file was not readable - I guess the exception raised was probably different and correctly caught).

@pieper
Copy link
Owner

pieper commented Sep 1, 2021

Let's go ahead and add an extra except clause for AttributeError to this block:

dicomsort/dicomsort.py

Lines 231 to 240 in c9cf5ad

try:
ds = dicom.read_file(file,stop_before_pixels=True)
except (IOError, os.error) as why:
print( "dicom.read_file() IO error on file %s, exception %s" % (file,str(why)) )
return False
except InvalidDicomError:
return False
except KeyError:
# needed for issue with pydicom 0.9.9 and some dicomdir files
return False

@pieper
Copy link
Owner

pieper commented Sep 1, 2021

Plus let's add a catch-all except at as a fallback. Probably we should be printing the errors for the other cases, probably with some logging level controls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants