Note
Click here to download the full example code
Read a DICOMDIR dataset¶
This example shows how to read a DICOM :File-set’s DICOMDIR dataset.
Note
The FileSet
class is a much better way of working
with DICOM File-sets and allows creation and modification of DICOMDIR
files. See the DICOM File-set example or the
File-set tutorial.
Out:
Root directory: /home/circleci/project/pydicom/data/test_files/dicomdirtests
PATIENT: PatientID=77654033, PatientName=Doe^Archibald
STUDY: StudyID=2, StudyDate=20010101, StudyDescription=XR C Spine Comp Min 4 Views
SERIES: SeriesNumber=1, Modality=CR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=77654033/CR1/6154
SERIES: SeriesNumber=2, Modality=CR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=77654033/CR2/6247
SERIES: SeriesNumber=3, Modality=CR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=77654033/CR3/6278
STUDY: StudyID=2, StudyDate=19950903, StudyDescription=CT, HEAD/BRAIN WO CONTRAST
SERIES: SeriesNumber=2, Modality=CT, SeriesDescription=(no value available) - 4 SOP Instances
IMAGE: Path=77654033/CT2/17106
IMAGE: Path=77654033/CT2/17136
IMAGE: Path=77654033/CT2/17166
IMAGE: Path=77654033/CT2/17196
PATIENT: PatientID=98890234, PatientName=Doe^Peter
STUDY: StudyID=2, StudyDate=20010101, StudyDescription=(no value available)
SERIES: SeriesNumber=4, Modality=CT, SeriesDescription=(no value available) - 2 SOP Instances
IMAGE: Path=98892001/CT2N/6293
IMAGE: Path=98892001/CT2N/6924
SERIES: SeriesNumber=5, Modality=CT, SeriesDescription=(no value available) - 5 SOP Instances
IMAGE: Path=98892001/CT5N/2062
IMAGE: Path=98892001/CT5N/2392
IMAGE: Path=98892001/CT5N/2693
IMAGE: Path=98892001/CT5N/3023
IMAGE: Path=98892001/CT5N/3353
STUDY: StudyID=428, StudyDate=20030505, StudyDescription=Carotids
SERIES: SeriesNumber=1, Modality=MR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=98892003/MR1/15820
SERIES: SeriesNumber=2, Modality=MR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=98892003/MR2/15970
STUDY: StudyID=134, StudyDate=20030505, StudyDescription=Brain
SERIES: SeriesNumber=1, Modality=MR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=98892003/MR1/4919
SERIES: SeriesNumber=2, Modality=MR, SeriesDescription=(no value available) - 3 SOP Instances
IMAGE: Path=98892003/MR2/4950
IMAGE: Path=98892003/MR2/5011
IMAGE: Path=98892003/MR2/4981
STUDY: StudyID=2, StudyDate=20030505, StudyDescription=Brain-MRA
SERIES: SeriesNumber=1, Modality=MR, SeriesDescription=(no value available) - 1 SOP Instance
IMAGE: Path=98892003/MR1/5641
SERIES: SeriesNumber=2, Modality=MR, SeriesDescription=(no value available) - 3 SOP Instances
IMAGE: Path=98892003/MR2/6935
IMAGE: Path=98892003/MR2/6605
IMAGE: Path=98892003/MR2/6273
SERIES: SeriesNumber=700, Modality=MR, SeriesDescription=(no value available) - 7 SOP Instances
IMAGE: Path=98892003/MR700/4558
IMAGE: Path=98892003/MR700/4528
IMAGE: Path=98892003/MR700/4588
IMAGE: Path=98892003/MR700/4467
IMAGE: Path=98892003/MR700/4618
IMAGE: Path=98892003/MR700/4678
IMAGE: Path=98892003/MR700/4648
import os
from pathlib import Path
from pydicom import dcmread
from pydicom.data import get_testdata_file
# fetch the path to the test data
path = get_testdata_file('DICOMDIR')
ds = dcmread(path)
root_dir = Path(ds.filename).resolve().parent
print(f'Root directory: {root_dir}\n')
# Iterate through the PATIENT records
for patient in ds.patient_records:
print(
f"PATIENT: PatientID={patient.PatientID}, "
f"PatientName={patient.PatientName}"
)
# Find all the STUDY records for the patient
studies = [
ii for ii in patient.children if ii.DirectoryRecordType == "STUDY"
]
for study in studies:
descr = study.StudyDescription or "(no value available)"
print(
f"{' ' * 1}STUDY: StudyID={study.StudyID}, "
f"StudyDate={study.StudyDate}, StudyDescription={descr}"
)
# Find all the SERIES records in the study
all_series = [
ii for ii in study.children if ii.DirectoryRecordType == "SERIES"
]
for series in all_series:
# Find all the IMAGE records in the series
images = [
ii for ii in series.children
if ii.DirectoryRecordType == "IMAGE"
]
plural = ('', 's')[len(images) > 1]
descr = getattr(
series, "SeriesDescription", "(no value available)"
)
print(
f"{' ' * 2}SERIES: SeriesNumber={series.SeriesNumber}, "
f"Modality={series.Modality}, SeriesDescription={descr} - "
f"{len(images)} SOP Instance{plural}"
)
# Get the absolute file path to each instance
# Each IMAGE contains a relative file path to the root directory
elems = [ii["ReferencedFileID"] for ii in images]
# Make sure the relative file path is always a list of str
paths = [[ee.value] if ee.VM == 1 else ee.value for ee in elems]
paths = [Path(*p) for p in paths]
# List the instance file paths
for p in paths:
print(f"{' ' * 3}IMAGE: Path={os.fspath(p)}")
# Optionally read the corresponding SOP Instance
# instance = dcmread(Path(root_dir) / p)
# print(instance.PatientName)
Total running time of the script: ( 0 minutes 0.016 seconds)