Reading .dcm files from a nested directory

Question:

This is the form of my nested directory:

/data/patient_level/study_level/series_level/

For each patient_level folder, I need to read the “.dcm” files from the corresponding “series_level” folder.

How can I access the “.dcm” file in the “series_level” folders?

I need 3 features from a DICOM object.

This is my source code:

import dicom
record = dicom.read_file("/data/patient_level/study_level/series_level/000001.dcm")
doc = {"PatientID": record.PatientID, "Manufacturer": record.Manufacturer, "SeriesTime": record.SeriesTime}

Then, I will insert this doc to a Mongo DB.

Any suggestions is appreciated.
Thanks in advance.

Asked By: user9439906

||

Answers:

It is not quite clear the problem you are trying to solve, but if all you want is to get a list of all .dcm files from that the data directory, you can use pathlib.Path():

from pathlib import Path

data = Path('/data')
list(data.rglob('*.dcm'))

To split a file in its components, you can use something like this:

dcm_file = '/patient_level/study_level/series_level/000001.dcm'
_, patient_level, study_level, series_level, filename = dcm_file.split('/')

Which would give you patient_level, study_level, series_level, and filename from dcm_file.

But it would be better to stick with Path() and its methods, like this:

dcm_file = Path('/patient_level/study_level/series_level/000001.dcm')
dcm_file.parts

Which would give you something like this:

('/', 'patient_level', 'study_level', 'series_level', '000001.dcm')

Those are just starting points anyway.

Answered By: accdias
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.