{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This kernel uses TorchIO to convert folders of DICOM MRI scans into a normalized, resized, and rotated NIfTI file. \n\nThis is part of a larger solution found at: https://www.kaggle.com/ohbewise/a-rsna-mri-solution-from-dicom-to-submission","metadata":{}},{"cell_type":"code","source":"!pip install --quiet torchio","metadata":{"execution":{"iopub.status.busy":"2021-09-24T17:16:32.432915Z","iopub.execute_input":"2021-09-24T17:16:32.433698Z","iopub.status.idle":"2021-09-24T17:16:51.192923Z","shell.execute_reply.started":"2021-09-24T17:16:32.433592Z","shell.execute_reply":"2021-09-24T17:16:51.191741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torchio as tio\nfrom pathlib import Path\n\n# Parameters to limit the processing power needed.\ndemo  = False # if True limits to 10 patients\nscan_types    = ['FLAIR','T1w','T1wCE','T2w'] # uses all scan types","metadata":{"execution":{"iopub.status.busy":"2021-09-24T17:16:51.194675Z","iopub.execute_input":"2021-09-24T17:16:51.194983Z","iopub.status.idle":"2021-09-24T17:16:53.386116Z","shell.execute_reply.started":"2021-09-24T17:16:51.194933Z","shell.execute_reply":"2021-09-24T17:16:53.385025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir   = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/'\nout_dir    = './processed'\n\nfor dataset in ['train']:\n    dataset_dir = f'{data_dir}{dataset}'\n    patients = os.listdir(dataset_dir)\n    if demo:\n        patients = patients[:10]\n    \n    # Remove cases the competion host said to exclude \n    # https://www.kaggle.com/c/rsna-miccai-brain-tumor-radiogenomic-classification/discussion/262046\n    if '00109' in patients: patients.remove('00109')\n    if '00123' in patients: patients.remove('00123')\n    if '00709' in patients: patients.remove('00709')\n    \n    print(f'Total patients in {dataset} dataset: {len(patients)}')\n\n    count = 0\n    for patient in patients:\n        count = count + 1\n        print(f'{dataset}: {count}/{len(patients)}')\n\n        for scan_type in scan_types:\n            scan_src  = f'{dataset_dir}/{patient}/{scan_type}/'\n            scan_dest = f'{out_dir}/{dataset}/{patient}/{scan_type}/'\n            Path(scan_dest).mkdir(parents=True, exist_ok=True)\n            image = tio.ScalarImage(scan_src)\n            transforms = [\n                tio.ToCanonical(),\n                tio.Resample(1),\n                tio.ZNormalization(masking_method=tio.ZNormalization.mean),\n                tio.CropOrPad((128,128,64)),\n                tio.RescaleIntensity((-1, 1)),\n            ]\n            transform = tio.Compose(transforms)\n            preprocessed = transform(image)\n            preprocessed.save(f'{scan_dest}/{scan_type}.nii.gz')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}