{"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":"markdown","source":"## Taking a look at them imgs","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom glob import glob\nimport matplotlib.pylab as plt\nimport nibabel as nib \nimport pydicom as dicom\n\ntrain_images = glob(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/*\")\n\nplt.style.use('default')\nfig, axes = plt.subplots(1,6, figsize=(18,8))\ntrain_images\nfor i, ax in enumerate(axes.reshape(-1)):\n    img_path = train_images[i]\n    img = dicom.dcmread(img_path)  \n    ax.imshow(img.pixel_array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:07.954373Z","iopub.execute_input":"2022-08-16T21:07:07.955307Z","iopub.status.idle":"2022-08-16T21:07:08.967761Z","shell.execute_reply.started":"2022-08-16T21:07:07.955267Z","shell.execute_reply":"2022-08-16T21:07:08.966002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:08.970648Z","iopub.execute_input":"2022-08-16T21:07:08.971167Z","iopub.status.idle":"2022-08-16T21:07:08.979841Z","shell.execute_reply.started":"2022-08-16T21:07:08.971122Z","shell.execute_reply":"2022-08-16T21:07:08.978709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What's in those csvs?","metadata":{}},{"cell_type":"code","source":"bb_df = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')\nprint(bb_df.shape)\nbb_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:08.981623Z","iopub.execute_input":"2022-08-16T21:07:08.982059Z","iopub.status.idle":"2022-08-16T21:07:09.014615Z","shell.execute_reply.started":"2022-08-16T21:07:08.982026Z","shell.execute_reply":"2022-08-16T21:07:09.013193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bb_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.016298Z","iopub.execute_input":"2022-08-16T21:07:09.016647Z","iopub.status.idle":"2022-08-16T21:07:09.047939Z","shell.execute_reply.started":"2022-08-16T21:07:09.016615Z","shell.execute_reply":"2022-08-16T21:07:09.046852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')\nprint(test.shape)\ntrain = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\nprint(train.shape)\ntrain.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.050511Z","iopub.execute_input":"2022-08-16T21:07:09.051081Z","iopub.status.idle":"2022-08-16T21:07:09.075643Z","shell.execute_reply.started":"2022-08-16T21:07:09.051047Z","shell.execute_reply":"2022-08-16T21:07:09.074574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.077013Z","iopub.execute_input":"2022-08-16T21:07:09.078204Z","iopub.status.idle":"2022-08-16T21:07:09.116595Z","shell.execute_reply.started":"2022-08-16T21:07:09.078152Z","shell.execute_reply":"2022-08-16T21:07:09.115615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Viz data from csvs...","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt \n\ndatacols = ['C1', 'C2', 'C3', 'C4', 'C5','C6', 'C7']\n\n\nvt = train.copy() # safely use traindf for viz\nvt_desc = vt[datacols].describe() # get descript\n\nvt['sum'] = vt[datacols].sum(axis = 1)\n\nvt.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.118034Z","iopub.execute_input":"2022-08-16T21:07:09.118406Z","iopub.status.idle":"2022-08-16T21:07:09.156225Z","shell.execute_reply.started":"2022-08-16T21:07:09.118374Z","shell.execute_reply":"2022-08-16T21:07:09.155185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vt.describe()['sum']","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.157438Z","iopub.execute_input":"2022-08-16T21:07:09.158043Z","iopub.status.idle":"2022-08-16T21:07:09.194507Z","shell.execute_reply.started":"2022-08-16T21:07:09.158007Z","shell.execute_reply":"2022-08-16T21:07:09.193196Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"75% of patients have at least 1 fracture, not all patients have fractures, patient with most fractures has 6 diagnosed fracs.","metadata":{}},{"cell_type":"code","source":"import scipy.stats as stats\n\nsns.histplot(vt['sum'],kde=True);\nplt.axvline(vt['sum'].mean(),color = 'blue');\nprint('sum')\nprint('Mean = '+str(vt['sum'].mean()))\nprint('Std = '+str(vt['sum'].std()))\nprint('Skew = '+str(stats.skew(vt['sum'])));\nprint('Kurtosis = '+str(stats.kurtosis(vt['sum'])));\nplt.ylabel('# pat');\nplt.xlabel('# frac');","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.196016Z","iopub.execute_input":"2022-08-16T21:07:09.196476Z","iopub.status.idle":"2022-08-16T21:07:09.475727Z","shell.execute_reply.started":"2022-08-16T21:07:09.196438Z","shell.execute_reply":"2022-08-16T21:07:09.474501Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2,4, figsize=(20,4))\n\n# for col in datacols:\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i<7:\n        thiscol = datacols[i]\n        sns.histplot(vt[thiscol],ax = ax,kde=True);\n        ax.axvline(vt[thiscol].mean(),color = 'blue');\n        ax.legend([thiscol])\n        ax.set_ylabel('# pat')\n        ax.set_xlabel('# frac')\n    \n        print('---------')\n        print(thiscol)\n        print('Mean = '+str(vt[thiscol].mean()))\n        print('Std = '+str(vt[thiscol].std()))\n        print('Skew = '+str(stats.skew(vt[thiscol])))\n        print('Kurtosis = '+str(stats.kurtosis(vt[thiscol])))\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:09.477649Z","iopub.execute_input":"2022-08-16T21:07:09.478219Z","iopub.status.idle":"2022-08-16T21:07:11.148852Z","shell.execute_reply.started":"2022-08-16T21:07:09.478179Z","shell.execute_reply":"2022-08-16T21:07:11.147722Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(vt['patient_overall'],kde=True);\nplt.axvline(vt['patient_overall'].mean(),color = 'blue');\nprint('patient_overall')\nprint('Mean = '+str(vt['patient_overall'].mean()))\nprint('Std = '+str(vt['patient_overall'].std()))\nprint('Skew = '+str(stats.skew(vt['patient_overall'])));\nprint('Kurtosis = '+str(stats.kurtosis(vt['patient_overall'])));\nplt.ylabel('# pat');\nplt.xlabel('# frac');","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:11.150121Z","iopub.execute_input":"2022-08-16T21:07:11.150447Z","iopub.status.idle":"2022-08-16T21:07:11.354534Z","shell.execute_reply.started":"2022-08-16T21:07:11.150418Z","shell.execute_reply":"2022-08-16T21:07:11.353663Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mf = vt.loc[vt['sum']>1].drop('patient_overall',axis =1) # get patients with multiple fractures\n\nmf.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:11.355821Z","iopub.execute_input":"2022-08-16T21:07:11.35641Z","iopub.status.idle":"2022-08-16T21:07:11.39544Z","shell.execute_reply.started":"2022-08-16T21:07:11.356374Z","shell.execute_reply":"2022-08-16T21:07:11.394344Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datacols = ['C1', 'C2', 'C3', 'C4', 'C5','C6', 'C7']\n\n\nfig, axes = plt.subplots(2,4, figsize=(20,4))\n\n# for col in datacols:\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i<7:\n        thiscol = datacols[i]\n        sns.histplot(mf[thiscol],ax = ax,kde=True);\n        ax.axvline(mf[thiscol].mean(),color = 'blue');\n        ax.legend([thiscol])\n        ax.set_ylabel('# pat')\n        ax.set_xlabel('# frac')\n    \n        print('---------')\n        print(thiscol)\n        print('Mean = '+str(mf[thiscol].mean()))\n        print('Std = '+str(mf[thiscol].std()))\n        print('Skew = '+str(stats.skew(mf[thiscol])))\n        print('Kurtosis = '+str(stats.kurtosis(mf[thiscol])))\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:11.39715Z","iopub.execute_input":"2022-08-16T21:07:11.397623Z","iopub.status.idle":"2022-08-16T21:07:13.183799Z","shell.execute_reply.started":"2022-08-16T21:07:11.39759Z","shell.execute_reply":"2022-08-16T21:07:13.182568Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## looking at segmentations","metadata":{}},{"cell_type":"code","source":"bb_df","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:07:13.187242Z","iopub.execute_input":"2022-08-16T21:07:13.188141Z","iopub.status.idle":"2022-08-16T21:07:13.208333Z","shell.execute_reply.started":"2022-08-16T21:07:13.188099Z","shell.execute_reply":"2022-08-16T21:07:13.206659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path, size = 512):\n    try:\n        path = '../input/rsna-2022-cervical-spine-fracture-detection/'+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        gc.collect()\n        return [img,cv2.cvtColor(data.reshape(512,512), cv2.COLOR_GRAY2RGB).transpose(2,0,1)]\n    except:\n        return [0,np.zeros((3,512,512)).transpose(2,0,1)]","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:19:39.557212Z","iopub.execute_input":"2022-08-16T21:19:39.55869Z","iopub.status.idle":"2022-08-16T21:19:39.567574Z","shell.execute_reply.started":"2022-08-16T21:19:39.558643Z","shell.execute_reply":"2022-08-16T21:19:39.5664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.StudyInstanceUID.values[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:50:19.596673Z","iopub.execute_input":"2022-08-16T21:50:19.59721Z","iopub.status.idle":"2022-08-16T21:50:19.604189Z","shell.execute_reply.started":"2022-08-16T21:50:19.597169Z","shell.execute_reply":"2022-08-16T21:50:19.603279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pessoa = train.StudyInstanceUID.values[0]\npessoa = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/'+pessoa+'/1.dcm'\nimg=dicom.dcmread(pessoa)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:21:42.539872Z","iopub.execute_input":"2022-08-16T21:21:42.540341Z","iopub.status.idle":"2022-08-16T21:21:42.581879Z","shell.execute_reply.started":"2022-08-16T21:21:42.540303Z","shell.execute_reply":"2022-08-16T21:21:42.580989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pessoa = train.StudyInstanceUID.values[0]\nprint(pessoa)\n\nimg,vec = load_dicom(pessoa)\nprint(vec.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-16T21:20:41.709528Z","iopub.execute_input":"2022-08-16T21:20:41.710031Z","iopub.status.idle":"2022-08-16T21:20:41.719557Z","shell.execute_reply.started":"2022-08-16T21:20:41.709991Z","shell.execute_reply":"2022-08-16T21:20:41.7186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = load_dicom()","metadata":{},"execution_count":null,"outputs":[]}]}