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
-
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 returnNone. Alternatively, you can supply your own string,image_label, and it will try to match that.label_dictis 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 tagsreturns a dictionary where values are a tuple of the unique tag combination and values contain another dictionary with the keys
infocontaining the original tag dict andfilescontaining 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_fnameas datetime object
-
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
return a dictionary for the given
tagsin the header of the DICOM filefilenametagsis 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_dirwith-sortedappended to the end
-
neural.dicom.reconstruct_files(input_dir)[source]¶ sorts
input_dirand 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
tagsis 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_hashisTrue, 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
fnameand reconstructs datasets intoout_dirDatasets are reconstructed and auto-named using
create_dset(). The raw directories that made the datasets are archive with the dataset name suffixed bytgz, and any other files found in the archive are put intoother_files.tgz