{"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\n<a id = 0></a>\n\nA malignant tumor in the brain is a life-threatening condition. Known as glioblastoma, it's both the most common form of brain cancer in adults and the one with the worst prognosis, with median survival being less than a year. The presence of a specific genetic sequence in the tumor known as MGMT promoter methylation has been shown to be a favorable prognostic factor and a strong predictor of responsiveness to chemotherapy","metadata":{}},{"cell_type":"markdown","source":"![](https://images.pexels.com/photos/4226119/pexels-photo-4226119.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940)","metadata":{}},{"cell_type":"markdown","source":"#           [Contents](#1)\n* [Title](#0)\n* [Dependencies](#1) \n* [Introduction](#2)\n* [MGMT Label](#3)\n* [Visualizing the MRI](#4)\n* [Animations](#5)\n","metadata":{}},{"cell_type":"markdown","source":"# Dependencies\n<a id = 1></a>","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport pydicom # A library that loads dicom(dcm) files\nimport numpy as np\nimport os\nimport glob\nfrom IPython.display import Markdown # we will require this to print Markdown in the console\ninput_df = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\"\nsample = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\"\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-22T14:14:39.541906Z","iopub.execute_input":"2021-07-22T14:14:39.542257Z","iopub.status.idle":"2021-07-22T14:14:40.936849Z","shell.execute_reply.started":"2021-07-22T14:14:39.542225Z","shell.execute_reply":"2021-07-22T14:14:40.935435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install celluloid #The library celluloid is not available in the default kaggle enviroment so we have to pip install it","metadata":{"execution":{"iopub.status.busy":"2021-07-22T14:14:40.938960Z","iopub.execute_input":"2021-07-22T14:14:40.939324Z","iopub.status.idle":"2021-07-22T14:14:48.499417Z","shell.execute_reply.started":"2021-07-22T14:14:40.939286Z","shell.execute_reply":"2021-07-22T14:14:48.498262Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's start by checking the train csv and sample csv","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(input_df)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.066038Z","iopub.execute_input":"2021-07-22T13:05:07.066401Z","iopub.status.idle":"2021-07-22T13:05:07.117805Z","shell.execute_reply.started":"2021-07-22T13:05:07.066365Z","shell.execute_reply":"2021-07-22T13:05:07.116546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(sample)\nsample_df","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.120011Z","iopub.execute_input":"2021-07-22T13:05:07.120405Z","iopub.status.idle":"2021-07-22T13:05:07.1444Z","shell.execute_reply.started":"2021-07-22T13:05:07.120365Z","shell.execute_reply":"2021-07-22T13:05:07.143269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Introduction \n<a id = 2></a>","metadata":{}},{"cell_type":"markdown","source":" Having a First Glance at the two datasets. I can summarize that each row BraTS21ID corresponds to a patient and the MGMT_value column is the probability that the patient has the methylation promoter which is a cancer causing gene. 1 means a 100% probability that a person has the gene and 0 means that the probability that the person has the gene is 0 % . There is a test file and train file which have the coressponding BraTS21ID files each containing images in the Dicom format.\n \n \n \nOur job is to find the relations between the images in the train set and the likelihood that the person has the methylation promoter gene. Then use the same relation to predict using the images in the test set the probability of that paitent having the methylation of the MGMT promoter gene","metadata":{}},{"cell_type":"markdown","source":"# MGMT Label \n<a id = 3></a>","metadata":{}},{"cell_type":"code","source":"sns.countplot(data = train_df , x = \"MGMT_value\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.146104Z","iopub.execute_input":"2021-07-22T13:05:07.146475Z","iopub.status.idle":"2021-07-22T13:05:07.334497Z","shell.execute_reply.started":"2021-07-22T13:05:07.146426Z","shell.execute_reply":"2021-07-22T13:05:07.333376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The values of MGMT_value which are 1 and 0 are close. Which is great while making the model the model will not tend to make a predicton which has a greater likelihood of occuring.","metadata":{}},{"cell_type":"markdown","source":"# Visualizing the MRI\n<a id = 4></a>","metadata":{}},{"cell_type":"code","source":"def load_dicom(path):\n    dicom = pydicom.read_file(path) # pydicom is a library made to process dicom files\n    data = dicom.pixel_array # pixel_array shows the value of each pixel in the array\n    data = data - np.min(data) #helps in normalizing the image (not really required in this case)\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(ID,path,type_ = \"flair\"):\n    plt.figure(figsize = (16,5))\n    data = load_dicom(path)\n    plt.imshow(data)#\n    label = train_df[train_df['BraTS21ID'] == ID][\"MGMT_value\"].item()#this will assign the MGMT_value of the Patient to label\n    plt.title(str(ID)+\" \" + type_ + \" MGMT_value: \" +str(label))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.336084Z","iopub.execute_input":"2021-07-22T13:05:07.336449Z","iopub.status.idle":"2021-07-22T13:05:07.345959Z","shell.execute_reply.started":"2021-07-22T13:05:07.336402Z","shell.execute_reply":"2021-07-22T13:05:07.344806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We'll first check the MRI's of patient 0000 who as we can see on the graph has the MGMT promoter methalation","metadata":{}},{"cell_type":"code","source":"visualize_sample(0 , \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR/Image-250.dcm\", \"FLAIR\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.348425Z","iopub.execute_input":"2021-07-22T13:05:07.348806Z","iopub.status.idle":"2021-07-22T13:05:07.547865Z","shell.execute_reply.started":"2021-07-22T13:05:07.348773Z","shell.execute_reply":"2021-07-22T13:05:07.546744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"FLAIR MRI :- **Fl**uid **A**ttenuated **I**nversion **r**ecovery . It is a technique that shows tissue as white and cerebrospinal fluid as darkened parts of the brain","metadata":{}},{"cell_type":"code","source":"display(visualize_sample(0 , \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w/Image-15.dcm\", \"T1w\"))\ndisplay(visualize_sample(0 , \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T2w/Image-250.dcm\", \"T2w\"))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:07.549549Z","iopub.execute_input":"2021-07-22T13:05:07.549906Z","iopub.status.idle":"2021-07-22T13:05:07.876775Z","shell.execute_reply.started":"2021-07-22T13:05:07.549871Z","shell.execute_reply":"2021-07-22T13:05:07.875627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"T1w and T2w:- From my understanding T1w and t2w shows tissue matter of the brain. The difference between them is than T1w has a lower relaxation time (The model of the brain is made using fewer images)","metadata":{}},{"cell_type":"code","source":"visualize_sample(0 , \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE/Image-75.dcm\", \"T1wCE\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:13.09426Z","iopub.execute_input":"2021-07-22T13:05:13.094693Z","iopub.status.idle":"2021-07-22T13:05:13.265984Z","shell.execute_reply.started":"2021-07-22T13:05:13.094654Z","shell.execute_reply":"2021-07-22T13:05:13.264356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"These Images are great they were however made to be mapped as a 3d model of the brain. Now lets animate these images in the next section","metadata":{}},{"cell_type":"markdown","source":"# Animations\n<a id = 5></a>","metadata":{}},{"cell_type":"code","source":"from celluloid import Camera\nfrom IPython.display import HTML\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(16, 5))\n    plt.axis('off')\n    im = plt.imshow(ims[0])\n    camera = Camera(fig)\n\n    for i in range(len(ims)):\n        plt.imshow(ims[i])\n        camera.snap() #Each instance of the plot is saved\n    \n    animation = camera.animate() # And is then animated\n    display(animation)\n\n#Although I find celluloid much easier to use matplotlib's animation function Is much more intuitive to use\n# def create_animation(ims):\n#     fig = plt.figure(figsize=(6, 6))\n#     plt.axis('off')\n#     im = plt.imshow(ims[0])\n\n#     def animate_func(i):\n#         im.set_array(ims[i])\n#         return [im]\n\n#     return display(animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:18.547095Z","iopub.execute_input":"2021-07-22T13:05:18.547561Z","iopub.status.idle":"2021-07-22T13:05:18.556941Z","shell.execute_reply.started":"2021-07-22T13:05:18.547519Z","shell.execute_reply":"2021-07-22T13:05:18.555733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom_animation(path):\n    t_path = sorted(\n                    glob.glob(os.path.join(path,'*'))\n    )\n    images = []\n    for name in t_path:\n        data = load_dicom(name)\n        images.append(data)\n    return create_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:22.452629Z","iopub.execute_input":"2021-07-22T13:05:22.453206Z","iopub.status.idle":"2021-07-22T13:05:22.458879Z","shell.execute_reply.started":"2021-07-22T13:05:22.453166Z","shell.execute_reply":"2021-07-22T13:05:22.457656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"types = [\"FLAIR\", \"T1w\" , \"T1wCE\" , \"T2w\"]\ndef show_anim(Id):\n    display(Markdown(\"<div align = 'center'  style = 'color : lightblue'>\"+\"ID : \"+Id+\"</div>\"))\n    display(Markdown(\"<div align = 'center'  style = 'color :lightblue'>\"+\"MGMT methylation promoter : \"+str(train_df[train_df['BraTS21ID'] == int(Id)][\"MGMT_value\"].item())+\"</div>\"))\n    #print(\"\\tMGMT methylation promoter : \" ,train_df[train_df['BraTS21ID'] == int(Id)][\"MGMT_value\"].item())\n    path = os.path.join(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train\",Id)\n    for x in types:\n        display(Markdown(\"<div align = 'center' style = 'color : violet'>\"+\"Type of MRI : \"+x+\"</div>\"))\n        dicom_animation(os.path.join(path,x))\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:28.16678Z","iopub.execute_input":"2021-07-22T13:05:28.167194Z","iopub.status.idle":"2021-07-22T13:05:28.175516Z","shell.execute_reply.started":"2021-07-22T13:05:28.167162Z","shell.execute_reply":"2021-07-22T13:05:28.174212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_anim(\"00000\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:05:36.476633Z","iopub.execute_input":"2021-07-22T13:05:36.477019Z","iopub.status.idle":"2021-07-22T13:06:57.637507Z","shell.execute_reply.started":"2021-07-22T13:05:36.476983Z","shell.execute_reply":"2021-07-22T13:06:57.636157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now let's check the same with a patient who has the a MRNA promoter methalation ","metadata":{}},{"cell_type":"code","source":"show_anim(\"00003\")","metadata":{"execution":{"iopub.status.busy":"2021-07-22T13:06:57.639275Z","iopub.execute_input":"2021-07-22T13:06:57.639618Z","iopub.status.idle":"2021-07-22T13:07:56.403849Z","shell.execute_reply.started":"2021-07-22T13:06:57.639586Z","shell.execute_reply":"2021-07-22T13:07:56.402588Z"},"trusted":true},"execution_count":null,"outputs":[]}]}