{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! pip install -qU pydicom","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:40.034947Z","iopub.execute_input":"2023-09-04T15:16:40.036442Z","iopub.status.idle":"2023-09-04T15:16:55.575137Z","shell.execute_reply.started":"2023-09-04T15:16:40.036395Z","shell.execute_reply":"2023-09-04T15:16:55.573564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport tqdm\n\nimport pydicom\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:55.577848Z","iopub.execute_input":"2023-09-04T15:16:55.579005Z","iopub.status.idle":"2023-09-04T15:16:57.164233Z","shell.execute_reply.started":"2023-09-04T15:16:55.578961Z","shell.execute_reply":"2023-09-04T15:16:57.16311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To do:\n\n- Test out instances that have an associated `image_level_label`\n- Try to combine the segmentation annotations\n- Examine the targets","metadata":{}},{"cell_type":"markdown","source":"## `train`\n\nThese are the labels for the training set.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-2023-abdominal-trauma-detection/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.165768Z","iopub.execute_input":"2023-09-04T15:16:57.166931Z","iopub.status.idle":"2023-09-04T15:16:57.216853Z","shell.execute_reply.started":"2023-09-04T15:16:57.166891Z","shell.execute_reply":"2023-09-04T15:16:57.215936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## `train_meta`\n\n- Contains mapping of the unique scan id (`series_id`) to a patient, since some patients underwent multiple scans.\n- Contains data on what phase of the heart pumping cycle via the `aortic_hu` variable.\n- Contains data if an organ was not included in a scan iva the `incomplete_organ` variable.\n","metadata":{}},{"cell_type":"code","source":"train_meta = pd.read_csv(\"../input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv\")\ntrain_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.219356Z","iopub.execute_input":"2023-09-04T15:16:57.21983Z","iopub.status.idle":"2023-09-04T15:16:57.242484Z","shell.execute_reply.started":"2023-09-04T15:16:57.219797Z","shell.execute_reply":"2023-09-04T15:16:57.241235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.24407Z","iopub.execute_input":"2023-09-04T15:16:57.244618Z","iopub.status.idle":"2023-09-04T15:16:57.25067Z","shell.execute_reply.started":"2023-09-04T15:16:57.244586Z","shell.execute_reply":"2023-09-04T15:16:57.24988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## `image_level_labels`\n\n- Labels associated with each of the DICOM images, indexed by `patient_id` and `series_id`.\n- Contains `instance_number` which indicates the image number within a particular scan\n- Contains an injury label either \"Bowel\" or \"Active_Extravasation\"\n","metadata":{}},{"cell_type":"code","source":"image_level_labels = pd.read_csv(\"../input/rsna-2023-abdominal-trauma-detection/image_level_labels.csv\")\nimage_level_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.251973Z","iopub.execute_input":"2023-09-04T15:16:57.252967Z","iopub.status.idle":"2023-09-04T15:16:57.293132Z","shell.execute_reply.started":"2023-09-04T15:16:57.252934Z","shell.execute_reply":"2023-09-04T15:16:57.291736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_level_labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.294752Z","iopub.execute_input":"2023-09-04T15:16:57.295179Z","iopub.status.idle":"2023-09-04T15:16:57.302948Z","shell.execute_reply.started":"2023-09-04T15:16:57.29514Z","shell.execute_reply":"2023-09-04T15:16:57.301647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_level_labels['injury_name'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.304721Z","iopub.execute_input":"2023-09-04T15:16:57.305133Z","iopub.status.idle":"2023-09-04T15:16:57.326137Z","shell.execute_reply.started":"2023-09-04T15:16:57.305095Z","shell.execute_reply":"2023-09-04T15:16:57.324769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_patient_id = 10004\nsample_series_id = 21057\n\nsample_scan = image_level_labels.loc[(image_level_labels['patient_id'] == sample_patient_id)&\n                                     (image_level_labels['series_id'] == sample_series_id)]\nsample_scan['instance_number']","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:17:44.001873Z","iopub.execute_input":"2023-09-04T15:17:44.002253Z","iopub.status.idle":"2023-09-04T15:17:44.018566Z","shell.execute_reply.started":"2023-09-04T15:17:44.002223Z","shell.execute_reply":"2023-09-04T15:17:44.017207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualise some images\n\n- First we need to add the image path to the train data","metadata":{}},{"cell_type":"code","source":"def view_img(patient_id: str, series_id: str):\n    N_IMGS = 4\n    scan_path = f\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/{patient_id}/{series_id}\"\n    instances = np.random.choice(sorted(os.listdir(scan_path)), (1, 4), replace=False).flatten()\n    \n    fig, axes = plt.subplots(2, 2, figsize=(6, 6))\n\n    for i in range(N_IMGS):\n        img_path = f\"{scan_path}/{instances[i]}\"\n        img = pydicom.dcmread(img_path).pixel_array\n        axes[i // 2][i % 2].imshow(img)\n        axes[i // 2][i % 2].axis(\"off\")\n        instance_number = instances[i].replace('.dcm', '')\n        axes[i // 2][i % 2].set_title(f\"{instance_number}\")\n    \n#     plt.suptitle(f\"CT scans for Patient: {patient_id} during Scan: {series_id}\")\n    plt.tight_layout()\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:51:15.115315Z","iopub.execute_input":"2023-09-04T15:51:15.115761Z","iopub.status.idle":"2023-09-04T15:51:15.126494Z","shell.execute_reply.started":"2023-09-04T15:51:15.115724Z","shell.execute_reply":"2023-09-04T15:51:15.125211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_img(patient_id=10004, series_id=21057)","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:51:15.941435Z","iopub.execute_input":"2023-09-04T15:51:15.941864Z","iopub.status.idle":"2023-09-04T15:51:16.796448Z","shell.execute_reply.started":"2023-09-04T15:51:15.94183Z","shell.execute_reply":"2023-09-04T15:51:16.795205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:49:39.816825Z","iopub.execute_input":"2023-09-04T15:49:39.817631Z","iopub.status.idle":"2023-09-04T15:49:39.824494Z","shell.execute_reply.started":"2023-09-04T15:49:39.817595Z","shell.execute_reply":"2023-09-04T15:49:39.823183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n## `sample_submission`\n\n- Same format as `train.csv`\n- Have to predict probabilities for each of the criteria, indexed by `patient_id`","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"../input/rsna-2023-abdominal-trauma-detection/sample_submission.csv\")\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-04T15:16:57.479348Z","iopub.status.idle":"2023-09-04T15:16:57.480104Z","shell.execute_reply.started":"2023-09-04T15:16:57.479876Z","shell.execute_reply":"2023-09-04T15:16:57.479903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}