code: def read_dicom_dir(
      dicom_path,
      sort_instance=True,
      sort_patient=False,
      check_series_uid=False,
  ):
      """Read dicom files for multiple patients and multiple instances / phases from a given directory arranged in the
          following structure:
  
              root/patient_a/.../phase_1.dcm
              root/patient_a/.../phase_2.dcm
              root/patient_a/.../phase_3.dcm
  
              root/patient_b/.../phase_1.dcm
              root/patient_b/.../phase_2.dcm
              root/patient_b/.../phase_3.dcm
  
              root/patient_m/.../phase_1.dcm
              root/patient_m/.../phase_2.dcm
              root/patient_m/.../phase_3.dcm
  
      Args:
          dicom_path (str): Directory of DICOM files.
          sort_instance (bool, optional): Whether sort images by InstanceNumber (i.e. phase number) for each subject.
              Defaults to True.
          sort_patient (bool, optional): Whether sort subjects' images by PatientID. Defaults to False.
          check_series_uid (bool, optional): Whether check if all series UIDs are the same. Defaults to False.
  
      Returns:
          [list[list]]: [a list of dicom dataset lists]
      """
      patient_dirs = [
          os.path.join(dicom_path, patient_dir)
          for patient_dir in os.listdir(dicom_path)
      ]
      patient_dirs = filter(os.path.isdir, patient_dirs)
      dcm_patients = []  # list of dicom dataset patient lists
  
      for patient_dir in patient_dirs:
          patient_dcm_list = read_dicom_phases(
              patient_dir, sort_instance
          )
          if check_series_uid:
              patient_dcm_list = check_dicom_series_uid(
                  patient_dcm_list, sort_instance
              )
              for dcm_series_instance in patient_dcm_list:
                  dcm_patients.append(dcm_series_instance)
          else:
              dcm_patients.append(patient_dcm_list)
  
      if sort_patient:
          dcm_patients.sort(key=lambda x: x[0].PatientID, reverse=False)
  
      return dcm_patients
  
imports: import glob
  import logging
  import os
  from enum import Enum
  import numpy
  import pydicom
  import torch
  from torchvision import datasets, transforms
  from kale.loaddata.dataset_access import DatasetAccess
  from kale.loaddata.mnistm import MNISTM
  from kale.loaddata.multi_domain import MultiDomainAccess, MultiDomainImageFolder
  from kale.loaddata.usps import USPS
  from kale.prepdata.image_transform import get_transform
  from kale.utils.download import download_file_by_url
label_desc: Use image node_id 4 for calling a global function with example usage: read_dicom_dir(dicom_path, sort_instance, sort_patient, check_series_uid) and returns: dcm_patients