{"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":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimport numpy as np\nfrom tqdm import tqdm\nimport os\nfrom matplotlib import pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:14:15.12013Z","iopub.execute_input":"2023-09-30T19:14:15.120995Z","iopub.status.idle":"2023-09-30T19:14:15.410838Z","shell.execute_reply.started":"2023-09-30T19:14:15.120953Z","shell.execute_reply":"2023-09-30T19:14:15.409818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = os.path.join('/kaggle/input','rsna-2022-cervical-spine-fracture-detection')","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:14:22.706012Z","iopub.execute_input":"2023-09-30T19:14:22.706928Z","iopub.status.idle":"2023-09-30T19:14:22.711256Z","shell.execute_reply.started":"2023-09-30T19:14:22.706885Z","shell.execute_reply":"2023-09-30T19:14:22.710516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:14:23.575383Z","iopub.execute_input":"2023-09-30T19:14:23.576257Z","iopub.status.idle":"2023-09-30T19:14:23.58442Z","shell.execute_reply.started":"2023-09-30T19:14:23.576226Z","shell.execute_reply":"2023-09-30T19:14:23.583479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('npy_segmentations')\n#creates a new directory under current working directory","metadata":{"execution":{"iopub.status.busy":"2023-09-26T03:44:08.588505Z","iopub.execute_input":"2023-09-26T03:44:08.588884Z","iopub.status.idle":"2023-09-26T03:44:08.593714Z","shell.execute_reply.started":"2023-09-26T03:44:08.588855Z","shell.execute_reply":"2023-09-26T03:44:08.592699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working')","metadata":{"execution":{"iopub.status.busy":"2023-09-26T03:50:40.063744Z","iopub.execute_input":"2023-09-26T03:50:40.064224Z","iopub.status.idle":"2023-09-26T03:50:40.072071Z","shell.execute_reply.started":"2023-09-26T03:50:40.064185Z","shell.execute_reply":"2023-09-26T03:50:40.070871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=os.path.join(base_dir,'segmentations')","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:14:28.042656Z","iopub.execute_input":"2023-09-30T19:14:28.043382Z","iopub.status.idle":"2023-09-30T19:14:28.047985Z","shell.execute_reply.started":"2023-09-30T19:14:28.043343Z","shell.execute_reply":"2023-09-30T19:14:28.047319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Reading npy\n# import numpy as np\n# from tqdm import tqdm\n\n# # load\n# path = './npy_segmentations'\n# dirs = os.listdir(path)   \n# for filename in tqdm(dirs):\n#     img=np.load(os.path.join(path,filename))\n    \n#     # show\n#     plt.figure(figsize=(16,9))\n#     num = 1\n#     queue = img.shape[2]\n#     x = 5\n#     y = 10\n#     for i in range(0,queue,8):#也可以取10等，you also can get the figure per 10 arrays\n#         img_arr=img[:,:,i]\n#         plt.subplot(x,y,num)\n#         plt.imshow(img_arr,cmap='gray')\n#         num+=1\n#         if num >= x*y:\n#             break\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-26T04:41:58.220567Z","iopub.execute_input":"2023-09-26T04:41:58.22103Z","iopub.status.idle":"2023-09-26T04:41:58.226473Z","shell.execute_reply.started":"2023-09-26T04:41:58.220996Z","shell.execute_reply":"2023-09-26T04:41:58.225354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load one sample nii file. Get nii array data using get_fdata()","metadata":{}},{"cell_type":"code","source":"study = '1.2.826.0.1.3680043.29425'\nstudy_nii = nib.load(os.path.join(base_dir,'segmentations/{}.nii'.format(study)))","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:35:46.900899Z","iopub.execute_input":"2023-09-30T19:35:46.901303Z","iopub.status.idle":"2023-09-30T19:35:46.91054Z","shell.execute_reply.started":"2023-09-30T19:35:46.90127Z","shell.execute_reply":"2023-09-30T19:35:46.909544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_data = study_nii.get_fdata()","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:35:48.55644Z","iopub.execute_input":"2023-09-30T19:35:48.556901Z","iopub.status.idle":"2023-09-30T19:35:48.763845Z","shell.execute_reply.started":"2023-09-30T19:35:48.556863Z","shell.execute_reply":"2023-09-30T19:35:48.762823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_data.shape # width, height, images","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:35:50.869315Z","iopub.execute_input":"2023-09-30T19:35:50.869907Z","iopub.status.idle":"2023-09-30T19:35:50.876528Z","shell.execute_reply.started":"2023-09-30T19:35:50.869875Z","shell.execute_reply":"2023-09-30T19:35:50.875446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_data[199]) #It doesn't show any bone information","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:19.786708Z","iopub.execute_input":"2023-09-30T19:36:19.787061Z","iopub.status.idle":"2023-09-30T19:36:19.798057Z","shell.execute_reply.started":"2023-09-30T19:36:19.787034Z","shell.execute_reply":"2023-09-30T19:36:19.797104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(seg_data[199])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:25.938586Z","iopub.execute_input":"2023-09-30T19:36:25.93947Z","iopub.status.idle":"2023-09-30T19:36:26.160525Z","shell.execute_reply.started":"2023-09-30T19:36:25.939434Z","shell.execute_reply":"2023-09-30T19:36:26.159498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Image is weird. The reason being, nii is in the format of width,height,images. Instead it should be images,height,width\n<br>\nPlease be aware that the NIFTI files consist of segmentation in the sagittal plane,\nwhile the DICOM files are in the axial plane. Please use the NIFTI header information to determine the appropriate orientation such that the DICOM images and segmentation match. Otherwise, you run the risk of having the segmentations flipped in the Z axis and mirrored in the X axis.","metadata":{}},{"cell_type":"code","source":"seg_data = study_nii.get_fdata()[:,::-1,::-1].transpose(2,1,0) #Swap width and num of images in correct way","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:30.359327Z","iopub.execute_input":"2023-09-30T19:36:30.359708Z","iopub.status.idle":"2023-09-30T19:36:30.364737Z","shell.execute_reply.started":"2023-09-30T19:36:30.359678Z","shell.execute_reply":"2023-09-30T19:36:30.363637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:31.163257Z","iopub.execute_input":"2023-09-30T19:36:31.164Z","iopub.status.idle":"2023-09-30T19:36:31.168772Z","shell.execute_reply.started":"2023-09-30T19:36:31.163964Z","shell.execute_reply":"2023-09-30T19:36:31.16793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(seg_data[199])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:36.900749Z","iopub.execute_input":"2023-09-30T19:36:36.901087Z","iopub.status.idle":"2023-09-30T19:36:37.164476Z","shell.execute_reply.started":"2023-09-30T19:36:36.90106Z","shell.execute_reply":"2023-09-30T19:36:37.163442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_data[199]) #It shows that slice 200 represents c5 and c6","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:36:58.387822Z","iopub.execute_input":"2023-09-30T19:36:58.389086Z","iopub.status.idle":"2023-09-30T19:36:58.402609Z","shell.execute_reply.started":"2023-09-30T19:36:58.389042Z","shell.execute_reply":"2023-09-30T19:36:58.401631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The other way to get image array information from the nii data is using img.dataobj function. This is more efficent way to read the image and convert to array","metadata":{}},{"cell_type":"code","source":"path = os.path.join(base_dir,'segmentations')\nfilename = '1.2.826.0.1.3680043.29425.nii'\n\nimg=nib.load(os.path.join(path,filename))\nwidth,height,queue=img.dataobj.shape\ndata = np.array(img.dataobj)\nprint(\"Before transpose: \", data.shape)\ndata = data[:,::-1,::-1].transpose(2,1,0)\nprint(\"After transpose: \", data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:42:52.408434Z","iopub.execute_input":"2023-09-30T19:42:52.408799Z","iopub.status.idle":"2023-09-30T19:42:52.533132Z","shell.execute_reply.started":"2023-09-30T19:42:52.408771Z","shell.execute_reply":"2023-09-30T19:42:52.532178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(seg_data[199])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:43:16.025596Z","iopub.execute_input":"2023-09-30T19:43:16.026102Z","iopub.status.idle":"2023-09-30T19:43:16.296921Z","shell.execute_reply.started":"2023-09-30T19:43:16.026067Z","shell.execute_reply":"2023-09-30T19:43:16.295974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_data[199]) #It shows that slice 200 represents c5 and c6","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:43:31.232268Z","iopub.execute_input":"2023-09-30T19:43:31.232638Z","iopub.status.idle":"2023-09-30T19:43:31.247249Z","shell.execute_reply.started":"2023-09-30T19:43:31.232599Z","shell.execute_reply":"2023-09-30T19:43:31.246115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading nii files everytime is a very time consuming process. Hence, lets load all the nii files and store it as .npy files so that loading is faster ","metadata":{}},{"cell_type":"code","source":"# import glob\n# path = '/kaggle/working/npy_segmentations'\n# files = glob.glob(os.path.join(path,'*.npy'))\n# for f in tqdm(files):\n#     os.remove(f)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:19:59.017934Z","iopub.execute_input":"2023-09-30T19:19:59.018797Z","iopub.status.idle":"2023-09-30T19:20:00.646683Z","shell.execute_reply.started":"2023-09-30T19:19:59.018738Z","shell.execute_reply":"2023-09-30T19:20:00.645986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path=os.path.join(base_dir,'segmentations')\ndirs = os.listdir(path)   \nos.mkdir('./npy_segmentations') #create a new directory in working dir\nfor filename in tqdm(dirs):\n    img=nib.load(os.path.join(path,filename))\n    \n    width,height,queue=img.dataobj.shape\n    data = np.array(img.dataobj)\n    print(data.shape)\n    data = data[:,::-1,::-1].transpose(2,1,0)\n    print(data.shape)\n    \n    stem, suffix = os.path.splitext(filename)\n    print(stem,suffix)\n    stem += '.npy'\n    np.save('npy_segmentations/'+stem,data)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:24:37.012461Z","iopub.execute_input":"2023-09-30T19:24:37.012818Z","iopub.status.idle":"2023-09-30T19:31:14.644216Z","shell.execute_reply.started":"2023-09-30T19:24:37.012788Z","shell.execute_reply":"2023-09-30T19:31:14.643159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load npy file","metadata":{}},{"cell_type":"code","source":"seg_data_npy = np.load(os.path.join('./npy_segmentations','1.2.826.0.1.3680043.29425.npy'))\nprint(seg_data_npy.shape)\nplt.imshow(seg_data[199])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:46:51.364489Z","iopub.execute_input":"2023-09-30T19:46:51.364865Z","iopub.status.idle":"2023-09-30T19:46:51.690118Z","shell.execute_reply.started":"2023-09-30T19:46:51.364832Z","shell.execute_reply":"2023-09-30T19:46:51.689129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_data[199])","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:47:19.080286Z","iopub.execute_input":"2023-09-30T19:47:19.080608Z","iopub.status.idle":"2023-09-30T19:47:19.091768Z","shell.execute_reply.started":"2023-09-30T19:47:19.080583Z","shell.execute_reply":"2023-09-30T19:47:19.091111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n!zip -r npy_segmentations.zip /kaggle/working/npy_segmentations","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:53:24.074189Z","iopub.execute_input":"2023-09-30T19:53:24.075277Z","iopub.status.idle":"2023-09-30T19:55:12.517159Z","shell.execute_reply.started":"2023-09-30T19:53:24.075232Z","shell.execute_reply":"2023-09-30T19:55:12.516226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FileLink(\"npy_segmentations.zip\")","metadata":{"execution":{"iopub.status.busy":"2023-09-30T19:56:35.03573Z","iopub.execute_input":"2023-09-30T19:56:35.036195Z","iopub.status.idle":"2023-09-30T19:56:35.043991Z","shell.execute_reply.started":"2023-09-30T19:56:35.036157Z","shell.execute_reply":"2023-09-30T19:56:35.04309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}