{"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":"import numpy as np\nimport pandas as pd\nimport cv2\nimport pydicom as pdm\nimport matplotlib.pyplot as plt\nimport glob\nimport os\nimport random\nfrom tqdm.auto import tqdm\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow import keras\nimport gc\ngc.enable()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-25T12:57:10.556912Z","iopub.execute_input":"2022-08-25T12:57:10.557398Z","iopub.status.idle":"2022-08-25T12:57:18.628679Z","shell.execute_reply.started":"2022-08-25T12:57:10.557303Z","shell.execute_reply":"2022-08-25T12:57:18.627425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = {\n    'train_df': '../input/rsna-2022-cervical-spine-fracture-detection/train.csv',\n    'test_df': '../input/rsna-2022-cervical-spine-fracture-detection/test.csv',\n    'train_img': '../input/rsna-2022-cervical-spine-fracture-detection/train_images/',\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:57:18.630574Z","iopub.execute_input":"2022-08-25T12:57:18.63144Z","iopub.status.idle":"2022-08-25T12:57:18.635548Z","shell.execute_reply.started":"2022-08-25T12:57:18.631404Z","shell.execute_reply":"2022-08-25T12:57:18.634724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(cfg['train_df'], dtype={'patient_overall':int, 'C1':int, 'C2':int, 'C3':int, 'C4':int, 'C5':int, 'C6':int, 'C7':int})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:57:18.636868Z","iopub.execute_input":"2022-08-25T12:57:18.637406Z","iopub.status.idle":"2022-08-25T12:57:18.684505Z","shell.execute_reply.started":"2022-08-25T12:57:18.637375Z","shell.execute_reply":"2022-08-25T12:57:18.683711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patients = os.listdir(cfg['train_img'])\ndata_df = {'img_paths': [], 'C_labels': [], 'patient_overall':[]}\nfor patient in tqdm(patients):\n    path = os.path.join(cfg['train_img'], patient)\n    scans = os.listdir(path)\n    c_label = []\n    row = df.loc[df['StudyInstanceUID'] == patient]\n    for i in range(1, 8):\n        c_label.append(int(row[f'C{i}']))\n    patient_overall = int(row['patient_overall'])\n    for scan in scans:\n        sub_path = os.path.join(path, scan)\n        data_df['img_paths'].append(sub_path)\n        data_df['C_labels'].append(c_label)\n        data_df['patient_overall'].append(patient_overall)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:57:18.688156Z","iopub.execute_input":"2022-08-25T12:57:18.688609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_csv = pd.DataFrame(data_df)\ndata_csv.to_csv('full_data.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_csv.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ref: https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_label_demo.html\nzeroes = []\nones = []\nfor i in range(1, 8):\n    zeroes.append(df[f'C{i}'].value_counts().to_dict()[0])\n    ones.append(df[f'C{i}'].value_counts().to_dict()[1])\n\nfig, ax = plt.subplots()\nidx = np.arange(7)\nfig.set_figheight(8)\nfig.set_figwidth(18)\np1 = ax.bar(idx, zeroes, width=0.8, label='Unfractured: 0')\np2 = ax.bar(idx, ones, bottom=zeroes, width=0.8, label='Fractured: 1')\n\nax.axhline(0, color='grey', linewidth=0.8)\nax.set_ylabel('Images')\nax.set_title('Fractured and Unfractured columns in each cases')\nax.set_xticks(idx, labels=[f'C{i}' for i in range(1,8)])\nax.legend(loc='lower right')\n\n# Label with label_type 'center' instead of the default 'edge'\nax.bar_label(p1, label_type='center')\nax.bar_label(p2, label_type='center')\nax.bar_label(p2)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.pie(ones,\n        labels=[f'C{i}' for i in range(1,8)],\n        autopct='%4.2f%%',\n        pctdistance=0.8,\n        labeldistance=1.05,\n        frame=True,\n        startangle=90\n)\nplt.title('Fractures by columns')\nplt.axis('off')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}