
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/input_output/plot_read_dicom_directory.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_input_output_plot_read_dicom_directory.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_input_output_plot_read_dicom_directory.py:


=======================
Read a DICOMDIR dataset
=======================

This example shows how to read a DICOM :File-set's DICOMDIR dataset.

.. note::

    The :class:`~pydicom.fileset.FileSet` class is a much better way of working
    with DICOM File-sets and allows creation and modification of DICOMDIR
    files. See the :doc:`DICOM File-set example<plot_read_fileset>` or the
    `File-set tutorial <../../tutorials/filesets.html>`_.

.. GENERATED FROM PYTHON SOURCE LINES 16-82




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Root directory: /build/reproducible-path/pydicom-2.4.3/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






|

.. code-block:: Python


    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)


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.009 seconds)


.. _sphx_glr_download_auto_examples_input_output_plot_read_dicom_directory.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_read_dicom_directory.ipynb <plot_read_dicom_directory.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_read_dicom_directory.py <plot_read_dicom_directory.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_read_dicom_directory.zip <plot_read_dicom_directory.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
