{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np \nimport pandas as pd \n\nimport os\nfrom pathlib import Path\nfrom glob import glob\nimport nibabel as nb\nfrom tqdm import tqdm\nimport tarfile\nfrom matplotlib import pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-31T11:08:56.552356Z","iopub.execute_input":"2021-08-31T11:08:56.552855Z","iopub.status.idle":"2021-08-31T11:08:56.559543Z","shell.execute_reply.started":"2021-08-31T11:08:56.552814Z","shell.execute_reply":"2021-08-31T11:08:56.558264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tar = tarfile.open('../input/tumorbrainnifty/input.tar')\ntar.extractall()\ntar.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-31T10:56:41.872543Z","iopub.execute_input":"2021-08-31T10:56:41.872944Z","iopub.status.idle":"2021-08-31T11:00:34.317139Z","shell.execute_reply.started":"2021-08-31T10:56:41.872905Z","shell.execute_reply":"2021-08-31T11:00:34.315632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tar = tarfile.open('../input/brats-2021-task1/BraTS2021_Training_Data.tar')\nout_dir = Path('/tmp/brats-task1')\nout_dir.mkdir(exist_ok=True)\ntar.extractall(out_dir)\ntar.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-31T10:52:10.086501Z","iopub.execute_input":"2021-08-31T10:52:10.086930Z","iopub.status.idle":"2021-08-31T10:55:16.836961Z","shell.execute_reply.started":"2021-08-31T10:52:10.086891Z","shell.execute_reply":"2021-08-31T10:55:16.835812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task1_files = glob('/tmp/brats-task1/*')\ntask2_files = glob('./nifti/train/*')\n\n# task2_files = [file.split('/')[-1] for file in task2_files]\n# task1_files = [file.split('_')[-1] for file in task1_files]","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:04:47.025707Z","iopub.execute_input":"2021-08-31T11:04:47.026311Z","iopub.status.idle":"2021-08-31T11:04:47.040321Z","shell.execute_reply.started":"2021-08-31T11:04:47.026266Z","shell.execute_reply":"2021-08-31T11:04:47.039258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(task2_files), len(task1_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:05.566136Z","iopub.execute_input":"2021-08-31T11:02:05.566649Z","iopub.status.idle":"2021-08-31T11:02:05.573828Z","shell.execute_reply.started":"2021-08-31T11:02:05.566614Z","shell.execute_reply":"2021-08-31T11:02:05.572658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task2_files[:3], task1_files[:3]","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:06.602904Z","iopub.execute_input":"2021-08-31T11:02:06.603318Z","iopub.status.idle":"2021-08-31T11:02:06.609685Z","shell.execute_reply.started":"2021-08-31T11:02:06.603277Z","shell.execute_reply":"2021-08-31T11:02:06.608651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(task2_files)), len(set(task1_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:08.055370Z","iopub.execute_input":"2021-08-31T11:02:08.055773Z","iopub.status.idle":"2021-08-31T11:02:08.064412Z","shell.execute_reply.started":"2021-08-31T11:02:08.055715Z","shell.execute_reply":"2021-08-31T11:02:08.062880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(task1_files) & set(task2_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:09.114923Z","iopub.execute_input":"2021-08-31T11:02:09.115322Z","iopub.status.idle":"2021-08-31T11:02:09.123439Z","shell.execute_reply.started":"2021-08-31T11:02:09.115288Z","shell.execute_reply":"2021-08-31T11:02:09.122111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(task2_files) - set(task1_files) & set(task2_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:10.660150Z","iopub.execute_input":"2021-08-31T11:02:10.660596Z","iopub.status.idle":"2021-08-31T11:02:10.668970Z","shell.execute_reply.started":"2021-08-31T11:02:10.660561Z","shell.execute_reply":"2021-08-31T11:02:10.667548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shapes = set()\nfor file in tqdm(glob('./nifti/train/*')):\n    for nii in glob(f'{file}/*'):\n        nb_file = nb.load(nii)\n        shapes.add(nb_file.shape)\nprint(shapes)","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:02:37.600956Z","iopub.execute_input":"2021-08-31T11:02:37.601488Z","iopub.status.idle":"2021-08-31T11:02:42.443436Z","shell.execute_reply.started":"2021-08-31T11:02:37.601452Z","shell.execute_reply":"2021-08-31T11:02:42.442370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task1_study = [file for file in task1_files if '00000' in file][0]\ntask2_study = [file for file in task2_files if '00000' in file][0]\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:06:49.048099Z","iopub.execute_input":"2021-08-31T11:06:49.048466Z","iopub.status.idle":"2021-08-31T11:06:49.053957Z","shell.execute_reply.started":"2021-08-31T11:06:49.048425Z","shell.execute_reply":"2021-08-31T11:06:49.052813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task1_study_files = glob(f'{task1_study}/*')\nfiles_n = len(task1_study_files)\n\nfig = plt.figure(figsize=(18, 18))\nfor i, file in enumerate(task1_study_files, 1):\n    nb_file = nb.load(file)\n    print(file, nb_file.shape)\n    ax = fig.add_subplot(1, files_n, i)\n    ax.imshow(nb_file.get_fdata()[:,:,77])","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:14:12.610883Z","iopub.execute_input":"2021-08-31T11:14:12.611320Z","iopub.status.idle":"2021-08-31T11:14:13.769755Z","shell.execute_reply.started":"2021-08-31T11:14:12.611285Z","shell.execute_reply":"2021-08-31T11:14:13.768647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task1_study_files = glob(f'{task2_study}/*')\nfiles_n = len(task1_study_files)\n\nfig = plt.figure(figsize=(18, 18))\nfor i, file in enumerate(task1_study_files, 1):\n    nb_file = nb.load(file)\n    print(file, nb_file.shape)\n    ax = fig.add_subplot(1, files_n, i)\n    ax.imshow(nb_file.get_fdata()[:,:,77])","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:14:18.385290Z","iopub.execute_input":"2021-08-31T11:14:18.385664Z","iopub.status.idle":"2021-08-31T11:14:19.850364Z","shell.execute_reply.started":"2021-08-31T11:14:18.385632Z","shell.execute_reply":"2021-08-31T11:14:19.849212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segm_mask = nb.load('/tmp/brats-task1/BraTS2021_00000/BraTS2021_00000_seg.nii.gz').get_fdata()\ntask2_volume = nb.load('./nifti/train/00000/T1CE_to_SRI.nii.gz').get_fdata()\n\ntask2_volume[segm_mask>0] = 0\nplt.imshow(task2_volume[:,:, 77])","metadata":{"execution":{"iopub.status.busy":"2021-08-31T11:19:31.713395Z","iopub.execute_input":"2021-08-31T11:19:31.714126Z","iopub.status.idle":"2021-08-31T11:19:32.227826Z","shell.execute_reply.started":"2021-08-31T11:19:31.714063Z","shell.execute_reply":"2021-08-31T11:19:32.226446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}