{"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 : A simple EDA (work in progress, more to come soon)\n\nIn this competition, we are provided with MRI images of glioblastoma (malignant brain tumor) patients and asked to predict MGMT methylation, a genetic subtype that is a strong predictor of a patient's responsiveness to chemotherapy. \n\nCurrently, in order to determine the genetic status of the tumor, a biopsy must be obtained during surgery, and it may take several weeks to genetically characterize the specimen. In this competition, we are directly predicting the genetic status from MRI images, which could limit the amount of surgeries required and also lead to accelerated treatment and better selection of chemotherapeutics. It will lead to better management of patients with brain cancer.\n\nThere is a paper [here](https://arxiv.org/abs/2107.02314) with more details about this competition.\n\nI'll provide a quick and simple EDA to help you get started with this very interesting competition!","metadata":{}},{"cell_type":"markdown","source":"# Imports\n\nLet's start out by setting up our environment by importing the required modules:","metadata":{}},{"cell_type":"code","source":"! conda install -c conda-forge gdcm -y","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:33:15.039566Z","iopub.execute_input":"2021-07-14T00:33:15.040015Z","iopub.status.idle":"2021-07-14T00:34:25.328608Z","shell.execute_reply.started":"2021-07-14T00:33:15.039916Z","shell.execute_reply":"2021-07-14T00:34:25.327453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 os\nimport pydicom\nimport glob\nfrom tqdm.notebook import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\nfrom skimage import exposure\nimport cv2\nimport warnings\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:34:25.330397Z","iopub.execute_input":"2021-07-14T00:34:25.330713Z","iopub.status.idle":"2021-07-14T00:34:28.946838Z","shell.execute_reply.started":"2021-07-14T00:34:25.33067Z","shell.execute_reply":"2021-07-14T00:34:28.945677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A look at the provided data\n\nLet's check what data is available to us:","metadata":{}},{"cell_type":"code","source":"dataset_path = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:34:48.977962Z","iopub.execute_input":"2021-07-14T00:34:48.978387Z","iopub.status.idle":"2021-07-14T00:34:48.98329Z","shell.execute_reply.started":"2021-07-14T00:34:48.978352Z","shell.execute_reply":"2021-07-14T00:34:48.981881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_path.ls()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:34:54.382781Z","iopub.execute_input":"2021-07-14T00:34:54.383381Z","iopub.status.idle":"2021-07-14T00:34:54.394403Z","shell.execute_reply.started":"2021-07-14T00:34:54.383345Z","shell.execute_reply":"2021-07-14T00:34:54.393566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(dataset_path/'train_labels.csv')\nprint(f'There are {len(train_df)} patients in the dataset')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:39:14.516035Z","iopub.execute_input":"2021-07-14T00:39:14.516521Z","iopub.status.idle":"2021-07-14T00:39:14.52848Z","shell.execute_reply.started":"2021-07-14T00:39:14.516486Z","shell.execute_reply":"2021-07-14T00:39:14.527575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:39:22.188831Z","iopub.execute_input":"2021-07-14T00:39:22.189258Z","iopub.status.idle":"2021-07-14T00:39:22.218707Z","shell.execute_reply.started":"2021-07-14T00:39:22.189217Z","shell.execute_reply":"2021-07-14T00:39:22.21783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see we have:\n\n* train_labels.csv - file containing the target `MGMT_value` for each of the 585 patients in the training data\n* sample_submission.csv - a sample submission file for us to predict `MGMT_value`.\n* train folder - comprises of MRI scans for 585 patients in DICOM format.\n* test folder - The hidden test dataset is >5x the size of the training set.","metadata":{}},{"cell_type":"code","source":"train_df['MGMT_value'].hist()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:40:08.607742Z","iopub.execute_input":"2021-07-14T00:40:08.608201Z","iopub.status.idle":"2021-07-14T00:40:08.800299Z","shell.execute_reply.started":"2021-07-14T00:40:08.608148Z","shell.execute_reply":"2021-07-14T00:40:08.799227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The distribution of the MGMT methylation status is fairly balanced.","metadata":{}},{"cell_type":"markdown","source":"# Dataset organization and MRI images\n\nThis competition dataset provides four types of MRI images:\n\n1. [Fluid Attenuated Inversion Recovery (FLAIR)](https://en.wikipedia.org/wiki/Fluid-attenuated_inversion_recovery)\n2. [T1-weighted pre-contrast (T1w)](https://radiopaedia.org/articles/t1-weighted-image?lang=us)\n3. [T1-weighted post-contrast (T1Gd)](https://radiopaedia.org/articles/t1-weighted-image?lang=us)\n4. [T2-weighted (T2)](https://radiopaedia.org/articles/t2-weighted-image?lang=us)\n\nLet's look at some example images:","metadata":{}},{"cell_type":"code","source":"def dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n        \n    \ndef plot_img(img, size=(7, 7), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n\n\ndef plot_imgs(imgs, cols=4, size=7, is_rgb=True, title=\"\", cmap='gray', img_size=(500,500)):\n    rows = len(imgs)//cols + 1\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None:\n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:47:51.728456Z","iopub.execute_input":"2021-07-14T00:47:51.729038Z","iopub.status.idle":"2021-07-14T00:47:51.743194Z","shell.execute_reply.started":"2021-07-14T00:47:51.728995Z","shell.execute_reply":"2021-07-14T00:47:51.741613Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_paths = [i.ls()[0] for i in (dataset_path/'train'/'00688').ls()]\nimgs = [dicom2array(path) for path in dicom_paths]\nplot_imgs(imgs)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T00:58:50.126544Z","iopub.execute_input":"2021-07-14T00:58:50.126939Z","iopub.status.idle":"2021-07-14T00:58:50.793907Z","shell.execute_reply.started":"2021-07-14T00:58:50.126907Z","shell.execute_reply":"2021-07-14T00:58:50.792999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I hope this is helpful (more to come soon), please upvote if you enjoyed this kernel! :)","metadata":{}}]}