{"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 os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport pydicom as dcm\nfrom ipywidgets import interact\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:06:42.583820Z","iopub.execute_input":"2021-08-17T13:06:42.584180Z","iopub.status.idle":"2021-08-17T13:06:42.953678Z","shell.execute_reply.started":"2021-08-17T13:06:42.584150Z","shell.execute_reply":"2021-08-17T13:06:42.952735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Research and About the Data\n\n**MRI**\n           \n    Magnetic resonance imaging (MRI) is a medical imaging technique that uses a magnetic field and computer-generated radio waves to create detailed images of the organs and tissues in your body.\n\n    Most MRI machines are large, tube-shaped magnets. When you lie inside an MRI machine, the magnetic field temporarily realigns water molecules in your body. Radio waves cause these aligned atoms to produce faint signals, which are used to create cross-sectional MRI images — like slices in a loaf of bread.\n\n    The MRI machine can also produce 3D images that can be viewed from different angles.\n    \n    It is one of the most commonly used tests in neurology and neurosurgery. MRI provides exquisite detail of brain, spinal cord and vascular anatomy, and has the advantage of being able to visualize anatomy in all three planes: axial, sagittal and coronal. \n\n![](https://case.edu/med/neurology/NR/mri%20slices%20new.jpg)\n\n**mpMRI**\n\n    A multi-parametric magnetic resonance imaging (mpMRI) scan is a special type of scan that creates more detailed pictures of your prostate than a standard MRI scan. It does this by combining four different types of image. These images give your doctor information about whether or not there is any cancer inside your prostate.\n\n    During the mpMRI scan, you will be injected with a Gadolinium-based contrast agent which is an essential part of this type of imaging. It allows for a clearer picture of the prostate. The gadolinium (a metal ion) in these dynamic contrast agents has been chemically adapted to make it safe to use as part of a mpMRI scan. Part of the quality control for mpMRI involves using the lowest possible effective dose of the contrast agent. There is not yet any clinical evidence that gadolinium causes any harm when used as a contrast agent for mpMRI, however, we will continue to monitor the situation carefully. The contrast agent will not be administered if it's clinically contraindicated, for example, when dealing with kidney problems.\n    \nVisit this site for further knowledge: https://case.edu/med/neurology/NR/MRI%20Basics.htm | https://www.youtube.com/watch?v=rumRGO_2H0E","metadata":{}},{"cell_type":"markdown","source":"## DATA\n\nThe Images provided are mpMRI images, which is, Multi-Parametric Magnetic Resonance Imaging. Each mpMRI given has the following format:\n    \n    > 0000\n    |\n    |-- FLAIR -Images\n    |--T1w - Images\n    |--T1wCE - Post Contrast - Images\n    |--T2w - Images\n    \n**MRI IMAGING SEQUENCES**\n\nRepetition Time (TR) is the amount of time between successive pulse sequences applied to the same slice. Time to Echo (TE) is the time between the delivery of the RF pulse and the receipt of the echo signal. \n\nThe most common MRI sequences are T1-weighted and T2-weighted scans. **T1-weighted images are produced by using short TE and TR times. The contrast and brightness of the image are predominately determined by T1 properties of tissue.** Conversely, **T2-weighted images are produced by using longer TE and TR times. In these images, the contrast and brightness are predominately determined by the T2 properties of tissue.**\n\n*In general, T1- and T2-weighted images can be easily differentiated by looking the CSF. CSF is dark on T1-weighted imaging and bright on T2-weighted imaging.*\n\nA third commonly used sequence is the **Fluid Attenuated Inversion Recovery (Flair)**. The Flair sequence is similar to a T2-weighted image except that the TE and TR times are very long. **By doing so, abnormalities remain bright but normal CSF fluid is attenuated and made dark. This sequence is very sensitive to pathology and makes the differentiation between CSF and an abnormality much easier.**\n\n**T1-weighted imaging can also be performed while infusing Gadolinium (Gad)**. Gad is a non-toxic paramagnetic contrast enhancement agent. When injected during the scan, Gad changes signal intensities by shortening T1. Thus, Gad is very bright on T1-weighted images. Gad enhanced images are especially useful in looking at vascular structures and breakdown in the blood-brain barrier.","metadata":{}},{"cell_type":"code","source":"flairs = t1ws = t2ws = t1gds = 0\nstudy = {}\nfor p in os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'):\n    for i in os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/' + p):\n        study[p] = {}\n        if i == 'FLAIR':\n            flairs = len(os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/' + p + '/' + i))\n        elif i == 'T1w':\n            t1ws = len(os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/' + p + '/' + i))\n        elif i == 'T2w':\n            t2ws = len(os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/' + p + '/' + i))\n        elif i == 'T1wCE':\n            t1gds = len(os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/' + p + '/' + i))\n        study[p]['FLAIR'] = flairs;study[p]['T1w'] = t1ws;study[p]['T2w'] = t2ws;study[p]['T1wCE'] = t1gds\nprint(f'Total of {len(os.listdir(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train\"))} patients')\nprint('Study Directory Created')","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:05:18.003097Z","iopub.execute_input":"2021-08-17T13:05:18.003361Z","iopub.status.idle":"2021-08-17T13:06:00.874802Z","shell.execute_reply.started":"2021-08-17T13:05:18.003335Z","shell.execute_reply":"2021-08-17T13:06:00.873718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study = pd.DataFrame(study).transpose()\nplt.figure(figsize=(30,5))\ntmp = study.sort_values('FLAIR',ascending=False)['FLAIR'][:10];plt.subplot(141);sns.barplot(x=tmp.index,y=tmp);plt.title('FLAIR TOP 10')\ntmp = study.sort_values('T1w',ascending=False)['T1w'][:10];plt.subplot(142);sns.barplot(x=tmp.index,y=tmp);plt.title('T1w TOP 10')\ntmp = study.sort_values('T2w',ascending=False)['T2w'][:10];plt.subplot(143);sns.barplot(x=tmp.index,y=tmp);plt.title('T2w TOP 10')\ntmp = study.sort_values('T1wCE',ascending=False)['T1wCE'][:10];plt.subplot(144);sns.barplot(x=tmp.index,y=tmp);plt.title('T1wCE TOP 10')\nplt.show()\nplt.figure(figsize=(20,5))\ntmp = ['FLAIR','T1w','T2w','T1wCE']\ntmp2 = []\nfor col in tmp:\n    tmp2.append(sum(study[col]))\nsns.barplot(x=tmp,y=tmp2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:06:00.878499Z","iopub.execute_input":"2021-08-17T13:06:00.878824Z","iopub.status.idle":"2021-08-17T13:06:01.931743Z","shell.execute_reply.started":"2021-08-17T13:06:00.878794Z","shell.execute_reply":"2021-08-17T13:06:01.930642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"On Examining the Patient dictionary I created, I found out that the different studies had different number of flair's, t1w's, t2w's and t1wce's. This was the case since the images were protocoled for a human to study and not for a computer.","metadata":{}},{"cell_type":"markdown","source":"# Analysis and Convenience ","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ndf.columns = ['id','mgmt']\ndf['id'] = df['id'].apply(lambda x : str(x).zfill(5))\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:13:57.181678Z","iopub.execute_input":"2021-08-17T13:13:57.182042Z","iopub.status.idle":"2021-08-17T13:13:57.200646Z","shell.execute_reply.started":"2021-08-17T13:13:57.182011Z","shell.execute_reply":"2021-08-17T13:13:57.199700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:14:06.168938Z","iopub.execute_input":"2021-08-17T13:14:06.169596Z","iopub.status.idle":"2021-08-17T13:14:06.182087Z","shell.execute_reply.started":"2021-08-17T13:14:06.169559Z","shell.execute_reply":"2021-08-17T13:14:06.180508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"BraTS2ID is the Study Number (0 refers to study 00000, 2 refers to 00002, etc.)\nMGMT_value is MGMT promoter methylation status\n\nSuch radiogenomic models could improve the efficiency and accuracy of diagnosis, prognosis and treatment planning for patients with glioblastoma.","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=df['mgmt']) # Fairly equally distributed\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:14:08.089983Z","iopub.execute_input":"2021-08-17T13:14:08.090513Z","iopub.status.idle":"2021-08-17T13:14:08.211477Z","shell.execute_reply.started":"2021-08-17T13:14:08.090452Z","shell.execute_reply":"2021-08-17T13:14:08.210700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{}},{"cell_type":"markdown","source":"The Images are in the DICOM Format, such that,\n\n![](https://www.researchgate.net/publication/341136795/figure/fig1/AS:887639720865792@1588641209109/A-stack-of-MRI-2D-images-a-The-resolution-in-the-through-plane-direction-coronal-and.jpg)\n\nEach image in a study belonging to a particular mriType, when stacked upon each other will give us the 3d model of the brain is all 3 axises. Some slices are missing since, these images were initially expected to be used solely by humans.","metadata":{}},{"cell_type":"code","source":"# Paths -- DON'T CHANGE -- \ntrain_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'\ntest_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test'\n\n# Key for Sorting\ndef crit(x):\n    return int(x.split('-')[1].split('.')[0])\n\n# Reading DICOM Files\ndef imread(path):\n    return dcm.dcmread(path).pixel_array\n\n# Showing DICOM Files\ndef imshow(arr):\n    plt.imshow(arr,cmap='gray')\n    plt.axis('off')\n\n# Showing the Entire 3D Model\ndef get_3d(idx,mriType,train=True):\n    path = os.path.join(train_path,idx,mriType) if train else os.path.join(test_path,idx,mriType)\n    final = []\n    imgs = os.listdir(path)\n    imgs.sort(key=crit)\n    for img in imgs:\n        final.append(dcm.dcmread(os.path.join(path,img)).pixel_array)\n    return np.transpose(np.array(final),(1,2,0)),len(final)\n\n# Add all the MRI's to get a 512 x 512 Image\ndef get_added(idx,mriType,train=True):\n    img,nlayers =  get_3d(idx,mriType,train)\n    return np.mean(np.transpose(img,(2,0,1)),axis=0)\n\n# Returns the Image of the most central layer (the mean of the central layer will be the largest)\ndef get_max(idx,mriType,train=True):\n    path = os.path.join(train_path,idx,mriType) if train else os.path.join(test_path,idx,mriType)\n    imgs = os.listdir(path)\n    max_layer = imread(os.path.join(path,imgs[0]))\n    max_val = np.mean(max_layer)\n    for img in imgs:\n        tmp = np.mean(imread(os.path.join(path,img)))\n        if tmp > max_val:\n            max_val = tmp\n            max_layer = imread(os.path.join(path,img))\n    return max_layer\n\n# To get the label for a Study\ndef get_mgmt(idx):\n    return df.loc[idx][0]","metadata":{"execution":{"iopub.status.busy":"2021-08-17T14:25:55.501499Z","iopub.execute_input":"2021-08-17T14:25:55.501891Z","iopub.status.idle":"2021-08-17T14:25:55.517157Z","shell.execute_reply.started":"2021-08-17T14:25:55.501859Z","shell.execute_reply":"2021-08-17T14:25:55.516260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualising a 3D Model","metadata":{}},{"cell_type":"code","source":"arr3d,nlayers = get_3d('00000','FLAIR')\ndef explore_3d(layer):\n    imshow(arr3d[:,:,layer])\n    plt.title(str(layer))\n    plt.show()\n    return layer\n\ninteract(explore_3d,layer=(1,nlayers-1))","metadata":{"execution":{"iopub.status.busy":"2021-08-17T14:25:57.264713Z","iopub.execute_input":"2021-08-17T14:25:57.265109Z","iopub.status.idle":"2021-08-17T14:25:58.421549Z","shell.execute_reply.started":"2021-08-17T14:25:57.265077Z","shell.execute_reply":"2021-08-17T14:25:58.420697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.subplot(141);imshow(get_added('00000','FLAIR'));plt.title('FLAIR')\nplt.subplot(142);imshow(get_added('00000','T1w'));plt.title('T1w')\nplt.subplot(143);imshow(get_added('00000','T2w'));plt.title('T2w')\nplt.subplot(144);imshow(get_added('00000','T1wCE'));plt.title('T1wCE')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T14:25:58.423311Z","iopub.execute_input":"2021-08-17T14:25:58.423990Z","iopub.status.idle":"2021-08-17T14:26:01.461550Z","shell.execute_reply.started":"2021-08-17T14:25:58.423944Z","shell.execute_reply":"2021-08-17T14:26:01.460406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.subplot(141);imshow(get_max('00561','FLAIR'));plt.title('FLAIR')\nplt.subplot(142);imshow(get_max('00561','T1w'));plt.title('T1w')\nplt.subplot(143);imshow(get_max('00561','T2w'));plt.title('T2w')\nplt.subplot(144);imshow(get_max('00561','T1wCE'));plt.title('T1wCE')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-17T14:26:01.463407Z","iopub.execute_input":"2021-08-17T14:26:01.463751Z","iopub.status.idle":"2021-08-17T14:26:03.737577Z","shell.execute_reply.started":"2021-08-17T14:26:01.463718Z","shell.execute_reply":"2021-08-17T14:26:03.736408Z"},"trusted":true},"execution_count":null,"outputs":[]}]}