{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":36363,"databundleVersionId":4050810,"sourceType":"competition"},{"sourceId":5718655,"sourceType":"datasetVersion","datasetId":2373279},{"sourceId":6587064,"sourceType":"datasetVersion","datasetId":3802375},{"sourceId":6733646,"sourceType":"datasetVersion","datasetId":3878164}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","execution":{"iopub.status.busy":"2024-09-24T15:44:23.658922Z","iopub.execute_input":"2024-09-24T15:44:23.659379Z","iopub.status.idle":"2024-09-24T15:44:23.665265Z","shell.execute_reply.started":"2024-09-24T15:44:23.659344Z","shell.execute_reply":"2024-09-24T15:44:23.664132Z"},"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":"2024-09-24T15:44:23.667863Z","iopub.execute_input":"2024-09-24T15:44:23.668323Z","iopub.status.idle":"2024-09-24T15:44:37.798584Z","shell.execute_reply.started":"2024-09-24T15:44:23.668282Z","shell.execute_reply":"2024-09-24T15:44:37.79709Z"},"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":"2024-09-24T15:46:01.327055Z","iopub.execute_input":"2024-09-24T15:46:01.327571Z","iopub.status.idle":"2024-09-24T15:46:01.517346Z","shell.execute_reply.started":"2024-09-24T15:46:01.327534Z","shell.execute_reply":"2024-09-24T15:46:01.516176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:37.983719Z","iopub.execute_input":"2024-09-24T15:44:37.984088Z","iopub.status.idle":"2024-09-24T15:44:37.991734Z","shell.execute_reply.started":"2024-09-24T15:44:37.984058Z","shell.execute_reply":"2024-09-24T15:44:37.990457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_meta_data.columns","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:37.99561Z","iopub.execute_input":"2024-09-24T15:44:37.995954Z","iopub.status.idle":"2024-09-24T15:44:38.006984Z","shell.execute_reply.started":"2024-09-24T15:44:37.995922Z","shell.execute_reply":"2024-09-24T15:44:38.005731Z"},"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":"2024-09-24T15:44:38.008447Z","iopub.execute_input":"2024-09-24T15:44:38.008768Z","iopub.status.idle":"2024-09-24T15:44:38.041552Z","shell.execute_reply.started":"2024-09-24T15:44:38.008738Z","shell.execute_reply":"2024-09-24T15:44:38.040479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fractured_data = train_data[train_data['patient_overall']==1]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.042952Z","iopub.execute_input":"2024-09-24T15:44:38.043277Z","iopub.status.idle":"2024-09-24T15:44:38.050278Z","shell.execute_reply.started":"2024-09-24T15:44:38.043241Z","shell.execute_reply":"2024-09-24T15:44:38.049152Z"},"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":"2024-09-24T15:44:38.051675Z","iopub.execute_input":"2024-09-24T15:44:38.052099Z","iopub.status.idle":"2024-09-24T15:44:38.06164Z","shell.execute_reply.started":"2024-09-24T15:44:38.052062Z","shell.execute_reply":"2024-09-24T15:44:38.060542Z"},"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":"2024-09-24T15:44:38.063128Z","iopub.execute_input":"2024-09-24T15:44:38.063471Z","iopub.status.idle":"2024-09-24T15:44:38.112615Z","shell.execute_reply.started":"2024-09-24T15:44:38.063442Z","shell.execute_reply":"2024-09-24T15:44:38.106634Z"},"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":"2024-09-24T15:46:31.383953Z","iopub.execute_input":"2024-09-24T15:46:31.384367Z","iopub.status.idle":"2024-09-24T15:46:31.394606Z","shell.execute_reply.started":"2024-09-24T15:46:31.384333Z","shell.execute_reply":"2024-09-24T15:46:31.393339Z"},"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":"2024-09-24T15:46:43.659602Z","iopub.execute_input":"2024-09-24T15:46:43.659985Z","iopub.status.idle":"2024-09-24T15:46:43.687023Z","shell.execute_reply.started":"2024-09-24T15:46:43.659955Z","shell.execute_reply":"2024-09-24T15:46:43.686029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img.pixel_array, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:46:51.418716Z","iopub.execute_input":"2024-09-24T15:46:51.419133Z","iopub.status.idle":"2024-09-24T15:46:51.829079Z","shell.execute_reply.started":"2024-09-24T15:46:51.419098Z","shell.execute_reply":"2024-09-24T15:46:51.827927Z"},"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":"2024-09-24T15:47:21.784102Z","iopub.execute_input":"2024-09-24T15:47:21.78507Z","iopub.status.idle":"2024-09-24T15:47:21.794702Z","shell.execute_reply.started":"2024-09-24T15:47:21.785031Z","shell.execute_reply":"2024-09-24T15:47:21.793687Z"},"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":"2024-09-24T15:47:47.848795Z","iopub.execute_input":"2024-09-24T15:47:47.849218Z","iopub.status.idle":"2024-09-24T15:47:47.861451Z","shell.execute_reply.started":"2024-09-24T15:47:47.849182Z","shell.execute_reply":"2024-09-24T15:47:47.860339Z"},"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":"2024-09-24T15:47:52.418428Z","iopub.execute_input":"2024-09-24T15:47:52.418796Z","iopub.status.idle":"2024-09-24T15:47:54.241259Z","shell.execute_reply.started":"2024-09-24T15:47:52.418766Z","shell.execute_reply":"2024-09-24T15:47:54.240084Z"},"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":"2024-09-24T15:48:09.233835Z","iopub.execute_input":"2024-09-24T15:48:09.234952Z","iopub.status.idle":"2024-09-24T15:48:09.240219Z","shell.execute_reply.started":"2024-09-24T15:48:09.234901Z","shell.execute_reply":"2024-09-24T15:48:09.238946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#subset[subset['C4']==1].index.max()","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.12878Z","iopub.status.idle":"2024-09-24T15:44:38.129184Z","shell.execute_reply.started":"2024-09-24T15:44:38.128991Z","shell.execute_reply":"2024-09-24T15:44:38.129009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#subset.loc[27114:27186]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.131133Z","iopub.status.idle":"2024-09-24T15:44:38.131518Z","shell.execute_reply.started":"2024-09-24T15:44:38.131335Z","shell.execute_reply":"2024-09-24T15:44:38.131353Z"},"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":"2024-09-24T15:44:38.132731Z","iopub.status.idle":"2024-09-24T15:44:38.133137Z","shell.execute_reply.started":"2024-09-24T15:44:38.132936Z","shell.execute_reply":"2024-09-24T15:44:38.132962Z"},"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":"2024-09-24T15:44:38.134503Z","iopub.status.idle":"2024-09-24T15:44:38.13492Z","shell.execute_reply.started":"2024-09-24T15:44:38.1347Z","shell.execute_reply":"2024-09-24T15:44:38.134718Z"},"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":"2024-09-24T15:48:24.784753Z","iopub.execute_input":"2024-09-24T15:48:24.785182Z","iopub.status.idle":"2024-09-24T15:48:25.238049Z","shell.execute_reply.started":"2024-09-24T15:48:24.785136Z","shell.execute_reply":"2024-09-24T15:48:25.236968Z"},"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":"2024-09-24T15:48:49.543932Z","iopub.execute_input":"2024-09-24T15:48:49.544342Z","iopub.status.idle":"2024-09-24T15:48:49.981363Z","shell.execute_reply.started":"2024-09-24T15:48:49.544309Z","shell.execute_reply":"2024-09-24T15:48:49.980191Z"},"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":"2024-09-24T15:49:01.928379Z","iopub.execute_input":"2024-09-24T15:49:01.929264Z","iopub.status.idle":"2024-09-24T15:49:02.363821Z","shell.execute_reply.started":"2024-09-24T15:49:01.929221Z","shell.execute_reply":"2024-09-24T15:49:02.362805Z"},"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":"2024-09-24T15:44:38.141585Z","iopub.status.idle":"2024-09-24T15:44:38.141989Z","shell.execute_reply.started":"2024-09-24T15:44:38.141764Z","shell.execute_reply":"2024-09-24T15:44:38.141781Z"},"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":"2024-09-24T15:44:38.143397Z","iopub.status.idle":"2024-09-24T15:44:38.143772Z","shell.execute_reply.started":"2024-09-24T15:44:38.143584Z","shell.execute_reply":"2024-09-24T15:44:38.143602Z"},"trusted":true},"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":"2024-09-24T15:44:38.145219Z","iopub.status.idle":"2024-09-24T15:44:38.145577Z","shell.execute_reply.started":"2024-09-24T15:44:38.145398Z","shell.execute_reply":"2024-09-24T15:44:38.145415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(cervical_index.values())","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:49:15.784481Z","iopub.execute_input":"2024-09-24T15:49:15.784923Z","iopub.status.idle":"2024-09-24T15:49:15.81834Z","shell.execute_reply.started":"2024-09-24T15:49:15.784852Z","shell.execute_reply":"2024-09-24T15:49:15.816841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subset.loc[list(cervical_index.values())]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.14884Z","iopub.status.idle":"2024-09-24T15:44:38.149333Z","shell.execute_reply.started":"2024-09-24T15:44:38.149127Z","shell.execute_reply":"2024-09-24T15:44:38.149147Z"},"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":"2024-09-24T15:44:38.151005Z","iopub.status.idle":"2024-09-24T15:44:38.15137Z","shell.execute_reply.started":"2024-09-24T15:44:38.151187Z","shell.execute_reply":"2024-09-24T15:44:38.151205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.152467Z","iopub.status.idle":"2024-09-24T15:44:38.152854Z","shell.execute_reply.started":"2024-09-24T15:44:38.152658Z","shell.execute_reply":"2024-09-24T15:44:38.152676Z"},"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":"2024-09-24T15:44:38.154032Z","iopub.status.idle":"2024-09-24T15:44:38.154409Z","shell.execute_reply.started":"2024-09-24T15:44:38.154223Z","shell.execute_reply":"2024-09-24T15:44:38.15424Z"},"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":"2024-09-24T15:50:34.703841Z","iopub.execute_input":"2024-09-24T15:50:34.704275Z","iopub.status.idle":"2024-09-24T15:50:34.713681Z","shell.execute_reply.started":"2024-09-24T15:50:34.704241Z","shell.execute_reply":"2024-09-24T15:50:34.712515Z"},"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":"2024-09-24T15:50:44.708762Z","iopub.execute_input":"2024-09-24T15:50:44.709721Z","iopub.status.idle":"2024-09-24T15:50:44.790485Z","shell.execute_reply.started":"2024-09-24T15:50:44.709675Z","shell.execute_reply":"2024-09-24T15:50:44.789542Z"},"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":"2024-09-24T15:44:38.16009Z","iopub.status.idle":"2024-09-24T15:44:38.160595Z","shell.execute_reply.started":"2024-09-24T15:44:38.160327Z","shell.execute_reply":"2024-09-24T15:44:38.16035Z"},"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":"2024-09-24T15:50:55.66401Z","iopub.execute_input":"2024-09-24T15:50:55.664423Z","iopub.status.idle":"2024-09-24T15:50:56.249081Z","shell.execute_reply.started":"2024-09-24T15:50:55.66439Z","shell.execute_reply":"2024-09-24T15:50:56.247855Z"},"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":"2024-09-24T15:51:20.903745Z","iopub.execute_input":"2024-09-24T15:51:20.904239Z","iopub.status.idle":"2024-09-24T15:51:21.37559Z","shell.execute_reply.started":"2024-09-24T15:51:20.904192Z","shell.execute_reply":"2024-09-24T15:51:21.374487Z"},"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":"2024-09-24T15:51:36.969051Z","iopub.execute_input":"2024-09-24T15:51:36.969453Z","iopub.status.idle":"2024-09-24T15:51:37.433072Z","shell.execute_reply.started":"2024-09-24T15:51:36.969421Z","shell.execute_reply":"2024-09-24T15:51:37.431924Z"},"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":"2024-09-24T15:51:53.840016Z","iopub.execute_input":"2024-09-24T15:51:53.840403Z","iopub.status.idle":"2024-09-24T15:51:54.302317Z","shell.execute_reply.started":"2024-09-24T15:51:53.840371Z","shell.execute_reply":"2024-09-24T15:51:54.301175Z"},"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":"2024-09-24T15:52:11.819748Z","iopub.execute_input":"2024-09-24T15:52:11.820185Z","iopub.status.idle":"2024-09-24T15:52:12.34376Z","shell.execute_reply.started":"2024-09-24T15:52:11.820149Z","shell.execute_reply":"2024-09-24T15:52:12.342625Z"},"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":"2024-09-24T15:44:38.173629Z","iopub.status.idle":"2024-09-24T15:44:38.174054Z","shell.execute_reply.started":"2024-09-24T15:44:38.173816Z","shell.execute_reply":"2024-09-24T15:44:38.173834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(seg_slice)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.175601Z","iopub.status.idle":"2024-09-24T15:44:38.175996Z","shell.execute_reply.started":"2024-09-24T15:44:38.175781Z","shell.execute_reply":"2024-09-24T15:44:38.175798Z"},"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":"2024-09-24T15:52:29.098898Z","iopub.execute_input":"2024-09-24T15:52:29.099638Z","iopub.status.idle":"2024-09-24T15:52:29.143325Z","shell.execute_reply.started":"2024-09-24T15:52:29.099602Z","shell.execute_reply":"2024-09-24T15:52:29.142201Z"},"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":"2024-09-24T15:44:38.178496Z","iopub.status.idle":"2024-09-24T15:44:38.178848Z","shell.execute_reply.started":"2024-09-24T15:44:38.17867Z","shell.execute_reply":"2024-09-24T15:44:38.178687Z"},"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":"2024-09-24T15:44:38.180243Z","iopub.status.idle":"2024-09-24T15:44:38.180624Z","shell.execute_reply.started":"2024-09-24T15:44:38.180442Z","shell.execute_reply":"2024-09-24T15:44:38.180459Z"},"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":"2024-09-24T15:44:38.181946Z","iopub.status.idle":"2024-09-24T15:44:38.182329Z","shell.execute_reply.started":"2024-09-24T15:44:38.182141Z","shell.execute_reply":"2024-09-24T15:44:38.182159Z"},"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":"2024-09-24T15:44:38.188407Z","iopub.status.idle":"2024-09-24T15:44:38.18881Z","shell.execute_reply.started":"2024-09-24T15:44:38.188613Z","shell.execute_reply":"2024-09-24T15:44:38.188632Z"},"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":"2024-09-24T15:44:38.189931Z","iopub.status.idle":"2024-09-24T15:44:38.190307Z","shell.execute_reply.started":"2024-09-24T15:44:38.190115Z","shell.execute_reply":"2024-09-24T15:44:38.190132Z"},"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":"2024-09-24T15:44:38.19154Z","iopub.status.idle":"2024-09-24T15:44:38.191936Z","shell.execute_reply.started":"2024-09-24T15:44:38.191719Z","shell.execute_reply":"2024-09-24T15:44:38.191735Z"},"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":"2024-09-24T15:44:38.193129Z","iopub.status.idle":"2024-09-24T15:44:38.193491Z","shell.execute_reply.started":"2024-09-24T15:44:38.19331Z","shell.execute_reply":"2024-09-24T15:44:38.193327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"only_bone_data[only_bone_data['C7']==1]","metadata":{"execution":{"iopub.status.busy":"2024-09-24T15:44:38.195775Z","iopub.status.idle":"2024-09-24T15:44:38.1962Z","shell.execute_reply.started":"2024-09-24T15:44:38.195996Z","shell.execute_reply":"2024-09-24T15:44:38.196015Z"},"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":"2024-09-24T15:44:38.197429Z","iopub.status.idle":"2024-09-24T15:44:38.197796Z","shell.execute_reply.started":"2024-09-24T15:44:38.197614Z","shell.execute_reply":"2024-09-24T15:44:38.197631Z"},"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":"2024-09-24T15:52:52.681252Z","iopub.execute_input":"2024-09-24T15:52:52.682208Z","iopub.status.idle":"2024-09-24T15:52:53.264163Z","shell.execute_reply.started":"2024-09-24T15:52:52.682159Z","shell.execute_reply":"2024-09-24T15:52:53.263037Z"},"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":"2024-09-24T15:44:38.201844Z","iopub.status.idle":"2024-09-24T15:44:38.20228Z","shell.execute_reply.started":"2024-09-24T15:44:38.202082Z","shell.execute_reply":"2024-09-24T15:44:38.202101Z"},"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":"2024-09-24T15:44:38.207281Z","iopub.status.idle":"2024-09-24T15:44:38.207686Z","shell.execute_reply.started":"2024-09-24T15:44:38.207492Z","shell.execute_reply":"2024-09-24T15:44:38.207512Z"},"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":"2024-09-24T15:44:38.209225Z","iopub.status.idle":"2024-09-24T15:44:38.209591Z","shell.execute_reply.started":"2024-09-24T15:44:38.209409Z","shell.execute_reply":"2024-09-24T15:44:38.209426Z"},"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":"2024-09-24T15:44:38.211149Z","iopub.status.idle":"2024-09-24T15:44:38.211515Z","shell.execute_reply.started":"2024-09-24T15:44:38.211335Z","shell.execute_reply":"2024-09-24T15:44:38.211351Z"},"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":"2024-09-24T15:44:38.212792Z","iopub.status.idle":"2024-09-24T15:44:38.213194Z","shell.execute_reply.started":"2024-09-24T15:44:38.213006Z","shell.execute_reply":"2024-09-24T15:44:38.213025Z"},"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":"2024-09-24T15:53:21.466022Z","iopub.execute_input":"2024-09-24T15:53:21.466445Z","iopub.status.idle":"2024-09-24T15:53:21.918119Z","shell.execute_reply.started":"2024-09-24T15:53:21.46641Z","shell.execute_reply":"2024-09-24T15:53:21.916958Z"},"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":"2024-09-24T15:44:38.216253Z","iopub.status.idle":"2024-09-24T15:44:38.216754Z","shell.execute_reply.started":"2024-09-24T15:44:38.216495Z","shell.execute_reply":"2024-09-24T15:44:38.216519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}