brianmanderson/Dicom_RT_and_Images_to_Mask

Indexes with ROIs

Closed this issue · 7 comments

Hello,

I just wanted to clarify, does the reading in of the DICOM files (CT and RT) with Dicom_reader.walk_through_folders(DICOM_path) guarantee the order of the indexes ? Meaning are the returned indexes from command Dicom_reader.which_indexes_have_all_rois() correspond to the actual time positioning from the CT acquisition ? Are they basically ordered according to UID and timestamp ?

Why I'm asking is: I have 4DCTs with GTVs marked throughout multiple breathing phases (usually 10 or sometimes more). In our case, breathing phase corresponds to RT file. So we create an association class with gtvs and getting all the indexes with ROIs (our breathing phases). We later on extract for each breathing phase the 3D numpy arrays of the whole chest area (the full available image) and the 3D numpy arrays of the mask (where the GTV/tumor is marked) and we use those 3D numpy arrays for deep learning purposes. So for us it is important, that there is a spatio-temporal relationship between each of these 3D numpy arrays corresponding to a breathing phase. And that is why I'm asking if lets say 1st index returned is actually the first breathing phase (the patient just starts taking a deep breath) and the 2nd index is second breathing phase (patient takes a bit more air in).

Here is what I'm talking about (this gif is created from the arrays extracted and using the order of indexes as I mentioned):

movie

Hi Darius,

Thanks for reaching out! First off, the indexes do not correspond to any aspect of the DICOM, they are completely arbitrary numbers which iterate from 0 up to the total number of unique DICOM images found.

What I would recommend you do, is sort the indexes based on the series description. When a 4D is acquired there is typically a % phase (0-90) in the series description. You can identify which indexes associate with each of the phases and then load the desired phases in your wanted order.

You could also add whatever DICOM tag you want with the 'image_sitk_string_keys'
image_sitk_string_keys = {"MyPatientName": "0010|0010"}
Check out the ReadMe file for an example

Please reach out if this helps or not!

Brian

Hello Brian,

Thank you for such a fast response.

I've just checked and it seems, that there isn't a series description tag in our case. Probably not filled in.

However, the structure the way 4DCT comes for a patient is already sorted in a way. I've attached two types of folder structures that we are dealing with:

structure_folder_dicom
alternative_structure

Also here is a gif of the first file structure from the screenshots above for that patient (seems like correct order, just running plain walk_through_folders):

output

So maybe, if running walk_through_folders on a Patient folder, containing one of these structures, would cover the case that I mentioned above (correct order of breathing phases) ?

As additional information, in each of those folders in the screenshot (CT 1 or 0% for example), there is one RS file (not RT!) and multiple CT files (100-500).

Best,

Darius

Hi Darius,

Sorry for the delayed response. What do you mean by RS file? I assume you mean structure file?

The program should function with this. Each of your 4DCTs will have the same frame of reference, BUT the structure should be tied to the image series instance UID, which is unique.

I'd recommend first running the walk_through_folders with the images, and then walk_through_folders through the structures folders. It ought to properly connect the two

Please tell me if that helps!
Brian

Also, if you are trying to order them based on index, you could look at the 'Description'

Could do it this way:

for phase_image in reader.series_instances_dictionary.values():
    phase = phase_image.Description # Assuming it will save "0%/10%/20%" etc.

Then sort them based on the phase

was this helpful at all?

Thank you for the answer and sorry for the delayed response!

So to ensure the correct order, what I did was this:

  1. I iterate over the folder of patients, which contains the patients (example -> patient_id_1 and inside are the subfolders of CT1,CT2 and so on)
  2. For each patient, I sort the inner folder names (manually in a sense) (initial was lets say CT1 , CT4,CT2 -> sorted -> CT1,CT2,CT4)
  3. Then I go through the sorted list and for each of the folders (example -> CT1) I create a DicomReaderWriter object and pass them through walk_through_folders.

This way the order is ensured.

Example through code:

` sorted_directories = sorted(directories, key=self.extract_number)
print("Sorted directory names:", sorted_directories)

    for el in sorted_directories:
        print("PROCESSING DIRECTORY/BREATHING PHASE:", el) 
        self._dicom_reader = DicomReaderWriter(description=self._patient_id, arg_max=True)
        self._dicom_reader.walk_through_folders(os.path.join(self._subfolder_path, el))`

Thank you for the help once again !

That's great! And no worries, the apology is all mine for taking so long in my response.

I'm glad everything is working =), and thanks for sharing how you solved this problem

Will close this for now, please open it if anything else comes up!