Use Simpleitk to reading DICOM series In multiple folders

Hi all,
Maybe someone here would know-
I want to get the series IDs of each patient.
the sitk.ImageSeriesReader.GetGDCMSeriesIDs need all dicom files are included in the file_path. But, what if the folder structure is:

contents

  folders1
    img1.dcm
    img2.dcm
    ...
  folders2
    img1.dcm
    img2.dcm
  folders ...
  img1.dcm
  img2.dcm
  ...

How can I get the Series IDs for each folder and save all Series IDs to a key ?

path_home = ‘TCGA-train’
path_dir = os.path.join(path_home,‘dataset’)
print (path_dir), len(path_dir)
paths = [path_dir +"/"+ x for x in os.listdir(path_dir)]

imgOriginal = {}
print (paths)

for k in range(len(num_count)):

reader = sitk.ImageSeriesReader()
filenamesDICOM = reader.GetGDCMSeriesFileNames(paths[k])
reader.SetFileNames(filenamesDICOM)
idx_slash_paths = paths[k].rfind('/')
idx_patient = paths[k][idx_slash_paths+1:]

imgOriginal[idx_patient] = reader.Execute()
print (idx_patient)
break

print (imgOriginal.keys())

Hello @kelly90802,

This code does what you want plus some other stuff. The inspect_series function traverses the directory structure and collects files into their respective study-series. Then the inspect_single_series reads a given series. Note that inspect_single_series accommodates repeated use of the same file names in a series so that there is no issue reading a series with files folder1/img1.dcm and folder2/img1.dcm.

1 Like