DicomReader#
- class medicalimageanalysis.read.DicomReader(files, only_tags, only_modality, only_load_roi_names, clear)[source]
Bases:
objectMain DICOM pipeline: read, group, reconstruct and register.
Reads files on a bounded thread pool, keeps the SOP classes listed in only_modality, groups slices into volumes and hands each group to the matching reader class. Images are created first so RTSTRUCT, REG and RTDOSE can be matched to them, then everything is sorted by date.
- Parameters:
files (dict) – Categorized file paths with a ‘Dicom’ key (output of file_parser).
only_tags (bool) – If True, load metadata only (no pixel arrays). Geometry is still computed.
only_modality (list of str or None) – Modalities to process. None means [‘CT’, ‘MR’, ‘PT’, ‘US’, ‘DX’, ‘RF’, ‘TOMOSYNTHESIS’, ‘CR’, ‘RTSTRUCT’, ‘REG’, ‘RTDOSE’].
only_load_roi_names (list of str or None) – ROI names to keep from RTSTRUCT files.
clear (bool) – If True, clears the global Data registry before loading.
- ds
Every dataset read (failed reads appear as empty lists).
- Type:
- ds_modality
Modality -> list of datasets (2D/RT) or list of slice groups (3D).
- Type:
Examples
Load a patient through the reader directly:
>>> import medicalimageanalysis as mia >>> from medicalimageanalysis import Data >>> from medicalimageanalysis.read.dicom import DicomReader >>> files = mia.file_parser(folder_path='C:/Data/Patient01') >>> reader = DicomReader(files, only_tags=False, only_modality=None, ... only_load_roi_names=None, clear=True) >>> reader.load() >>> Data.image_list() ['CT 01', 'MR 02']
Inspect the grouping after a load:
>>> {key: len(groups) for key, groups in reader.ds_modality.items() if groups} {'CT': 1, 'MR': 1, 'RTSTRUCT': 1, 'REG': 1}
The read_dicoms wrapper builds the file dictionary for you:
>>> mia.read_dicoms(folder_path='C:/Data/Patient01')
- load(display_time=False)[source]
Run the full pipeline: read -> group -> create -> sort by date.
- Parameters:
display_time (bool) – If True, print the total runtime.
- Return type:
None
Examples
Load a folder and time it:
>>> import medicalimageanalysis as mia >>> from medicalimageanalysis import Data >>> from medicalimageanalysis.read.dicom import DicomReader >>> files = mia.file_parser(folder_path='C:/Data/Patient01') >>> reader = DicomReader(files, False, None, None, True) >>> reader.load(display_time=True) Dicom Read Time: 3.41 >>> Data.image_list() ['CT 01', 'MR 02']
- read()[source]
Read every file in files[‘Dicom’] into self.ds with a thread pool.
Uses a bounded ThreadPoolExecutor (Python’s default worker count, min(32, CPU count + 4)), so a study with thousands of files does not start thousands of threads. Unreadable files are stored as empty lists and skipped later. self.ds keeps the input file order.
- Return type:
None
Examples
>>> from medicalimageanalysis.read.dicom import DicomReader >>> reader = DicomReader({'Dicom': ['C:/Data/ct_001.dcm', 'C:/Data/ct_002.dcm']}, ... True, ['CT'], None, False) >>> reader.read() >>> len(reader.ds), reader.ds[0].Modality (2, 'CT')
Results line up with the input paths:
>>> reader.ds[1].filename 'C:/Data/ct_002.dcm'
- separate_modalities_and_images()[source]
Group the read datasets into self.ds_modality.
US, DX, RF, CR, TOMOSYNTHESIS, RTSTRUCT, REG and RTDOSE: one entry per dataset.
CT, MR, PT: one entry per volume. Slices are split by SeriesInstanceUID, then orientation (rounded to 3 decimals), then AcquisitionNumber, and sorted along the acquisition plane. Acquisitions that tile without overlapping and with equal gaps are merged back into one volume; otherwise each stays its own entry.
Slices missing ImageOrientationPatient or ImagePositionPatient are dropped.
- Return type:
None
Examples
A CT series and a two-orientation MR series become three volumes:
>>> from medicalimageanalysis.read.dicom import DicomReader >>> reader = DicomReader(files, False, ['CT', 'MR'], None, True) >>> reader.read() >>> reader.separate_modalities_and_images() >>> len(reader.ds_modality['CT']), len(reader.ds_modality['MR']) (1, 2) >>> len(reader.ds_modality['CT'][0]) 142
- image_creation()[source]
Turn the grouped datasets into registered objects.
Order matters so the RT objects can find their images:
CT/MR/PT -> Read3D; DX/CR/TOMOSYNTHESIS -> ReadXRay; RF -> ReadRF; US -> ReadUS
RTSTRUCT -> ReadRTStruct, attached via Image.input_rtstruct (a message is printed when no matching image is found)
REG -> ReadREG
RTDOSE -> ReadRTDose
- Return type:
None
Examples
>>> from medicalimageanalysis import Data >>> from medicalimageanalysis.read.dicom import DicomReader >>> reader = DicomReader(files, False, None, None, True) >>> reader.read() >>> reader.separate_modalities_and_images() >>> reader.image_creation() >>> Data.image_list(), Data.dose_list() (['CT 01', 'MR 02'], ['RTDOSE 01'])