0

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?

  • Which version of `pydicom` are you using? You use `dicom` instead of `pydicom`, which has been used in pre-1.0 versions several years ago. Apart from that, your code looks ok, provided all the images are CT images with transverse orientation. – MrBean Bremen Oct 12 '20 at 19:18
  • It should be the most recent version, I used pip install and imported pydicom as dicom. I don't think it is a problem with the CT scan, I get the same error with pydicom's testdata files – Jake hockman Oct 12 '20 at 19:31
  • The pydicom test data files don't all have `ImagePositionPatient`. Are you sure you have only CT images in your folder? – MrBean Bremen Oct 12 '20 at 19:32
  • The last file was a scout image or something. It seems to be working now after removing it. Thanks a lot for your help! – Jake hockman Oct 12 '20 at 19:57

0 Answers0