{"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 ../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-08-11T06:08:34.499032Z","iopub.execute_input":"2022-08-11T06:08:34.499512Z","iopub.status.idle":"2022-08-11T06:08:48.050812Z","shell.execute_reply.started":"2022-08-11T06:08:34.49942Z","shell.execute_reply":"2022-08-11T06:08:48.04913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport pydicom as pyd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport nibabel as nib\nfrom glob import glob\nfrom tqdm import tqdm\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-11T06:08:48.053977Z","iopub.execute_input":"2022-08-11T06:08:48.054356Z","iopub.status.idle":"2022-08-11T06:08:49.160497Z","shell.execute_reply.started":"2022-08-11T06:08:48.054316Z","shell.execute_reply":"2022-08-11T06:08:49.159227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images='../input/rsna-2022-cervical-spine-fracture-detection/train_images'\ntest_images='../input/rsna-2022-cervical-spine-fracture-detection/test_images/'\nseg_images='../input/rsna-2022-cervical-spine-fracture-detection/segmentations/'\n\ntrain_df=pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntest_df=pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')\nbbox_df=pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.16205Z","iopub.execute_input":"2022-08-11T06:08:49.162426Z","iopub.status.idle":"2022-08-11T06:08:49.207913Z","shell.execute_reply.started":"2022-08-11T06:08:49.162391Z","shell.execute_reply":"2022-08-11T06:08:49.207079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training data shape:' ,train_df.shape)\nprint('Test data shape: ',test_df.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.209396Z","iopub.execute_input":"2022-08-11T06:08:49.210083Z","iopub.status.idle":"2022-08-11T06:08:49.216112Z","shell.execute_reply.started":"2022-08-11T06:08:49.210047Z","shell.execute_reply":"2022-08-11T06:08:49.21496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training head\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.218795Z","iopub.execute_input":"2022-08-11T06:08:49.219554Z","iopub.status.idle":"2022-08-11T06:08:49.244622Z","shell.execute_reply.started":"2022-08-11T06:08:49.21952Z","shell.execute_reply":"2022-08-11T06:08:49.243699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.246036Z","iopub.execute_input":"2022-08-11T06:08:49.246616Z","iopub.status.idle":"2022-08-11T06:08:49.257424Z","shell.execute_reply.started":"2022-08-11T06:08:49.246584Z","shell.execute_reply":"2022-08-11T06:08:49.25631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Target Distribution\n#We have 7 targets along with one 'patient_overall' target which will be 1 if any of the vertebrae are fractured\n#Lets plot the distribution of 'patient_overall target'\n\ntarget_dist=train_df['patient_overall'].value_counts()\nprint('Percentage of normal scans: {:.1f}%'.format(target_dist[0]*100/2019))\nprint('Percentage of fractured scans: {:.1f}%'.format(target_dist[1]*100/2019))\n\nsns.displot(train_df['patient_overall'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.259028Z","iopub.execute_input":"2022-08-11T06:08:49.259702Z","iopub.status.idle":"2022-08-11T06:08:49.574851Z","shell.execute_reply.started":"2022-08-11T06:08:49.259658Z","shell.execute_reply":"2022-08-11T06:08:49.573793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plot other classes\ntarget_cols=train_df.columns[2:]\nr=c=3\nfig=plt.figure(figsize=(15,10))\n\nfor i in range(1,r*c+1):\n    if i<=7:\n        fig.add_subplot(r,c,i)\n        sns.distplot(train_df[target_cols[i-1]])\nplt.show()\n\n#Now this must look like we have huge class imbalance for each scan but at overall level, \n#there's no class imabalance and this could mean that a single scan might have multiple vertebrae fracture.\n#This means that we have multilabel classification task.","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:49.576252Z","iopub.execute_input":"2022-08-11T06:08:49.576616Z","iopub.status.idle":"2022-08-11T06:08:51.139662Z","shell.execute_reply.started":"2022-08-11T06:08:49.576585Z","shell.execute_reply":"2022-08-11T06:08:51.138593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('We have {} total stanceids and {} unique stance ids'.format(\n    2019,train_df['StudyInstanceUID'].nunique()))\n\n#Average count of scans for each patient\nscan_count=[ ]\nfor id_ in tqdm(train_df['StudyInstanceUID']):\n    scan_count.append(len(os.listdir(os.path.join(train_images,id_))))\n\nprint('Average count of scans: ',np.round(np.mean(scan_count)))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:08:51.140912Z","iopub.execute_input":"2022-08-11T06:08:51.141905Z","iopub.status.idle":"2022-08-11T06:09:46.685599Z","shell.execute_reply.started":"2022-08-11T06:08:51.141868Z","shell.execute_reply":"2022-08-11T06:09:46.684392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets plot some samples alongn with their masks\nr,c=3,3\nfig=plt.figure(figsize=(10,10))\nfor i in range(1,r*c+1):\n    id_=train_df['StudyInstanceUID'].values[i]\n    images_path=glob(os.path.join(train_images,id_,'*.dcm'))\n    img=pyd.read_file(images_path[i]).pixel_array\n    \n    fig.add_subplot(r,c,i)\n    plt.imshow(img,cmap='bone')\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-11T06:09:46.686974Z","iopub.execute_input":"2022-08-11T06:09:46.687448Z","iopub.status.idle":"2022-08-11T06:09:48.036767Z","shell.execute_reply.started":"2022-08-11T06:09:46.687403Z","shell.execute_reply":"2022-08-11T06:09:48.035675Z"},"trusted":true},"execution_count":null,"outputs":[]}]}