{"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":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>1 Intro/Context</b></p>\n</div>\n\nThis notebook is an attempt at stacking all dcm files of one patient to create an nii file to extract the sagittal view. This is done because not all patients have segmentation data (no nii file) and therefore vertebrae detection needs to be approached differently.\n\nFor the full EDA check out: https://www.kaggle.com/code/andir16/rsna-2022-cervical-spine-fracture-detection-eda\n\nI am still tweaking and working on it. But I already wanted to share the progress with you.\n\nFeedback and input is very much appreciated. ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>2 Libraries / Setup</b></p>\n</div>\n","metadata":{}},{"cell_type":"code","source":"!pip install dicom2nifti","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:23.392888Z","iopub.execute_input":"2022-09-14T19:01:23.393886Z","iopub.status.idle":"2022-09-14T19:01:41.574331Z","shell.execute_reply.started":"2022-09-14T19:01:23.393761Z","shell.execute_reply":"2022-09-14T19:01:41.572736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pydicom\nimport dicom2nifti\nimport pandas as pd\nimport nibabel as nib\nimport SimpleITK as sitk\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom pydicom.datadict import dictionary_VR\n","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:41.57743Z","iopub.execute_input":"2022-09-14T19:01:41.577936Z","iopub.status.idle":"2022-09-14T19:01:42.544761Z","shell.execute_reply.started":"2022-09-14T19:01:41.577883Z","shell.execute_reply":"2022-09-14T19:01:42.543403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<hr>","metadata":{}},{"cell_type":"code","source":"patient_id = \"1.2.826.0.1.3680043.10449\"\nbase_path = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/\"","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.546205Z","iopub.execute_input":"2022-09-14T19:01:42.546588Z","iopub.status.idle":"2022-09-14T19:01:42.551733Z","shell.execute_reply.started":"2022-09-14T19:01:42.546553Z","shell.execute_reply":"2022-09-14T19:01:42.550561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>3 DICOM Metadata</b></p>\n</div>\n\nWe will use the [dicom2nifti](https://dicom2nifti.readthedocs.io/en/latest/index.html) library to convert the dicom images to one nii image. However, dicom2nifti needs specific values in the metadata, one of which is the type of scan (modality) that the image contains. This is not included in the metadata of the training images.","metadata":{}},{"cell_type":"code","source":"example_path = f\"{base_path}{patient_id}/1.dcm\"\n\ndcm_example = pydicom.dcmread(example_path)\ndcm_example","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.554243Z","iopub.execute_input":"2022-09-14T19:01:42.554866Z","iopub.status.idle":"2022-09-14T19:01:42.591193Z","shell.execute_reply.started":"2022-09-14T19:01:42.554833Z","shell.execute_reply":"2022-09-14T19:01:42.59008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<hr>","metadata":{}},{"cell_type":"markdown","source":"The modality entry is (0008, 0060) [(DICOM Modalities)](http://dicomlookup.com/modalities.asp) and the dictionary entry is \"CS\". ","metadata":{}},{"cell_type":"code","source":"dictionary_VR([0x0008, 0x0060])","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.592755Z","iopub.execute_input":"2022-09-14T19:01:42.593083Z","iopub.status.idle":"2022-09-14T19:01:42.599193Z","shell.execute_reply.started":"2022-09-14T19:01:42.593054Z","shell.execute_reply":"2022-09-14T19:01:42.598185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"With this information we can add the modality entry for the CT scan to the metadata:","metadata":{}},{"cell_type":"code","source":"dcm_example.add_new([0x0008, 0x0060], \"CS\", \"CT\")\ndcm_example","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.600627Z","iopub.execute_input":"2022-09-14T19:01:42.600963Z","iopub.status.idle":"2022-09-14T19:01:42.612501Z","shell.execute_reply.started":"2022-09-14T19:01:42.600918Z","shell.execute_reply":"2022-09-14T19:01:42.61149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>4 Edit/Export DICOM Files</b></p>\n</div>\n\nBecause we need to add the modality information to all dcm images, a working folder is created and all images for one patient are edited (adds the modality entry) and exported to that folder. ","metadata":{}},{"cell_type":"code","source":"export_dir = \"/kaggle/working/dcm_export\"\n\n# Create export folder for one patient\nos.mkdir(export_dir)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.613889Z","iopub.execute_input":"2022-09-14T19:01:42.614255Z","iopub.status.idle":"2022-09-14T19:01:42.623191Z","shell.execute_reply.started":"2022-09-14T19:01:42.614224Z","shell.execute_reply":"2022-09-14T19:01:42.622282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_nr = 1\n\npatient_path = f\"{base_path}{patient_id}/\"\n\nfor filename in os.listdir(patient_path):\n    path = patient_path + str(image_nr) + \".dcm\"\n    dcm_example = pydicom.dcmread(path)\n    dcm_example.add_new([0x0008, 0x0060], \"CS\", \"CT\")\n    pydicom.filewriter.write_file(f\"{export_dir}/{image_nr}.dcm\", dcm_example, write_like_original=True)\n    image_nr += 1\n\nprint(f\"Saved {image_nr-1} images\")","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:42.624811Z","iopub.execute_input":"2022-09-14T19:01:42.625475Z","iopub.status.idle":"2022-09-14T19:01:50.317878Z","shell.execute_reply.started":"2022-09-14T19:01:42.625442Z","shell.execute_reply":"2022-09-14T19:01:50.316743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>5 Create nii File</b></p>\n</div>\n\nWe use [SimpleITK](https://simpleitk.org/) to create the nii file from the exported dcm images, then import the created nii file and prepare it according to the post by [Harshit Sheoran](https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/340612)","metadata":{}},{"cell_type":"code","source":"reader = sitk.ImageSeriesReader()\ndicom_names = reader.GetGDCMSeriesFileNames(export_dir)\nreader.SetFileNames(dicom_names)\nimage = reader.Execute()\nimage = sitk.PermuteAxes(image, [2, 1, 0])\n\nsitk.WriteImage(image, f\"{patient_id}.nii.gz\")","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:01:50.319357Z","iopub.execute_input":"2022-09-14T19:01:50.319701Z","iopub.status.idle":"2022-09-14T19:02:15.344439Z","shell.execute_reply.started":"2022-09-14T19:01:50.319671Z","shell.execute_reply":"2022-09-14T19:02:15.342197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/340612\nexample_path_nii = f\"{patient_id}.nii.gz\"\nnii_example = nib.load(example_path_nii)\ndata = nii_example.get_fdata()\n\ndata_transposed = data[:, ::-1, ::-1].transpose(2, 1, 0)\ndata_transposed.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:02:15.3492Z","iopub.execute_input":"2022-09-14T19:02:15.34963Z","iopub.status.idle":"2022-09-14T19:02:18.716223Z","shell.execute_reply.started":"2022-09-14T19:02:15.349592Z","shell.execute_reply":"2022-09-14T19:02:18.715321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>6 Display Sagittal View</b></p>\n</div>\n\nWe define the middle slice (based on the number of images) and display this slice to get the sagittal view.","metadata":{}},{"cell_type":"code","source":"middle_point = int(data_transposed.shape[2] / 2)\nprint(middle_point)","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:02:18.717368Z","iopub.execute_input":"2022-09-14T19:02:18.717961Z","iopub.status.idle":"2022-09-14T19:02:18.724494Z","shell.execute_reply.started":"2022-09-14T19:02:18.71792Z","shell.execute_reply":"2022-09-14T19:02:18.723076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,15))\n\nplt.imshow(data_transposed[middle_point], cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-09-14T19:02:18.725857Z","iopub.execute_input":"2022-09-14T19:02:18.726191Z","iopub.status.idle":"2022-09-14T19:02:19.337569Z","shell.execute_reply.started":"2022-09-14T19:02:18.726162Z","shell.execute_reply":"2022-09-14T19:02:19.335869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;\n            background-color:#48AFFF;font-size:160%;\n            font-family:Arial\">\n    <p style=\"padding: 4px;color:white;\"><b>7 References</b></p>\n</div>\n\n* [RSNA 2022 Cervical Spine Fracture Detection - EDA](https://www.kaggle.com/code/andir16/rsna-2022-cervical-spine-fracture-detection-eda) by [Andreas Renz](https://www.kaggle.com/andir16)\n* [Explaining Data and Submission in detail](https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/340612) by [Harshit Sheoran](https://www.kaggle.com/harshitsheoran)","metadata":{}}]}