<jupyter_start><jupyter_text>Dicom -> Nifti data conversionBefore making use of the atlas based segmentation tools in this repository, all data should be converted from Dicom to Nifti as a first step. This Jupyter Notebook demonstrates how this can be done.<jupyter_code>import sys
import os
sys.path.append('../../..')

import pydicom

from platipy.dicom.rtstruct_to_nifti.convert import convert_rtstruct

dicom_search_path = "/path/to/dicom/data" # Location where Dicom data is stored
data_path = './data' # Path where converted files should be placed<jupyter_output><empty_output><jupyter_text>**The following cell crawls the Dicom search path to find one series per directory***Note: this was designed to be run on data that was exported from MIM*<jupyter_code>data_objects = {}

for root, dirs, files in os.walk(dicom_search_path, topdown=False):
    for name in files:
        f = os.path.join(root, name)
        ds = pydicom.read_file(f, force=True)
        
        if not 'Modality' in ds:
            continue
        
        case = ds.PatientID.split(' ')[1]

        if not case in data_objects:
            data_objects[case] = {}
        
        p = f
        if ds.Modality == 'CT':
            p = os.path.dirname(f)
            
        do = {
            'seriesInstanceUID': ds.SeriesInstanceUID, 
            'path': p}
        data_objects[case][ds.Modality] = do
        
        break # Only read one file per dir<jupyter_output><empty_output><jupyter_text>**Now that all Dicom data has been found, convert it to Nifti**<jupyter_code>import SimpleITK as sitk

for case in data_objects:
    
    case_dir = os.path.join(data_path, case)
    if not os.path.exists(case_dir):
        os.makedirs(case_dir)
    do = data_objects[case]
   
    # Convert the CT and RT Struct
    ct_path = do['CT']['path']
    rtstruct_path = do['RTSTRUCT']['path']
    convert_rtstruct(ct_path, rtstruct_path, output_img='CT.nii.gz', output_dir=case_dir)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09753645047762695, 'fraction_numerical': 0.0005027652086475615, 'mean_word_length': 3.0365111561866125, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '648132', 'license': 'Apache-2.0', 'n_tokens_mistral': 653, 'n_tokens_neox': 645, 'n_words': 184, 'path': 'platipy/examples/sample/1_convert_data.ipynb', 'repo_name': 'SimonBiggs/platipy', 'size': '1989'}