Converting a set of dicom files of one serie to nrrd using python API

Hi. I wonder if anyone can help me with this. I am using about 400 studies (each study includes couple of series. The DICOM SEG file is also included. Imagine that I have downloaded all the data and I have found the serie to which the segmentation has been applied. Now, for each study I have a set of CT DICOM files that should be converted to one nrrd and an associted DICOM SEG file that should be converted to nrrd so that they can be used in Pyradiomics to extract the radiomics features. I know how to convert dicom files to nrrd in slicer but if I want to do that I have to do one by one for each study and it does not make sense. I want to know how I can import a set of dicom files (or one dicom SEG file) in a python script and convert (save) it to/as one nrrd file so that it can be used in pyradiomics. This way I can import the data related to all 400 studies, convert them to nrrd files and extract radiomics using one python script. Could you please help me with it?

A general problem is that there’s a lot more flexibility in the SEG+DICOM CT representation than there is in the nrrd version as needed for radiomics. That is, the SEG could be discontinuous or irregularly spaced compared to the CT, which itself may in general have geometric inconsistencies. So at some level you have to “know your data” to be sure it will work for your intended use.

That said, the process should be pretty well defined and even the edge cases should be handled by Slicer. I’d start by importing all the data into the dicom database. Then you can iterate by patient/study (something like this) and find the SEG for each study. It should reference the instance UIDs of the source images which should be the CT and you can import both into Slicer. If there are inconsistencies in spacing or other geometry you can harden any acquisition transform and resample to a common space. From there you can export to nrrd and run PyRadiomics.

Thank you so much for the reply. So, you think it is even better to upload the CT dicom images + the associated SEG files for each study one by one for a better analysis and then save them as nrrd to be able to use pyradiomics, right?

If I follow your question correctly I think yes, you should treat each study independently as part of the iteration through the dicom database. Most likely each study will follow the same pattern and the SEG will correspond to the CT such that you can easily export the loaded volumes as nrrd, ready for PyRadiomics. If there are issue, like irregular spacing, you could post examples of anything that arises, ideally with some sample datasets illustrating any problems.

Thank you for the answer. I am looking for a more automated way to convert dicom files to nrrd in order to be used in pyradiomcs. I do not want to use slicer to convert them to nrrd. I need the dicom files to be converted in my python script. Is there any way?

You can use a loop like this to iterate through all the data in your local dicom database. This makes it easy to sort a large number of dicom studies and provides a framework for adding logic to identify the series of interest that you want to do further processing on:

Another option is just to run dcm2niix or similar and sort through the results. There are also python packages to convert dicom to other formats like nrrd, but keep in mind that dicom can be messy and it’s not in general a one-to-one mapping from dicom series to a scalar volume.