{"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":"<div style=\"color:white;\n           display:fill;\n           border-radius:2px;\n           background-color:skyblue;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n          text-align: center;\n          font-size:150%;\n          color:brown;\">\n          RSNA 2023 Abdominal Trauma Detection\n</p>\n</div> \n<p><center style=\"color:black; font-family: times; font-size: 40px;\">Detect and Classify Traumatic Abdominal Injuries</center></p>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:pink;\n           font-size:200%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:green;\">\n           🤓🤓 Overview of This Competition 🤓🤓\n</p>\n<style>\n        h1{text-align: center;}\n </style>  ","metadata":{}},{"cell_type":"markdown","source":"1. <p style=\"font-family: arial; font-size: 20px;\">👉🏻 <b>Traumatic injury</b> stands as the leading cause of <b>death</b> within the initial four decades of life, representing a significant global public health challenge. The number of annual fatalities worldwide due to traumatic injury is estimated to exceed <b>5</b> million.</p>\n\n2. <p style=\"font-family: arial; font-size: 20px;\">👉🏻 The timely and precise <b>diagnosis</b> of traumatic injuries is of utmost importance as it enables the initiation of suitable and prompt <b>interventions</b>, ultimately leading to substantial <b>enhancements</b> in patient outcomes and <b>survival rates</b>.</p>\n\n3. <p style=\"font-family: arial; font-size: 20px;\">👉🏻 Due to its capability to produce detailed <b>cross-sectional images</b> of the abdomen, <b>CT</b> is the most popular choice for assessing patients with suspected abdominal injuries. </p>\n\n4. <p style=\"font-family: arial; font-size: 20px;\">👉🏻 Interpreting CT scans for abdominal trauma could be complex and time-inefficient for human-being, even for experts in this field, which could be possibly accomplished by <b>advanced AI/ML solutions</b> in an efficient manner.</p>\n\n![](https://www.kaggle.com/competitions/52254/images/header)\n\n# The ultimate goal is to classify and detect traumatic abdominal jnjuries from CT scans, via ML-aided solutions! ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:red;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:black;\">\n           📚📚Importing Libraries<span style='font-size:50px;'>&#128295;</span>\n</p>\n<style>\n        h1{text-align: center;}\n </style> \n","metadata":{}},{"cell_type":"code","source":"%%capture\n# Install required packages \"dataprep\" and \"seaborn_image\" quietly using pip.\n# -U: Upgrade the package to the latest version, if it's already installed.\n# -q: Quiet mode, suppresses the output and makes the installation less verbose.\n# -qU: A combination of -q and -U.\n!pip install -qU scikit-learn\n!pip install -qU seaborn_image \n!pip install -qU wandb","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:18:03.390033Z","iopub.execute_input":"2023-07-28T04:18:03.390429Z","iopub.status.idle":"2023-07-28T04:18:40.530822Z","shell.execute_reply.started":"2023-07-28T04:18:03.390397Z","shell.execute_reply":"2023-07-28T04:18:40.529332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport warnings\n# Ignore warning messages for cleaner output\nwarnings.filterwarnings(\"ignore\")\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport cv2\nfrom glob import glob\nimport os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # to avoid too many logging messages\nimport random\nimport seaborn as sns\nimport seaborn_image as snsi\nimport pydicom\nimport ydata_profiling\nimport matplotlib.animation as animation\nfrom IPython.display import HTML\nimport wandb # WandB is a central dashboard to keep track of your hyperparameters, system metrics, and predictions so you can compare models live, and share your findings.\nfrom tqdm import tqdm\nimport sklearn\nimport tensorflow as tf\n\n# Enable inline plotting for matplotlib to display plots directly in the notebook\n%matplotlib inline\n\n# Set some default configurations for matplotlib\n# Use the \"fivethirtyeight\" style for the plots\nplt.style.use(\"fivethirtyeight\")\n# Set figure settings to automatically adjust layout\nplt.rc(\"figure\", autolayout=True)\n# Customize axes labels and title properties\nplt.rc(\n    \"axes\",\n    labelweight=\"bold\",\n    labelsize=\"large\",\n    titleweight=\"bold\",\n    titlesize=14,\n    titlepad=10,\n)\n\n# Print a message indicating that the required packages are installed and libraries are imported\nprint('Required Packages Installed and Imported!')\nprint('='*30)\nprint('Version Info of Imported Packages:\\n')\nprint('Numpy:', np.__version__)\nprint('Pandas:', pd.__version__)\nprint('Sklearn:', sklearn.__version__)\nprint('Tensorflow:',tf.__version__)\nprint('Weights and Bias:', wandb.__version__)\nprint('='*30)\ndef seeding(SEED):\n    np.random.seed(SEED)\n    random.seed(SEED)\n    os.environ['PYTHONHASHSEED'] = str(SEED)\n#     os.environ['TF_CUDNN_DETERMINISTIC'] = str(SEED)\n    tf.random.set_seed(SEED)\n    print('Seeded for Reproducibility!')\nseeding(42)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:18:40.535312Z","iopub.execute_input":"2023-07-28T04:18:40.536224Z","iopub.status.idle":"2023-07-28T04:18:40.558195Z","shell.execute_reply.started":"2023-07-28T04:18:40.536175Z","shell.execute_reply":"2023-07-28T04:18:40.556458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:violet;\n           font-size:200%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:brown;\">\n          <span style='font-size:50px;'>&#128229;</span> Loading and Inspecting the Train dataset<span style='font-size:50px;'>&#128160;</span>\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n\n</div>\n","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv') ","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:18:40.560122Z","iopub.execute_input":"2023-07-28T04:18:40.560756Z","iopub.status.idle":"2023-07-28T04:18:40.583733Z","shell.execute_reply.started":"2023-07-28T04:18:40.560721Z","shell.execute_reply":"2023-07-28T04:18:40.5827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.head().style.set_properties(**{'background-color': 'black',\n                                                'color': 'lawngreen',\n                                                'border': '1.5px  white'}))\nprint(f'Shape of train: {train.shape}\\n', '='*30)\nprint(f'Columns of train:\\n{train.columns}\\n', '='*30)\nprint(f'Type of elements per column:\\n{train.dtypes}')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:18:40.586752Z","iopub.execute_input":"2023-07-28T04:18:40.587188Z","iopub.status.idle":"2023-07-28T04:18:40.614296Z","shell.execute_reply.started":"2023-07-28T04:18:40.587151Z","shell.execute_reply":"2023-07-28T04:18:40.613192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.profile_report() # alternatively, we can use pandas_profilling to generate report for dataframe.","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:18:40.615841Z","iopub.execute_input":"2023-07-28T04:18:40.616813Z","iopub.status.idle":"2023-07-28T04:19:06.565507Z","shell.execute_reply.started":"2023-07-28T04:18:40.616721Z","shell.execute_reply":"2023-07-28T04:19:06.564598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_wo_id = train.iloc[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:06.567391Z","iopub.execute_input":"2023-07-28T04:19:06.567991Z","iopub.status.idle":"2023-07-28T04:19:06.573358Z","shell.execute_reply.started":"2023-07-28T04:19:06.567958Z","shell.execute_reply":"2023-07-28T04:19:06.572489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_wo_id = train_wo_id.sum().to_frame().reset_index().rename(columns={'index': 'Injury Type', 0: 'Counts'})\ntrain_wo_id","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:06.57471Z","iopub.execute_input":"2023-07-28T04:19:06.575473Z","iopub.status.idle":"2023-07-28T04:19:06.593823Z","shell.execute_reply.started":"2023-07-28T04:19:06.575441Z","shell.execute_reply":"2023-07-28T04:19:06.592856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot out the Sorted Counts of Injury Types.\n\nplt.figure(figsize=(10, 8))\nsns.barplot(data=train_wo_id.sort_values(by=['Counts']), x='Injury Type', y='Counts')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:06.595207Z","iopub.execute_input":"2023-07-28T04:19:06.596078Z","iopub.status.idle":"2023-07-28T04:19:07.082452Z","shell.execute_reply.started":"2023-07-28T04:19:06.596041Z","shell.execute_reply":"2023-07-28T04:19:07.081514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:gold;\n           font-size:200%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:black;\">          \n          <span style='font-size:50px;'>&#128221;</span> First Impression on Train Images <span style='font-size:50px;'>&#128227;</span>\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n    \n</div>\n","metadata":{}},{"cell_type":"code","source":"def random_sample_files(directory_path, sample_size):\n    # Get a list of all files in the directory\n    file_list = os.listdir(directory_path)\n    \n    # Check if the number of files in the directory is smaller than the desired sample size\n    if len(file_list) < sample_size:\n        raise ValueError(\"Sample size is greater than the number of files in the directory.\")\n    \n    # Randomly sample files from the list\n    random_sample = random.sample(file_list, sample_size)\n    \n    return random_sample","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:07.084119Z","iopub.execute_input":"2023-07-28T04:19:07.084812Z","iopub.status.idle":"2023-07-28T04:19:07.091743Z","shell.execute_reply.started":"2023-07-28T04:19:07.084778Z","shell.execute_reply":"2023-07-28T04:19:07.090655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_samples = random_sample_files('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/1027/60982', 6)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:07.096392Z","iopub.execute_input":"2023-07-28T04:19:07.096898Z","iopub.status.idle":"2023-07-28T04:19:07.106157Z","shell.execute_reply.started":"2023-07-28T04:19:07.09687Z","shell.execute_reply":"2023-07-28T04:19:07.105053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, fig in enumerate(image_samples, start=1):\n    plt.figure(figsize=(10, 8))\n    snsi.imgplot(pydicom.read_file(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/1027/60982/\" + fig).pixel_array, cmap=plt.cm.turbo, cbar=False)\n    plt.title(f'Image Sample {i}')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:07.107771Z","iopub.execute_input":"2023-07-28T04:19:07.108147Z","iopub.status.idle":"2023-07-28T04:19:08.868379Z","shell.execute_reply.started":"2023-07-28T04:19:07.108115Z","shell.execute_reply":"2023-07-28T04:19:08.867452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Acknowledgement: This chunk of codes is heavily inspired by Franklin Shih0617's notebook: https://www.kaggle.com/code/franklinshih0617/rsna-eda-with-animation\n\n# Sample out dcm files with an interval of n for a quick check. We can alter 1 with n in the slicing operator [::1] with n, to make the animation less smooth but more time-efficient.\ndcms = sorted(glob('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/1027/24515/*.dcm'))[::1]\n\n# Read the dcm files and extract the pixel arrays\ndcmis = [pydicom.dcmread(f).pixel_array for f in dcms]\n\n# Create a figure and axis for plotting\nfig, ax = plt.subplots(figsize=(10, 8))\n\n# Plot the first frame and keep the plot object for later\nim = ax.imshow(dcmis[0], cmap=plt.cm.turbo)\n\n# Create the animation\namn = animation.FuncAnimation(fig, lambda i : im.set_array(dcmis[i]), frames=range(len(dcmis)), repeat=True, repeat_delay=50000)\n\n# Demonstrate the animation\nHTML(amn.to_jshtml())\n","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:08.869889Z","iopub.execute_input":"2023-07-28T04:19:08.870943Z","iopub.status.idle":"2023-07-28T04:19:53.194216Z","shell.execute_reply.started":"2023-07-28T04:19:08.870908Z","shell.execute_reply":"2023-07-28T04:19:53.193195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:yellow;\n           font-size:200%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:brown;\">\n          <span style='font-size:50px;'>&#128229;</span> Loading and Inspecting the Train Series Meta dataset<span style='font-size:50px;'>&#128160;</span>\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n\n</div>\n","metadata":{}},{"cell_type":"code","source":"train_series_meta = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:53.195846Z","iopub.execute_input":"2023-07-28T04:19:53.196495Z","iopub.status.idle":"2023-07-28T04:19:53.210586Z","shell.execute_reply.started":"2023-07-28T04:19:53.196458Z","shell.execute_reply":"2023-07-28T04:19:53.208835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_series_meta.head().style.set_properties(**{'background-color': 'black',\n                                                'color': 'lawngreen',\n                                                'border': '1.5px  white'}))\nprint(f'Shape of Train Series Meta: {train_series_meta.shape}\\n', '='*30)\nprint(f'Columns of Train Series Meta:\\n{train_series_meta.columns}\\n', '='*30)\nprint(f'Type of elements per column:\\n{train_series_meta.dtypes}')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:53.212755Z","iopub.execute_input":"2023-07-28T04:19:53.213655Z","iopub.status.idle":"2023-07-28T04:19:53.23501Z","shell.execute_reply.started":"2023-07-28T04:19:53.213608Z","shell.execute_reply":"2023-07-28T04:19:53.233802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta.profile_report()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:19:53.237057Z","iopub.execute_input":"2023-07-28T04:19:53.237897Z","iopub.status.idle":"2023-07-28T04:20:09.608854Z","shell.execute_reply.started":"2023-07-28T04:19:53.237853Z","shell.execute_reply":"2023-07-28T04:20:09.607954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:200%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:brown;\">\n          <span style='font-size:50px;'>&#128229;</span> Loading and Inspecting the Image Level Labels dataset<span style='font-size:50px;'>&#128160;</span>\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n\n</div>\n","metadata":{}},{"cell_type":"code","source":"image_level_labels = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/image_level_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:20:09.610315Z","iopub.execute_input":"2023-07-28T04:20:09.611228Z","iopub.status.idle":"2023-07-28T04:20:09.628449Z","shell.execute_reply.started":"2023-07-28T04:20:09.611193Z","shell.execute_reply":"2023-07-28T04:20:09.627361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(image_level_labels.head().style.set_properties(**{'background-color': 'black',\n                                                'color': 'lawngreen',\n                                                'border': '1.5px  white'}))\nprint(f'Shape of Image Level Labels: {image_level_labels.shape}\\n', '='*30)\nprint(f'Columns of Image Level Labels:\\n{image_level_labels.columns}\\n', '='*30)\nprint(f'Type of elements per column:\\n{image_level_labels.dtypes}')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:20:09.630089Z","iopub.execute_input":"2023-07-28T04:20:09.630702Z","iopub.status.idle":"2023-07-28T04:20:09.646393Z","shell.execute_reply.started":"2023-07-28T04:20:09.630668Z","shell.execute_reply":"2023-07-28T04:20:09.645447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_level_labels.profile_report()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:20:09.648083Z","iopub.execute_input":"2023-07-28T04:20:09.648784Z","iopub.status.idle":"2023-07-28T04:20:26.484262Z","shell.execute_reply.started":"2023-07-28T04:20:09.648749Z","shell.execute_reply":"2023-07-28T04:20:26.483351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Weights & Biases\n> Weights and Biases (W&B) is a machine learning tool for experiment tracking, visualization, and collaboration. It helps manage projects, log parameters, visualize results, and support team collaboration.\n\n1. Experiment Tracking\n1. Visualization and Analysis\n1. Model Artifacts and Versioning\n1. Collaboration and Sharing\n1. Integration and Compatibility\n1. Hyperparameter Sweeps\n1. Automated Reports","metadata":{}},{"cell_type":"code","source":"# Codes that is used to help you get your WandB account logged in\ntry:\n    from kaggle_secrets import UserSecretsClient\n    user_secrets = UserSecretsClient()\n    api_key = user_secrets.get_secret(\"WANDB\")\n\n    wandb.login(key=api_key)\n    anonymous = None\nexcept:\n    anonymous = \"must\"\n    print('To use your W&B account,\\nGo to Add-ons -> Secrets and provide your W&B access token. Use the Label name as WANDB. \\nGet your W&B access token from here: https://wandb.ai/authorize')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:20:26.485757Z","iopub.execute_input":"2023-07-28T04:20:26.486332Z","iopub.status.idle":"2023-07-28T04:20:27.116253Z","shell.execute_reply.started":"2023-07-28T04:20:26.486291Z","shell.execute_reply":"2023-07-28T04:20:27.114855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To be completed. If you got something out of this notebook, please upvote it. Cheers, mate!","metadata":{"execution":{"iopub.status.busy":"2023-07-28T04:20:27.1178Z","iopub.execute_input":"2023-07-28T04:20:27.118177Z","iopub.status.idle":"2023-07-28T04:20:27.125992Z","shell.execute_reply.started":"2023-07-28T04:20:27.118141Z","shell.execute_reply":"2023-07-28T04:20:27.124601Z"},"trusted":true},"execution_count":null,"outputs":[]}]}