{"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":"!pip install -qU ../input/for-pydicom/python_gdcm-3.0.22-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl ../input/for-pydicom/pylibjpeg-1.4.0-py3-none-any.whl --find-links frozen_packages --no-index","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:07.370651Z","iopub.execute_input":"2023-10-30T06:17:07.371168Z","iopub.status.idle":"2023-10-30T06:17:20.36794Z","shell.execute_reply.started":"2023-10-30T06:17:07.371119Z","shell.execute_reply":"2023-10-30T06:17:20.366516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport pydicom as dicom\nimport glob\nimport nibabel as nib\nimport os\nimport cv2\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:24.009743Z","iopub.execute_input":"2023-10-30T06:17:24.010275Z","iopub.status.idle":"2023-10-30T06:17:25.030135Z","shell.execute_reply.started":"2023-10-30T06:17:24.010207Z","shell.execute_reply":"2023-10-30T06:17:25.028722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = r'/kaggle/input/rsna-2022-cervical-spine-fracture-detection'\ntrain_images = os.path.join(base_dir,'train_images')\ntest_images = os.path.join(base_dir,'test_images')\nsegmentation_data = r'/kaggle/input/rsna-cervical-fracture-segmentations-npy/npy_segmentations'\ntrain_data = pd.read_csv(os.path.join(base_dir,'train.csv'))\nsegmentation_meta_data = pd.read_csv(r'/kaggle/input/rsna-cervical-fracture-segmentation-metadata/meta_segmentation.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:29.446013Z","iopub.execute_input":"2023-10-30T06:17:29.44659Z","iopub.status.idle":"2023-10-30T06:17:29.736759Z","shell.execute_reply.started":"2023-10-30T06:17:29.446556Z","shell.execute_reply":"2023-10-30T06:17:29.735533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:33.501763Z","iopub.execute_input":"2023-10-30T06:17:33.502561Z","iopub.status.idle":"2023-10-30T06:17:33.512307Z","shell.execute_reply.started":"2023-10-30T06:17:33.502516Z","shell.execute_reply":"2023-10-30T06:17:33.510577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:35.729415Z","iopub.execute_input":"2023-10-30T06:17:35.72986Z","iopub.status.idle":"2023-10-30T06:17:35.739432Z","shell.execute_reply.started":"2023-10-30T06:17:35.729824Z","shell.execute_reply":"2023-10-30T06:17:35.737772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data = segmentation_meta_data.sort_values(['StudyInstanceUID','InstanceNumber']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:42.74998Z","iopub.execute_input":"2023-10-30T06:17:42.750399Z","iopub.status.idle":"2023-10-30T06:17:42.800758Z","shell.execute_reply.started":"2023-10-30T06:17:42.750367Z","shell.execute_reply":"2023-10-30T06:17:42.799563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fractured_data = train_data[train_data['patient_overall']==1]","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:46.286829Z","iopub.execute_input":"2023-10-30T06:17:46.287201Z","iopub.status.idle":"2023-10-30T06:17:46.293775Z","shell.execute_reply.started":"2023-10-30T06:17:46.287165Z","shell.execute_reply":"2023-10-30T06:17:46.292764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"cervicals","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Calculate number of bones fractured per study id who have segmentation data </h2>","metadata":{}},{"cell_type":"code","source":"#Calculate number of bones fractured per study id whg have segmentations \nfractured_data[fractured_data['StudyInstanceUID'].isin(segmentation_meta_data['StudyInstanceUID'].unique())][cervicals].sum(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T06:17:50.775848Z","iopub.execute_input":"2023-10-30T06:17:50.776279Z","iopub.status.idle":"2023-10-30T06:17:51.232048Z","shell.execute_reply.started":"2023-10-30T06:17:50.776232Z","shell.execute_reply":"2023-10-30T06:17:51.230097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#This Instance id has higher number of fractures\nfractured_data.loc[1373]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:09:02.743403Z","iopub.execute_input":"2023-10-22T02:09:02.743822Z","iopub.status.idle":"2023-10-22T02:09:02.75312Z","shell.execute_reply.started":"2023-10-22T02:09:02.743788Z","shell.execute_reply":"2023-10-22T02:09:02.751932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets visualize both DICOM and Segmentation image for the above 7 slices\n\ndef load_dicom(path):\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\n    data=img.pixel_array\n    data=data-np.min(data)\n    if np.max(data) != 0:\n        data=data/np.max(data)\n    data=(data*255).astype(np.uint8)        \n    return cv2.cvtColor(data.reshape(512, 512), cv2.COLOR_GRAY2RGB)\n\ndef RSNAImgGenerator(slice_id,path):\n           \n    path = os.path.join(path, slice_id)\n    if os.path.exists(path):\n        img=load_dicom(path)\n        img=cv2.resize(img,(128, 128))\n        image=np.array(img)\n        image=image/255.0\n\n    return np.array(image)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T21:44:48.250191Z","iopub.execute_input":"2023-10-25T21:44:48.250576Z","iopub.status.idle":"2023-10-25T21:44:48.25925Z","shell.execute_reply.started":"2023-10-25T21:44:48.250548Z","shell.execute_reply":"2023-10-25T21:44:48.257549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_id = '1.2.826.0.1.3680043.5783/103.dcm'\nimg = dicom.dcmread(os.path.join(train_images,study_id))","metadata":{"execution":{"iopub.status.busy":"2023-10-25T21:43:53.138643Z","iopub.execute_input":"2023-10-25T21:43:53.138992Z","iopub.status.idle":"2023-10-25T21:43:53.169285Z","shell.execute_reply.started":"2023-10-25T21:43:53.138964Z","shell.execute_reply":"2023-10-25T21:43:53.168572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img.pixel_array, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2023-10-25T21:45:28.928591Z","iopub.execute_input":"2023-10-25T21:45:28.928935Z","iopub.status.idle":"2023-10-25T21:45:29.193072Z","shell.execute_reply.started":"2023-10-25T21:45:28.928907Z","shell.execute_reply":"2023-10-25T21:45:29.191663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_id ='1.2.826.0.1.3680043.5783'\nsubset = segmentation_meta_data[segmentation_meta_data['StudyInstanceUID']==study_id]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:38:00.03309Z","iopub.execute_input":"2023-10-22T02:38:00.033475Z","iopub.status.idle":"2023-10-22T02:38:00.044834Z","shell.execute_reply.started":"2023-10-22T02:38:00.033442Z","shell.execute_reply":"2023-10-22T02:38:00.044083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_img = np.load(os.path.join(segmentation_data,'{}.npy'.format(study_id)))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:43:10.955154Z","iopub.execute_input":"2023-10-22T02:43:10.956232Z","iopub.status.idle":"2023-10-22T02:43:12.754572Z","shell.execute_reply.started":"2023-10-22T02:43:10.956188Z","shell.execute_reply":"2023-10-22T02:43:12.753223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cervicals = ['C{}'.format(i) for i in range(1,8)]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:43:18.904218Z","iopub.execute_input":"2023-10-22T02:43:18.90489Z","iopub.status.idle":"2023-10-22T02:43:18.909956Z","shell.execute_reply.started":"2023-10-22T02:43:18.90484Z","shell.execute_reply":"2023-10-22T02:43:18.908736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#subset[subset['C4']==1].index.max()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:27:20.427411Z","iopub.execute_input":"2023-10-22T02:27:20.427805Z","iopub.status.idle":"2023-10-22T02:27:20.432797Z","shell.execute_reply.started":"2023-10-22T02:27:20.427775Z","shell.execute_reply":"2023-10-22T02:27:20.431668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#subset.loc[27114:27186]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:27:25.851951Z","iopub.execute_input":"2023-10-22T02:27:25.852622Z","iopub.status.idle":"2023-10-22T02:27:25.857133Z","shell.execute_reply.started":"2023-10-22T02:27:25.852587Z","shell.execute_reply":"2023-10-22T02:27:25.856005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"single_bone_slice_data = subset[subset[cervicals].sum(axis=1)==1].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:43:22.62739Z","iopub.execute_input":"2023-10-22T02:43:22.627813Z","iopub.status.idle":"2023-10-22T02:43:22.637555Z","shell.execute_reply.started":"2023-10-22T02:43:22.627779Z","shell.execute_reply":"2023-10-22T02:43:22.636279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"single_bone_slice_data[single_bone_slice_data['C6']==1]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:48:36.205155Z","iopub.execute_input":"2023-10-22T02:48:36.206464Z","iopub.status.idle":"2023-10-22T02:48:36.239199Z","shell.execute_reply.started":"2023-10-22T02:48:36.206412Z","shell.execute_reply":"2023-10-22T02:48:36.237787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing C1,C2,C4,C7\nC1_slice_id = study_id+'/103.dcm'\nseg_slice = cv2.resize(seg_img[102],(128,128))\ndcm_img = RSNAImgGenerator(C1_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C1\")\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:44:01.500963Z","iopub.execute_input":"2023-10-22T02:44:01.501378Z","iopub.status.idle":"2023-10-22T02:44:01.884305Z","shell.execute_reply.started":"2023-10-22T02:44:01.501342Z","shell.execute_reply":"2023-10-22T02:44:01.883142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C2_slice_id = study_id+'/208.dcm'\nseg_slice = cv2.resize(seg_img[207],(128,128))\ndcm_img = RSNAImgGenerator(C2_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C2\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:47:18.083262Z","iopub.execute_input":"2023-10-22T02:47:18.083686Z","iopub.status.idle":"2023-10-22T02:47:18.505274Z","shell.execute_reply.started":"2023-10-22T02:47:18.083651Z","shell.execute_reply":"2023-10-22T02:47:18.504164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C4_slice_id = study_id+'/301.dcm'\nseg_slice = cv2.resize(seg_img[300],(128,128))\ndcm_img = RSNAImgGenerator(C4_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C4\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:48:00.959486Z","iopub.execute_input":"2023-10-22T02:48:00.959912Z","iopub.status.idle":"2023-10-22T02:48:01.404407Z","shell.execute_reply.started":"2023-10-22T02:48:00.959867Z","shell.execute_reply":"2023-10-22T02:48:01.403282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C6_slice_id = study_id+'/410.dcm'\nseg_slice = cv2.resize(seg_img[409],(128,128))\ndcm_img = RSNAImgGenerator(C6_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C6\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:49:17.130035Z","iopub.execute_input":"2023-10-22T02:49:17.130458Z","iopub.status.idle":"2023-10-22T02:49:17.530246Z","shell.execute_reply.started":"2023-10-22T02:49:17.130421Z","shell.execute_reply":"2023-10-22T02:49:17.529061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C6_slice_id = study_id+'/410.dcm'\nseg_slice = cv2.resize(seg_img[409],(128,128))\ndcm_img = RSNAImgGenerator(C6_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C6\")\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#identify indexes where new bone starts\ncervical_index = {cervical:subset[subset[cervical]==1].index.min() for cervical in cervicals}\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:15:42.752815Z","iopub.execute_input":"2023-10-22T02:15:42.753191Z","iopub.status.idle":"2023-10-22T02:15:42.766935Z","shell.execute_reply.started":"2023-10-22T02:15:42.753159Z","shell.execute_reply":"2023-10-22T02:15:42.76545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(cervical_index.values())","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:39:58.196611Z","iopub.execute_input":"2023-10-21T23:39:58.197086Z","iopub.status.idle":"2023-10-21T23:39:58.205482Z","shell.execute_reply.started":"2023-10-21T23:39:58.197052Z","shell.execute_reply":"2023-10-21T23:39:58.204124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subset.loc[list(cervical_index.values())]","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:00.218811Z","iopub.execute_input":"2023-10-21T23:40:00.219466Z","iopub.status.idle":"2023-10-21T23:40:00.257949Z","shell.execute_reply.started":"2023-10-21T23:40:00.219419Z","shell.execute_reply":"2023-10-21T23:40:00.25648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir(os.path.join(train_images,'1.2.826.0.1.3680043.10633'))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T21:48:45.166796Z","iopub.execute_input":"2023-10-21T21:48:45.167193Z","iopub.status.idle":"2023-10-21T21:48:45.171867Z","shell.execute_reply.started":"2023-10-21T21:48:45.16716Z","shell.execute_reply":"2023-10-21T21:48:45.170733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T01:59:13.105164Z","iopub.execute_input":"2023-10-22T01:59:13.105648Z","iopub.status.idle":"2023-10-22T01:59:13.605867Z","shell.execute_reply.started":"2023-10-22T01:59:13.105598Z","shell.execute_reply":"2023-10-22T01:59:13.604168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"#Observation: When we order the slices in ascending order, there is a clear pattern on how bone is recognized\n#Each bone representation starts at C1 and gradually move towards c7 in an order. Once C1 is over,\n#It would repeat again. Any slice before C1 beginning or after C7 end are 0. They don't add value to the analysis","metadata":{"execution":{"iopub.status.busy":"2023-10-21T20:58:25.47776Z","iopub.execute_input":"2023-10-21T20:58:25.478224Z","iopub.status.idle":"2023-10-21T20:58:25.484405Z","shell.execute_reply.started":"2023-10-21T20:58:25.478192Z","shell.execute_reply":"2023-10-21T20:58:25.482781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets visualize both DICOM and Segmentation image for the above 7 slices\n\ndef load_dicom(path):\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\n    data=img.pixel_array\n    data=data-np.min(data)\n    if np.max(data) != 0:\n        data=data/np.max(data)\n    data=(data*255).astype(np.uint8)        \n    return cv2.cvtColor(data.reshape(512, 512), cv2.COLOR_GRAY2RGB)\n\ndef RSNAImgGenerator(slice_id,path):\n           \n    path = os.path.join(path, slice_id)\n    if os.path.exists(path):\n        img=load_dicom(path)\n        img=cv2.resize(img,(128, 128))\n        image=np.array(img)\n        image=image/255.0\n\n    return np.array(image)","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:03.227599Z","iopub.execute_input":"2023-10-21T23:40:03.228081Z","iopub.status.idle":"2023-10-21T23:40:03.238137Z","shell.execute_reply.started":"2023-10-21T23:40:03.228047Z","shell.execute_reply":"2023-10-21T23:40:03.236512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_img = np.load(os.path.join(segmentation_data,'1.2.826.0.1.3680043.10633.npy'))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:07.830318Z","iopub.execute_input":"2023-10-21T23:40:07.830832Z","iopub.status.idle":"2023-10-21T23:40:07.976705Z","shell.execute_reply.started":"2023-10-21T23:40:07.830794Z","shell.execute_reply":"2023-10-21T23:40:07.975511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/113.dcm'\nseg_slice = cv2.resize(seg_img[112],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\n\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[112]))\n","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:08.869258Z","iopub.execute_input":"2023-10-21T23:40:08.869677Z","iopub.status.idle":"2023-10-21T23:40:09.418592Z","shell.execute_reply.started":"2023-10-21T23:40:08.869643Z","shell.execute_reply":"2023-10-21T23:40:09.417096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/131.dcm'\nseg_slice = cv2.resize(seg_img[130],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[130]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:12.195573Z","iopub.execute_input":"2023-10-21T23:40:12.19604Z","iopub.status.idle":"2023-10-21T23:40:12.743715Z","shell.execute_reply.started":"2023-10-21T23:40:12.196006Z","shell.execute_reply":"2023-10-21T23:40:12.742491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/176.dcm'\nseg_slice=cv2.resize(seg_img[175],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[175]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:15.47223Z","iopub.execute_input":"2023-10-21T23:40:15.473206Z","iopub.status.idle":"2023-10-21T23:40:16.030692Z","shell.execute_reply.started":"2023-10-21T23:40:15.473163Z","shell.execute_reply":"2023-10-21T23:40:16.029347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/203.dcm'\nseg_slice = cv2.resize(seg_img[202],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[202]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:20.85497Z","iopub.execute_input":"2023-10-21T23:40:20.855395Z","iopub.status.idle":"2023-10-21T23:40:21.388713Z","shell.execute_reply.started":"2023-10-21T23:40:20.855364Z","shell.execute_reply":"2023-10-21T23:40:21.387576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/233.dcm'\nseg_slice = cv2.resize(seg_img[232],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[232]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:24.917244Z","iopub.execute_input":"2023-10-21T23:40:24.918588Z","iopub.status.idle":"2023-10-21T23:40:25.506187Z","shell.execute_reply.started":"2023-10-21T23:40:24.918529Z","shell.execute_reply":"2023-10-21T23:40:25.504531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/255.dcm'\nseg_slice = cv2.resize(seg_img[254],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[254]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:29.646912Z","iopub.execute_input":"2023-10-21T23:40:29.64731Z","iopub.status.idle":"2023-10-21T23:40:30.209596Z","shell.execute_reply.started":"2023-10-21T23:40:29.64728Z","shell.execute_reply":"2023-10-21T23:40:30.208475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"slice_id = '1.2.826.0.1.3680043.10633/278.dcm'\nseg_slice = cv2.resize(seg_img[277],(128,128))\ndcm_img = RSNAImgGenerator(slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(np.unique(seg_img[277]))","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:40:32.99759Z","iopub.execute_input":"2023-10-21T23:40:32.998547Z","iopub.status.idle":"2023-10-21T23:40:33.56499Z","shell.execute_reply.started":"2023-10-21T23:40:32.998502Z","shell.execute_reply":"2023-10-21T23:40:33.563584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_slice)","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:28:57.685663Z","iopub.execute_input":"2023-10-21T23:28:57.68613Z","iopub.status.idle":"2023-10-21T23:28:57.695694Z","shell.execute_reply.started":"2023-10-21T23:28:57.686096Z","shell.execute_reply":"2023-10-21T23:28:57.694276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Fetch indices where only one bone is visible in the segmentation image\nsubset[subset[cervicals].sum(axis=1)==1]","metadata":{"execution":{"iopub.status.busy":"2023-10-21T23:51:32.491017Z","iopub.execute_input":"2023-10-21T23:51:32.491572Z","iopub.status.idle":"2023-10-21T23:51:32.573234Z","shell.execute_reply.started":"2023-10-21T23:51:32.491531Z","shell.execute_reply":"2023-10-21T23:51:32.571799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing C1,C2,C4,C7\nC1_slice_id = '1.2.826.0.1.3680043.10633/125.dcm'\nseg_slice = cv2.resize(seg_img[124],(128,128))\ndcm_img = RSNAImgGenerator(C1_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C1\")\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:01:37.320235Z","iopub.execute_input":"2023-10-22T00:01:37.321742Z","iopub.status.idle":"2023-10-22T00:01:37.8306Z","shell.execute_reply.started":"2023-10-22T00:01:37.321681Z","shell.execute_reply":"2023-10-22T00:01:37.828938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing C1,C2,C4,C7\nC2_slice_id = '1.2.826.0.1.3680043.10633/170.dcm'\nseg_slice = cv2.resize(seg_img[169],(128,128))\ndcm_img = RSNAImgGenerator(C2_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C2\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:02:36.768565Z","iopub.execute_input":"2023-10-22T00:02:36.769372Z","iopub.status.idle":"2023-10-22T00:02:37.290187Z","shell.execute_reply.started":"2023-10-22T00:02:36.769322Z","shell.execute_reply":"2023-10-22T00:02:37.288526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing C1,C2,C4,C7\nC4_slice_id = '1.2.826.0.1.3680043.10633/231.dcm'\nseg_slice = cv2.resize(seg_img[230],(128,128))\ndcm_img = RSNAImgGenerator(C4_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C4\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:03:57.122253Z","iopub.execute_input":"2023-10-22T00:03:57.122907Z","iopub.status.idle":"2023-10-22T00:03:57.622062Z","shell.execute_reply.started":"2023-10-22T00:03:57.122868Z","shell.execute_reply":"2023-10-22T00:03:57.620657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C7_slice_id = '1.2.826.0.1.3680043.10633/321.dcm'\nseg_slice = cv2.resize(seg_img[320],(128,128))\ndcm_img = RSNAImgGenerator(C7_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C7\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:04:51.049807Z","iopub.execute_input":"2023-10-22T00:04:51.050308Z","iopub.status.idle":"2023-10-22T00:04:51.564928Z","shell.execute_reply.started":"2023-10-22T00:04:51.05027Z","shell.execute_reply":"2023-10-22T00:04:51.563485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data[segmentation_meta_data[cervicals].sum(axis=1)==1].groupby(['StudyInstanceUID']).sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T02:15:05.369599Z","iopub.execute_input":"2023-10-22T02:15:05.369986Z","iopub.status.idle":"2023-10-22T02:15:05.424762Z","shell.execute_reply.started":"2023-10-22T02:15:05.369954Z","shell.execute_reply":"2023-10-22T02:15:05.423929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_subset = segmentation_meta_data[segmentation_meta_data['StudyInstanceUID']=='1.2.826.0.1.3680043.10921'].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:28:04.425944Z","iopub.execute_input":"2023-10-22T00:28:04.426824Z","iopub.status.idle":"2023-10-22T00:28:04.439484Z","shell.execute_reply.started":"2023-10-22T00:28:04.426787Z","shell.execute_reply":"2023-10-22T00:28:04.43792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"only_bone_data = new_subset[new_subset[cervicals].sum(axis=1)==1].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:29:14.8682Z","iopub.execute_input":"2023-10-22T00:29:14.868649Z","iopub.status.idle":"2023-10-22T00:29:14.876706Z","shell.execute_reply.started":"2023-10-22T00:29:14.868618Z","shell.execute_reply":"2023-10-22T00:29:14.87561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"only_bone_data[only_bone_data['C7']==1]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:42:15.241098Z","iopub.execute_input":"2023-10-22T00:42:15.241546Z","iopub.status.idle":"2023-10-22T00:42:15.29549Z","shell.execute_reply.started":"2023-10-22T00:42:15.241513Z","shell.execute_reply":"2023-10-22T00:42:15.294242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_img = np.load(os.path.join(segmentation_data,'1.2.826.0.1.3680043.10921.npy'))","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:36:02.416713Z","iopub.execute_input":"2023-10-22T00:36:02.417234Z","iopub.status.idle":"2023-10-22T00:36:03.621753Z","shell.execute_reply.started":"2023-10-22T00:36:02.417203Z","shell.execute_reply":"2023-10-22T00:36:03.620128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C1_slice_id = '1.2.826.0.1.3680043.10921/57.dcm'\nseg_slice = cv2.resize(seg_img[56],(128,128))\ndcm_img = RSNAImgGenerator(C1_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C1\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:37:39.201626Z","iopub.execute_input":"2023-10-22T00:37:39.202109Z","iopub.status.idle":"2023-10-22T00:37:39.688586Z","shell.execute_reply.started":"2023-10-22T00:37:39.202076Z","shell.execute_reply":"2023-10-22T00:37:39.686654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C2_slice_id = '1.2.826.0.1.3680043.10921/100.dcm'\nseg_slice = cv2.resize(seg_img[99],(128,128))\ndcm_img = RSNAImgGenerator(C2_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C2\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:38:20.855558Z","iopub.execute_input":"2023-10-22T00:38:20.85606Z","iopub.status.idle":"2023-10-22T00:38:21.35496Z","shell.execute_reply.started":"2023-10-22T00:38:20.856026Z","shell.execute_reply":"2023-10-22T00:38:21.353815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C3_slice_id = '1.2.826.0.1.3680043.10921/127.dcm'\nseg_slice = cv2.resize(seg_img[126],(128,128))\ndcm_img = RSNAImgGenerator(C3_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C3\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:39:11.921354Z","iopub.execute_input":"2023-10-22T00:39:11.921892Z","iopub.status.idle":"2023-10-22T00:39:12.4014Z","shell.execute_reply.started":"2023-10-22T00:39:11.921846Z","shell.execute_reply":"2023-10-22T00:39:12.399698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C4_slice_id = '1.2.826.0.1.3680043.10921/150.dcm'\nseg_slice = cv2.resize(seg_img[149],(128,128))\ndcm_img = RSNAImgGenerator(C4_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C4\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:40:13.752728Z","iopub.execute_input":"2023-10-22T00:40:13.753213Z","iopub.status.idle":"2023-10-22T00:40:14.240237Z","shell.execute_reply.started":"2023-10-22T00:40:13.753177Z","shell.execute_reply":"2023-10-22T00:40:14.238504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C5_slice_id = '1.2.826.0.1.3680043.10921/173.dcm'\nseg_slice = cv2.resize(seg_img[172],(128,128))\ndcm_img = RSNAImgGenerator(C5_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C5\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:41:08.19307Z","iopub.execute_input":"2023-10-22T00:41:08.194581Z","iopub.status.idle":"2023-10-22T00:41:08.695237Z","shell.execute_reply.started":"2023-10-22T00:41:08.194537Z","shell.execute_reply":"2023-10-22T00:41:08.694355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C6_slice_id = '1.2.826.0.1.3680043.10921/200.dcm'\nseg_slice = cv2.resize(seg_img[199],(128,128))\ndcm_img = RSNAImgGenerator(C6_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C6\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:41:58.160303Z","iopub.execute_input":"2023-10-22T00:41:58.16084Z","iopub.status.idle":"2023-10-22T00:41:58.640212Z","shell.execute_reply.started":"2023-10-22T00:41:58.160772Z","shell.execute_reply":"2023-10-22T00:41:58.639097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C7_slice_id = '1.2.826.0.1.3680043.10921/230.dcm'\nseg_slice = cv2.resize(seg_img[229],(128,128))\ndcm_img = RSNAImgGenerator(C7_slice_id,train_images)\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(dcm_img)\nax2.imshow(seg_slice)\nplt.title(\"C7\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:42:49.610201Z","iopub.execute_input":"2023-10-22T00:42:49.61074Z","iopub.status.idle":"2023-10-22T00:42:50.120275Z","shell.execute_reply.started":"2023-10-22T00:42:49.610705Z","shell.execute_reply":"2023-10-22T00:42:50.118934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[train_data['StudyInstanceUID']=='1.2.826.0.1.3680043.10921']","metadata":{"execution":{"iopub.status.busy":"2023-10-22T00:44:37.109977Z","iopub.execute_input":"2023-10-22T00:44:37.110542Z","iopub.status.idle":"2023-10-22T00:44:37.129709Z","shell.execute_reply.started":"2023-10-22T00:44:37.110503Z","shell.execute_reply":"2023-10-22T00:44:37.12797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}