{"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":"Quick notebook showing how to create masks for any patient in the training dataset using TotalSegmentator.\n\nIt works offline, the weights are already downloaded.","metadata":{}},{"cell_type":"code","source":"#!pip install SimpleITK==2.0.2","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:12:24.529674Z","iopub.execute_input":"2023-09-10T16:12:24.530106Z","iopub.status.idle":"2023-09-10T16:12:24.53742Z","shell.execute_reply.started":"2023-09-10T16:12:24.530076Z","shell.execute_reply":"2023-09-10T16:12:24.536231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport time","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:12:24.540496Z","iopub.execute_input":"2023-09-10T16:12:24.541216Z","iopub.status.idle":"2023-09-10T16:12:24.552798Z","shell.execute_reply.started":"2023-09-10T16:12:24.541179Z","shell.execute_reply":"2023-09-10T16:12:24.551713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install git+https://github.com/wasserth/TotalSegmentator.git","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:12:24.554291Z","iopub.execute_input":"2023-09-10T16:12:24.554888Z","iopub.status.idle":"2023-09-10T16:12:27.373604Z","shell.execute_reply.started":"2023-09-10T16:12:24.554856Z","shell.execute_reply":"2023-09-10T16:12:27.3724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Installation**","metadata":{}},{"cell_type":"code","source":"!cp ../input/totalsegmentatorkaggle/totalSegmentatorKaggle/install.sh . && chmod +x install.sh && ./install.sh","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:12:27.377539Z","iopub.execute_input":"2023-09-10T16:12:27.377831Z","iopub.status.idle":"2023-09-10T16:15:03.040268Z","shell.execute_reply.started":"2023-09-10T16:12:27.377803Z","shell.execute_reply":"2023-09-10T16:15:03.03913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from totalsegmentator.python_api import totalsegmentator\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:15:03.042642Z","iopub.execute_input":"2023-09-10T16:15:03.043099Z","iopub.status.idle":"2023-09-10T16:15:05.138682Z","shell.execute_reply.started":"2023-09-10T16:15:03.043056Z","shell.execute_reply":"2023-09-10T16:15:05.137721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Patient_id 10104 and serie_id 27573 for the example, change it to any other patient if you want.\n\n!mkdir -p /kaggle/working/masks","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:15:05.139934Z","iopub.execute_input":"2023-09-10T16:15:05.140566Z","iopub.status.idle":"2023-09-10T16:15:06.098026Z","shell.execute_reply.started":"2023-09-10T16:15:05.140531Z","shell.execute_reply":"2023-09-10T16:15:06.096628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Generate masks**","metadata":{}},{"cell_type":"code","source":"%time\n# -f if you want to use 3d_lowres, faster and less memory intensive\nstart_time = time.time()\n!TotalSegmentator \\\n-i /kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10127/53380 \\\n-o /kaggle/working/masks/10127/53380 \\\n-ot 'nifti' \\\n-rs spleen kidney_left kidney_right liver \\\n--nr_thr_saving 1 \n#esophagus colon duodenum small_bowel stomach\nend_time = time.time()\nexecution_time = end_time - start_time\nprint(f'Время выполнения: {execution_time} секунд \\n')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:15:06.100327Z","iopub.execute_input":"2023-09-10T16:15:06.100708Z","iopub.status.idle":"2023-09-10T16:17:23.717697Z","shell.execute_reply.started":"2023-09-10T16:15:06.100662Z","shell.execute_reply":"2023-09-10T16:17:23.716379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"execution_time_slow = execution_time","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:17:23.719705Z","iopub.execute_input":"2023-09-10T16:17:23.720438Z","iopub.status.idle":"2023-09-10T16:17:23.725354Z","shell.execute_reply.started":"2023-09-10T16:17:23.720397Z","shell.execute_reply":"2023-09-10T16:17:23.724184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\n# -f if you want to use 3d_lowres, faster and less memory intensive\nstart_time = time.time()\n!TotalSegmentator \\\n-i /kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10127/53380 \\\n-o /kaggle/working/masks/10127/53380 \\\n-ot 'nifti' \\\n-rs spleen kidney_left kidney_right liver \\\n--fast \\\n--nr_thr_saving 1 \n#esophagus colon duodenum small_bowel stomach\nend_time = time.time()\nexecution_time_s1 = end_time - start_time\n\nprint(f'Время выполнения LowRes s1: {execution_time_s1} секунд \\n')\nprint(f'Время выполнения FullRes: {execution_time_slow} секунд \\n')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:17:23.727177Z","iopub.execute_input":"2023-09-10T16:17:23.727536Z","iopub.status.idle":"2023-09-10T16:18:53.820583Z","shell.execute_reply.started":"2023-09-10T16:17:23.727504Z","shell.execute_reply":"2023-09-10T16:18:53.819281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the generated masks\n\n!ls /kaggle/working/masks/10127/53380","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:18:53.825052Z","iopub.execute_input":"2023-09-10T16:18:53.827025Z","iopub.status.idle":"2023-09-10T16:18:54.820792Z","shell.execute_reply.started":"2023-09-10T16:18:53.826991Z","shell.execute_reply":"2023-09-10T16:18:54.819377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nfrom PIL import Image\n\nmask_path = '/kaggle/working/masks/10127/53380'\npng_path = '/kaggle/input/train-5of8/train/10127/53380'\n\norgans_list = ['kidney_left.nii.gz', 'kidney_right.nii.gz', 'liver.nii.gz', 'spleen.nii.gz']\n\ndef paint_masks(organ_name):\n    print(organ_name)\n    example_filename = mask_path +'/'+ organ_name\n    nii_img = nib.load(example_filename)\n    hdr = nii_img.header\n    print(hdr)\n    nii_data = nii_img.get_fdata()\n    \n    # Display the slices one at a time\n    for i in range(nii_data.shape[2]):\n        if np.any(nii_data[:, :, i]):\n            fig, ax = plt.subplots(1, 2, figsize=(15, 7))\n         # Поворот данных на 90 градусов против часовой стрелки\n            rotated_data = np.rot90(nii_data[:, :, i], k=1)\n            ax[0].imshow(rotated_data, cmap='gray')\n            ax[0].set_title(f'Slice {i}')\n            \n            png_filename = f'{png_path}/{str(i).zfill(4)}.png'\n            png_img = Image.open(png_filename)\n            ax[1].imshow(png_img)\n            ax[1].set_title(f'PNG {i}')\n            \n            plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:18:54.82296Z","iopub.execute_input":"2023-09-10T16:18:54.824545Z","iopub.status.idle":"2023-09-10T16:18:54.837286Z","shell.execute_reply.started":"2023-09-10T16:18:54.824498Z","shell.execute_reply":"2023-09-10T16:18:54.836077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = os.listdir(mask_path)\n\n# Фильтрация файлов по расширению .nii.gz\norgans_list = [file for file in files if file.endswith('.nii.gz')]\nprint(organs_list)\norgans_list = ['liver.nii.gz', 'spleen.nii.gz']","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:18:54.840082Z","iopub.execute_input":"2023-09-10T16:18:54.840427Z","iopub.status.idle":"2023-09-10T16:18:54.851834Z","shell.execute_reply.started":"2023-09-10T16:18:54.840398Z","shell.execute_reply":"2023-09-10T16:18:54.850649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for organ in organs_list:\n    paint_masks(organ)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T16:18:54.853451Z","iopub.execute_input":"2023-09-10T16:18:54.853825Z","iopub.status.idle":"2023-09-10T16:23:00.824423Z","shell.execute_reply.started":"2023-09-10T16:18:54.853794Z","shell.execute_reply":"2023-09-10T16:23:00.823343Z"},"trusted":true},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}