{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":52254,"databundleVersionId":9674523,"sourceType":"competition"},{"sourceId":1421668,"sourceType":"datasetVersion","datasetId":832340},{"sourceId":4079315,"sourceType":"datasetVersion","datasetId":2410273},{"sourceId":5718655,"sourceType":"datasetVersion","datasetId":2373279},{"sourceId":6608647,"sourceType":"datasetVersion","datasetId":3696987},{"sourceId":6618995,"sourceType":"datasetVersion","datasetId":3696991},{"sourceId":6634427,"sourceType":"datasetVersion","datasetId":3787208},{"sourceId":9806537,"sourceType":"datasetVersion","datasetId":2422513},{"sourceId":63372526,"sourceType":"kernelVersion"}],"dockerImageVersionId":30747,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ../input/python-module-whl-files/Monai/monai-0.9.1-202207251608-py3-none-any.whl","metadata":{"_uuid":"61ed4c82-c2bd-4670-b910-05fa0188052f","_cell_guid":"84c2b594-5cff-455c-a609-a8cbd74f46cc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:26:12.932355Z","iopub.execute_input":"2024-11-04T19:26:12.932654Z","iopub.status.idle":"2024-11-04T19:26:47.855359Z","shell.execute_reply.started":"2024-11-04T19:26:12.932625Z","shell.execute_reply":"2024-11-04T19:26:47.854364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Codes from this cell are adopted from Quadcore/Richard Epstein public notebook\n# This notebook loads GDCM without Internet access.\n# GDCM is needed to read some DICOM compressed images.\n# Once you run a notebook and get the GDCM error, you must restart that Kernel to read the files, even if you load the GDCM software.\n# Note that you do not \"import GDCM\". You just \"import pydicom\".\n# The Dataset (gdcm-conda-install) was provided by Ronaldo S.A. Batista. Definitely deserves an upvote!\n\n!cp ../input/gdcm-conda-install/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2\n\nprint(\"GDCM installed.\")\n\nimport pydicom","metadata":{"_uuid":"1f25fc00-6328-499e-bf6f-715903050871","_cell_guid":"637f8c8c-ca65-4399-98e1-02de4e14ca1b","jupyter":{"outputs_hidden":false},"collapsed":false,"execution":{"iopub.status.busy":"2024-11-04T19:26:47.860261Z","iopub.execute_input":"2024-11-04T19:26:47.860532Z","iopub.status.idle":"2024-11-04T19:27:31.052155Z","shell.execute_reply.started":"2024-11-04T19:26:47.860506Z","shell.execute_reply":"2024-11-04T19:27:31.051259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# append codes that are needed\nimport sys\nsys.path.append('../input/ysmedicalnet')\nsys.path.append('../input/yeelocallibrary')\nsys.path.append('../input/rsnaabdtraumalmandconfigs')","metadata":{"_uuid":"91e0078d-1838-4c9c-8ddf-def136aad18b","_cell_guid":"a4ff42bb-9664-4412-a871-d2d4e56ab815","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:27:31.053332Z","iopub.execute_input":"2024-11-04T19:27:31.053603Z","iopub.status.idle":"2024-11-04T19:27:31.05829Z","shell.execute_reply.started":"2024-11-04T19:27:31.05358Z","shell.execute_reply":"2024-11-04T19:27:31.057411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport math\nimport pandas as pd\nimport cv2\nimport monai\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import Dataset as BaseDataset\nimport tqdm\nfrom torch.utils.tensorboard import SummaryWriter\nfrom torchvision import models\nimport torchvision.transforms as transforms\nimport pytorch_lightning as pl\nimport torchmetrics\nfrom pytorch_lightning.callbacks import ModelCheckpoint\nfrom pytorch_lightning.loggers import WandbLogger\nimport torch.nn.functional as F\nfrom torchmetrics.classification import BinaryAccuracy\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import roc_auc_score, classification_report, roc_curve, average_precision_score\nfrom sklearn.model_selection import GroupShuffleSplit\nfrom pydicom import dcmread\nimport glob\nimport pickle\n#import scipy\nfrom datetime import datetime\nimport yaml\nimport argparse\nfrom os.path import exists\nimport matplotlib.pyplot as plt\n\nimport timm\nimport ys_utilities\nfrom torch.cuda.amp import autocast","metadata":{"_uuid":"a38978c9-d366-4cad-9b86-3d0b081dda83","_cell_guid":"96f6d757-6e15-49c5-8b9a-0072fcd8a2a8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:29:07.963261Z","iopub.execute_input":"2024-11-04T19:29:07.964255Z","iopub.status.idle":"2024-11-04T19:29:07.971879Z","shell.execute_reply.started":"2024-11-04T19:29:07.964229Z","shell.execute_reply":"2024-11-04T19:29:07.971036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# config\nclass YS_cfg(): \n    def __init__(self):\n        self.segNumSlices = 256\n        self.segImageSize = 256\n        self.classNumSlices = 64\n        self.classImageSize = 256\n        self.numChannels = 5\n        self.sanityCheck = True\n        if self.sanityCheck == True:\n            self.metaDataframePath = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv'\n            self.data_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/patientID/seriesID'\n        else:\n            self.metaDataframePath = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_series_meta.csv'\n            self.data_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/patientID/seriesID'\n\nys_cfg = YS_cfg()","metadata":{"_uuid":"72babae5-a818-43dc-a934-81ad1ac218e8","_cell_guid":"7caac2d9-9515-4635-974f-82ce27389d4d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:30:00.208887Z","iopub.execute_input":"2024-11-04T19:30:00.209318Z","iopub.status.idle":"2024-11-04T19:30:00.216262Z","shell.execute_reply.started":"2024-11-04T19:30:00.209286Z","shell.execute_reply":"2024-11-04T19:30:00.215207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load stage 1 segmentation model\nimport BAA_run_segmentation\n\ncheckpointPath = '/kaggle/input/rsnaabdtraumacheckpoints-ys/2308291651_SegModel01_cv4-epoch90-valid_seg_accuracy0.9978.ckpt'\nsegModel = BAA_run_segmentation.AbdTraumaSegLightningModule.load_from_checkpoint(checkpointPath).cuda().eval()","metadata":{"_uuid":"41ec102c-ba90-40fe-a04d-939314ceb9b0","_cell_guid":"a3e81c37-d4fd-4624-b200-43640099c4a6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:21.423068Z","iopub.execute_input":"2024-11-04T19:28:21.423393Z","iopub.status.idle":"2024-11-04T19:28:24.372597Z","shell.execute_reply.started":"2024-11-04T19:28:21.423364Z","shell.execute_reply":"2024-11-04T19:28:24.3718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listOfClassModels = []\n\n# Load stage 2 classification 2.5D model\nimport GBA_run_CNNRNNwCrop\n\nlistOfCheckpointPaths = ['/kaggle/input/rsnaabdtraumacheckpoints-ys/2309261907_CNNRNN_GBA_bowel_cv1-epoch23-val_comp_all_study0.8063.ckpt',\n                        '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309271101_CNNRNN_GBA_bowel_cv3-epoch26-val_comp_all_study0.8899.ckpt',\n                        '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309270437_CNNRNN_GBA_bowel_cv4-epoch28-val_comp_all_study0.8781.ckpt',\n                        '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309261907_CNNRNN_GBA_bowel_cv1-epoch22-val_comp_all_study0.9011.ckpt',\n                         '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309271101_CNNRNN_GBA_bowel_cv3-epoch25-val_comp_all_study1.0278.ckpt',\n                        '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309270437_CNNRNN_GBA_bowel_cv4-epoch33-val_comp_all_study0.8541.ckpt']\nfor eachPath in listOfCheckpointPaths:\n    modelDict = {\n        'model' :  GBA_run_CNNRNNwCrop.AbdTraumaLightningModule.load_from_checkpoint(eachPath).cuda().eval(),\n        'crop' : 'KLSB',\n        'UCAED' : True\n    }\n    listOfClassModels.append(modelDict)\n    \nimport JAA_run_Extrav\n\nlistOfCheckpointPaths = ['/kaggle/input/rsnaabdtraumacheckpoints-ys/2309290925_CNNRNN_GBA_extrav_cv3-epoch13-val_comp_all_study1.0654.ckpt',\n                         '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309281742_CNNRNN_GBA_extrav_cv4-epoch25-val_comp_all_study1.0350.ckpt']\nfor eachPath in listOfCheckpointPaths:\n    modelDict = {\n        'model' :  JAA_run_Extrav.AbdTraumaLightningModule.load_from_checkpoint(eachPath).cuda().eval(),\n        'crop' : 'None',\n        'UCAED' : True\n    }\n    listOfClassModels.append(modelDict)\n    \nimport IBA_run_KLS\n\nlistOfCheckpointPaths = ['/kaggle/input/rsnaabdtraumacheckpoints-ys/2309291950_IBA_KLS_cv2-epoch21-val_comp_all_study1.7082.ckpt',\n                         '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309291248_IBA_KLS_cv3-epoch16-val_comp_all_study1.7127.ckpt',\n                        '/kaggle/input/rsnaabdtraumacheckpoints-ys/2309290540_IBA_KLS_cv4-epoch18-val_comp_all_study1.4825.ckpt']\nfor eachPath in listOfCheckpointPaths:\n    modelDict = {\n        'model' :  IBA_run_KLS.AbdTraumaLightningModule.load_from_checkpoint(eachPath).cuda().eval(),\n        'crop' : 'KLS',\n        'UCAED' : False\n    }\n    listOfClassModels.append(modelDict)\n    \nscoreGrid = np.array([[0.5, 0.5, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                      [0.5, 0.5, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                      [0.5, 0.5, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                      [1, 1, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],\n                      [1, 1, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],\n                      [1, 1, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],\n                     [0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],\n                     [0.5, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],\n                     [0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n                     [0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2],\n                     [0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2],])","metadata":{"_uuid":"9e6dbaa6-f637-4b46-8262-288b68893d10","_cell_guid":"77698a79-0feb-49ef-8bc8-84f68bad8752","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:24.37371Z","iopub.execute_input":"2024-11-04T19:28:24.374015Z","iopub.status.idle":"2024-11-04T19:28:37.032165Z","shell.execute_reply.started":"2024-11-04T19:28:24.37398Z","shell.execute_reply":"2024-11-04T19:28:37.03119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(scoreGrid.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-04T19:28:37.033457Z","iopub.execute_input":"2024-11-04T19:28:37.03426Z","iopub.status.idle":"2024-11-04T19:28:37.039525Z","shell.execute_reply.started":"2024-11-04T19:28:37.034226Z","shell.execute_reply":"2024-11-04T19:28:37.038617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segResizeTransform = monai.transforms.Resize([ys_cfg.segImageSize, ys_cfg.segImageSize, ys_cfg.segNumSlices]) # x, y, z\nclassResizeTransform = monai.transforms.Resize([ys_cfg.classNumSlices, ys_cfg.classImageSize, ys_cfg.classImageSize]) # z, x, y\nUCAED_ResizeTransform = monai.transforms.Resize([ys_cfg.classNumSlices*ys_cfg.numChannels, ys_cfg.classImageSize, ys_cfg.classImageSize])","metadata":{"_uuid":"a5590f53-4fb9-4d17-a65b-24e11564d454","_cell_guid":"87c6df03-bfdc-44fd-b9fa-1b8e8e830cbb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:37.040856Z","iopub.execute_input":"2024-11-04T19:28:37.04114Z","iopub.status.idle":"2024-11-04T19:28:37.062611Z","shell.execute_reply.started":"2024-11-04T19:28:37.041116Z","shell.execute_reply":"2024-11-04T19:28:37.061914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bounding_cube_via_sum(mask, channel):\n    # Assume the input mask is a PyTorch tensor in the form C, X, Y, numslice\n        \n    if channel == 'KLSB':\n        mask = torch.sum(mask, dim=0)\n    elif channel == 'KLS':\n        mask = mask[0:3,:,:,:]\n        mask = torch.sum(mask, dim=0)\n    else:\n        mask = mask[channel,:,:,:]\n\n    sizeX, sizeY, sizeZ = mask.shape[0], mask.shape[1], mask.shape[2]\n\n    # Get projections by summing along each axis\n    sum_x = torch.sum(mask, dim=(1, 2))\n    sum_y = torch.sum(mask, dim=(0, 2))\n    sum_z = torch.sum(mask, dim=(0, 1))\n\n    # Determine min and max coordinates from the projections\n    x_bounds_min = torch.where(sum_x > 50)[0][0].item()/sizeX\n    x_bounds_max = torch.where(sum_x > 50)[0][-1].item()/sizeX\n    y_bounds_min = torch.where(sum_y > 50)[0][0].item()/sizeY\n    y_bounds_max = torch.where(sum_y > 50)[0][-1].item()/sizeY\n    z_bounds_min = torch.where(sum_z > 50)[0][0].item()/sizeZ\n    z_bounds_max = torch.where(sum_z > 50)[0][-1].item()/sizeZ\n\n    return {\n        'x_bds': (x_bounds_min, x_bounds_max),\n        'y_bds': (y_bounds_min, y_bounds_max),\n        'z_bds': (z_bounds_min, z_bounds_max)\n    }\n\ndef segmentationInference(imgStack, segModel):\n    # Input is in the form of stack of images (top->bottom) [(C, X, Y)_0, (C, X, Y)_1, ... (C, X, Y)_numslices]\n    imgVol = np.stack(imgStack, axis=0)\n    imgVol = imgVol[np.newaxis,:,:,:]\n    imgVol = imgVol.transpose(0,2,3,1)\n    imgVol = segResizeTransform(imgVol)\n    imgVol = imgVol.unsqueeze(0)\n    imgVol = imgVol.cuda()\n    with torch.no_grad():\n        with autocast():\n            segs = segModel(imgVol)\n            segs = torch.sigmoid(segs)\n    # filter low values\n    mask = segs<0.5\n    segs[mask] = 0\n    bdBxes = {\n        'KLSB' : bounding_cube_via_sum(segs[0,:,:,:,:], 'KLSB'),\n        'KLS' : bounding_cube_via_sum(segs[0,:,:,:,:], 'KLS'),\n        'None' : {'x_bds': (0.001, 0.999),\n                'y_bds': (0.001, 0.999),\n                'z_bds': (0.001, 0.999)}\n    }\n    return bdBxes","metadata":{"_uuid":"9a487690-feb8-4ed8-9100-0e4708cfa77c","_cell_guid":"8197e286-bcaa-4981-a82c-cf92a0e263c1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:37.063918Z","iopub.execute_input":"2024-11-04T19:28:37.064315Z","iopub.status.idle":"2024-11-04T19:28:37.078338Z","shell.execute_reply.started":"2024-11-04T19:28:37.064285Z","shell.execute_reply":"2024-11-04T19:28:37.077627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def displayVolume(toDisplay, orientation='axial'):\n    if orientation == 'axial':\n        # Takes in tensor in the form 1(batch), C, X, Y, Z\n        toDisplay = toDisplay.cpu().detach().numpy()\n        for eachIndex in range(0,toDisplay.shape[4],10):\n            plt.figure()\n            plt.imshow(toDisplay[0,0,:,:,eachIndex])\n    else:\n       # Takes in tensor in the form 1(batch), C, X, Y, Z\n        toDisplay = toDisplay.cpu().detach().numpy()\n        for eachIndex in range(0,toDisplay.shape[2],10):\n            plt.figure()\n            plt.imshow(toDisplay[0,0,eachIndex,:,:])","metadata":{"_uuid":"da5b217e-13bc-434d-b814-24337761091d","_cell_guid":"3e45f707-f582-491a-8c1a-c6b630992141","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:37.079401Z","iopub.execute_input":"2024-11-04T19:28:37.079696Z","iopub.status.idle":"2024-11-04T19:28:37.092164Z","shell.execute_reply.started":"2024-11-04T19:28:37.079674Z","shell.execute_reply":"2024-11-04T19:28:37.091467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classification inference\ndef cropImageStack(imageStack, bdBx):\n    sizeX = imageStack[0].shape[0]\n    sizeY = imageStack[0].shape[1]\n    x_lowerBound = math.floor(bdBx['x_bds'][0] * sizeX)\n    x_upperBound = math.ceil(bdBx['x_bds'][1] * sizeX)\n    y_lowerBound = math.floor(bdBx['y_bds'][0] * sizeY)\n    y_upperBound = math.ceil(bdBx['y_bds'][1] * sizeY)\n    return [eachImage[x_lowerBound:x_upperBound+1, y_lowerBound:y_upperBound+1] for eachImage in imageStack]\n\ndef classificationInferenceToEmbeddings(imgStack, modelDict, bdBxes):\n    crop = modelDict['crop']\n    # Crop imageStack\n    z_low = math.floor(bdBxes[crop]['z_bds'][0]*len(imgStack))\n    z_upp = math.ceil(bdBxes[crop]['z_bds'][1]*len(imgStack))\n    imgStack = imgStack[z_low:z_upp+1] # crop stack\n    imgStack = cropImageStack(imgStack, bdBxes[crop]) # crop every image in stack\n    imgVol = np.stack(imgStack, axis=0)\n    imgVol = imgVol[np.newaxis,:,:,:]\n    if modelDict['UCAED']:\n        imgVol = UCAED_ResizeTransform(imgVol)\n        imgVol = [imgVol[:,eachChannel:ys_cfg.classNumSlices*ys_cfg.numChannels-ys_cfg.numChannels + eachChannel + 1:ys_cfg.numChannels,:,:] for eachChannel in range(ys_cfg.numChannels)]\n        imgVol = torch.cat(imgVol, dim=0)\n    else:\n        imgVol = classResizeTransform(imgVol)\n        imgVol = F.pad(input=imgVol, pad=(0, 0, 0, 0, 2, 2), mode='constant', value=0)\n        imgVol = torch.cat([imgVol[:,0:-4,:,:], imgVol[:,1:-3,:,:], imgVol[:,2:-2,:,:], imgVol[:,3:-1,:,:], imgVol[:,4:,:,:]], dim=0)\n    imgVol = imgVol.unsqueeze(0)\n    imgVol = imgVol.cuda()\n    scores = []\n    with torch.no_grad():\n        with autocast():\n            embeddings = modelDict['model'].forwardToEmbeddings(imgVol)\n    embeddings = embeddings.detach()\n    return embeddings, imgVol.detach()\n\ndef classificationInferenceEmbeddingsToPredictions(listOfEmbeddings, modelDict):\n    if len(listOfEmbeddings)<2:\n        listOfEmbeddings = listOfEmbeddings*2\n    concatEmbeddings = torch.cat(listOfEmbeddings, dim=1)\n    concatEmbeddings = concatEmbeddings.cuda()\n    with torch.no_grad():\n        with autocast():\n            _, z_study = modelDict['model'].forwardFromEmbeddingsToStudyOutputs(concatEmbeddings)\n    probBowel = F.softmax(z_study[:,0:2], dim=1)\n    probExtrav = F.softmax(z_study[:,2:4], dim=1)\n    probKidneys = F.softmax(z_study[:,4:7], dim=1)\n    probLiver = F.softmax(z_study[:,7:10], dim=1)\n    probSpleen = F.softmax(z_study[:,10:13], dim=1)\n    pred_study = torch.cat([probBowel, probExtrav, probKidneys, probLiver, probSpleen], dim=-1).cpu().detach().numpy()\n    return pred_study","metadata":{"_uuid":"89623715-7008-4567-9bf9-e5c68974c49a","_cell_guid":"4e55c159-6ff3-43b5-b92d-98ee954cc713","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:37.093479Z","iopub.execute_input":"2024-11-04T19:28:37.093802Z","iopub.status.idle":"2024-11-04T19:28:37.111627Z","shell.execute_reply.started":"2024-11-04T19:28:37.093774Z","shell.execute_reply":"2024-11-04T19:28:37.110799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns=['patient_id', \n          'bowel_healthy', \n          'bowel_injury',\n          'extravasation_healthy',\n         'extravasation_injury',\n         'kidney_healthy',\n         'kidney_low',\n         'kidney_high',\n         'liver_healthy',\n         'liver_low',\n         'liver_high',\n         'spleen_healthy',\n         'spleen_low',\n         'spleen_high']","metadata":{"_uuid":"f540ba1a-7a8d-4609-896c-9b4343fd3778","_cell_guid":"5145d6cc-8c1c-41a3-957b-7ec056eefbfe","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:28:37.115267Z","iopub.execute_input":"2024-11-04T19:28:37.115787Z","iopub.status.idle":"2024-11-04T19:28:37.127658Z","shell.execute_reply.started":"2024-11-04T19:28:37.115763Z","shell.execute_reply":"2024-11-04T19:28:37.126954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(BaseDataset):\n    def __init__(\n            self,\n            ptIdList=None,\n            dataframe=None,\n            augmentation=None,\n            transform=None,\n            args=None\n    ):\n        self.ptIdList = ptIdList\n        self.dataframe = dataframe\n        self.augmentation = augmentation\n        self.transform = transform\n        self.args = args\n\n    def __getitem__(self, i):\n        ptID = self.ptIdList[i]\n        return self.getPatient(ptID)\n\n    def __len__(self):\n        return len(self.ptIdList)\n\n    def getPatient(self, ptID):\n        filePaths = self.args.data_path.replace('patientID', ptID).replace('seriesID', '*')\n        listOfPaths = glob.glob(filePaths)\n        listOfSeriesIDs = [eachPath.split('/')[-1] for eachPath in listOfPaths]\n        if len(listOfSeriesIDs)>1:\n            listOfSeriesIDs = sorted(listOfSeriesIDs, key=lambda x: self.dataframe.loc[x, 'aortic_hu'])\n        \n        return listOfSeriesIDs, ptID","metadata":{"_uuid":"67e56c49-85b9-44fd-bbfa-3da8232b4ead","_cell_guid":"ad2d57b0-ee37-40e4-bb83-26e117953683","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:30:12.088072Z","iopub.execute_input":"2024-11-04T19:30:12.088792Z","iopub.status.idle":"2024-11-04T19:30:12.097114Z","shell.execute_reply.started":"2024-11-04T19:30:12.088763Z","shell.execute_reply":"2024-11-04T19:30:12.096297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metaData = pd.read_csv(ys_cfg.metaDataframePath, dtype={'patient_id':'str', 'series_id':'str'}, index_col='series_id')\nlistOfPatientIDs = metaData['patient_id'].unique().tolist()\nif ys_cfg.sanityCheck == True:\n    listOfPatientIDs = listOfPatientIDs[50:60]\ntestDataset = Dataset(ptIdList = listOfPatientIDs, dataframe = metaData, args = ys_cfg)","metadata":{"_uuid":"88726116-c75b-4eaa-a6f7-ea07fc24fbdc","_cell_guid":"d2763d0e-4c0f-4df5-badc-89bdb8b49c8a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:30:14.465485Z","iopub.execute_input":"2024-11-04T19:30:14.466192Z","iopub.status.idle":"2024-11-04T19:30:14.492171Z","shell.execute_reply.started":"2024-11-04T19:30:14.466157Z","shell.execute_reply":"2024-11-04T19:30:14.491471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDF = pd.DataFrame(columns=columns)\n\n# Iterating through the dataset\nfor eachSortedListOfSeries, eachPtID in testDataset:\n    try:\n        listOfEmbeddingSets = []\n        for eachSeries in eachSortedListOfSeries:\n            listOfEmbeddings = []\n            eachSeriesPath = ys_cfg.data_path.replace('patientID', eachPtID).replace('seriesID', eachSeries)\n            imgStack = ys_utilities.getNumpyStacksByPyDicomWresize(eachSeriesPath)\n            bdBxes = segmentationInference(imgStack, segModel)\n            for eachModel in listOfClassModels:\n                embeddings, _ = classificationInferenceToEmbeddings(imgStack, eachModel, bdBxes)\n                listOfEmbeddings.append([embeddings])\n            listOfEmbeddingSets.append(listOfEmbeddings)\n\n        # Ensure there are 2 sets of embeddings, add the second embedding to the first set\n        for eachIndex in range(len(listOfEmbeddings[0])):\n            if len(listOfEmbeddingSets)==2:\n                listOfEmbeddingSets[0][eachIndex] = listOfEmbeddingSets[0][eachIndex]+listOfEmbeddingSets[1][eachIndex]\n\n        listOfPreds = []\n        for eachIndex, eachModel in enumerate(listOfClassModels):\n            pred = classificationInferenceEmbeddingsToPredictions(listOfEmbeddingSets[0][eachIndex], listOfClassModels[eachIndex])\n            listOfPreds.append(pred[0])\n        pred = np.array(listOfPreds)\n        pred = np.multiply(pred,scoreGrid)\n        score = np.divide(pred.sum(axis=0),scoreGrid.sum(axis=0))\n    except:\n        score = np.array([0.979663, 0.081347, 0.936447, 1.779473, 0.942167, 0.146171, 0.127741, 0.897998, 0.329202, 0.118208, 0.887512, 0.252939, 0.29552])\n    rowList = [eachPtID] + score.tolist()\n    resultsDF.loc[len(resultsDF)] = rowList","metadata":{"execution":{"iopub.status.busy":"2024-11-04T19:30:16.152534Z","iopub.execute_input":"2024-11-04T19:30:16.152895Z","iopub.status.idle":"2024-11-04T19:34:11.825073Z","shell.execute_reply.started":"2024-11-04T19:30:16.152867Z","shell.execute_reply":"2024-11-04T19:34:11.824156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDF","metadata":{"_uuid":"64c4d3ab-17b2-4b20-92de-5a91d0da4792","_cell_guid":"81b1e124-3b0f-4bc6-9f71-c55e1e03e7fd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:34:11.826881Z","iopub.execute_input":"2024-11-04T19:34:11.827208Z","iopub.status.idle":"2024-11-04T19:34:11.849489Z","shell.execute_reply.started":"2024-11-04T19:34:11.827176Z","shell.execute_reply":"2024-11-04T19:34:11.848354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sanity Check\nif ys_cfg.sanityCheck == True:\n    dataDF = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_2024.csv', dtype = {'patient_id':'str'}, index_col='patient_id')\n    print(dataDF.iloc[50:60])","metadata":{"_uuid":"512c2d15-4999-4268-aa01-26d660940eeb","_cell_guid":"6b3fcc6b-baec-4535-b7e6-ec6f6432daf1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:34:40.923381Z","iopub.execute_input":"2024-11-04T19:34:40.923757Z","iopub.status.idle":"2024-11-04T19:34:40.955348Z","shell.execute_reply.started":"2024-11-04T19:34:40.923728Z","shell.execute_reply":"2024-11-04T19:34:40.954234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultsDF.to_csv('submission.csv', index=False)","metadata":{"_uuid":"37c5e92d-da8b-4590-a197-d84b6103ce1f","_cell_guid":"a47f6192-dd61-4c75-922d-5f1d5d6f8d31","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-11-04T19:34:12.635714Z","iopub.status.idle":"2024-11-04T19:34:12.636236Z","shell.execute_reply.started":"2024-11-04T19:34:12.635949Z","shell.execute_reply":"2024-11-04T19:34:12.635988Z"},"trusted":true},"execution_count":null,"outputs":[]}]}