{"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":"# Hyperparameters","metadata":{}},{"cell_type":"markdown","source":"## Run Configuration","metadata":{}},{"cell_type":"code","source":"RUN_CFG = {\n    'seed': 42,\n    'load_config': False, # Not used in this ipynb\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.843669Z","iopub.execute_input":"2023-10-10T12:50:53.844015Z","iopub.status.idle":"2023-10-10T12:50:53.851717Z","shell.execute_reply.started":"2023-10-10T12:50:53.843989Z","shell.execute_reply":"2023-10-10T12:50:53.85077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Configuration","metadata":{}},{"cell_type":"code","source":"MODEL_CFG = {\n    'backbone': {\n        'class': 'ConvNextV2ForImageClassification',\n        'pretrained_model_name_or_path': \"/kaggle/input/convnextv2forimageclassification/model\",\n        'backbone_argument': None,\n    },\n    # Note that in Python<3.7, the order of 'head' is not guaranteed.\n    'head': {\n        'Linear': {\n            'in_features': 1000,\n            'out_features':13,\n            'bias': True\n        }\n    },\n    \n    'tuned_model_path': '/kaggle/input/convnextv2-tiny-l13-flexible-2023-09-19-04-47-22/model.pt',\n    \n    'image_processor': {\n        'pretrained_model_name_or_path': '/kaggle/input/convnextv2forimageclassification/ImageProcessor'\n    },\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.85376Z","iopub.execute_input":"2023-10-10T12:50:53.854364Z","iopub.status.idle":"2023-10-10T12:50:53.86515Z","shell.execute_reply.started":"2023-10-10T12:50:53.854335Z","shell.execute_reply":"2023-10-10T12:50:53.864256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Metrics Configuration","metadata":{}},{"cell_type":"code","source":"METRICS_CFG = {\n    'loss_function': 'BCEWithLogitsLoss',\n    'loss_function_arguments': {\n    },\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.866335Z","iopub.execute_input":"2023-10-10T12:50:53.867154Z","iopub.status.idle":"2023-10-10T12:50:53.879257Z","shell.execute_reply.started":"2023-10-10T12:50:53.867126Z","shell.execute_reply":"2023-10-10T12:50:53.878385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Configuration","metadata":{}},{"cell_type":"code","source":"DATASET_CFG = {\n    'png_size': 256,\n    'competition_data_dir': '/kaggle/input/rsna-2023-abdominal-trauma-detection',\n    'image_transform': [\n        # ToTensor()\n    ],\n    'label_transform': [\n    ],\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.881393Z","iopub.execute_input":"2023-10-10T12:50:53.882345Z","iopub.status.idle":"2023-10-10T12:50:53.88862Z","shell.execute_reply.started":"2023-10-10T12:50:53.882315Z","shell.execute_reply":"2023-10-10T12:50:53.887742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DataLoader Configuration","metadata":{}},{"cell_type":"code","source":"DATALOADER_CFG = {\n    'batch_size': 60,\n    'shuffle': False,\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.890092Z","iopub.execute_input":"2023-10-10T12:50:53.890784Z","iopub.status.idle":"2023-10-10T12:50:53.901118Z","shell.execute_reply.started":"2023-10-10T12:50:53.89075Z","shell.execute_reply":"2023-10-10T12:50:53.900253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Meta Configuration","metadata":{}},{"cell_type":"code","source":"META_CFG = {\n}","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.902503Z","iopub.execute_input":"2023-10-10T12:50:53.9031Z","iopub.status.idle":"2023-10-10T12:50:53.913022Z","shell.execute_reply.started":"2023-10-10T12:50:53.90307Z","shell.execute_reply":"2023-10-10T12:50:53.912211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Oversight of Configuration","metadata":{}},{"cell_type":"code","source":"CFG = dict()\nCFG.update(RUN_CFG)\nCFG.update(MODEL_CFG)\nCFG.update(METRICS_CFG)\nCFG.update(DATASET_CFG)\nCFG.update(DATALOADER_CFG)\nCFG.update(META_CFG)","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.914371Z","iopub.execute_input":"2023-10-10T12:50:53.915006Z","iopub.status.idle":"2023-10-10T12:50:53.924791Z","shell.execute_reply.started":"2023-10-10T12:50:53.914978Z","shell.execute_reply":"2023-10-10T12:50:53.924057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing","metadata":{}},{"cell_type":"code","source":"import wandb\n\nimport os\nimport glob\nfrom pathlib import Path\n\nfrom tqdm.notebook import tqdm\nimport copy\nimport random\n\nimport time\nimport datetime\nimport json\nfrom PIL import Image\nimport cv2\nimport pydicom\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import GroupShuffleSplit, GroupKFold\n\nimport torch\nfrom torch.nn import Sequential, Dropout, Linear, BCEWithLogitsLoss\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\nfrom torchvision.io import read_image\nfrom torchvision.transforms import Compose, ToTensor, ToPILImage\n\nfrom transformers import AutoImageProcessor, ConvNextV2ForImageClassification\n\nfrom scipy.special import softmax\n\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:50:53.925942Z","iopub.execute_input":"2023-10-10T12:50:53.926758Z","iopub.status.idle":"2023-10-10T12:51:06.546724Z","shell.execute_reply.started":"2023-10-10T12:50:53.926729Z","shell.execute_reply":"2023-10-10T12:51:06.545828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Hyperparameters set automatically","metadata":{}},{"cell_type":"markdown","source":"## Device","metadata":{}},{"cell_type":"code","source":"CFG['device'] = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(f'Using {CFG[\"device\"]}')","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.550318Z","iopub.execute_input":"2023-10-10T12:51:06.550848Z","iopub.status.idle":"2023-10-10T12:51:06.594202Z","shell.execute_reply.started":"2023-10-10T12:51:06.550824Z","shell.execute_reply":"2023-10-10T12:51:06.593263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Seeds","metadata":{}},{"cell_type":"code","source":"def seed_everything(seed=42):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything(seed=42)","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.595749Z","iopub.execute_input":"2023-10-10T12:51:06.596343Z","iopub.status.idle":"2023-10-10T12:51:06.610985Z","shell.execute_reply.started":"2023-10-10T12:51:06.596312Z","shell.execute_reply":"2023-10-10T12:51:06.610141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading","metadata":{}},{"cell_type":"code","source":"# Competition Data\ntrain_labels = pd.read_csv(f'{CFG[\"competition_data_dir\"]}/train.csv')\ntrain_series_meta = pd.read_csv(f'{CFG[\"competition_data_dir\"]}/train_series_meta.csv')\ntest_series_meta = pd.read_csv(f'{CFG[\"competition_data_dir\"]}/test_series_meta.csv')\nsample_submission = pd.read_csv(f'{CFG[\"competition_data_dir\"]}/sample_submission.csv')\nimage_level_labels = pd.read_csv(f'{CFG[\"competition_data_dir\"]}/image_level_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.612168Z","iopub.execute_input":"2023-10-10T12:51:06.613005Z","iopub.status.idle":"2023-10-10T12:51:06.665074Z","shell.execute_reply.started":"2023-10-10T12:51:06.612974Z","shell.execute_reply":"2023-10-10T12:51:06.664193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_columns = list(train_labels.columns)\nlabels_columns.remove('patient_id')\nlabels_columns.remove('any_injury')\nlabels_columns","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.666446Z","iopub.execute_input":"2023-10-10T12:51:06.667119Z","iopub.status.idle":"2023-10-10T12:51:06.674548Z","shell.execute_reply.started":"2023-10-10T12:51:06.667089Z","shell.execute_reply":"2023-10-10T12:51:06.673606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"def build_model_and_processor(cfg=CFG):\n        \n    # initialize backbone\n    model = eval(cfg['backbone']['class'])\n    model = model.from_pretrained(cfg['backbone']['pretrained_model_name_or_path'])\n    \n    # define head\n    head = Sequential()\n    for module_class, module_arguments in cfg['head'].items():\n        head.append(eval(module_class)(**module_arguments))\n    \n    model.classifier = Sequential(model.classifier, eval(module_class)(**module_arguments))\n    \n    # build image processor\n    image_processor = AutoImageProcessor.from_pretrained(cfg['image_processor']['pretrained_model_name_or_path'])\n    \n    return model, image_processor","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.675895Z","iopub.execute_input":"2023-10-10T12:51:06.676715Z","iopub.status.idle":"2023-10-10T12:51:06.684485Z","shell.execute_reply.started":"2023-10-10T12:51:06.67667Z","shell.execute_reply":"2023-10-10T12:51:06.68361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, image_processor = build_model_and_processor()\nmodel.load_state_dict(torch.load(CFG['tuned_model_path']))","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:06.685596Z","iopub.execute_input":"2023-10-10T12:51:06.686379Z","iopub.status.idle":"2023-10-10T12:51:16.323613Z","shell.execute_reply.started":"2023-10-10T12:51:06.686352Z","shell.execute_reply":"2023-10-10T12:51:16.322745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to(CFG['device'])\n\nis_cuda_list = []\nfor name, parameter in model.named_parameters():\n    \n    is_cuda_list.append(parameter.is_cuda)\n    \nif all(is_cuda_list):\n    print('All parameters is in cuda')\n        \nelse:\n    print('One of the parameters is not in the cuda.')","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.324936Z","iopub.execute_input":"2023-10-10T12:51:16.32586Z","iopub.status.idle":"2023-10-10T12:51:16.383655Z","shell.execute_reply.started":"2023-10-10T12:51:16.325829Z","shell.execute_reply":"2023-10-10T12:51:16.382766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Datasets","metadata":{}},{"cell_type":"markdown","source":"## dicom to png conversion","metadata":{}},{"cell_type":"code","source":"def standardize_pixel_array(dcm: pydicom.dataset.FileDataset) -> np.ndarray:\n    \n    \"\"\"\n    Source : https://www.kaggle.com/competitions/rsna-2023-abdominal-trauma-detection/discussion/427217\n    \"\"\"\n    \n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    \n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        pixel_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n        # pixel_array = pydicom.pixel_data_handlers.util.apply_modality_lut(new_array, dcm)\n\n    intercept = float(dcm.RescaleIntercept)\n    slope = float(dcm.RescaleSlope)\n    center = int(dcm.WindowCenter)\n    width = int(dcm.WindowWidth)\n    low = center - width / 2\n    high = center + width / 2    \n    \n    pixel_array = (pixel_array * slope) + intercept\n    pixel_array = np.clip(pixel_array, low, high)\n\n    return pixel_array","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.384955Z","iopub.execute_input":"2023-10-10T12:51:16.385459Z","iopub.status.idle":"2023-10-10T12:51:16.392713Z","shell.execute_reply.started":"2023-10-10T12:51:16.385427Z","shell.execute_reply":"2023-10-10T12:51:16.391819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_dicom_to_png(dicoms_dir='/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images', cfg=CFG):\n            \n    patients_list = os.listdir(dicoms_dir)\n\n    for patient in patients_list:\n\n        for study in sorted(os.listdir(f'{dicoms_dir}/{patient}')):\n\n            save_dir = f'/kaggle/working/test_images/{patient}/{study}'\n            os.makedirs(save_dir, exist_ok=True)\n\n            imgs = {}\n\n            for f in sorted(glob.glob(f\"{dicoms_dir}/{patient}/{study}/*.dcm\")):\n                \n                # test_images/3124/5842/514.dcm is broke\n                if (patient=='3124') and (study=='5842') and (Path(f).stem=='514'):\n                    \n                    continue\n\n                dicom = pydicom.dcmread(f)\n\n                # pos_z is the image position information in a series of CT images\n                pos_z = dicom[(0x20, 0x32)].value[-1]\n\n                img = standardize_pixel_array(dicom)\n                img = (img - img.min()) / (img.max() - img.min() + 1e-6)\n\n                if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n\n                    img = 1 - img\n\n                imgs[pos_z] = img\n\n            for i, k in enumerate(sorted(imgs.keys())):\n\n                i = str(i).zfill(5)\n                img = imgs[k]\n\n                if cfg['png_size'] is not None:\n                    img = cv2.resize(img, (cfg['png_size'], cfg['png_size']))\n\n                cv2.imwrite(f\"{save_dir}/{i}.png\", (img * 255).astype(np.uint8))\n\n    print('dicom converted to png')","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.393644Z","iopub.execute_input":"2023-10-10T12:51:16.394252Z","iopub.status.idle":"2023-10-10T12:51:16.405507Z","shell.execute_reply.started":"2023-10-10T12:51:16.39423Z","shell.execute_reply":"2023-10-10T12:51:16.404616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convert_dicom_to_png()","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.40668Z","iopub.execute_input":"2023-10-10T12:51:16.407596Z","iopub.status.idle":"2023-10-10T12:51:16.543541Z","shell.execute_reply.started":"2023-10-10T12:51:16.407566Z","shell.execute_reply":"2023-10-10T12:51:16.542645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_image_paths_df(test_images_dir='/kaggle/working/test_images', cfg=CFG):\n        \n    patient_id_list = []\n    series_id_list = []\n    image_path_list = []\n\n    for patient_id in os.listdir(f'{test_images_dir}'):\n\n        for series_id in os.listdir(f'{test_images_dir}/{patient_id}'):\n\n            image_paths = glob.glob(f'{test_images_dir}/{patient_id}/{series_id}/*.png')\n\n            image_path_list.extend(image_paths)\n            series_id_list.extend([series_id for n in range(len(image_paths))])\n            patient_id_list.extend([patient_id for n in range(len(image_paths))])\n\n    image_paths_df = pd.DataFrame({\n        'patient_id': patient_id_list,\n        'series_id': series_id_list,\n        'image_path': image_path_list\n    })\n\n    return image_paths_df","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.544714Z","iopub.execute_input":"2023-10-10T12:51:16.545643Z","iopub.status.idle":"2023-10-10T12:51:16.552557Z","shell.execute_reply.started":"2023-10-10T12:51:16.545611Z","shell.execute_reply":"2023-10-10T12:51:16.551564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths_df = make_image_paths_df()","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.553804Z","iopub.execute_input":"2023-10-10T12:51:16.554729Z","iopub.status.idle":"2023-10-10T12:51:16.566917Z","shell.execute_reply.started":"2023-10-10T12:51:16.554681Z","shell.execute_reply":"2023-10-10T12:51:16.565989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Defining Image Transform","metadata":{}},{"cell_type":"code","source":"def define_image_transform(cfg=CFG):\n    \n    image_transform = None\n    \n    if cfg['image_transform']:\n        \n        transform_list = []\n        \n        for module in cfg['image_transform']:\n            \n            transform_list.append(eval(module))\n        \n        image_transform = Compose(transform_list)\n        \n        print('using image_transform')\n    \n    return image_transform","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.56807Z","iopub.execute_input":"2023-10-10T12:51:16.568766Z","iopub.status.idle":"2023-10-10T12:51:16.575945Z","shell.execute_reply.started":"2023-10-10T12:51:16.568731Z","shell.execute_reply":"2023-10-10T12:51:16.575002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_transform = define_image_transform()\nimage_transform","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.577012Z","iopub.execute_input":"2023-10-10T12:51:16.577882Z","iopub.status.idle":"2023-10-10T12:51:16.587309Z","shell.execute_reply.started":"2023-10-10T12:51:16.577854Z","shell.execute_reply":"2023-10-10T12:51:16.586365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constructing Dataset","metadata":{}},{"cell_type":"code","source":"class TraumaDataset(Dataset):\n\n    def __init__(self,\n                 image_paths_df:pd.DataFrame,\n                 image_processor=image_processor,\n                 image_transform=image_transform,\n                 ):\n        self.image_paths_df = image_paths_df\n        self.image_transform = image_transform\n\n    def __len__(self):\n        return len(self.image_paths_df)\n\n    def __getitem__(self, index):\n        \n        # image\n        image_path = self.image_paths_df.loc[index, 'image_path']\n        image = Image.open(image_path).convert('RGB')\n        image = image_processor(image, return_tensors='pt')['pixel_values'][0]\n        \n        if self.image_transform:\n            image = self.image_transform(image)\n        \n        return image","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.588659Z","iopub.execute_input":"2023-10-10T12:51:16.589018Z","iopub.status.idle":"2023-10-10T12:51:16.598452Z","shell.execute_reply.started":"2023-10-10T12:51:16.588991Z","shell.execute_reply":"2023-10-10T12:51:16.597572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = TraumaDataset(image_paths_df=image_paths_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.602879Z","iopub.execute_input":"2023-10-10T12:51:16.603132Z","iopub.status.idle":"2023-10-10T12:51:16.612954Z","shell.execute_reply.started":"2023-10-10T12:51:16.603093Z","shell.execute_reply":"2023-10-10T12:51:16.611969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DataLoaders","metadata":{}},{"cell_type":"code","source":"test_dataloader = DataLoader(\n    dataset=test_dataset,\n    batch_size=CFG['batch_size'],\n    shuffle=CFG['shuffle']\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.614215Z","iopub.execute_input":"2023-10-10T12:51:16.615179Z","iopub.status.idle":"2023-10-10T12:51:16.622769Z","shell.execute_reply.started":"2023-10-10T12:51:16.615151Z","shell.execute_reply":"2023-10-10T12:51:16.621913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Infering","metadata":{}},{"cell_type":"code","source":"pred_test_df = image_paths_df.copy()\npred_test_df[labels_columns] = 0","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.623911Z","iopub.execute_input":"2023-10-10T12:51:16.624823Z","iopub.status.idle":"2023-10-10T12:51:16.64089Z","shell.execute_reply.started":"2023-10-10T12:51:16.624789Z","shell.execute_reply":"2023-10-10T12:51:16.640058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def infer(model=model, test_dataloader=test_dataloader, pred_test_df=pred_test_df, cfg=CFG):\n    \n    model.eval()\n    \n    index = 0\n    \n    with torch.no_grad():\n        \n        for images in tqdm(test_dataloader):\n            \n            images = images.to(cfg['device'])\n            pred = model(images).logits\n            \n            pred = pred.cpu().detach().numpy()\n            pred_test_df.loc[index:(index+cfg['batch_size']-1), labels_columns] = pred\n            \n            del images\n            torch.cuda.empty_cache()\n            \n            index += cfg['batch_size']\n    \n    return pred_test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.642157Z","iopub.execute_input":"2023-10-10T12:51:16.64313Z","iopub.status.idle":"2023-10-10T12:51:16.651247Z","shell.execute_reply.started":"2023-10-10T12:51:16.643101Z","shell.execute_reply":"2023-10-10T12:51:16.650436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Execution","metadata":{}},{"cell_type":"code","source":"pred_test_df = infer()","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:16.652453Z","iopub.execute_input":"2023-10-10T12:51:16.653298Z","iopub.status.idle":"2023-10-10T12:51:21.111158Z","shell.execute_reply.started":"2023-10-10T12:51:16.653269Z","shell.execute_reply":"2023-10-10T12:51:21.11026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Postprocessing","metadata":{}},{"cell_type":"code","source":"def standardize(row):\n    \n    bowel = ['bowel_healthy', 'bowel_injury']\n    extravasation = ['extravasation_healthy', 'extravasation_injury']\n    kidney = ['kidney_healthy', 'kidney_low', 'kidney_high']\n    liver = ['liver_healthy', 'liver_low', 'liver_high']\n    spleen = ['spleen_healthy', 'spleen_low', 'spleen_high']\n    \n    row[bowel] = softmax(row[bowel])\n    row[extravasation] = softmax(row[extravasation])\n    row[kidney] = softmax(row[kidney])\n    row[liver] = softmax(row[liver])\n    row[spleen] = softmax(row[spleen])\n        \n    return row","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:21.11273Z","iopub.execute_input":"2023-10-10T12:51:21.113362Z","iopub.status.idle":"2023-10-10T12:51:21.120714Z","shell.execute_reply.started":"2023-10-10T12:51:21.11333Z","shell.execute_reply":"2023-10-10T12:51:21.119821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def any_injury(row):\n    \n    healthy_labels = [\n    'bowel_healthy',\n    'extravasation_healthy',\n    'kidney_healthy',\n    'liver_healthy',\n    'spleen_healthy'\n    ]\n    \n    healthy = row[healthy_labels].min()\n    any_injury = (1 - healthy)\n    \n    return any_injury","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:21.122193Z","iopub.execute_input":"2023-10-10T12:51:21.122839Z","iopub.status.idle":"2023-10-10T12:51:21.134367Z","shell.execute_reply.started":"2023-10-10T12:51:21.122811Z","shell.execute_reply":"2023-10-10T12:51:21.133438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def postprocess(pred_df):\n    \n    copied_df = pred_df.copy()\n    copied_df.groupby('patient_id').max().reset_index(inplace=True)\n    \n    # standardize & add 'any_injury'\n    copied_df[labels_columns] = copied_df[labels_columns].apply(standardize, axis=1)\n    copied_df['any_injury'] = copied_df.apply(any_injury, axis=1)\n    \n    copied_df.drop(columns=['series_id', 'image_path'], inplace=True)\n    \n    return copied_df","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:21.135484Z","iopub.execute_input":"2023-10-10T12:51:21.135853Z","iopub.status.idle":"2023-10-10T12:51:21.146523Z","shell.execute_reply.started":"2023-10-10T12:51:21.135824Z","shell.execute_reply":"2023-10-10T12:51:21.145592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processed_pred_test_df = postprocess(pred_test_df)\nprocessed_pred_test_df = processed_pred_test_df.sort_values(by='patient_id', ascending=True)\nprocessed_pred_test_df = processed_pred_test_df.drop(columns=['any_injury'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:33.740809Z","iopub.execute_input":"2023-10-10T12:51:33.741149Z","iopub.status.idle":"2023-10-10T12:51:33.777876Z","shell.execute_reply.started":"2023-10-10T12:51:33.741123Z","shell.execute_reply":"2023-10-10T12:51:33.776972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processed_pred_test_df = processed_pred_test_df.astype('float64')\nprocessed_pred_test_df = processed_pred_test_df.astype({\n    'patient_id': 'int64'\n})\nprocessed_pred_test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:34.769813Z","iopub.execute_input":"2023-10-10T12:51:34.770459Z","iopub.status.idle":"2023-10-10T12:51:34.796654Z","shell.execute_reply.started":"2023-10-10T12:51:34.77043Z","shell.execute_reply":"2023-10-10T12:51:34.79539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:36.234297Z","iopub.execute_input":"2023-10-10T12:51:36.23503Z","iopub.status.idle":"2023-10-10T12:51:36.250195Z","shell.execute_reply.started":"2023-10-10T12:51:36.234992Z","shell.execute_reply":"2023-10-10T12:51:36.249067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processed_pred_test_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:42.180125Z","iopub.execute_input":"2023-10-10T12:51:42.180468Z","iopub.status.idle":"2023-10-10T12:51:42.193176Z","shell.execute_reply.started":"2023-10-10T12:51:42.18044Z","shell.execute_reply":"2023-10-10T12:51:42.192089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit","metadata":{}},{"cell_type":"code","source":"processed_pred_test_df.to_csv('submission.csv', index=False, float_format='%.20f')","metadata":{"execution":{"iopub.status.busy":"2023-10-10T12:51:55.578421Z","iopub.execute_input":"2023-10-10T12:51:55.578807Z","iopub.status.idle":"2023-10-10T12:51:55.587538Z","shell.execute_reply.started":"2023-10-10T12:51:55.578777Z","shell.execute_reply":"2023-10-10T12:51:55.586502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}