in Neuroimaging, Python

NeuroCoder – pydicom

NeuroCoder – pydicom

DICOM files

DICOM is a medical standard for storing image data from a large range of devices. As a Cognitive Neuroscientist, the primary data I am currently interested in is magnetic resonance imaging data. Now the raw data as received from the radiographer is in the DICOM file format.

Working with DICOMS

Most of the neuroimaging analysis tools that I use work with other file formats – a popular one being NIfTI – but before the conversion from DICOM files a few things need to often be done.

Scanner parameters can change – either small changes via an MRI software update or perhaps much larger changes due to operator error. To aid in the detection of these changes an automated way of extracting scanner parameters from the DICOMS is particularly handy. This is where pydicom becomes essential.


pydicom‘s predominant purpose is not for viewing DICOM images, but more for accessing and modifying DICOM data elements. Installation is simple with pip install pydicom and using it is almost as easy.

For instance, a simple script to view the first DICOM image from a series of folders and output the data element details corresponding to dates, sequences etc. could go as follows:

import dicom
import os

for folder in folders:
    files = os.listdir(os.path.realpath(folder))
    dataset = dicom.readfile(files[0])
    print('Patient Name: ', dataset[0x10,0x10]) #Access via tag number
    print('Sequence Date: ', dataset.SequenceName) #Access via tag name

Other uses

As well as accessing details of a scan to see that it matches other participants sequences there is one other major use that is part of the example scripts provided, namely the anonymize module. This is a simple script that removes identifying patient details from the header information to allow for sharing of the data with other researchers in an ethically responsible way.

##Final Notes and caveats

  • The anonymize functionality provided is not guaranteed to remove all possible identifying data, and care must be taken.
  • The new version due soon seems likely to replace the import call from dicom to pydicom

Leave a Reply