{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nplt.style.use('seaborn-talk')\nimport torch\nimport glob\nimport random\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-11T11:54:00.938565Z","iopub.execute_input":"2021-08-11T11:54:00.938923Z","iopub.status.idle":"2021-08-11T11:54:00.943966Z","shell.execute_reply.started":"2021-08-11T11:54:00.938891Z","shell.execute_reply":"2021-08-11T11:54:00.943025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:00.949006Z","iopub.execute_input":"2021-08-11T11:54:00.949477Z","iopub.status.idle":"2021-08-11T11:54:00.979097Z","shell.execute_reply.started":"2021-08-11T11:54:00.949430Z","shell.execute_reply":"2021-08-11T11:54:00.977850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nsns.countplot(data=df, x=\"MGMT_value\");","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:00.981000Z","iopub.execute_input":"2021-08-11T11:54:00.981433Z","iopub.status.idle":"2021-08-11T11:54:01.100450Z","shell.execute_reply.started":"2021-08-11T11:54:00.981387Z","shell.execute_reply":"2021-08-11T11:54:01.099374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nfrom collections import defaultdict\nmri_types_dict = defaultdict(list)\nsamples = glob.glob('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/*')\n\nfor sample in samples:\n    for mri_t_dir in glob.glob(f'{sample}/*'):\n        mri_t = mri_t_dir.split('/')[-1]\n        mri_types_dict[mri_t].append(len(glob.glob(f'{mri_t_dir}/*')))\n\n            \n","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:01.102526Z","iopub.execute_input":"2021-08-11T11:54:01.102974Z","iopub.status.idle":"2021-08-11T11:54:03.498547Z","shell.execute_reply.started":"2021-08-11T11:54:01.102928Z","shell.execute_reply":"2021-08-11T11:54:03.497584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mri_types_df = pd.DataFrame(data=np.array(list(mri_types_dict.values())).T, columns=mri_types_dict.keys())\nmri_types_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:03.500198Z","iopub.execute_input":"2021-08-11T11:54:03.500594Z","iopub.status.idle":"2021-08-11T11:54:03.511395Z","shell.execute_reply.started":"2021-08-11T11:54:03.500550Z","shell.execute_reply":"2021-08-11T11:54:03.510379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mri_types_df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:03.512805Z","iopub.execute_input":"2021-08-11T11:54:03.513361Z","iopub.status.idle":"2021-08-11T11:54:03.542672Z","shell.execute_reply.started":"2021-08-11T11:54:03.513313Z","shell.execute_reply":"2021-08-11T11:54:03.541617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for mri_t in mri_types_df.columns:\n    print(mri_types_df[mri_t].plot.kde())\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:03.543964Z","iopub.execute_input":"2021-08-11T11:54:03.544576Z","iopub.status.idle":"2021-08-11T11:54:04.196462Z","shell.execute_reply.started":"2021-08-11T11:54:03.544527Z","shell.execute_reply":"2021-08-11T11:54:04.195320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from collections import defaultdict\n# import glob\n# from matplotlib import pyplot as plt\n# import pydicom\n# from collections import Counter\n\n# counters = []\n# for i in range(5):\n#     counter = Counter()\n    \n#     fold_df = df[df.fold==i]\n#     for idx, r in fold_df.iterrows():\n#         path = f\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{str(r['BraTS21ID']).zfill(5)}/\"        \n#         for mri_type_dir in glob.glob(f'{path}/*'):\n#             mri_name = mri_type_dir.split('/')[-1]\n#             for file in glob.glob(f'{mri_type_dir}/*'):\n#                 np_img = pydicom.read_file(file).pixel_array\n#                 counter[np_img.shape] += 1\n            \n#     counters.append(counter)\n\n\n# for counter in counters:\n#     keys = list(map(str, counter.keys()))\n#     values = np.array(list(counter.values())).astype('float')\n#     values -= values.min()\n#     values /= values.max()\n#     plt.bar(keys, values)\n#     plt.xticks(rotation=90)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:04.198107Z","iopub.execute_input":"2021-08-11T11:54:04.198550Z","iopub.status.idle":"2021-08-11T11:54:04.203132Z","shell.execute_reply.started":"2021-08-11T11:54:04.198504Z","shell.execute_reply":"2021-08-11T11:54:04.202099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n\ndef visualize_sample(\n    brats21id, \n    slice_i,\n    mgmt_value,\n    types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")\n):\n    plt.figure(figsize=(16, 5))\n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\", \n        str(brats21id).zfill(5),\n    )\n    for i, t in enumerate(types, 1):\n        t_paths = sorted(\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),\n        )\n        data = load_dicom(t_paths[int(len(t_paths) * slice_i)])\n        plt.subplot(1, 4, i)\n        plt.imshow(data, cmap=\"gray\")\n        plt.title(f\"{t}\", fontsize=16)\n        plt.axis(\"off\")\n\n    plt.suptitle(f\"MGMT_value: {mgmt_value}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:04.205865Z","iopub.execute_input":"2021-08-11T11:54:04.206295Z","iopub.status.idle":"2021-08-11T11:54:04.221381Z","shell.execute_reply.started":"2021-08-11T11:54:04.206252Z","shell.execute_reply":"2021-08-11T11:54:04.220414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:04.223005Z","iopub.execute_input":"2021-08-11T11:54:04.223290Z","iopub.status.idle":"2021-08-11T11:54:04.235206Z","shell.execute_reply.started":"2021-08-11T11:54:04.223262Z","shell.execute_reply":"2021-08-11T11:54:04.234248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from collections import Counter\n# counter = Counter()\n\n# for path in glob.glob(f'../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/*'):\n#         for mri_type_dir in glob.glob(f'{path}/*'):\n#             mri_name = mri_type_dir.split('/')[-1]\n#             for file in glob.glob(f'{mri_type_dir}/*'):\n#                 dicom = pydicom.read_file(file)\n#                 counter[dicom.PhotometricInterpretation] += 1\n#                 print(counter, end='\\r')","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:04.236832Z","iopub.execute_input":"2021-08-11T11:54:04.237258Z","iopub.status.idle":"2021-08-11T11:54:04.251036Z","shell.execute_reply.started":"2021-08-11T11:54:04.237211Z","shell.execute_reply":"2021-08-11T11:54:04.250012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(range(df.shape[0]), 10):\n    _brats21id = df.iloc[i][\"BraTS21ID\"]\n    _mgmt_value = df.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:04.252536Z","iopub.execute_input":"2021-08-11T11:54:04.252950Z","iopub.status.idle":"2021-08-11T11:54:07.994199Z","shell.execute_reply.started":"2021-08-11T11:54:04.252915Z","shell.execute_reply":"2021-08-11T11:54:07.993189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap=\"gray\")\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:07.995490Z","iopub.execute_input":"2021-08-11T11:54:07.995809Z","iopub.status.idle":"2021-08-11T11:54:08.001652Z","shell.execute_reply.started":"2021-08-11T11:54:07.995776Z","shell.execute_reply":"2021-08-11T11:54:08.000862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_line(path):\n    t_paths = sorted(\n        glob.glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    images = []\n    for filename in t_paths:\n        data = load_dicom(filename)\n        if data.max() == 0:\n            continue\n        images.append(data)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:08.002786Z","iopub.execute_input":"2021-08-11T11:54:08.003238Z","iopub.status.idle":"2021-08-11T11:54:08.015927Z","shell.execute_reply.started":"2021-08-11T11:54:08.003192Z","shell.execute_reply":"2021-08-11T11:54:08.015072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:54:08.017034Z","iopub.execute_input":"2021-08-11T11:54:08.017495Z","iopub.status.idle":"2021-08-11T11:54:28.902656Z","shell.execute_reply.started":"2021-08-11T11:54:08.017452Z","shell.execute_reply":"2021-08-11T11:54:28.901502Z"},"trusted":true},"execution_count":null,"outputs":[]}]}