{"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":"code","source":"# draw inspiration from multiple notebook, discussion and past competitions. If you see familiar code please do let me know and I shall credit! \nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-14T22:43:40.481841Z","iopub.execute_input":"2023-09-14T22:43:40.48253Z","iopub.status.idle":"2023-09-14T22:43:40.493589Z","shell.execute_reply.started":"2023-09-14T22:43:40.482503Z","shell.execute_reply":"2023-09-14T22:43:40.492618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\n\nfile_path = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations/10000.nii\"\n\nnifti_img = nib.load(file_path)\n\nimg_arry = nifti_img.get_fdata()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:40.495541Z","iopub.execute_input":"2023-09-14T22:43:40.496093Z","iopub.status.idle":"2023-09-14T22:43:44.315458Z","shell.execute_reply.started":"2023-09-14T22:43:40.496061Z","shell.execute_reply":"2023-09-14T22:43:44.314305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nifti_img.header","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:44.318128Z","iopub.execute_input":"2023-09-14T22:43:44.318476Z","iopub.status.idle":"2023-09-14T22:43:44.336093Z","shell.execute_reply.started":"2023-09-14T22:43:44.318449Z","shell.execute_reply":"2023-09-14T22:43:44.334829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nifti_img.affine","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:44.338125Z","iopub.execute_input":"2023-09-14T22:43:44.338664Z","iopub.status.idle":"2023-09-14T22:43:44.34829Z","shell.execute_reply.started":"2023-09-14T22:43:44.338631Z","shell.execute_reply":"2023-09-14T22:43:44.346054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I did not know what does affine matrix does. ","metadata":{}},{"cell_type":"markdown","source":"Formal definition:<br>\nAn affine transformation matrix is a mathematical representation of a linear transformation that includes translation, rotation, scaling, and shearing. In the context of medical imaging and NIfTI files, the affine matrix is used to map the coordinates of voxels in the image to real-world coordinates in a specific coordinate system.","metadata":{}},{"cell_type":"markdown","source":"In simple words, the images we see in computer needs to be translated into real world coordinates.","metadata":{}},{"cell_type":"code","source":"img_arry.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:44.353802Z","iopub.execute_input":"2023-09-14T22:43:44.354181Z","iopub.status.idle":"2023-09-14T22:43:44.364401Z","shell.execute_reply.started":"2023-09-14T22:43:44.354145Z","shell.execute_reply":"2023-09-14T22:43:44.363276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I am justs going to pick one slice and plot it.","metadata":{}},{"cell_type":"markdown","source":"When you observe the color bar in the image visualization, it showcases five specific numeric labels, each signifying a distinct structure within the image:\n\n- **0**: Background\n- **1**: Liver\n- **2**: Spleen\n- **3**: Left Kidney\n- **4**: Right Kidney\n- **5**: Bowel\n\nThese numeric labels serve as a legend, helping us comprehend the correspondence between different colors and specific anatomical structures. For example, a color represented by \"1\" corresponds to the liver, \"3\" designates the left kidney, and so on. This color-coded legend aids in the identification and differentiation of various organs and structures within the medical image.","metadata":{}},{"cell_type":"code","source":"import nibabel as nib\nimport matplotlib.pyplot as plt\n\n# Load the NIfTI file\nnifti_image = nib.load(file_path)  # Replace with your file path\n\n# Get the image data\nimage_data = nifti_image.get_fdata()\n\n# Choose a slice index (for example, the middle slice along the z-axis)\nslice_index = image_data.shape[-1] // 2\n\n# Plot the selected slice\nplt.imshow(image_data[:, :, slice_index], cmap='viridis')\nplt.title(\"NIfTI Image Slice\")\nplt.colorbar()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:44.367339Z","iopub.execute_input":"2023-09-14T22:43:44.368396Z","iopub.status.idle":"2023-09-14T22:43:45.407718Z","shell.execute_reply.started":"2023-09-14T22:43:44.36837Z","shell.execute_reply":"2023-09-14T22:43:45.405929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Explore methods of nifti header","metadata":{}},{"cell_type":"code","source":"# Load the NIfTI file\nnifti_image = nib.load(file_path)  # Replace with your file path\n\n# Get the header object\nnifti_header = nifti_image.header\n\n# Access specific header fields\ndata_type = nifti_header.get_data_dtype()  # Data type of the image\ndimensions = nifti_header.get_data_shape()  # Dimensions of the image\naffine_matrix = nifti_header.get_qform()  # Affine transformation matrix\nvoxel_sizes = nifti_header.get_zooms()  # Voxel sizes in each dimension\n\n# Print the header information\nprint(\"Data type:\", data_type)\nprint(\"Dimensions:\", dimensions)\nprint(\"Affine matrix:\\n\", affine_matrix)\nprint(\"Voxel sizes:\", voxel_sizes)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:45.408927Z","iopub.execute_input":"2023-09-14T22:43:45.409782Z","iopub.status.idle":"2023-09-14T22:43:45.4203Z","shell.execute_reply.started":"2023-09-14T22:43:45.409746Z","shell.execute_reply":"2023-09-14T22:43:45.419383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nifti_header.get_xyzt_units()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:45.421624Z","iopub.execute_input":"2023-09-14T22:43:45.42301Z","iopub.status.idle":"2023-09-14T22:43:45.433163Z","shell.execute_reply.started":"2023-09-14T22:43:45.422985Z","shell.execute_reply":"2023-09-14T22:43:45.432216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport xarray as xr\nimport plotly.express as px\nfrom pathlib import Path\nimport pydicom\nimport xarray\nimport plotly.express as px\n\nimport shutil\n\nimport nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:45.434786Z","iopub.execute_input":"2023-09-14T22:43:45.435655Z","iopub.status.idle":"2023-09-14T22:43:46.239863Z","shell.execute_reply.started":"2023-09-14T22:43:45.435621Z","shell.execute_reply":"2023-09-14T22:43:46.238972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src = Path('/kaggle/input/rsna-2023-abdominal-trauma-detection')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:46.241583Z","iopub.execute_input":"2023-09-14T22:43:46.24227Z","iopub.status.idle":"2023-09-14T22:43:46.247384Z","shell.execute_reply.started":"2023-09-14T22:43:46.242212Z","shell.execute_reply":"2023-09-14T22:43:46.246133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Import in dicom files that corresponding to its segmentation","metadata":{}},{"cell_type":"markdown","source":"Code from another notebook, making sure the rotation is correct to match the segmentation images. ","metadata":{}},{"cell_type":"markdown","source":"get all the segmentation number","metadata":{}},{"cell_type":"code","source":"import os\n\ndirectory = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations\"\nmasks_file = []\nfor filename in os.listdir(directory):\n    masks_file.append(filename.split('.')[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:46.248967Z","iopub.execute_input":"2023-09-14T22:43:46.249683Z","iopub.status.idle":"2023-09-14T22:43:46.322967Z","shell.execute_reply.started":"2023-09-14T22:43:46.249651Z","shell.execute_reply":"2023-09-14T22:43:46.322059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport shutil\n\nsrc = Path('/kaggle/input/rsna-2023-abdominal-trauma-detection')\nworking = Path('/kaggle/working')\nmask_file = masks_file[0]\n\nshutil.copy(src / 'segmentations' / f'{mask_file}.nii', working / f'{mask_file}.nii')  # assuming you have a destination_path defined\nmasks = nib.load(working / f'{mask_file}.nii').get_fdata()\nmasks = masks.transpose((2, 1, 0))[::-1, ::-1, :] # we change the order of the dimensions. Flip horizontally and vertically.","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:46.326313Z","iopub.execute_input":"2023-09-14T22:43:46.326652Z","iopub.status.idle":"2023-09-14T22:43:48.510164Z","shell.execute_reply.started":"2023-09-14T22:43:46.326621Z","shell.execute_reply":"2023-09-14T22:43:48.509187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After we make sure nii have the same rotation as Segmentation images. Lets plot them and take a look!","metadata":{}},{"cell_type":"code","source":"# use number in segmentation to find imgs in dicom\ndicom_path = list((src/ 'train_images').glob(f'*/{mask_file}'))[0]\npatient_id, series_id = dicom_path.parent.name, dicom_path.name\nprint('patient_id is ' + patient_id, 'series_id is ' +series_id + ' in Segmentation folder')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:48.511528Z","iopub.execute_input":"2023-09-14T22:43:48.511868Z","iopub.status.idle":"2023-09-14T22:43:52.275553Z","shell.execute_reply.started":"2023-09-14T22:43:48.511838Z","shell.execute_reply":"2023-09-14T22:43:52.274617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:52.281169Z","iopub.execute_input":"2023-09-14T22:43:52.281494Z","iopub.status.idle":"2023-09-14T22:43:52.289466Z","shell.execute_reply.started":"2023-09-14T22:43:52.281467Z","shell.execute_reply":"2023-09-14T22:43:52.288527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"get the dicom images that is corresponding to the segmentation","metadata":{}},{"cell_type":"code","source":"imgs = []\n\nfiles = sorted(\n    list((src / 'train_images' / patient_id / series_id).glob('*.dcm')),\n    key = lambda x : int(x.stem)\n)\n\nfor f in files:\n    imgs.append(pydicom.dcmread(f).pixel_array)\n    \nimgs = np.stack(imgs)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:52.291118Z","iopub.execute_input":"2023-09-14T22:43:52.292065Z","iopub.status.idle":"2023-09-14T22:43:56.448565Z","shell.execute_reply.started":"2023-09-14T22:43:52.292032Z","shell.execute_reply":"2023-09-14T22:43:56.447278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we have the same dimension for sanity check","metadata":{}},{"cell_type":"code","source":"imgs.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:56.453538Z","iopub.execute_input":"2023-09-14T22:43:56.455933Z","iopub.status.idle":"2023-09-14T22:43:56.46631Z","shell.execute_reply.started":"2023-09-14T22:43:56.455897Z","shell.execute_reply":"2023-09-14T22:43:56.465225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:56.471047Z","iopub.execute_input":"2023-09-14T22:43:56.473788Z","iopub.status.idle":"2023-09-14T22:43:56.482683Z","shell.execute_reply.started":"2023-09-14T22:43:56.473755Z","shell.execute_reply":"2023-09-14T22:43:56.481762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xarr_imgs = xr.DataArray(\n    imgs,\n    dims = ['file', 'height', 'width'],\n    coords = [\n        [f.name for f in files],\n        [i for i in range(512)],\n        [i for i in range(512)],\n    ]\n)\n\nxarr_mask = xr.DataArray(\n    masks,\n    dims = ['file', 'height', 'width'],\n    coords = [\n        [f.name for f in files],\n        [i for i in range(512)],\n        [i for i in range(512)],\n    ]\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:56.485566Z","iopub.execute_input":"2023-09-14T22:43:56.486976Z","iopub.status.idle":"2023-09-14T22:43:56.504974Z","shell.execute_reply.started":"2023-09-14T22:43:56.486227Z","shell.execute_reply":"2023-09-14T22:43:56.50382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the first image\nxarr_mask.isel(file=1).plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:56.508961Z","iopub.execute_input":"2023-09-14T22:43:56.511218Z","iopub.status.idle":"2023-09-14T22:43:58.704032Z","shell.execute_reply.started":"2023-09-14T22:43:56.511187Z","shell.execute_reply":"2023-09-14T22:43:58.70313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Get the first image from xarr_imgs and xarr_mask\nnum = 133\nimg1 = xarr_imgs.isel(file=num).values\nimg2 = xarr_mask.isel(file=num).values\n\n# Create a figure and axis\nfig, ax = plt.subplots()\n\n# Display the first image\nax.imshow(img1, cmap='gray')  # You can choose a different colormap if needed\n\n# Overlay the second image with some transparency\nax.imshow(img2, cmap='jet', alpha=0.5)  # Adjust alpha for desired transparency\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:58.706218Z","iopub.execute_input":"2023-09-14T22:43:58.706881Z","iopub.status.idle":"2023-09-14T22:43:59.014712Z","shell.execute_reply.started":"2023-09-14T22:43:58.706845Z","shell.execute_reply":"2023-09-14T22:43:59.013863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The segmentation labels are as follows:\n   1. 0 = background\n   2. 1 = liver\n   3. 2 = spleen\n   4. 3 = left kidney\n   5. 4 = right kidney\n   6. 5 = bowel\n","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom IPython.display import HTML\n","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:59.01572Z","iopub.execute_input":"2023-09-14T22:43:59.016076Z","iopub.status.idle":"2023-09-14T22:43:59.024905Z","shell.execute_reply.started":"2023-09-14T22:43:59.016042Z","shell.execute_reply":"2023-09-14T22:43:59.023876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# # Create a figure and axis\n# fig, ax = plt.subplots()\n\n# # Initial display\n# num = 0\n# img1 = xarr_imgs.isel(file=num).values\n# img2 = xarr_mask.isel(file=num).values\n# im1_display = ax.imshow(img1, cmap='gray')\n# im2_display = ax.imshow(img2, cmap='jet', alpha=0.5)\n\n# cbar = fig.colorbar(im2_display, ax=ax, orientation='vertical')\n\n# ax.axis('off')  # hide axis labels and ticks\n# title_text = ax.set_title(f\"Num: {num}\")\n\n# def update(frame):\n#     num = 0 + frame\n\n#     # Get the images from xarr_imgs and xarr_mask\n#     img1 = xarr_imgs.isel(file=num).values\n#     img2 = xarr_mask.isel(file=num).values\n    \n#     # Update the displayed image data\n#     im1_display.set_array(img1)\n#     im2_display.set_array(img2)\n#     title_text.set_text(f\"Num: {num}\")\n\n#     return im1_display, im2_display, title_text\n\n# ani = FuncAnimation(fig, update, frames=207, blit=True)\n# HTML(ani.to_jshtml())","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:43:59.027213Z","iopub.execute_input":"2023-09-14T22:43:59.027968Z","iopub.status.idle":"2023-09-14T22:44:21.526585Z","shell.execute_reply.started":"2023-09-14T22:43:59.027937Z","shell.execute_reply":"2023-09-14T22:44:21.525505Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')\ntrain = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:44:21.528294Z","iopub.execute_input":"2023-09-14T22:44:21.528634Z","iopub.status.idle":"2023-09-14T22:44:21.594175Z","shell.execute_reply.started":"2023-09-14T22:44:21.528603Z","shell.execute_reply":"2023-09-14T22:44:21.593202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train.bowel_injury == 1]","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:44:21.595863Z","iopub.execute_input":"2023-09-14T22:44:21.59622Z","iopub.status.idle":"2023-09-14T22:44:21.621394Z","shell.execute_reply.started":"2023-09-14T22:44:21.596186Z","shell.execute_reply":"2023-09-14T22:44:21.620568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train.patient_id == 47065]","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:44:21.622904Z","iopub.execute_input":"2023-09-14T22:44:21.623561Z","iopub.status.idle":"2023-09-14T22:44:21.639977Z","shell.execute_reply.started":"2023-09-14T22:44:21.623529Z","shell.execute_reply":"2023-09-14T22:44:21.638816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta[train_series_meta.series_id == 39222]","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:44:21.641422Z","iopub.execute_input":"2023-09-14T22:44:21.641765Z","iopub.status.idle":"2023-09-14T22:44:21.655838Z","shell.execute_reply.started":"2023-09-14T22:44:21.641733Z","shell.execute_reply":"2023-09-14T22:44:21.65491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ../input/totalsegmentatorkaggle/totalSegmentatorKaggle/install.sh . && chmod +x install.sh && ./install.sh","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:44:21.657255Z","iopub.execute_input":"2023-09-14T22:44:21.657662Z","iopub.status.idle":"2023-09-14T22:47:17.003124Z","shell.execute_reply.started":"2023-09-14T22:44:21.657631Z","shell.execute_reply":"2023-09-14T22:47:17.001955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/temp/masks","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:47:17.005052Z","iopub.execute_input":"2023-09-14T22:47:17.006521Z","iopub.status.idle":"2023-09-14T22:47:17.959847Z","shell.execute_reply.started":"2023-09-14T22:47:17.006484Z","shell.execute_reply":"2023-09-14T22:47:17.958491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!TotalSegmentator \\\n-i /kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057 \\\n-o /kaggle/temp/masks \\\n-ot 'nifti' \\\n-rs spleen small_bowel kidney_left kidney_right liver\n# --fast ","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:51:05.095692Z","iopub.execute_input":"2023-09-14T22:51:05.09609Z","iopub.status.idle":"2023-09-14T22:54:35.324351Z","shell.execute_reply.started":"2023-09-14T22:51:05.096055Z","shell.execute_reply":"2023-09-14T22:54:35.323034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/temp/masks","metadata":{"execution":{"iopub.status.busy":"2023-09-14T22:58:37.269448Z","iopub.execute_input":"2023-09-14T22:58:37.269929Z","iopub.status.idle":"2023-09-14T22:58:38.275991Z","shell.execute_reply.started":"2023-09-14T22:58:37.269892Z","shell.execute_reply":"2023-09-14T22:58:38.274748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = nib.load('/kaggle/temp/masks/small_bowel.nii.gz').get_fdata()\n# masks = masks.transpose((2, 1, 0))[::-1, ::-1, ::-1]\nmasks = masks.transpose((2, 0, 1))[::-1,:,:]\nmasks.shape\n# np.rot90(nii_data[:, :, i], k=1)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:38:01.153058Z","iopub.execute_input":"2023-09-14T23:38:01.1535Z","iopub.status.idle":"2023-09-14T23:38:02.88554Z","shell.execute_reply.started":"2023-09-14T23:38:01.153463Z","shell.execute_reply":"2023-09-14T23:38:02.883149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_xarr_mask = xr.DataArray(\n    masks,\n    dims = ['file', 'height', 'width'],\n    coords = [\n        [i for i in range(masks.shape[0])],\n        [i for i in range(512)],\n        [i for i in range(512)],\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:38:03.890639Z","iopub.execute_input":"2023-09-14T23:38:03.893305Z","iopub.status.idle":"2023-09-14T23:38:03.900717Z","shell.execute_reply.started":"2023-09-14T23:38:03.893266Z","shell.execute_reply":"2023-09-14T23:38:03.899588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Get the first image from xarr_imgs and xarr_mask\n# num = 500\n# seg_img = seg_xarr_mask.isel(file=num).values\n# seg_img = np.rot90(seg_img, k=1)\n# # Create a figure and axis\n# fig, ax = plt.subplots()\n\n# # Display the first image\n# ax.imshow(seg_img, cmap='gray')  # You can choose a different colormap if needed\n\n# # Overlay the second image with some transparency\n# # ax.imshow(img2, cmap='jet', alpha=0.5)  # Adjust alpha for desired transparency\n\n# plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:27:21.490883Z","iopub.execute_input":"2023-09-14T23:27:21.491284Z","iopub.status.idle":"2023-09-14T23:27:21.496829Z","shell.execute_reply.started":"2023-09-14T23:27:21.491244Z","shell.execute_reply":"2023-09-14T23:27:21.495673Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read in the dcm images to overlay dcm and TotalSegmentator masks\n\nseg_imgs = []\n\nfiles = sorted(\n    list((src / 'train_images' / '10004' / '21057').glob('*.dcm')),\n    key = lambda x : int(x.stem)\n)\n\nfor f in files:\n    seg_imgs.append(pydicom.dcmread(f).pixel_array)\n    \nimgs = np.stack(seg_imgs)\n\nxarr_imgs = xr.DataArray(\n    imgs,\n    dims = ['file', 'height', 'width'],\n    coords = [\n        [f.name for f in files],\n        [i for i in range(512)],\n        [i for i in range(512)],\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:38:06.265359Z","iopub.execute_input":"2023-09-14T23:38:06.265764Z","iopub.status.idle":"2023-09-14T23:38:26.746826Z","shell.execute_reply.started":"2023-09-14T23:38:06.265731Z","shell.execute_reply":"2023-09-14T23:38:25.814119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the first image from xarr_imgs and xarr_mask\nnum = 570\nimg1 = xarr_imgs.isel(file=num).values\nimg2 = seg_xarr_mask.isel(file=num).values\nimg2 = np.rot90(img2, k=1)\n# Create a figure and axis\nfig, ax = plt.subplots()\n\n# Display the first image\nax.imshow(img1, cmap='gray')  # You can choose a different colormap if needed\n\n# Overlay the second image with some transparency\nax.imshow(img2, cmap='jet', alpha=0.2)  # Adjust alpha for desired transparency\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:43:42.838047Z","iopub.execute_input":"2023-09-14T23:43:42.838471Z","iopub.status.idle":"2023-09-14T23:43:43.809057Z","shell.execute_reply.started":"2023-09-14T23:43:42.838437Z","shell.execute_reply":"2023-09-14T23:43:43.808154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Number of images to display\nstart_num = 500\nend_num = 550\ntotal_images = end_num - start_num + 1\n\n# Calculate the number of rows and columns for the subplots\n# Here, I'm assuming a grid of 5 columns. You can adjust this as needed.\ncols = 5\nrows = int(np.ceil(total_images / cols))\n\n# Create the subplots\nfig, axes = plt.subplots(rows, cols, figsize=(40, 40))  # Adjust the figsize for your preference\nfig.subplots_adjust(hspace=0.5)  # Adjust space between plots\n\nfor i, num in enumerate(range(start_num, end_num + 1)):\n    # Get the images\n    img1 = xarr_imgs.isel(file=num).values\n    img2 = seg_xarr_mask.isel(file=num).values\n    img2 = np.rot90(img2, k=1)\n    \n    # Determine the current row and column\n    current_row = i // cols\n    current_col = i % cols\n    \n    ax = axes[current_row, current_col]\n    \n    # Display the images on the current subplot\n    ax.imshow(img1, cmap='gray')\n    ax.imshow(img2, cmap='jet', alpha=0.5)\n    ax.set_title(f\"Num: {num}\")\n    ax.axis('off')  # Hide axes for each subplot\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T23:42:38.649796Z","iopub.execute_input":"2023-09-14T23:42:38.650197Z","iopub.status.idle":"2023-09-14T23:42:45.173333Z","shell.execute_reply.started":"2023-09-14T23:42:38.650164Z","shell.execute_reply":"2023-09-14T23:42:45.172184Z"},"trusted":true},"execution_count":null,"outputs":[]}]}