{"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":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#BC13FE; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #BC13FE\">RSNA : RadioLogical Society of North America </p>","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-02T17:30:26.185911Z","iopub.execute_input":"2023-08-02T17:30:26.186362Z","iopub.status.idle":"2023-08-02T17:30:26.221936Z","shell.execute_reply.started":"2023-08-02T17:30:26.186328Z","shell.execute_reply":"2023-08-02T17:30:26.221092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#BC13FE solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThe $RSNA$ $2023$ $Abdominal$ $Trauma$ $Detection$ competition is a `machine learning challenge` that aims to `develop algorithms` to `detect abdominal trauma` in $Computed$ $Tomography$ $(CT)$ images. The competition is hosted by the $Radiological$ $Society$ of $North$ $America$ $(RSNA)$ \n    \nI couldnt find a good meme related to $RSNA$, so here is $Lightning$ $McQueen$ to cheer $($ $Ka$ $Choww$ $)$\n    \n<img src = \"https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExdHUxOHhmNm9zY25jejFzYnBjNHZpdjY1eG10NWRnZXZtcmF1Znk1MSZlcD12MV9naWZzX3NlYXJjaCZjdD1n/B1CrvUCoMxhy8/giphy.gif\" width = 300>\n\nThanks to **[Theo Viel](https://www.kaggle.com/theoviel)** for providing small chunks of data to experiment \n    \n$KUDOS$ $!!!$","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#21FC0D; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #21FC0D\">1 | Data 📊</p>","metadata":{}},{"cell_type":"code","source":"import pandas as pd \nimport pydicom","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-02T17:30:27.24965Z","iopub.execute_input":"2023-08-02T17:30:27.250052Z","iopub.status.idle":"2023-08-02T17:30:27.395736Z","shell.execute_reply.started":"2023-08-02T17:30:27.250017Z","shell.execute_reply":"2023-08-02T17:30:27.39463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#21FC0D solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n    \nOur main data is situated in a `CSV` file $--->$ `train.csv` ","metadata":{}},{"cell_type":"code","source":"train_csv = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv\")\nprint(train_csv.shape)\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T17:30:28.681732Z","iopub.execute_input":"2023-08-02T17:30:28.682164Z","iopub.status.idle":"2023-08-02T17:30:28.732942Z","shell.execute_reply.started":"2023-08-02T17:30:28.682131Z","shell.execute_reply":"2023-08-02T17:30:28.731918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#21FC0D solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThis csv file contains the `patient_id` and further explanatory columns \n\n* $Patient$ $ID$ `patient_id` - THis column contains the ids of $3,147$ patients.\n\nFurther columns denote what symptons were shown by the specific patient. We can use the `patient_id` to get information from `train_images` Folder.\n\n$Train$ $Images$ is a big $3$ $level$ nested - directory, contianing images for the specific patient . There are different folders in the Direcotry that denote the `patient_id`\n\nThe `images` are `labeled` with the `presence` or `absence of abdominal trauma`, as well as the `location of any injuries`. The data is provided in `DICOM` format, which is a standard format for medical images.\n    \nThe type of data in the competition dataset is $CT$ images of `patients with abdominal trauma`. The images are labeled with the `presence`/`absence` of `abdominal trauma`, as well as the `location of any injuries`. The data is provided in `DICOM` `format`, which is a standard format for medical images.\n\nWe can read the `DCIM` file through `pydicom`","metadata":{}},{"cell_type":"code","source":"image_file = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057/1001.dcm\"\nds = pydicom.read_file(image_file)\n\nds","metadata":{"execution":{"iopub.status.busy":"2023-08-02T17:30:30.801924Z","iopub.execute_input":"2023-08-02T17:30:30.802347Z","iopub.status.idle":"2023-08-02T17:30:30.821458Z","shell.execute_reply.started":"2023-08-02T17:30:30.802313Z","shell.execute_reply":"2023-08-02T17:30:30.820697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#FFF700; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #FFF700\">2 | Visualization 🔬</p>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport tqdm\nimport os \n\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nimport seaborn as sns\n\nfrom IPython.display import HTML","metadata":{"execution":{"iopub.status.busy":"2023-08-02T17:30:32.533358Z","iopub.execute_input":"2023-08-02T17:30:32.533741Z","iopub.status.idle":"2023-08-02T17:30:33.461915Z","shell.execute_reply.started":"2023-08-02T17:30:32.533712Z","shell.execute_reply":"2023-08-02T17:30:33.460655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#FFF700 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThanks to **[Yuanjian Li](https://www.kaggle.com/yuanjianli)=>[EDA with Animation BeginnerFriendly](https://www.kaggle.com/code/yuanjianli/eda-with-animation-beginnerfriendly/comments)/[Jocelyn Dumlao\n](https://www.kaggle.com/jocelyndumlao)=>[Unleashing the Healing Potential: Abdominal Trauma](https://www.kaggle.com/code/jocelyndumlao/unleashing-the-healing-potential-abdominal-trauma)** for providing great $CSV-EDA$ Do checkout","metadata":{}},{"cell_type":"code","source":"organ_columns = ['bowel', 'extravasation', 'kidney', 'liver', 'spleen']\n\norgan_counts = pd.DataFrame()\norgan_counts['Organ'] = train_csv.columns[1:]\norgan_counts[\"count\"] = [0 for _ in range(organ_counts.shape[0])]\nfor index , column in enumerate(train_csv.columns[1:]):\n    organ_counts['count'][index] = train_csv[column].sum()\n    \nplt.figure(figsize=(10, 3))\nsns.barplot(data=organ_counts.sort_values(by=['count']), x='Organ', y='count')\nplt.xticks(rotation=90)\nplt.title(\"Distribution of Injury\")\nplt.xlabel(\"Injury --->\")\nplt.ylabel(\"Count --->\")\nplt.show()","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-08-02T06:15:07.234464Z","iopub.execute_input":"2023-08-02T06:15:07.234896Z","iopub.status.idle":"2023-08-02T06:15:07.642292Z","shell.execute_reply.started":"2023-08-02T06:15:07.234863Z","shell.execute_reply":"2023-08-02T06:15:07.641189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#FFF700 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThanks to **[Franklin Shih0617](https://www.kaggle.com/franklinshih0617)=>[RSNA Abdominal Trauma Detect EDA animation](https://www.kaggle.com/code/franklinshih0617/rsna-abdominal-trauma-detect-eda-animation)** for providing great $Animations$. Do checkout","metadata":{}},{"cell_type":"code","source":"file_1 = ['/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057/' + file\n         for file in os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057')]\nfile_2 = ['/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/51033/' + file\n         for file in os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/51033')]\nsample_files = file_1 + file_2\n\nsample_vid = [pydicom.dcmread(file).pixel_array for file in tqdm.tqdm(sample_files , total = len(sample_files))]\n\nfig, ax = plt.subplots()\nim = ax.imshow(sample_vid[0], cmap=plt.cm.bone)\n\nupdate = lambda i : im.set_array(sample_vid[i])\n\nani = animation.FuncAnimation(fig, update, frames=range(len(sample_vid)), repeat=True)\n\nHTML(ani.to_jshtml())","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-08-02T06:21:40.534082Z","iopub.execute_input":"2023-08-02T06:21:40.534506Z","iopub.status.idle":"2023-08-02T06:23:42.401093Z","shell.execute_reply.started":"2023-08-02T06:21:40.534475Z","shell.execute_reply":"2023-08-02T06:23:42.400274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#FFC0CB; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #FFC0CB\">3 | Preprocessing 🔨</p>\n\nOur data is in Dicom File, but we want them in `NPY` format, which can take a lot of time, but lets try for the first $80$ inputs","metadata":{}},{"cell_type":"code","source":"train_list = sorted(os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'))[:79]\n\nfor folder_1 in tqdm.tqdm(train_list , total = len(train_list)):\n    \n    folder_1_list = sorted(os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/' + folder_1))\n    \n    os.makedirs('/kaggle/working/Inputs/' + folder_1 + '/')\n    lis = list()\n    \n    for folder_2 in folder_1_list:\n        \n        folder_2_list = sorted(os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/' + folder_1 + '/' + folder_2))\n        \n        for files in folder_2_list:\n            \n            file = pydicom.read_file('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/' + folder_1 + '/' + folder_2 + '/' + files)\n            \n            arr = file.pixel_array\n            arr = np.resize(arr , new_shape = (512 , 512))\n            lis.append(arr)\n            \n        np.save('/kaggle/working/Inputs/' + folder_1 + '/' + 'file', np.stack(lis , -1))","metadata":{"execution":{"iopub.status.busy":"2023-08-02T17:33:10.907567Z","iopub.execute_input":"2023-08-02T17:33:10.908021Z","iopub.status.idle":"2023-08-02T17:35:50.930223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#87CEEB; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #87CEEB\">4 | Ending ☑️</p>\n\n<div style=\"border-radius:10px; border:#87CEEB solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n**THAT IT FOR TODAY GUYS**\n\n**WE WILL GO DEEPER INTO THE DATA IN THE UPCOMING VERSIONS**\n\n**PLEASE COMMENT YOUR THOUGHTS, HIHGLY APPRICIATED**\n\n**DONT FORGET TO MAKE AN UPVOTE, IF YOU LIKED MY WORK :)**\n    \n<IMG SRC = \"https://i.imgflip.com/19aadg.jpg\">\n\n**PEACE OUT !!!! :)**","metadata":{}}]}