dicom - Functions to interact with DICOM images

methods to analyze DICOM format images

class neural.dicom.DicomInfo(frames=[], sex_info={}, slice_times=[])[source]

container for header information from DICOM file

Header frames are dictionaries with the following values:

Addr:tuple of (group,element) tags in hex
Label:description
Offset:offset of data
Size:size of data
Value:data value
addr(address)[source]

returns dictionary with frame information for given address (a tuple of two hex numbers)

label(label)[source]

returns dictionary with frame information for a given text label

neural.dicom.classify(label_dict, image_fname=None, image_label=None)[source]

tries to classify a DICOM image based on known string patterns (with fuzzy matching)

Takes the label from the DICOM header and compares to the entries in label_dict. If it finds something close it will return the image type, otherwise it will return None. Alternatively, you can supply your own string, image_label, and it will try to match that.

label_dict is a dictionary where the keys are dataset types and the values are lists of strings that match that type. For example:

{
    'anatomy': ['SPGR','MPRAGE','anat','anatomy'],
    'dti': ['DTI'],
    'field_map': ['fieldmap','TE7','B0']
}
neural.dicom.cluster_files(file_dict)[source]

takes output from scan_dir() and organizes into lists of files with the same tags

returns a dictionary where values are a tuple of the unique tag combination and values contain another dictionary with the keys info containing the original tag dict and files containing a list of files that match

neural.dicom.create_dset(directory, slice_order='alt+z', sort_order='zt', force_slices=None)[source]

tries to autocreate a dataset from images in the given directory

neural.dicom.create_dset_to3d(prefix, file_list, file_order='zt', num_slices=None, num_reps=None, TR=None, slice_order='alt+z', only_dicoms=True, sort_filenames=False)[source]

manually create dataset by specifying everything (not recommended, but necessary when autocreation fails)

If num_slices or num_reps is omitted, it will be inferred by the number of images. If both are omitted, it assumes that this it not a time-dependent dataset

Only_dicoms:filter the given list by readable DICOM images
Sort_filenames:sort the given files by filename using the right-most number in the filename
neural.dicom.date_for_image(image_fname)[source]

returns date from DICOM header of image_fname as datetime object

neural.dicom.date_for_str(date_str)[source]

tries to guess date from ambiguous date string

neural.dicom.find_dups(file_dict)[source]

takes output from scan_dir() and returns list of duplicate files

neural.dicom.info(filename)[source]

returns a DicomInfo object containing the header information in filename

neural.dicom.info_for_tags(filename, tags)[source]

return a dictionary for the given tags in the header of the DICOM file filename

tags is expected to be a list of tuples that contains the DICOM address in hex values.

basically a rewrite of info() because it’s so slow. This is a lot faster and more reliable

neural.dicom.is_dicom(filename)[source]

returns Boolean of whether the given file has the DICOM magic number

neural.dicom.max_diff(dset1, dset2)[source]

calculates maximal voxel-wise difference in datasets (in %)

Useful for checking if datasets have the same data. For example, if the maximum difference is < 1.0%, they’re probably the same dataset

neural.dicom.organize_dir(orig_dir)[source]

scans through the given directory and organizes DICOMs that look similar into subdirectories

output directory is the orig_dir with -sorted appended to the end

neural.dicom.reconstruct_files(input_dir)[source]

sorts input_dir and tries to reconstruct the subdirectories found

neural.dicom.scan_dir(dirname, tags=None, md5_hash=False)[source]

scans a directory tree and returns a dictionary with files and key DICOM tags

return value is a dictionary absolute filenames as keys and with dictionaries of tags/values as values

the param tags is the list of DICOM tags (given as tuples of hex numbers) that will be obtained for each file. If not given, the default list is:

0008 0021:Series date
0008 0031:Series time
0008 103E:Series description
0008 0080:Institution name
0010 0020:Patient ID
0028 0010:Image rows
0028 0011:Image columns

If the param md5_hash is True, this will also return the MD5 hash of the file. This is useful for detecting duplicate files

neural.dicom.unpack_archive(fname, out_dir)[source]

unpacks the archive file fname and reconstructs datasets into out_dir

Datasets are reconstructed and auto-named using create_dset(). The raw directories that made the datasets are archive with the dataset name suffixed by tgz, and any other files found in the archive are put into other_files.tgz