{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport os\nimport matplotlib.pyplot as plt\nimport cv2\nimport seaborn as sns\nimport glob\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-09T23:57:31.061958Z","iopub.execute_input":"2021-08-09T23:57:31.062331Z","iopub.status.idle":"2021-08-09T23:57:31.068021Z","shell.execute_reply.started":"2021-08-09T23:57:31.062298Z","shell.execute_reply":"2021-08-09T23:57:31.067088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/gdcm-conda-install/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:15:11.805389Z","iopub.execute_input":"2021-08-09T21:15:11.805774Z","iopub.status.idle":"2021-08-09T21:15:40.916206Z","shell.execute_reply.started":"2021-08-09T21:15:11.805737Z","shell.execute_reply":"2021-08-09T21:15:40.914961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_input_path = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification\"","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:15:40.918008Z","iopub.execute_input":"2021-08-09T21:15:40.918301Z","iopub.status.idle":"2021-08-09T21:15:40.922628Z","shell.execute_reply.started":"2021-08-09T21:15:40.918269Z","shell.execute_reply":"2021-08-09T21:15:40.921583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(os.path.join(data_input_path, \"train_labels.csv\"))","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:15:40.924349Z","iopub.execute_input":"2021-08-09T21:15:40.924655Z","iopub.status.idle":"2021-08-09T21:15:40.952325Z","shell.execute_reply.started":"2021-08-09T21:15:40.924626Z","shell.execute_reply":"2021-08-09T21:15:40.951286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:17:21.720265Z","iopub.execute_input":"2021-08-09T21:17:21.720831Z","iopub.status.idle":"2021-08-09T21:17:21.740673Z","shell.execute_reply.started":"2021-08-09T21:17:21.72078Z","shell.execute_reply":"2021-08-09T21:17:21.739552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"The total patient ids are {df_train['BraTS21ID'].count()}, from those the unique ids are {df_train['BraTS21ID'].value_counts().shape[0]} \")","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:18:12.310595Z","iopub.execute_input":"2021-08-09T21:18:12.310964Z","iopub.status.idle":"2021-08-09T21:18:12.317544Z","shell.execute_reply.started":"2021-08-09T21:18:12.310933Z","shell.execute_reply":"2021-08-09T21:18:12.316607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Distribution","metadata":{}},{"cell_type":"code","source":"sns.set()\n\nplt.figure(figsize = (5,5))\n# Count the number of images per category\nsns.countplot(x = 'MGMT_value', color = '#169DE3',data = df_train)\n\nplt.title('Categories Distribution'.title(),size=22 , color = '#169DE3')\nplt.xlabel('MGMT_value',size=17 , color = '#169DE3')\nplt.ylabel('Count',size=17 , color = '#169DE3')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:19:04.779036Z","iopub.execute_input":"2021-08-09T21:19:04.779569Z","iopub.status.idle":"2021-08-09T21:19:04.925002Z","shell.execute_reply.started":"2021-08-09T21:19:04.779535Z","shell.execute_reply":"2021-08-09T21:19:04.924267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Vis","metadata":{}},{"cell_type":"code","source":"import pydicom","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:19:25.068069Z","iopub.execute_input":"2021-08-09T21:19:25.068436Z","iopub.status.idle":"2021-08-09T21:19:25.548839Z","shell.execute_reply.started":"2021-08-09T21:19:25.068405Z","shell.execute_reply":"2021-08-09T21:19:25.547872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Images","metadata":{}},{"cell_type":"code","source":"#This is from: https://www.kaggle.com/ihelon/brain-tumor-eda-with-animations-and-modeling\ndef 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\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        os.path.join(data_input_path, \"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-09T21:19:31.028345Z","iopub.execute_input":"2021-08-09T21:19:31.028725Z","iopub.status.idle":"2021-08-09T21:19:31.03934Z","shell.execute_reply.started":"2021-08-09T21:19:31.028691Z","shell.execute_reply":"2021-08-09T21:19:31.038468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(range(df_train.shape[0]), 5):\n    _brats21id = df_train.iloc[i][\"BraTS21ID\"]\n    _mgmt_value = df_train.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:57:42.435732Z","iopub.execute_input":"2021-08-09T23:57:42.436247Z","iopub.status.idle":"2021-08-09T23:57:45.295839Z","shell.execute_reply.started":"2021-08-09T23:57:42.436213Z","shell.execute_reply":"2021-08-09T23:57:45.294721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Animations","metadata":{}},{"cell_type":"code","source":"##This is from: https://www.kaggle.com/ihelon/brain-tumor-eda-with-animations-and-modeling\nfrom 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)\n\ndef 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-09T23:57:47.839759Z","iopub.execute_input":"2021-08-09T23:57:47.840150Z","iopub.status.idle":"2021-08-09T23:57:47.849867Z","shell.execute_reply.started":"2021-08-09T23:57:47.840116Z","shell.execute_reply":"2021-08-09T23:57:47.848710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(os.path.join(data_input_path,\"train/00000/FLAIR\"))\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:57:49.156167Z","iopub.execute_input":"2021-08-09T23:57:49.156538Z","iopub.status.idle":"2021-08-09T23:58:17.447191Z","shell.execute_reply.started":"2021-08-09T23:57:49.156510Z","shell.execute_reply":"2021-08-09T23:58:17.446324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(os.path.join(data_input_path,\"train/00000/T1w\"))\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:58:17.448531Z","iopub.execute_input":"2021-08-09T23:58:17.448807Z","iopub.status.idle":"2021-08-09T23:58:20.516265Z","shell.execute_reply.started":"2021-08-09T23:58:17.448781Z","shell.execute_reply":"2021-08-09T23:58:20.515196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(os.path.join(data_input_path,\"train/00000/T1wCE\"))\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:58:20.518308Z","iopub.execute_input":"2021-08-09T23:58:20.518597Z","iopub.status.idle":"2021-08-09T23:58:29.112023Z","shell.execute_reply.started":"2021-08-09T23:58:20.518570Z","shell.execute_reply":"2021-08-09T23:58:29.111080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_line(os.path.join(data_input_path,\"train/00000/T2w\"))\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:58:29.113245Z","iopub.execute_input":"2021-08-09T23:58:29.113519Z","iopub.status.idle":"2021-08-09T23:58:56.663806Z","shell.execute_reply.started":"2021-08-09T23:58:29.113493Z","shell.execute_reply":"2021-08-09T23:58:56.662839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert train data to PNG data","metadata":{}},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:29:29.537202Z","iopub.execute_input":"2021-08-09T21:29:29.537689Z","iopub.status.idle":"2021-08-09T21:29:29.542346Z","shell.execute_reply.started":"2021-08-09T21:29:29.537649Z","shell.execute_reply":"2021-08-09T21:29:29.541306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:29:31.191397Z","iopub.execute_input":"2021-08-09T21:29:31.191776Z","iopub.status.idle":"2021-08-09T21:29:31.197634Z","shell.execute_reply.started":"2021-08-09T21:29:31.19174Z","shell.execute_reply":"2021-08-09T21:29:31.196551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_dcm_as_png(source, dest, mode = \"train\", size = 512):\n    image = load_dicom(source)\n\n    #orig_shapes[mode].append((image.shape[1], image.shape[0]))\n    \n    image = resize(image, size)\n    image.save(dest)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:29:36.642363Z","iopub.execute_input":"2021-08-09T21:29:36.642738Z","iopub.status.idle":"2021-08-09T21:29:36.64766Z","shell.execute_reply.started":"2021-08-09T21:29:36.642704Z","shell.execute_reply":"2021-08-09T21:29:36.646735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_png(mode = \"train\", size = 512):\n    root_path = os.path.join(data_input_path, mode)\n    \n    for patient_folder in os.listdir(root_path):\n        patient_folder_path = os.path.join(root_path, patient_folder)\n        \n        for imagery_type in os.listdir(patient_folder_path):\n            imagery_type_path = os.path.join(patient_folder_path, imagery_type)\n            output_path = \"{}/{}/{}\".format(mode, imagery_type, patient_folder)\n            os.makedirs(output_path, exist_ok = True)\n            \n            for image in os.listdir(imagery_type_path):\n                image_path = os.path.join(imagery_type_path, image)\n                save_dcm_as_png(image_path,\n                               os.path.join(output_path, \n                                            image_path.split(\"/\")[-1][:-3] + \"png\"),\n                               mode = mode)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:29:40.840388Z","iopub.execute_input":"2021-08-09T21:29:40.840751Z","iopub.status.idle":"2021-08-09T21:29:40.848809Z","shell.execute_reply.started":"2021-08-09T21:29:40.840716Z","shell.execute_reply":"2021-08-09T21:29:40.847855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convert_to_png()","metadata":{"execution":{"iopub.status.busy":"2021-08-09T21:29:44.760717Z","iopub.execute_input":"2021-08-09T21:29:44.761119Z","iopub.status.idle":"2021-08-09T23:17:19.438107Z","shell.execute_reply.started":"2021-08-09T21:29:44.761061Z","shell.execute_reply":"2021-08-09T23:17:19.436793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check\nmode = \"train\"\nroot_path = os.path.join(data_input_path, mode)\n\nfor patient_folder in os.listdir(root_path):\n    patient_folder_path = os.path.join(root_path, patient_folder)\n\n    for imagery_type in os.listdir(patient_folder_path):\n        imagery_type_path = os.path.join(patient_folder_path, imagery_type)\n        output_path = \"{}/{}/{}\".format(mode, imagery_type, patient_folder)\n\n        assert len(os.listdir(imagery_type_path)) == len(os.listdir(output_path))","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:17:19.452812Z","iopub.execute_input":"2021-08-09T23:17:19.453236Z","iopub.status.idle":"2021-08-09T23:18:06.941582Z","shell.execute_reply.started":"2021-08-09T23:17:19.453194Z","shell.execute_reply":"2021-08-09T23:18:06.940862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv ./","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:18:06.942811Z","iopub.execute_input":"2021-08-09T23:18:06.943199Z","iopub.status.idle":"2021-08-09T23:18:07.718328Z","shell.execute_reply.started":"2021-08-09T23:18:06.943172Z","shell.execute_reply":"2021-08-09T23:18:07.717338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r dataset_btrc.zip train train_labels.csv","metadata":{"execution":{"iopub.status.busy":"2021-08-09T23:18:15.21986Z","iopub.execute_input":"2021-08-09T23:18:15.220222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"dataset_btrc.zip\"> Download File </a>","metadata":{}}]}