{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# RSNA-MICCAI Brain Tumor Radiogenomic Classification: Show animation results of preprocessing the dataset by TorchIO with animation","metadata":{}},{"cell_type":"markdown","source":"## Summary\n\nIn this notebook, we will show results of preprocessing the dataset by TorchIO with animation.\n\nThe work uses some ideas from great work below:\n\n- https://www.kaggle.com/fepegar/preprocessing-mri-with-torchio\n- https://torchio.readthedocs.io/datasets.html?highlight=rsnamiccai#id4","metadata":{}},{"cell_type":"markdown","source":"## Implementation","metadata":{"execution":{"iopub.status.busy":"2021-09-25T14:14:35.634074Z","iopub.execute_input":"2021-09-25T14:14:35.634435Z","iopub.status.idle":"2021-09-25T14:14:35.650614Z","shell.execute_reply.started":"2021-09-25T14:14:35.63433Z","shell.execute_reply":"2021-09-25T14:14:35.649685Z"}}},{"cell_type":"markdown","source":"### Import modules and define consts and functions","metadata":{}},{"cell_type":"code","source":"!pip install torchio==0.18.57","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:54:13.652147Z","iopub.execute_input":"2021-09-25T22:54:13.653133Z","iopub.status.idle":"2021-09-25T22:54:26.007509Z","shell.execute_reply.started":"2021-09-25T22:54:13.652896Z","shell.execute_reply":"2021-09-25T22:54:26.006393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nfrom pathlib import Path\n\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\nimport numpy as np\nimport torch\nimport torchio as tio","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:56:27.617644Z","iopub.execute_input":"2021-09-25T22:56:27.61794Z","iopub.status.idle":"2021-09-25T22:56:27.623104Z","shell.execute_reply.started":"2021-09-25T22:56:27.617912Z","shell.execute_reply":"2021-09-25T22:56:27.622008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_IMAGES = 64\nPROCESSED_DATA_DIR = Path(\"../input/rsna-miccai-preprocessed-by-torchio/rsna-preprocessed\")\nANIMATION_OUT_DIR = Path(\"./animation\")\nMRI_TYPES = [\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\"]","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:54:28.171209Z","iopub.execute_input":"2021-09-25T22:54:28.171523Z","iopub.status.idle":"2021-09-25T22:54:28.177258Z","shell.execute_reply.started":"2021-09-25T22:54:28.171484Z","shell.execute_reply":"2021-09-25T22:54:28.176302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_img(img: torch.Tensor) -> torch.Tensor:\n    middle_idx = img.shape[-1] // 2\n    num_half_imgs = NUM_IMAGES // 2\n    start_idx = max(0, middle_idx - num_half_imgs)\n    end_idx = min(img.shape[-1], middle_idx + num_half_imgs)\n    return img.data.transpose(0, 3)[start_idx: end_idx].transpose(0, 3)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:54:28.178928Z","iopub.execute_input":"2021-09-25T22:54:28.179337Z","iopub.status.idle":"2021-09-25T22:54:28.189971Z","shell.execute_reply.started":"2021-09-25T22:54:28.179297Z","shell.execute_reply":"2021-09-25T22:54:28.189146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_animation(subject: tio.data.image.Image, save_path: str, *, skip_if_exists=False):\n    if skip_if_exists and save_path.exists():\n        return\n    \n    fig = plt.figure()\n\n    ims = []\n    for sub_2d in extract_img(subject.data).transpose(0, 3):\n        im = plt.imshow(sub_2d, animated=True)\n        ims.append([im])\n    \n    ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000)\n    ani.save(save_path, writer='pillow', fps=30)\n    plt.close()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:54:28.192801Z","iopub.execute_input":"2021-09-25T22:54:28.193225Z","iopub.status.idle":"2021-09-25T22:54:28.203405Z","shell.execute_reply.started":"2021-09-25T22:54:28.19314Z","shell.execute_reply":"2021-09-25T22:54:28.202524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ANIMATION_OUT_DIR.mkdir(exist_ok=True, parents=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:54:28.20509Z","iopub.execute_input":"2021-09-25T22:54:28.205408Z","iopub.status.idle":"2021-09-25T22:54:28.216513Z","shell.execute_reply.started":"2021-09-25T22:54:28.205368Z","shell.execute_reply":"2021-09-25T22:54:28.215703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:56:34.256871Z","iopub.execute_input":"2021-09-25T22:56:34.257173Z","iopub.status.idle":"2021-09-25T22:56:34.262801Z","shell.execute_reply.started":"2021-09-25T22:56:34.257142Z","shell.execute_reply":"2021-09-25T22:56:34.261966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Example: BraTS21ID-00000\n\nLet's show first sample whose MGMT_value is 1.","metadata":{}},{"cell_type":"code","source":"id_ = 0\nfor mri_type in MRI_TYPES:\n    sub = tio.Image(PROCESSED_DATA_DIR.joinpath(f\"train/{str(id_).zfill(5)}/{mri_type}/{mri_type}.nii\"))\n    make_animation(sub, ANIMATION_OUT_DIR.joinpath(f\"sub_{id_}_{mri_type}.gif\"), skip_if_exists=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:56:36.577961Z","iopub.execute_input":"2021-09-25T22:56:36.578689Z","iopub.status.idle":"2021-09-25T22:56:36.59322Z","shell.execute_reply.started":"2021-09-25T22:56:36.578634Z","shell.execute_reply":"2021-09-25T22:56:36.59236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- FLAIR\n\n![](./animation/sub_0_FLAIR.gif)\n\n- T1w\n\n![](./animation/sub_0_T1w.gif)\n\n- T1wCE\n\n![](./animation/sub_0_T1wCE.gif)\n\n- T2w\n\n![](./animation/sub_0_T2w.gif)","metadata":{}},{"cell_type":"markdown","source":"### Example: BraTS21ID-00002\n\nNext, we show second sample whose MGMT_value is 1.","metadata":{}},{"cell_type":"code","source":"id_ = 2\nfor mri_type in MRI_TYPES:\n    sub = tio.Image(PROCESSED_DATA_DIR.joinpath(f\"train/{str(id_).zfill(5)}/{mri_type}/{mri_type}.nii\"))\n    make_animation(sub, ANIMATION_OUT_DIR.joinpath(f\"sub_{id_}_{mri_type}.gif\"), skip_if_exists=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:58:23.867766Z","iopub.execute_input":"2021-09-25T22:58:23.86811Z","iopub.status.idle":"2021-09-25T22:58:43.032696Z","shell.execute_reply.started":"2021-09-25T22:58:23.868075Z","shell.execute_reply":"2021-09-25T22:58:43.031671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- FLAIR\n\n![](./animation/sub_2_FLAIR.gif)\n\n- T1w\n\n![](./animation/sub_2_T1w.gif)\n\n- T1wCE\n\n![](./animation/sub_2_T1wCE.gif)\n\n- T2w\n\n![](./animation/sub_2_T2w.gif)","metadata":{}},{"cell_type":"markdown","source":"### Example: BraTS21ID-00003\n\nAnd we show first sample whose MGMT_value is 0.","metadata":{}},{"cell_type":"code","source":"id_ = 3\nfor mri_type in MRI_TYPES:\n    sub = tio.Image(PROCESSED_DATA_DIR.joinpath(f\"train/{str(id_).zfill(5)}/{mri_type}/{mri_type}.nii\"))\n    make_animation(sub, ANIMATION_OUT_DIR.joinpath(f\"sub_{id_}_{mri_type}.gif\"), skip_if_exists=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:00:35.237528Z","iopub.execute_input":"2021-09-25T23:00:35.238281Z","iopub.status.idle":"2021-09-25T23:00:53.404106Z","shell.execute_reply.started":"2021-09-25T23:00:35.238236Z","shell.execute_reply":"2021-09-25T23:00:53.403294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- FLAIR\n\n![](./animation/sub_3_FLAIR.gif)\n\n- T1w\n\n![](./animation/sub_3_T1w.gif)\n\n- T1wCE\n\n![](./animation/sub_3_T1wCE.gif)\n\n- T2w\n\n![](./animation/sub_3_T2w.gif)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:00:11.040116Z","iopub.status.idle":"2021-09-25T23:00:11.0405Z","shell.execute_reply.started":"2021-09-25T23:00:11.040326Z","shell.execute_reply":"2021-09-25T23:00:11.040345Z"}}},{"cell_type":"markdown","source":"### Example: BraTS21ID-00009\n\nFinally, we show second sample whose MGMT_value is 0.","metadata":{}},{"cell_type":"code","source":"id_ = 9\nfor mri_type in MRI_TYPES:\n    sub = tio.Image(PROCESSED_DATA_DIR.joinpath(f\"train/{str(id_).zfill(5)}/{mri_type}/{mri_type}.nii\"))\n    make_animation(sub, ANIMATION_OUT_DIR.joinpath(f\"sub_{id_}_{mri_type}.gif\"), skip_if_exists=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:02:37.719155Z","iopub.execute_input":"2021-09-25T23:02:37.719456Z","iopub.status.idle":"2021-09-25T23:02:57.245965Z","shell.execute_reply.started":"2021-09-25T23:02:37.719426Z","shell.execute_reply":"2021-09-25T23:02:57.245105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- FLAIR\n\n![](./animation/sub_9_FLAIR.gif)\n\n- T1w\n\n![](./animation/sub_9_T1w.gif)\n\n- T1wCE\n\n![](./animation/sub_9_T1wCE.gif)\n\n- T2w\n\n![](./animation/sub_9_T2w.gif)","metadata":{}},{"cell_type":"markdown","source":"## Conclusion\n\nIn this notebook, we confirmed below;\n\n- Each sample is in same voxel space.\n- There are some differences between MGMT_value 1 and 0 after preprocessing.\n- In view of the above, the preprocess is working well!","metadata":{}}]}