pydicom AttributeError: Dataset does not have attribute 'ImagePositionPatient'

805 views Asked by At

I'm new to python. I'm trying to import dicom files. The following code is used in many tutorials for import dicom files using pydicom (for example https://www.kaggle.com/gzuidhof/full-preprocessing-tutorial):

INPUT_FOLDER = '../input/sample_images/'
patients = os.listdir(INPUT_FOLDER)
patients.sort()

Load the scans in given folder path

def load_scan(path):
    slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
    try:
        slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
    except:
        slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
    
    for s in slices:
        s.SliceThickness = slice_thickness
    
    return slices

first_patient = load_scan(INPUT_FOLDER + patients[0])

Every time I run this, I get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-14-883826d71e95> in <module>()
----> 1 first_patient = load_scan(INPUT_FOLDER + patients[0])

2 frames
/usr/local/lib/python3.6/dist-packages/dicom/dataset.py in __getattr__(self, name)
    255         if tag not in self:
    256             raise AttributeError("Dataset does not have attribute "
--> 257                                  "'{0:s}'.".format(name))
    258         else:  # do have that dicom data_element
    259             return self[tag].value

AttributeError: Dataset does not have attribute 'ImagePositionPatient'.

It's probably obvious to everyone but this code imports separate dicom files that together make up a CT scan and combines them into a single array "slices." When I define "slices" as a single dicom file rather than trying to combine all of them, I can write slices.ImagePositionPatient and it returns something like [x, y] so the original files definitely have this attribute. It seems like when I import them all into a single array, it somehow loses the attribute. As far as I can tell, I'm the only one who has had this issue. Can anyone provide any insight?

0

There are 0 answers