'FileDataset' object has no attribute 'InstanceNumber'

1,623 views
Skip to first unread message

randall heidorn

unread,
Mar 4, 2018, 10:59:24 AM3/4/18
to pydicom
Hello,

I'm just getting into image processing and using pydicom.  I've got a list of DICOM images that I'm pulling in for analysis.
However, when I run the code below I get the error :

/******************************************************************************************************/
ERROR:
  File "/anaconda3/lib/python3.6/site-packages/pydicom/dataset.py", line 480, in __getattr__
    return super(Dataset, self).__getattribute__(name)

AttributeError: 'FileDataset' object has no attribute 'InstanceNumber'
/******************************************************************************************************/


Here's the code I'm running, which was borrowed from 


/******************************************************************************************************/
CODE:

def load_scan(path):
    slices = [dicom.read_file(path + '/' + s,force = True) for s in os.listdir(path)]
    slices.sort(key = lambda x: int(x.InstanceNumber))
    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

def get_pixels_hu(scans):
    image = np.stack([s.pixel_array for s in scans])
    # Convert to int16 (from sometimes int16), 
    # should be possible as values should always be low enough (<32k)
    image = image.astype(np.int16)

    # Set outside-of-scan pixels to 1
    # The intercept is usually -1024, so air is approximately 0
    image[image == -2000] = 0
    
    # Convert to Hounsfield units (HU)
    intercept = scans[0].RescaleIntercept
    slope = scans[0].RescaleSlope
    
    if slope != 1:
        image = slope * image.astype(np.float64)
        image = image.astype(np.int16)
        
    image += np.int16(intercept)
    
    return np.array(image, dtype=np.int16)

id=0
patient = load_scan(filepath)
)
imgs = get_pixels_hu(patient)
/******************************************************************************************************/


Anybody have an idea what's causing this or if there's a modification I can make in dataset.py to get around this? Is InstanceNumber unique to my dataset?

Thanks

Darcy Mason

unread,
Mar 4, 2018, 11:23:59 AM3/4/18
to pydicom
I'd suggest putting a print statement in the loop to see which file the error occurs on. It may be that one of the files in your folder is not actually a dicom file, or at least not an image.

Aidos Sarsembayev

unread,
Mar 18, 2019, 5:04:00 PM3/18/19
to pydicom
I faced the same issue (but with the ImagePositionPatient attribute) while following the further tutorial from Kaggle 

The exception was telling me that there's no such attribute and indeed there wasn't.

for s in os.listdir(path):
    f = dicom.read_file(path +'\\'+ s)
    print(s)
    print(f.ImagePositionPatient)

I simply printed out each file name and tracked which one threw me the exception.

Then I read that file and printed out all meta. There was no ImagePositionPatient in it.

So, it seems like you don't have such an attribute in one of the files.

воскресенье, 4 марта 2018 г., 21:59:24 UTC+6 пользователь randall heidorn написал:
Reply all
Reply to author
Forward
0 new messages