{"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":"# !pip install -qU \"python-gdcm\" pydicom pylibjpeg \"opencv-python-headless\"","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:25.261276Z","iopub.execute_input":"2022-10-06T05:37:25.262003Z","iopub.status.idle":"2022-10-06T05:37:25.28071Z","shell.execute_reply.started":"2022-10-06T05:37:25.261916Z","shell.execute_reply":"2022-10-06T05:37:25.279826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qU ../input/for-pydicom/python_gdcm-3.0.14-cp37-cp37m-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":"2022-10-06T05:37:25.282653Z","iopub.execute_input":"2022-10-06T05:37:25.283321Z","iopub.status.idle":"2022-10-06T05:37:36.070592Z","shell.execute_reply.started":"2022-10-06T05:37:25.283286Z","shell.execute_reply":"2022-10-06T05:37:36.06944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nfrom plotly.offline import init_notebook_mode, iplot, plot\nfrom tqdm.notebook import tqdm\nfrom pathlib import Path\nfrom collections import Counter\n\nimport matplotlib as mpl\nfrom matplotlib import cm\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom matplotlib.offsetbox import AnnotationBbox, OffsetImage\nfrom matplotlib.colors import ListedColormap, LinearSegmentedColormap\nfrom matplotlib.patches import Rectangle\n\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:36.073857Z","iopub.execute_input":"2022-10-06T05:37:36.074248Z","iopub.status.idle":"2022-10-06T05:37:38.224864Z","shell.execute_reply.started":"2022-10-06T05:37:36.074213Z","shell.execute_reply":"2022-10-06T05:37:38.223925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for dcm and nii\nimport pydicom\nimport nibabel as nib\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.22864Z","iopub.execute_input":"2022-10-06T05:37:38.228927Z","iopub.status.idle":"2022-10-06T05:37:38.577188Z","shell.execute_reply.started":"2022-10-06T05:37:38.228901Z","shell.execute_reply":"2022-10-06T05:37:38.576229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Main DataFrame\n","metadata":{}},{"cell_type":"code","source":"paths = {\n    'train_df': Path('../input/rsna-2022-cervical-spine-fracture-detection/train.csv'),\n    'train_bbox': Path('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv'),\n    'train_images': Path('../input/rsna-2022-cervical-spine-fracture-detection/train_images'),\n    'train_nifti_segments': Path('../input/rsna-2022-cervical-spine-fracture-detection/segmentations'),\n    'test_df': Path('../input/rsna-2022-cervical-spine-fracture-detection/test.csv'),\n    'test_images': Path('../input/rsna-2022-cervical-spine-fracture-detection/test_images')\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.578571Z","iopub.execute_input":"2022-10-06T05:37:38.578938Z","iopub.status.idle":"2022-10-06T05:37:38.586692Z","shell.execute_reply.started":"2022-10-06T05:37:38.578905Z","shell.execute_reply":"2022-10-06T05:37:38.585722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Preparing Main DataFrame**\n___","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(paths['train_df'])\ntest_df = pd.read_csv(paths['test_df'])","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.588218Z","iopub.execute_input":"2022-10-06T05:37:38.588841Z","iopub.status.idle":"2022-10-06T05:37:38.612632Z","shell.execute_reply.started":"2022-10-06T05:37:38.588805Z","shell.execute_reply":"2022-10-06T05:37:38.611488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['total_fractures']= train_df.loc[:,[f\"C{i}\" for i in range(1,8)]].sum(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.613879Z","iopub.execute_input":"2022-10-06T05:37:38.614574Z","iopub.status.idle":"2022-10-06T05:37:38.638647Z","shell.execute_reply.started":"2022-10-06T05:37:38.614533Z","shell.execute_reply":"2022-10-06T05:37:38.63735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Preprocessing**","metadata":{}},{"cell_type":"markdown","source":"**Creating Segment Path**","metadata":{}},{"cell_type":"code","source":"train_df['segment_path'] = train_df['StudyInstanceUID'].map(lambda x: paths['train_images']/x)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.640238Z","iopub.execute_input":"2022-10-06T05:37:38.64663Z","iopub.status.idle":"2022-10-06T05:37:38.6713Z","shell.execute_reply.started":"2022-10-06T05:37:38.646583Z","shell.execute_reply":"2022-10-06T05:37:38.670659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating nii segment path**\n\n","metadata":{}},{"cell_type":"code","source":"def add_nii_segment_path(uid):\n    base_path = paths['train_nifti_segments']\n    # path if exists else None\n    path = base_path/(uid+'.nii')\n    if path.exists():\n        return path\n    return None\n\ntrain_df['nii_segments_path'] = train_df['StudyInstanceUID'].map(add_nii_segment_path)\n\n\n# train_df['nii_segments_path'] = train_df['StudyInstanceUID'].map(lambda x: paths['train_nifti_segments']+/x)","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:38.676762Z","iopub.execute_input":"2022-10-06T05:37:38.679453Z","iopub.status.idle":"2022-10-06T05:37:39.69966Z","shell.execute_reply.started":"2022-10-06T05:37:38.679376Z","shell.execute_reply":"2022-10-06T05:37:39.698643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df    #adding segmentpath with studyinstanceUId","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:39.703681Z","iopub.execute_input":"2022-10-06T05:37:39.703992Z","iopub.status.idle":"2022-10-06T05:37:39.728936Z","shell.execute_reply.started":"2022-10-06T05:37:39.703966Z","shell.execute_reply":"2022-10-06T05:37:39.72791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Scans in reverse order**","metadata":{}},{"cell_type":"code","source":"def check_reverse_required(path):\n    paths = list(path.glob('*'))\n    paths.sort(key=lambda x:int(x.stem))\n    z_first = pydicom.dcmread(paths[0]).get(\"ImagePositionPatient\")[-1]\n    z_last = pydicom.dcmread(paths[-1]).get(\"ImagePositionPatient\")[-1]\n    if z_last < z_first:\n        return False\n    return True","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:39.730672Z","iopub.execute_input":"2022-10-06T05:37:39.731017Z","iopub.status.idle":"2022-10-06T05:37:39.73847Z","shell.execute_reply.started":"2022-10-06T05:37:39.730982Z","shell.execute_reply":"2022-10-06T05:37:39.736426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checks = train_df['segment_path'].map(check_reverse_required)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:37:39.739829Z","iopub.execute_input":"2022-10-06T05:37:39.74089Z","iopub.status.idle":"2022-10-06T05:39:20.345998Z","shell.execute_reply.started":"2022-10-06T05:37:39.740792Z","shell.execute_reply":"2022-10-06T05:39:20.344467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['reverse_required'] = checks\nCounter(checks)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:20.347857Z","iopub.execute_input":"2022-10-06T05:39:20.348466Z","iopub.status.idle":"2022-10-06T05:39:20.364434Z","shell.execute_reply.started":"2022-10-06T05:39:20.348419Z","shell.execute_reply":"2022-10-06T05:39:20.363294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices_where_reverse_required = [i for i,req in (checks.reset_index()).values if req is True]\nprint(indices_where_reverse_required)","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:20.365924Z","iopub.execute_input":"2022-10-06T05:39:20.366894Z","iopub.status.idle":"2022-10-06T05:39:20.377217Z","shell.execute_reply.started":"2022-10-06T05:39:20.366858Z","shell.execute_reply":"2022-10-06T05:39:20.376036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"**DCM Images**","metadata":{}},{"cell_type":"code","source":"def get_dcm_images(path):\n    paths = list(path.glob('*'))\n    paths.sort(key=lambda x:int(x.stem)) # sort based on slice index which is the filename: index.dcm\n    data = [pydicom.dcmread(f) for f in paths]\n    images = [apply_voi_lut(dcm.pixel_array, dcm) for dcm in data]\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:20.378979Z","iopub.execute_input":"2022-10-06T05:39:20.379336Z","iopub.status.idle":"2022-10-06T05:39:20.385725Z","shell.execute_reply.started":"2022-10-06T05:39:20.379301Z","shell.execute_reply":"2022-10-06T05:39:20.384117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.loc[60,:])\ndcm_images = get_dcm_images(train_df['segment_path'][60]) # 4 fractures\ndcm_np = np.array(dcm_images)\n# if train_df['reverse_required'][60] == True:\n#     dcm_np = dcm_np[::-1]\nsaggital_view = dcm_np[:,:,256]\nplt.imshow(saggital_view, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:20.388134Z","iopub.execute_input":"2022-10-06T05:39:20.388536Z","iopub.status.idle":"2022-10-06T05:39:35.469284Z","shell.execute_reply.started":"2022-10-06T05:39:20.3885Z","shell.execute_reply":"2022-10-06T05:39:35.468287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Nii Images**","metadata":{}},{"cell_type":"code","source":"def add_nii_segment_path(uid):\n    base_path = paths['train_nifti_segments']\n    # path if exists else None\n    path = base_path/(uid+'.nii')\n    if path.exists():\n        return path\n    return None\n\n\ndef get_nii_segments(path):\n    f = nib.load(path)\n    segmentations = f.get_fdata()[:, ::-1, ::-1].transpose(2, 1, 0)\n    return segmentations","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:35.470277Z","iopub.execute_input":"2022-10-06T05:39:35.470615Z","iopub.status.idle":"2022-10-06T05:39:35.47788Z","shell.execute_reply.started":"2022-10-06T05:39:35.470584Z","shell.execute_reply":"2022-10-06T05:39:35.476766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_index = train_df.iloc[99,:]\ndcm_images = get_dcm_images(sample_index['segment_path'])\nif sample_index['reverse_required'] == True:\n    dcm_images.sort(reverse=True)\n\nsegments = get_nii_segments(sample_index['nii_segments_path'])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:35.479177Z","iopub.execute_input":"2022-10-06T05:39:35.480472Z","iopub.status.idle":"2022-10-06T05:39:40.450947Z","shell.execute_reply.started":"2022-10-06T05:39:35.480401Z","shell.execute_reply":"2022-10-06T05:39:40.449791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\nfig, [ax1,ax2] = plt.subplots(1,2)\nax1.axis('off')\nax2.axis('off')\nimages = []\nfor i in tqdm(range(len(dcm_images))):\n    im1 = ax1.imshow(dcm_images[i], animated=True, cmap='bone')\n    im2 = ax2.imshow(segments[i,:,:], animated=True, cmap='bone')\n    if i==0:\n        ax1.imshow(dcm_images[i], cmap='bone')\n        ax2.imshow(segments[i,:,:], cmap='bone')\n    images.append([im1,im2])\n        \n\nani = animation.ArtistAnimation(fig, images, interval=50, blit=True,\n                                repeat_delay=1000)\nplt.close()\nani","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:39:40.452206Z","iopub.execute_input":"2022-10-06T05:39:40.452592Z","iopub.status.idle":"2022-10-06T05:40:19.19526Z","shell.execute_reply.started":"2022-10-06T05:39:40.452553Z","shell.execute_reply":"2022-10-06T05:40:19.193887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Statistics**","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv(paths['train_df'])\ndf = pd.DataFrame(data)","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:40:19.197342Z","iopub.execute_input":"2022-10-06T05:40:19.198275Z","iopub.status.idle":"2022-10-06T05:40:19.215208Z","shell.execute_reply.started":"2022-10-06T05:40:19.198225Z","shell.execute_reply":"2022-10-06T05:40:19.2131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(data)\n\nf_sum =  [0] * 7\nnf_sum = [0] * 7\nfor i in range(len(df)):\n    for j in range(2,9):\n            if df.iloc[i][j] == 1:\n                  f_sum[j-2] = f_sum[j-2] + 1\n            else:\n                  nf_sum[j-2] = nf_sum[j-2] + 1\n\ntf_sum = 0\ntnf_sum = 0\n\nfor i in range(len(df)):\n            if df.iloc[i][1] == 1:\n                  tf_sum = tf_sum + 1\n            else:\n                  tnf_sum = tnf_sum + 1\n\npatients_overall = [tf_sum,tnf_sum]\npie_labels = [\"Fractured\",\"Not Fractured\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:40:19.21742Z","iopub.execute_input":"2022-10-06T05:40:19.218246Z","iopub.status.idle":"2022-10-06T05:40:20.988753Z","shell.execute_reply.started":"2022-10-06T05:40:19.218195Z","shell.execute_reply":"2022-10-06T05:40:20.987738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = ['C1','C2','C3','C4','C5','C6','C7']\nplt.bar(x,f_sum,width= 0.6,label=\"Fractured\")\nplt.bar(x,nf_sum,width= 0.6,bottom = f_sum,label=\"Not Fractured\")\n\nplt.xlabel(\"Cervical spines\")\nplt.ylabel(\"No. of patients\")\nplt.title(\"Fractures as per Vertebra\")\nplt.legend(loc=(1,1),fontsize=\"12\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:40:20.990327Z","iopub.execute_input":"2022-10-06T05:40:20.990699Z","iopub.status.idle":"2022-10-06T05:40:21.219245Z","shell.execute_reply.started":"2022-10-06T05:40:20.990663Z","shell.execute_reply":"2022-10-06T05:40:21.218428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.pie(patients_overall,autopct=\"%0.1f%%\",radius=4,startangle=180)\nplt.axis(\"equal\")\nplt.title(\"Patient Fracture Overall Distribution\\n\\n\")\nplt.legend(loc=(1,1),labels=pie_labels,fontsize=\"12\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T05:40:21.220637Z","iopub.execute_input":"2022-10-06T05:40:21.220939Z","iopub.status.idle":"2022-10-06T05:40:21.343143Z","shell.execute_reply.started":"2022-10-06T05:40:21.220913Z","shell.execute_reply":"2022-10-06T05:40:21.341785Z"},"trusted":true},"execution_count":null,"outputs":[]}]}