dicomsort icon indicating copy to clipboard operation
dicomsort copied to clipboard

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

Open denbonte opened this issue 3 years ago • 2 comments

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).

denbonte avatar Sep 01 '21 09:09 denbonte

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

https://github.com/pieper/dicomsort/blob/c9cf5ad16fb3021bb5c6e94ec5c63aec7ea9a99c/dicomsort.py#L231-L240

pieper avatar Sep 01 '21 14:09 pieper

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.

pieper avatar Sep 01 '21 14:09 pieper