{"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":"markdown","source":"\n# Inference Notebook for RSNA 2023\n\nThis notebook outlines the steps to run inference using the pre-trained models developed for the RSNA 2023 Kaggle competition. The models were trained using a 2.5D CNN approach leveraging EfficientNet backbone for feature extraction. \n\nIn the following sections, we will set up the environment, load the pre-trained models, and define utility functions to process the data. Following this, we will load the test data, run the inference to generate predictions, and prepare a submission file.\n","metadata":{}},{"cell_type":"code","source":"!pip install -q /kaggle/input/d/raisinbl/abdominal-segment/python_gdcm-3.0.22-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q /kaggle/input/d/raisinbl/abdominal-segment/pylibjpeg-1.4.0-py3-none-any.whl\n!cp -r /kaggle/input/d/raisinbl/abdominal-segment/* .\n!pip install --no-index --no-deps ./wheels/*.whl --quiet","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:11:20.251769Z","iopub.execute_input":"2023-09-25T16:11:20.252158Z","iopub.status.idle":"2023-09-25T16:12:28.38773Z","shell.execute_reply.started":"2023-09-25T16:11:20.252127Z","shell.execute_reply":"2023-09-25T16:12:28.386468Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n# Import necessary packages\n# import packages\nimport os\nimport pickle\nfrom tqdm.notebook import tqdm\nimport random\nfrom tabulate import tabulate\n\nimport cv2\nimport torch\nimport timm\nfrom glob import glob\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport numpy as np\nimport pandas as pd\nimport pydicom\nimport matplotlib.pyplot as plt\nimport torchvision.transforms.v2 as t\nimport gc\n\nfrom sklearn.model_selection import KFold, StratifiedKFold\nfrom sklearn.metrics import accuracy_score, roc_auc_score\nfrom torch.utils.data import Dataset, DataLoader, Subset\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nfrom PIL import Image\nimport torch.optim as optim\nfrom torchvision import models\nfrom torchvision.transforms.v2 import Resize, Compose, RandomHorizontalFlip, ColorJitter, RandomAffine, RandomErasing, ToTensor\n\nfrom os.path import join\nfrom os import makedirs, listdir\n\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom typing import Tuple, Union, List\n\nimport numpy as np\nimport torch\nfrom acvl_utils.cropping_and_padding.padding import pad_nd_image\nfrom batchgenerators.utilities.file_and_folder_operations import load_json, join\n\nfrom nnunetv2.inference.data_iterators import PreprocessAdapterFromNpy, preprocessing_iterator_fromnpy\nfrom nnunetv2.inference.export_prediction import convert_predicted_logits_to_segmentation_with_correct_shape\nfrom nnunetv2.inference.sliding_window_prediction import compute_gaussian, compute_steps_for_sliding_window\nfrom nnunetv2.utilities.find_class_by_name import recursive_find_python_class\nfrom nnunetv2.utilities.helpers import empty_cache\nfrom nnunetv2.utilities.label_handling.label_handling import determine_num_input_channels\nfrom nnunetv2.utilities.plans_handling.plans_handler import PlansManager\n\nfrom nnunetv2.imageio.nibabel_reader_writer import NibabelIOWithReorient\n\nimport nibabel\nimport cupy as cp\nimport gc\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:28.390103Z","iopub.execute_input":"2023-09-25T16:12:28.39043Z","iopub.status.idle":"2023-09-25T16:12:34.274347Z","shell.execute_reply.started":"2023-09-25T16:12:28.390403Z","shell.execute_reply":"2023-09-25T16:12:34.269274Z"},"trusted":true},"execution_count":3,"outputs":[{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)","Cell \u001b[0;32mIn[3], line 46\u001b[0m\n\u001b[1;32m     43\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01macvl_utils\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcropping_and_padding\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpadding\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pad_nd_image\n\u001b[1;32m     44\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbatchgenerators\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfile_and_folder_operations\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m load_json, join\n\u001b[0;32m---> 46\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdata_iterators\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PreprocessAdapterFromNpy, preprocessing_iterator_fromnpy\n\u001b[1;32m     47\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport_prediction\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m convert_predicted_logits_to_segmentation_with_correct_shape\n\u001b[1;32m     48\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01minference\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msliding_window_prediction\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m compute_gaussian, compute_steps_for_sliding_window\n","File \u001b[0;32m/kaggle/working/nnunetv2/inference/data_iterators.py:13\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbatchgenerators\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdataloading\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdata_loader\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DataLoader\n\u001b[1;32m     12\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlabel_handling\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlabel_handling\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m convert_labelmap_to_one_hot\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mplans_handling\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mplans_handler\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m PlansManager, ConfigurationManager\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpreprocess_fromfiles_save_to_queue\u001b[39m(list_of_lists: List[List[\u001b[38;5;28mstr\u001b[39m]],\n\u001b[1;32m     17\u001b[0m                                        list_of_segs_from_prev_stage_files: Union[\u001b[38;5;28;01mNone\u001b[39;00m, List[\u001b[38;5;28mstr\u001b[39m]],\n\u001b[1;32m     18\u001b[0m                                        output_filenames_truncated: Union[\u001b[38;5;28;01mNone\u001b[39;00m, List[\u001b[38;5;28mstr\u001b[39m]],\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     24\u001b[0m                                        abort_event: Event,\n\u001b[1;32m     25\u001b[0m                                        verbose: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[1;32m     26\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n","File \u001b[0;32m/kaggle/working/nnunetv2/utilities/plans_handling/plans_handler.py:17\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\n\u001b[1;32m     15\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbatchgenerators\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfile_and_folder_operations\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m load_json, join\n\u001b[0;32m---> 17\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mreader_writer_registry\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m recursive_find_reader_writer_by_name\n\u001b[1;32m     18\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfind_class_by_name\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m recursive_find_python_class\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlabel_handling\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlabel_handling\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m get_labelmanager_class_from_plans\n","File \u001b[0;32m/kaggle/working/nnunetv2/imageio/reader_writer_registry.py:7\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mbatchgenerators\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfile_and_folder_operations\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m join\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnibabel_reader_writer\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m NibabelIO, NibabelIOWithReorient\n\u001b[1;32m      8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbase_reader_writer\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BaseReaderWriter\n\u001b[1;32m      9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mfind_class_by_name\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m recursive_find_python_class\n","File \u001b[0;32m/kaggle/working/nnunetv2/imageio/nibabel_reader_writer.py:21\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnibabel\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m io_orientation\n\u001b[1;32m     20\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnnunetv2\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbase_reader_writer\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m BaseReaderWriter\n\u001b[0;32m---> 21\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mnifti_max_z\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m construct_3d\n\u001b[1;32m     22\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnibabel\u001b[39;00m\n\u001b[1;32m     25\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mNibabelIO\u001b[39;00m(BaseReaderWriter):\n","File \u001b[0;32m/kaggle/working/dicom2nifti/__init__.py:13\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msettings\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m disable_validate_slice_increment, \\\n\u001b[1;32m      2\u001b[0m     disable_validate_orientation, \\\n\u001b[1;32m      3\u001b[0m     disable_validate_orthogonal, \\\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     11\u001b[0m     enable_validate_multiframe_implicit, \\\n\u001b[1;32m     12\u001b[0m     enable_resampling\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconvert_dicom\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m dicom_series_to_nifti\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconvert_dir\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m convert_directory\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpatch_pydicom_encodings\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpatch_pydicom_encodings\u001b[39;00m\n","File \u001b[0;32m/kaggle/working/dicom2nifti/convert_dicom.py:11\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnibabel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpydicom_compat\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m pydicom\n\u001b[1;32m      9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpydicom\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtag\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Tag\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcommon\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mcommon\u001b[39;00m\n\u001b[1;32m     12\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconvert_ge\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mconvert_ge\u001b[39;00m\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdicom2nifti\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mconvert_generic\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mconvert_generic\u001b[39;00m\n","\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'dicom2nifti.common'"],"ename":"ModuleNotFoundError","evalue":"No module named 'dicom2nifti.common'","output_type":"error"}]},{"cell_type":"markdown","source":"# Segment code","metadata":{}},{"cell_type":"code","source":"class nnUNetPredictor(object):\n    def __init__(self,\n                 tile_step_size: float = 0.5,\n                 use_gaussian: bool = True,\n                 perform_everything_on_gpu: bool = True,\n                 device: torch.device = torch.device('cuda'),\n                 verbose: bool = False,\n                 verbose_preprocessing: bool = False):\n        self.verbose = verbose\n        self.verbose_preprocessing = verbose_preprocessing\n\n        self.plans_manager, self.configuration_manager, self.list_of_parameters, self.network, self.dataset_json, \\\n        self.trainer_name, self.allowed_mirroring_axes, self.label_manager = None, None, None, None, None, None, None, None\n\n        self.tile_step_size = tile_step_size\n        self.use_gaussian = use_gaussian\n        self.use_mirroring = False\n        self.device = device\n        self.perform_everything_on_gpu = perform_everything_on_gpu\n\n    def initialize_from_trained_model_folder(self, model_training_output_dir, checkpoint_name, nnunetv2_path):\n        \"\"\"\n        This is used when making predictions with a trained model\n        \"\"\"\n\n        dataset_json = load_json(join(model_training_output_dir, 'dataset.json'))\n        plans = load_json(join(model_training_output_dir, 'plans.json'))\n        plans_manager = PlansManager(plans)\n\n        checkpoint = torch.load(join(model_training_output_dir, 'fold_all', checkpoint_name))\n        trainer_name = checkpoint['trainer_name']\n        configuration_name = checkpoint['init_args']['configuration']\n        configuration_name = '3d_fullres'\n        inference_allowed_mirroring_axes = None\n        parameters = []\n        parameters.append(checkpoint['network_weights'])\n\n        configuration_manager = plans_manager.get_configuration(configuration_name)\n        # restore network\n        num_input_channels = determine_num_input_channels(plans_manager, configuration_manager, dataset_json)\n        trainer_class = recursive_find_python_class(join(nnunetv2_path, \"training\", \"nnUNetTrainer\"),\n                                                    trainer_name, 'nnunetv2.training.nnUNetTrainer')\n        network = trainer_class.build_network_architecture(plans_manager, dataset_json, configuration_manager,\n                                                           num_input_channels, enable_deep_supervision=False)\n        self.plans_manager = plans_manager\n        self.configuration_manager = configuration_manager\n        self.list_of_parameters = parameters\n        self.network = network\n        self.dataset_json = dataset_json\n        self.trainer_name = trainer_name\n        self.allowed_mirroring_axes = inference_allowed_mirroring_axes\n        self.label_manager = plans_manager.get_label_manager(dataset_json)\n\n\n    def get_data_iterator_from_raw_npy_data(self,\n                                            image_or_list_of_images: Union[np.ndarray, List[np.ndarray]],\n                                            segs_from_prev_stage_or_list_of_segs_from_prev_stage: Union[None,\n                                                                                                        np.ndarray,\n                                                                                                        List[\n                                                                                                            np.ndarray]],\n                                            properties_or_list_of_properties: Union[dict, List[dict]],\n                                            truncated_ofname: Union[str, List[str], None],\n                                            num_processes: int = 3):\n\n        list_of_images = [image_or_list_of_images] if not isinstance(image_or_list_of_images, list) else \\\n            image_or_list_of_images\n\n        if isinstance(segs_from_prev_stage_or_list_of_segs_from_prev_stage, np.ndarray):\n            segs_from_prev_stage_or_list_of_segs_from_prev_stage = [\n                segs_from_prev_stage_or_list_of_segs_from_prev_stage]\n\n        if isinstance(truncated_ofname, str):\n            truncated_ofname = [truncated_ofname]\n\n        if isinstance(properties_or_list_of_properties, dict):\n            properties_or_list_of_properties = [properties_or_list_of_properties]\n\n        num_processes = min(num_processes, len(list_of_images))\n        pp = preprocessing_iterator_fromnpy(\n            list_of_images,\n            segs_from_prev_stage_or_list_of_segs_from_prev_stage,\n            properties_or_list_of_properties,\n            truncated_ofname,\n            self.plans_manager,\n            self.dataset_json,\n            self.configuration_manager,\n            num_processes,\n            self.device.type == 'cuda',\n            self.verbose_preprocessing\n        )\n\n        return pp\n\n\n    def predict_single_npy_array(self, input_image: np.ndarray, image_properties: dict,\n                                 segmentation_previous_stage: np.ndarray = None,\n                                 output_file_truncated: str = None):\n        \"\"\"\n        image_properties must only have a 'spacing' key!\n        \"\"\"\n        ppa = PreprocessAdapterFromNpy([input_image], [segmentation_previous_stage], [image_properties],\n                                       [output_file_truncated],\n                                       self.plans_manager, self.dataset_json, self.configuration_manager,\n                                       num_threads_in_multithreaded=1, verbose=self.verbose)\n        dct = next(ppa)\n\n        predicted_logits = self.predict_logits_from_preprocessed_data(dct['data'])\n\n        ret = convert_predicted_logits_to_segmentation_with_correct_shape(predicted_logits, self.plans_manager,\n                                                                              self.configuration_manager,\n                                                                              self.label_manager,\n                                                                              dct['data_properites'],\n                                                                              return_probabilities=False)\n\n        return ret\n    \n\n    def predict_logits_from_preprocessed_data(self, data: torch.Tensor) -> torch.Tensor:\n        \"\"\"\n        IMPORTANT! IF YOU ARE RUNNING THE CASCADE, THE SEGMENTATION FROM THE PREVIOUS STAGE MUST ALREADY BE STACKED ON\n        TOP OF THE IMAGE AS ONE-HOT REPRESENTATION! SEE PreprocessAdapter ON HOW THIS SHOULD BE DONE!\n\n        RETURNED LOGITS HAVE THE SHAPE OF THE INPUT. THEY MUST BE CONVERTED BACK TO THE ORIGINAL IMAGE SIZE.\n        SEE convert_predicted_logits_to_segmentation_with_correct_shape\n        \"\"\"\n        with torch.no_grad():\n            prediction = None\n            for params in self.list_of_parameters:\n                self.network.load_state_dict(params)\n\n                if prediction is None:\n                    prediction = self.predict_sliding_window_return_logits(data)\n                else:\n                    prediction += self.predict_sliding_window_return_logits(data)\n\n            if len(self.list_of_parameters) > 1:\n                prediction /= len(self.list_of_parameters)\n                    \n        return prediction\n\n\n    def _internal_get_sliding_window_slicers(self, image_size: Tuple[int, ...]):\n        slicers = []\n        if len(self.configuration_manager.patch_size) < len(image_size):\n            assert len(self.configuration_manager.patch_size) == len(\n                image_size) - 1, 'if tile_size has less entries than image_size, ' \\\n                                 'len(tile_size) ' \\\n                                 'must be one shorter than len(image_size) ' \\\n                                 '(only dimension ' \\\n                                 'discrepancy of 1 allowed).'\n            steps = compute_steps_for_sliding_window(image_size[1:], self.configuration_manager.patch_size,\n                                                     self.tile_step_size)\n            if self.verbose: print(f'n_steps {image_size[0] * len(steps[0]) * len(steps[1])}, image size is'\n                                   f' {image_size}, tile_size {self.configuration_manager.patch_size}, '\n                                   f'tile_step_size {self.tile_step_size}\\nsteps:\\n{steps}')\n            for d in range(image_size[0]):\n                for sx in steps[0]:\n                    for sy in steps[1]:\n                        slicers.append(\n                            tuple([slice(None), d, *[slice(si, si + ti) for si, ti in\n                                                     zip((sx, sy), self.configuration_manager.patch_size)]]))\n        else:\n            steps = compute_steps_for_sliding_window(image_size, self.configuration_manager.patch_size,\n                                                     self.tile_step_size)\n            if self.verbose: print(\n                f'n_steps {np.prod([len(i) for i in steps])}, image size is {image_size}, tile_size {self.configuration_manager.patch_size}, '\n                f'tile_step_size {self.tile_step_size}\\nsteps:\\n{steps}')\n            for sx in steps[0]:\n                for sy in steps[1]:\n                    for sz in steps[2]:\n                        slicers.append(\n                            tuple([slice(None), *[slice(si, si + ti) for si, ti in\n                                                  zip((sx, sy, sz), self.configuration_manager.patch_size)]]))\n        return slicers\n\n\n    def predict_sliding_window_return_logits(self, input_image: torch.Tensor) \\\n            -> Union[np.ndarray, torch.Tensor]:\n        assert isinstance(input_image, torch.Tensor)\n        self.network = self.network.to(self.device)\n        self.network.eval()\n\n        empty_cache(self.device)\n\n        with torch.no_grad():\n            with torch.autocast(self.device.type, enabled=True):\n                assert len(input_image.shape) == 4, 'input_image must be a 4D np.ndarray or torch.Tensor (c, x, y, z)'\n\n                # if input_image is smaller than tile_size we need to pad it to tile_size.\n                data, slicer_revert_padding = pad_nd_image(input_image, self.configuration_manager.patch_size,\n                                                           'constant', {'value': 0}, True,\n                                                           None)\n\n                slicers = self._internal_get_sliding_window_slicers(data.shape[1:])\n\n                # preallocate results and num_predictions\n                if self.verbose: \n                    print('preallocating arrays')\n                try:\n                    data = data.to(self.device)\n                    predicted_logits = torch.zeros((self.label_manager.num_segmentation_heads, *data.shape[1:]),\n                                                   dtype=torch.half,\n                                                   device=self.device)\n                    n_predictions = torch.zeros(data.shape[1:], dtype=torch.half,\n                                                device=self.device)\n                    if self.use_gaussian:\n                        gaussian = compute_gaussian(tuple(self.configuration_manager.patch_size), sigma_scale=1. / 8,\n                                                    value_scaling_factor=1000,\n                                                    device=self.device)\n\n                finally:\n                    empty_cache(self.device)\n\n                if self.verbose: print('running prediction')\n                for sl in slicers:\n                    workon = data[sl][None]\n                    workon = workon.to(self.device, non_blocking=False)\n\n                    prediction = self.network(workon)[0].to(self.device)\n\n                    predicted_logits[sl] += (prediction * gaussian if self.use_gaussian else prediction)\n                    n_predictions[sl[1:]] += (gaussian if self.use_gaussian else 1)\n\n                predicted_logits /= n_predictions\n        empty_cache(self.device)\n        return predicted_logits[tuple([slice(None), *slicer_revert_padding[1:]])]","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.27547Z","iopub.status.idle":"2023-09-25T16:12:34.275809Z","shell.execute_reply.started":"2023-09-25T16:12:34.275646Z","shell.execute_reply":"2023-09-25T16:12:34.275662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef standardize_pixel_array(dicom_image):\n    \"\"\"\n    Standardizes a DICOM pixel array by applying various transformations.\n    \n    Args:\n        dicom_path (str): Path to the DICOM image file.\n        \n    Returns:\n        np.ndarray: The standardized pixel array of the DICOM image.\n    \"\"\"\n    pixel_array = dicom_image.pixel_array\n    \n    if dicom_image.PixelRepresentation == 1:\n        bit_shift = dicom_image.BitsAllocated - dicom_image.BitsStored\n        dtype = pixel_array.dtype \n        new_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n        pixel_array = pydicom.pixel_data_handlers.util.apply_modality_lut(new_array, dicom_image)\n\n    if dicom_image.PhotometricInterpretation == \"MONOCHROME1\":\n        pixel_array = 1 - pixel_array\n\n    # transform to hounsfield units\n    intercept = dicom_image.RescaleIntercept\n    slope = dicom_image.RescaleSlope\n    pixel_array = pixel_array * slope + intercept\n\n    # windowing\n    window_center = int(dicom_image.WindowCenter)\n    window_width = int(dicom_image.WindowWidth)\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    pixel_array = pixel_array.copy()\n    pixel_array[pixel_array < img_min] = img_min\n    pixel_array[pixel_array > img_max] = img_max\n\n    # normalization\n    if pixel_array.max() == pixel_array.min():\n        pixel_array = np.zeros_like(pixel_array)  # Handle case of constant array\n    else:\n        pixel_array = (pixel_array - pixel_array.min()) / (pixel_array.max() - pixel_array.min())\n\n    return pixel_array\n\ndef read_xray(path, fix_monochrome = True):\n    dicom = pydicom.dcmread(path)\n    data = standardize_pixel_array(dicom)\n    data = data - np.min(data)\n    data = data / (np.max(data) + 1e-5)\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = 1.0 - data\n    IMG_SIZE = [224, 224]\n    data = cv2.resize(data, IMG_SIZE, cv2.INTER_LINEAR)\n    data = (data * 255).astype(np.uint8)\n    return data\n\ndef load_scan(paths, NUM_SLICES = 32):\n    IMG_SIZE = [224, 224]\n    img = np.empty(shape=(*IMG_SIZE, NUM_SLICES), dtype=np.uint8)\n    for i, path in enumerate(paths):\n        img[...,i] = read_xray(path)\n    img = img.transpose(2, 0, 1)\n    return img\n\ndef load_img(path):\n    img = cv2.imread(path, -1)[...,::-1]\n    return img\n    \ndef resize_and_save(paths):\n    img = load_scan(paths, NUM_SLICES = 32)\n    file_path = paths[0]\n    sub_path = file_path.split(\"/\",4)[-1].split('.dcm')[0] + '.png'\n    infos = sub_path.split('/')\n    split = infos[-4]\n    pid = infos[-3]\n    sid = infos[-2]\n    iid = infos[-1]; iid = iid.replace('.png','')\n    new_path = os.path.join(IMG_DIR, split, pid, sid + '.png')\n    os.makedirs(new_path.rsplit('/',1)[0], exist_ok=True)\n    cv2.imwrite(new_path, img[...,::-1])\n    del img; gc.collect()\n    return \n\ndef show_img(img):\n    num_channels = img.shape[-1]\n    fig, axes = plt.subplots(1, num_channels+1, figsize=(num_channels*5, 5))\n    axes[0].imshow(img)\n    axes[0].set_title('Original Image')\n    axes[0].axis('off')\n\n    for i in range(num_channels):\n        axes[i+1].imshow(img[:, :, i], cmap='gray')\n        axes[i+1].set_title(f'Channel: {i:02d}')\n        axes[i+1].axis('off')\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.280873Z","iopub.status.idle":"2023-09-25T16:12:34.281624Z","shell.execute_reply.started":"2023-09-25T16:12:34.281383Z","shell.execute_reply":"2023-09-25T16:12:34.281406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SegmentationPredictor:\n    def __init__(self, cuda=0, nnUNet_path='', checkpoint_name='', nnunetv2_path=''):\n        self.predictor = self.initialize_predictor(cuda, nnUNet_path, checkpoint_name, nnunetv2_path)\n\n    def initialize_predictor(self, cuda, nnUNet_path, checkpoint_name, nnunetv2_path):\n        \"\"\"\n        Initialize the nnUNetPredictor.\n        \"\"\"\n        nnUNet_results = nnUNet_path\n        predictor = nnUNetPredictor(\n            tile_step_size=0.5,\n            use_gaussian=True,\n            perform_everything_on_gpu=True,\n            device=torch.device('cuda', cuda),\n            verbose=False,\n            verbose_preprocessing=False,\n        )\n        predictor.initialize_from_trained_model_folder(\n            nnUNet_results,\n            checkpoint_name=checkpoint_name,\n            nnunetv2_path=nnunetv2_path,\n        )\n        return predictor\n\n    def clear_memory(self):\n        \"\"\"\n        Clear GPU and RAM memory.\n        \"\"\"\n        mempool = cp.get_default_memory_pool()\n        mempool.free_all_blocks()\n        pinned_mempool = cp.get_default_pinned_memory_pool()\n        pinned_mempool.free_all_blocks()\n        gc.collect()\n    \n    def xray_seg(self, dicom_path, channels=64, clear_mempool=False):\n        img = read_xray(dicom_path)\n    def volume_and_seg(self, dicom_folder, channels=64, clear_mempool=False, segment = True):\n        \"\"\"\n        Returns a list with the DICOM paths, the segmentation, and volume in numpy format.\n        \"\"\"\n        img, props, dicom_list = NibabelIOWithReorient().read_images([dicom_folder], size_z=channels)\n        if segment:\n            seg = self.predictor.predict_single_npy_array(img, props, None, None)\n        \n        if clear_mempool:\n            self.clear_memory()\n        if segment:\n            return dicom_list, seg, img.squeeze()\n        else:\n            return img.squeeze()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.282998Z","iopub.status.idle":"2023-09-25T16:12:34.283746Z","shell.execute_reply.started":"2023-09-25T16:12:34.2835Z","shell.execute_reply":"2023-09-25T16:12:34.283522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image_with_seg(volume, volume_seg=None, num_subplots=20):\n    \"\"\"\n    Plot slices from a volume with their corresponding segmentation.\n    \n    :param volume: 3D numpy array representing the volume.\n    :param volume_seg: 3D numpy array representing the volume segmentation.\n    :param num_subplots: Number of slices to display.\n    \"\"\"\n    if volume_seg is None:\n        volume_seg = np.empty_like(volume, dtype=bool)  # Empty boolean array\n\n    slices = np.linspace(0, volume.shape[0]-1, num_subplots).astype(int)\n\n    rows = max(np.floor(np.sqrt(num_subplots)).astype(int) - 2, 1)\n    cols = int(np.ceil(num_subplots / rows))\n\n    fig, axes = plt.subplots(rows, cols, figsize=(cols * 2, rows * 4))\n\n    # Ensure axes is a 1D array for consistent indexing\n    axes = np.ravel(axes)\n\n    for ax in axes:\n        ax.axis('off')\n\n    for idx, slice_idx in enumerate(slices):\n        ax = axes[idx]\n        ax.imshow(volume[slice_idx], cmap='gray')\n        \n        mask = np.where(volume_seg[slice_idx], volume_seg[slice_idx], np.nan)\n        ax.imshow(mask, cmap='Set1', alpha=0.5)\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.285053Z","iopub.status.idle":"2023-09-25T16:12:34.285798Z","shell.execute_reply.started":"2023-09-25T16:12:34.285545Z","shell.execute_reply":"2023-09-25T16:12:34.285567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmenter = SegmentationPredictor(cuda=0,\n                                  nnUNet_path=\"/kaggle/working/model_seg\", \n                                  checkpoint_name=\"checkpoint_best.pth\",\n                                  nnunetv2_path = \"/kaggle/working/nnunetv2\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.287302Z","iopub.status.idle":"2023-09-25T16:12:34.288042Z","shell.execute_reply.started":"2023-09-25T16:12:34.28781Z","shell.execute_reply":"2023-09-25T16:12:34.287832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The segmentation classes: \n* liver: 1\n* spleen: 2\n* lkidney: 3\n* rkidney: 4\n* bowel: 5","metadata":{}},{"cell_type":"code","source":"#dicom_list, seg, img = segmenter.volume_and_seg(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10105/42418\", channels=32, clear_mempool=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.289345Z","iopub.status.idle":"2023-09-25T16:12:34.29007Z","shell.execute_reply.started":"2023-09-25T16:12:34.289837Z","shell.execute_reply":"2023-09-25T16:12:34.289858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot_image_with_seg(img,seg)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.291385Z","iopub.status.idle":"2023-09-25T16:12:34.292098Z","shell.execute_reply.started":"2023-09-25T16:12:34.291866Z","shell.execute_reply":"2023-09-25T16:12:34.291888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_missing_data_tensor(paths, NUM_SLICES = 32, segment = True):\n    if segment:\n        tmp = load_scan(paths, NUM_SLICES)\n        choices = [0, 1, 2, 3, 4, 5]\n        data = np.empty((NUM_SLICES*2, 224, 224), dtype=tmp.dtype)\n        data[0::2] = tmp\n        data[1::2] = np.random.choice(choices, size=(32, 224, 224))\n    else:\n        data = load_scan(paths, NUM_SLICES)\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.293405Z","iopub.status.idle":"2023-09-25T16:12:34.294133Z","shell.execute_reply.started":"2023-09-25T16:12:34.293902Z","shell.execute_reply":"2023-09-25T16:12:34.293924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# c = 128\n# path = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10494/65369\"\n# dicom_list, seg, img = segmenter.volume_and_seg(path, channels=c, clear_mempool=True)\n# interleaved = np.empty((c*2, 256, 256), dtype=img.dtype)\n# interleaved[0::2] = img\n# interleaved[1::2] = seg","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.295439Z","iopub.status.idle":"2023-09-25T16:12:34.296177Z","shell.execute_reply.started":"2023-09-25T16:12:34.295926Z","shell.execute_reply":"2023-09-25T16:12:34.295948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_slices(tensor):\n    # Assuming tensor shape is [64, 256, 256]\n    fig, axarr = plt.subplots(8, 8, figsize=(15, 15))\n    \n    for i in range(8):\n        for j in range(8):\n            slice_idx = i * 8 + j\n            axarr[i, j].imshow(tensor[slice_idx], cmap='gray')\n            axarr[i, j].axis('off')\n            axarr[i, j].set_title(f'Slice {slice_idx}')\n    \n    plt.subplots_adjust(wspace=0.2, hspace=0.5)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.297501Z","iopub.status.idle":"2023-09-25T16:12:34.298221Z","shell.execute_reply.started":"2023-09-25T16:12:34.297987Z","shell.execute_reply":"2023-09-25T16:12:34.298008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot_slices(interleaved)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.299525Z","iopub.status.idle":"2023-09-25T16:12:34.300253Z","shell.execute_reply.started":"2023-09-25T16:12:34.300019Z","shell.execute_reply":"2023-09-25T16:12:34.300042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# seg = seg == 4  # Change the number to the organ that you want to visualize\n# 0 is the background of the segmentation, if you want to use it as a mask of the combined segmentations you need to invert it first\n# mask = 1 - (seg == 0).astype(np.uint8)  # Change type from boolean to uint8 and swap the 0s and 1s (now the background is 0 and the segmentation 1)\n#plot_image_with_seg(img1, volume_seg = seg1 == 1, num_subplots=seg1.shape[0])\n#plot_image_with_seg(img, volume_seg = seg, num_subplots=seg.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.301554Z","iopub.status.idle":"2023-09-25T16:12:34.30227Z","shell.execute_reply.started":"2023-09-25T16:12:34.302036Z","shell.execute_reply":"2023-09-25T16:12:34.302057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Meta Data","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    torch.backends.cudnn.benchmark = True ","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.303568Z","iopub.status.idle":"2023-09-25T16:12:34.304278Z","shell.execute_reply.started":"2023-09-25T16:12:34.304045Z","shell.execute_reply":"2023-09-25T16:12:34.304067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/rsna-2023-abdominal-trauma-detection'\nIMG_DIR = '/tmp/dataset/rsna-atd'","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.305581Z","iopub.status.idle":"2023-09-25T16:12:34.306312Z","shell.execute_reply.started":"2023-09-25T16:12:34.306077Z","shell.execute_reply":"2023-09-25T16:12:34.306099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Dataframe","metadata":{}},{"cell_type":"code","source":"IMG_DIR = '/tmp/Dataset/rsna-atd'\n!rm -r {IMG_DIR}\nos.makedirs(f'{IMG_DIR}/test_images', exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.307625Z","iopub.status.idle":"2023-09-25T16:12:34.308346Z","shell.execute_reply.started":"2023-09-25T16:12:34.308115Z","shell.execute_reply":"2023-09-25T16:12:34.308136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(f'{BASE_PATH}/test_series_meta.csv')\ntest_df['dicom_folder'] = BASE_PATH + '/' + 'test_images'\\\n                                    + '/' + test_df.patient_id.astype(str)\\\n                                    + '/' + test_df.series_id.astype(str)\ntest_df['tensor_path'] = IMG_DIR + '/' + 'test_images'\\\n                                    + '/' + test_df.patient_id.astype(str)\\\n                                    + '/' + test_df.series_id.astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.309667Z","iopub.status.idle":"2023-09-25T16:12:34.310383Z","shell.execute_reply.started":"2023-09-25T16:12:34.310138Z","shell.execute_reply":"2023-09-25T16:12:34.310159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.31168Z","iopub.status.idle":"2023-09-25T16:12:34.312402Z","shell.execute_reply.started":"2023-09-25T16:12:34.31215Z","shell.execute_reply":"2023-09-25T16:12:34.312173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(f'/tmp/corrupt/', exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.313708Z","iopub.status.idle":"2023-09-25T16:12:34.314434Z","shell.execute_reply.started":"2023-09-25T16:12:34.314186Z","shell.execute_reply":"2023-09-25T16:12:34.314207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\nfiltered_df = test_df.loc[(test_df['patient_id'] == 3124) & (test_df['series_id'] == 5842), 'dicom_folder']\n\nif not filtered_df.empty:\n    src_dir = filtered_df.values[0]\n    dest_dir = '/tmp/corrupt/'\n    \n    # Continue with your operations...\n    if os.path.exists(src_dir):\n        for item in os.listdir(src_dir):\n            s = os.path.join(src_dir, item)\n            d = os.path.join(dest_dir, item)\n            if os.path.isdir(s):\n                shutil.copytree(s, d, False, None)\n            else:\n                shutil.copy2(s, d)\n    else:\n        print(f\"The folder {src_dir} does not exist.\")\nelse:\n    print(\"No matching rows found in the DataFrame.\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.315736Z","iopub.status.idle":"2023-09-25T16:12:34.316482Z","shell.execute_reply.started":"2023-09-25T16:12:34.316234Z","shell.execute_reply":"2023-09-25T16:12:34.316256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using .loc[] to change a cell value\ntest_df.loc[(test_df['patient_id'] == 3124) & (test_df['series_id'] == 5842), 'dicom_folder'] = \"/tmp/corrupt/\"","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.317797Z","iopub.status.idle":"2023-09-25T16:12:34.318524Z","shell.execute_reply.started":"2023-09-25T16:12:34.318274Z","shell.execute_reply":"2023-09-25T16:12:34.318297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /tmp/corrupt/","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.319824Z","iopub.status.idle":"2023-09-25T16:12:34.320555Z","shell.execute_reply.started":"2023-09-25T16:12:34.320306Z","shell.execute_reply":"2023-09-25T16:12:34.320328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = \"/tmp/corrupt/514.dcm\"\n# Check if the file exists\nif os.path.exists(file_path):\n    # Remove the file\n    os.remove(file_path)\n    print(f\"The file {file_path} has been removed.\")\nelse:\n    print(f\"The file {file_path} does not exist.\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.321857Z","iopub.status.idle":"2023-09-25T16:12:34.322582Z","shell.execute_reply.started":"2023-09-25T16:12:34.322335Z","shell.execute_reply":"2023-09-25T16:12:34.322371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /tmp/corrupt/","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.323908Z","iopub.status.idle":"2023-09-25T16:12:34.324629Z","shell.execute_reply.started":"2023-09-25T16:12:34.324395Z","shell.execute_reply":"2023-09-25T16:12:34.324417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_segment = True\n\ntest_folders = test_df.dicom_folder.tolist()\n\ntest_paths = []\nfor i, folder in enumerate(test_folders):\n    paths = sorted(glob(os.path.join(folder, '*dcm')),\n                   key=lambda x: int(x.split('/')[-1].split('.')[0]))\n    NUM_DICOM = len(paths)\n    if len(test_folders)>6: # private test; contains all dicom files/folders\n        test_paths += [paths]\n        if no_segment:\n            if NUM_DICOM > 32:\n                img = segmenter.volume_and_seg(folder, channels=64, clear_mempool=True, segment = False)\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", img)\n            else:            \n                data = create_missing_data_tensor(paths, NUM_SLICES = 64, segment = True)\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", data)\n        else:\n            if NUM_DICOM > 32:\n                dicom_list, seg, img = segmenter.volume_and_seg(folder, channels=32, clear_mempool=True, segment = True)\n                interleaved = np.empty((32*2, 224, 224), dtype=img.dtype)\n                interleaved[0::2] = img\n                interleaved[1::2] = seg\n                # Save the array to disk\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", interleaved)\n            else:            \n                data = create_missing_data_tensor(paths, NUM_SLICES = 32, segment = True)\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", data)\n    else: # we can't access all the test dicom files in public test\n        test_paths += [paths]\n        if len(paths) > 0:\n            if no_segment:\n                data = create_missing_data_tensor(paths, NUM_SLICES = 64, segment = False)\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", data)\n            else:\n                data = create_missing_data_tensor(paths, NUM_SLICES = 32, segment = True)\n                os.makedirs(test_df.iloc[i]['tensor_path'], exist_ok = True)\n                np.save(test_df.iloc[i]['tensor_path'] + \"/tensor.npy\", data)\ntest_df['dicom_paths'] = test_paths\ntest_df = test_df[test_df.dicom_paths.map(len)>0] # in public test not all folder contains dicom file","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.325979Z","iopub.status.idle":"2023-09-25T16:12:34.32671Z","shell.execute_reply.started":"2023-09-25T16:12:34.326467Z","shell.execute_reply":"2023-09-25T16:12:34.32649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.327991Z","iopub.status.idle":"2023-09-25T16:12:34.32872Z","shell.execute_reply.started":"2023-09-25T16:12:34.328477Z","shell.execute_reply":"2023-09-25T16:12:34.328499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp /kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10232/51162/* ./10005/18669/","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.330005Z","iopub.status.idle":"2023-09-25T16:12:34.33073Z","shell.execute_reply.started":"2023-09-25T16:12:34.330486Z","shell.execute_reply":"2023-09-25T16:12:34.330509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Create a DataFrame containing the new row\n# new_row = {\n#     'patient_id': 10005,\n#     'series_id': 18669,\n#     'aortic_hu': 340.9,\n#     'dicom_folder': '/kaggle/working/10005/18669',\n#     'tensor_path': '/kaggle/working/tensor/10005/18669',\n#     'dicom_paths': 'dpath3.dcm'\n# }\n# test_df = pd.concat([test_df, pd.DataFrame([new_row])], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.332026Z","iopub.status.idle":"2023-09-25T16:12:34.332759Z","shell.execute_reply.started":"2023-09-25T16:12:34.332516Z","shell.execute_reply":"2023-09-25T16:12:34.332538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# dicom_list, seg, img = segmenter.volume_and_seg(test_df.iloc[4].dicom_folder, channels=32, clear_mempool=True)\n# interleaved = np.empty((32*2, 256, 256), dtype=img.dtype)\n# interleaved[0::2] = img\n# interleaved[1::2] = seg\n# # Save the array to disk\n# os.makedirs(test_df.iloc[4].tensor_path, exist_ok = True)\n# np.save(test_df.iloc[4].tensor_path + \"/tensor.npy\", interleaved)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.334046Z","iopub.status.idle":"2023-09-25T16:12:34.334772Z","shell.execute_reply.started":"2023-09-25T16:12:34.33453Z","shell.execute_reply":"2023-09-25T16:12:34.334552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!ls '/kaggle/working/tensor/10005/18667'","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.33605Z","iopub.status.idle":"2023-09-25T16:12:34.33679Z","shell.execute_reply.started":"2023-09-25T16:12:34.336548Z","shell.execute_reply":"2023-09-25T16:12:34.33657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Pipeline","metadata":{}},{"cell_type":"code","source":"class AbdominalTestData(Dataset):\n    \"\"\"\n    Custom dataset class for handling abdominal trauma test data classification.\n    \n    Args:\n        img_paths (list of strings): List containing all image paths of a patient\n        target_size (tuple): The target size to resize the images to\n        ext (str): The extension of the image files\n        transform (callable, optional): A function/transform to apply to the images\n    \"\"\"\n    \n    def __init__(self, df):\n        \n        super().__init__()\n        \n        self.df = df\n        self.img_paths = self.df['tensor_path'].to_list()\n    \n    def __len__(self):\n        \"\"\"\n        Returns the total number of samples in the dataset.\n        \"\"\"\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        \"\"\"\n        Returns a sample from the dataset at the given index.\n        \n        Args:\n            idx (int): Index\n        \n        Returns:\n            tuple: (image, file_path)\n        \"\"\"\n        \n        file_path = self.img_paths[idx]\n        img = np.load(file_path + \"/tensor.npy\")\n        return torch.from_numpy(img).float()  ","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.338096Z","iopub.status.idle":"2023-09-25T16:12:34.338831Z","shell.execute_reply.started":"2023-09-25T16:12:34.338587Z","shell.execute_reply":"2023-09-25T16:12:34.338609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{}},{"cell_type":"code","source":"# def display_batch(batch, size=2):\n#     if isinstance(batch, tuple):\n#         imgs, tars = batch\n#         tars = torch.cat(tars, dim=-1).numpy()\n#     else:\n#         imgs = batch\n#         tars = None\n\n#     for img_idx in range(size):\n#         plot_slices(imgs[img_idx])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.340118Z","iopub.status.idle":"2023-09-25T16:12:34.340851Z","shell.execute_reply.started":"2023-09-25T16:12:34.340612Z","shell.execute_reply":"2023-09-25T16:12:34.340634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fold_df = test_df.copy()\n# dataset = AbdominalTestData(fold_df)\n# dataloader = DataLoader(dataset, batch_size=20,shuffle=False, num_workers=2)\n# for batch in dataloader:\n#     display_batch(batch,size=3)\n#     break","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.342149Z","iopub.status.idle":"2023-09-25T16:12:34.342885Z","shell.execute_reply.started":"2023-09-25T16:12:34.342646Z","shell.execute_reply":"2023-09-25T16:12:34.342677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility","metadata":{}},{"cell_type":"code","source":"def mc_proc(pred):\n    argmax = np.argmax(pred, axis=1).astype('uint8')\n    one_hot = tf.keras.utils.to_categorical(argmax, num_classes=3)\n    return one_hot.astype('uint8')\n\ndef sc_proc(pred, thr=0.5):\n    proc_pred = (pred > thr).astype('uint8')\n    return proc_pred\n\ndef post_proc(pred):\n    proc_pred = np.empty((pred.shape[0], 2 + 2 + 3*3), dtype=np.uint8)\n\n    # bowel, extravasation\n    proc_pred[:, 0] = sc_proc(pred[:, 0])\n    proc_pred[:, 1] = 1 - proc_pred[:, 0]\n    proc_pred[:, 2] = sc_proc(pred[:, 1])\n    proc_pred[:, 3] = 1 - proc_pred[:, 2]\n    \n    # liver, kidney, sneel\n    proc_pred[:, 4:7] = mc_proc(pred[:, 2:5])\n    proc_pred[:, 7:10] = mc_proc(pred[:, 5:8])\n    proc_pred[:, 10:13] = mc_proc(pred[:, 8:11])\n\n    return proc_pred\n\ndef post_proc_v2(pred):\n    proc_pred = np.empty((pred.shape[0], 2*2 + 3*3), dtype='float32')\n\n    # bowel, extravasation\n    proc_pred[:, 0] = 1 - pred[:, 0] # bowel-healthy\n    proc_pred[:, 1] = pred[:, 0] # bowel-injured\n    proc_pred[:, 2] = 1 - pred[:, 1] # extra-healthy\n    proc_pred[:, 3] = pred[:, 1] # extra-injured\n    \n    # liver, kidney, sneel\n    proc_pred[:, 4:7] = pred[:, 2:5]\n    proc_pred[:, 7:10] = pred[:, 5:8]\n    proc_pred[:, 10:13] = pred[:, 8:11]\n\n    return proc_pred","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.34423Z","iopub.status.idle":"2023-09-25T16:12:34.344957Z","shell.execute_reply.started":"2023-09-25T16:12:34.344722Z","shell.execute_reply":"2023-09-25T16:12:34.344745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Architecture","metadata":{}},{"cell_type":"code","source":"class ChannelSqueezer(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.expand = nn.Sequential(\n            nn.Conv2d(64, 32, kernel_size=3, padding=1),\n            nn.GELU()\n        )\n        self.squeeze = nn.Sequential(\n            nn.Conv2d(32, 3, kernel_size=3, padding=1),\n            nn.GELU()\n        )\n\n    def forward(self, x):\n        x = self.expand(x)\n        x = self.squeeze(x)\n        return x\n\nclass CNNModel(nn.Module):\n    def __init__(self, backbone, pretrained=False):\n        super().__init__()\n        \n        self.channel_squeezer = ChannelSqueezer()\n        self.feature_extractor = timm.create_model(\n            backbone,\n            in_chans=3,\n            pretrained=pretrained\n        )\n        \n#         f = self.feature_extractor.classifier.in_features\n#         self.feature_extractor.classifier = nn.Identity()\n        f = self.feature_extractor.head.in_features\n        self.feature_extractor.head = nn.Identity()\n        self.mlp = nn.Sequential(\n#             nn.BatchNorm2d(f),\n            nn.Linear(f, 64),\n            nn.SiLU(),\n#             nn.BatchNorm2d(64),\n            nn.Linear(64, 32),\n            nn.SiLU(),\n            nn.Dropout(0.4)\n        )\n        # for bowel and extravasation\n        self.logit1 = nn.Linear(32, 1)\n        self.logit2 = nn.Linear(32, 1)\n        \n        # for kidney, liver, spleen\n        self.logit3 = nn.Linear(32, 3)\n        self.logit4 = nn.Linear(32, 3)\n        self.logit5 = nn.Linear(32, 3)\n    \n    def forward(self, x):\n        x = self.channel_squeezer(x)\n        x = self.feature_extractor(x)\n        x = torch.flatten(x, 1)\n        x = self.mlp(x)\n        \n        # output logits\n        bowel = self.logit1(x)\n        extravasation = self.logit2(x)\n        kidney = self.logit3(x)\n        liver = self.logit4(x)\n        spleen = self.logit5(x)\n        \n        return bowel, extravasation, kidney, liver, spleen","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.346282Z","iopub.status.idle":"2023-09-25T16:12:34.347015Z","shell.execute_reply.started":"2023-09-25T16:12:34.34678Z","shell.execute_reply":"2023-09-25T16:12:34.346803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the Model","metadata":{}},{"cell_type":"code","source":"# Define the model architecture (adjust as necessary)\nbackbone = 'vit_base_patch16_224.augreg_in21k_ft_in1k'\nmodel_dir_cls = '/kaggle/input/3d-segment-pretrained'\ndevice = torch.device('cuda')\nREPLICAS = 1\nimg_size = [224,224]\nprint(f'REPLICAS: {REPLICAS}')\n# Load pre-trained models (add paths to your pre-trained model weights)\nmodel_paths = sorted(glob(f'{model_dir_cls}/*.pth'))[:1]\nprint(model_paths)\nmodels = []\n# model_paths = [\"/kaggle/input/efficentnet-b0-rsna/No_segmen_efficientnet_b0_2.589.pth\"]\nfor model_path in model_paths:\n    try:\n        model = CNNModel(backbone, pretrained=False)\n        model = model.to(device)\n        sd = torch.load(model_path)\n\n        # Checking if the loaded object is a dict or a model instance\n        if isinstance(sd, dict):\n            if 'model_state_dict' in sd.keys():\n                sd = sd['model_state_dict']\n            sd = {k[7:] if k.startswith('module.') else k: sd[k] for k in sd.keys()}\n        elif isinstance(sd, torch.nn.Module):\n            # Handling case where the entire model object was saved\n            # (not recommended due to potential compatibility issues)\n            model = sd\n            sd = None\n        else:\n            raise ValueError(\"Unrecognized format for loaded state dict\")\n\n        if sd is not None:\n            model.load_state_dict(sd, strict=True)\n        \n        model.eval()\n        models.append(model)\n    except Exception as e:\n        print(f\"Could not load model from {model_path}: {e}\")\nlen(models)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.348497Z","iopub.status.idle":"2023-09-25T16:12:34.349205Z","shell.execute_reply.started":"2023-09-25T16:12:34.348974Z","shell.execute_reply":"2023-09-25T16:12:34.348996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting unique patient IDs from test dataset\nimport torch.nn.functional as F\npatient_ids = test_df['patient_id'].unique()\n\n# Initializing array to store predictions\npatient_preds = np.zeros(shape=(len(patient_ids), 2*2 + 3*3), dtype='float32')\nwith torch.no_grad():\n# Iterating over each patient\n    for pidx, patient_id in tqdm(enumerate(patient_ids), total=len(patient_ids), desc=\"Patients \"):\n        # Query the dataframe for a particular patient\n        patient_df = test_df.query(\"patient_id == @patient_id\", engine='python')\n\n        # Initializing model predictions array\n        model_preds = np.zeros(shape=(1, 11), dtype=np.float32)\n\n        print(\"=\"*25)\n        print(f\"   Patient ID: {patient_id}\")\n        print(\"=\"*25)\n\n        # Iterating over each model\n        for _, model in enumerate(models):\n\n            # Getting image paths for a patient\n            patient_paths = patient_df.tensor_path.tolist()\n            #print(patient_paths)\n            # Setting batch size based on number of patient paths and dimension of image\n            dim = np.prod(img_size)**0.5\n            if dim >= 1024:\n                batch_size = REPLICAS * int(4 * 2)\n            elif dim >= 768:\n                batch_size = REPLICAS * int(16 * 2)\n            elif dim >= 640:\n                batch_size = REPLICAS * int(28 * 2)\n            else:\n                batch_size = REPLICAS * int(32 * 2)\n                \n            min_bs = 2**np.floor(np.log2(len(patient_paths)))\n            batch_size = min(min_bs, batch_size)\n            batch_size = int(batch_size)\n            # Building dataset for prediction\n            preds = []\n            #display(patient_df)\n            test_data = AbdominalTestData(patient_df)\n            dtest = DataLoader(test_data, batch_size=batch_size, shuffle=False)\n            # Iterating over each fold\n            # Loading a PyTorch model from a fold path\n\n            # Iterating over batches and getting predictions\n            #print(batch_size)\n            for batch_idx, batch_data in enumerate(tqdm(dtest)):\n                inputs = batch_data.to(device)\n                #print(inputs)\n                pred = model(inputs)\n                bowel =F.sigmoid(pred[0].cpu()).numpy().flatten()\n                extra = F.sigmoid(pred[1].cpu()).numpy().flatten()\n                kidney = F.softmax(pred[2].cpu(),dim =1).numpy().flatten()\n                liver = F.softmax(pred[3].cpu(),dim =1).numpy().flatten()\n                spleen = F.softmax(pred[4].cpu(),dim =1).numpy().flatten()\n\n                preds.append(np.concatenate((bowel,extra, kidney, liver, spleen), axis=0))                           \n            \n            preds = np.array(preds).astype('float32')\n            print(len(patient_paths))\n            print(preds.shape)\n            preds = preds.reshape(len(patient_paths), 11)\n            pred = np.max(preds, axis=0)\n\n            # Store model's prediction\n            model_preds += pred / (len(models))\n\n                # Deleting variables to free up memory\n            del model, pred\n            gc.collect()\n            print('\\n')\n\n            del dtest, patient_paths; gc.collect()\n\n        # Adding processed predictions to patient_preds\n        # (define the post_proc_v2 function to work with your predictions)\n        patient_preds[pidx, :] += post_proc_v2(model_preds)[0]\n\n        del model_preds\n        gc.collect()\n\nprint(\"Prediction Done!\")\n","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.350563Z","iopub.status.idle":"2023-09-25T16:12:34.351291Z","shell.execute_reply.started":"2023-09-25T16:12:34.351052Z","shell.execute_reply":"2023-09-25T16:12:34.351076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# Create Submission\ntarget_col  = [\"bowel_healthy\", \"bowel_injury\", \"extravasation_healthy\",\n                   \"extravasation_injury\", \"kidney_healthy\", \"kidney_low\",\n                   \"kidney_high\", \"liver_healthy\", \"liver_low\", \"liver_high\",\n                   \"spleen_healthy\", \"spleen_low\", \"spleen_high\"]\npred_df = pd.DataFrame({'patient_id':patient_ids,})\npred_df[target_col] = patient_preds.astype('float32')\n\n# Align with sample submission\nsub_df = pd.read_csv(f'{BASE_PATH}/sample_submission.csv')\nsub_df = sub_df[['patient_id']]\nsub_df = sub_df.merge(pred_df, on='patient_id', how='left')\n\n# Store submission\nsub_df.to_csv('submission.csv',index=False)\nsub_df","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.352607Z","iopub.status.idle":"2023-09-25T16:12:34.353332Z","shell.execute_reply.started":"2023-09-25T16:12:34.353098Z","shell.execute_reply":"2023-09-25T16:12:34.35312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scale_by_2 = ['kidney_low','liver_low','spleen_low','spleen_high']\n# scale_by_4 = ['bowel_injury','kidney_high','liver_high']\n# scale_by_6 = ['extravasation_injury']\n# scale_healthy = ['bowel_healthy', 'extravasation_healthy', 'kidney_healthy', 'liver_healthy', 'spleen_healthy']\n# sf_2 = 2\n# sf_4 = 4.841531\n# sf_6 = 20.81635153\n# scale_h = 0.99519515313","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.354666Z","iopub.status.idle":"2023-09-25T16:12:34.355392Z","shell.execute_reply.started":"2023-09-25T16:12:34.35514Z","shell.execute_reply":"2023-09-25T16:12:34.355163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # # Scale each target \n# sub_df[scale_by_2] *=sf_2\n# sub_df[scale_by_4] *=sf_4\n# sub_df[scale_by_6] *=sf_6\n# sub_df[scale_healthy] *=scale_h","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.356803Z","iopub.status.idle":"2023-09-25T16:12:34.357531Z","shell.execute_reply.started":"2023-09-25T16:12:34.35728Z","shell.execute_reply":"2023-09-25T16:12:34.357303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # # Store submission\n# sub_df.to_csv('submission.csv',index=False)\n# sub_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:12:34.358834Z","iopub.status.idle":"2023-09-25T16:12:34.35955Z","shell.execute_reply.started":"2023-09-25T16:12:34.359302Z","shell.execute_reply":"2023-09-25T16:12:34.359324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}