{"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":"## Extension of R Kwiatkowski's work: [What are .DCM and .NII files and how to read them](https://www.kaggle.com/code/datark1/what-are-dcm-and-nii-files-and-how-to-read-them)","metadata":{}},{"cell_type":"code","source":"import matplotlib.pylab as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:01:25.672012Z","iopub.execute_input":"2022-08-15T16:01:25.674074Z","iopub.status.idle":"2022-08-15T16:01:25.706554Z","shell.execute_reply.started":"2022-08-15T16:01:25.673951Z","shell.execute_reply":"2022-08-15T16:01:25.705252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **What are .nii files?**\n\nNII format is created by **Neuroimaging Informatics Technology Initiative**. It is commonly used to store magnetic resonance imaging (MRI) data.\n\n## **How to open it?**\nTo open files in this standard we can use [nibabel](https://nipy.org/nibabel/gettingstarted.html) library.","metadata":{}},{"cell_type":"code","source":"import nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:01:25.709863Z","iopub.execute_input":"2022-08-15T16:01:25.710259Z","iopub.status.idle":"2022-08-15T16:01:25.9921Z","shell.execute_reply.started":"2022-08-15T16:01:25.710208Z","shell.execute_reply":"2022-08-15T16:01:25.990811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.10921.nii'\n\nimg = nib.load(path).get_fdata()\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:01:25.993804Z","iopub.execute_input":"2022-08-15T16:01:25.994852Z","iopub.status.idle":"2022-08-15T16:01:27.552536Z","shell.execute_reply.started":"2022-08-15T16:01:25.994796Z","shell.execute_reply":"2022-08-15T16:01:27.550797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize segmentations in Sagittal plane\n\n- use first dimension to get sagittal\n- take slices every 10 voxels\n- flip and rotate so that C1 is at the top\n- each vertebrae is shown as a separate color label","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(4,4, figsize=(12,12))\n#starting point 70 pixels before midline\nslice = int(img.shape[0]/2 - 70)\nfor i, ax in enumerate(axes.reshape(-1)):\n    #use first dimension to get sagittal views\n    arr = img[slice + (i*10),:,:]\n    #flip and rotate so that C1 is at the top\n    arr = np.flip(arr, 0)\n    arr = np.rot90(arr)\n    ax.imshow(arr)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:04:08.638363Z","iopub.execute_input":"2022-08-15T16:04:08.638826Z","iopub.status.idle":"2022-08-15T16:04:10.847666Z","shell.execute_reply.started":"2022-08-15T16:04:08.638789Z","shell.execute_reply":"2022-08-15T16:04:10.846501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}