{"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":"# Intro\nWelcome to the [RSNA-MICCAI Brain Tumor Radiogenomic Classificatio](https://www.kaggle.com/c/rsna-miccai-brain-tumor-radiogenomic-classification/data) compedition\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/29653/logos/header.png)\n\nThe exact mpMRI scans included are:\n\n* [Fluid Attenuated Inversion Recovery](https://en.wikipedia.org/wiki/Fluid-attenuated_inversion_recovery) (FLAIR)\n* T1-weighted pre-contrast (T1w)\n* T1-weighted post-contrast (T1Gd)\n* T2-weighted (T2)\n\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\nimport ast\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.198424Z","iopub.execute_input":"2021-07-14T06:13:59.199087Z","iopub.status.idle":"2021-07-14T06:13:59.203769Z","shell.execute_reply.started":"2021-07-14T06:13:59.199049Z","shell.execute_reply":"2021-07-14T06:13:59.202835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{}},{"cell_type":"code","source":"def plot_examples(row = 0, cat = 'FLAIR'):\n    \"\"\" Plot examples of a train sample (row) for a given category \"\"\"\n    \n    folder = str(train_data.loc[row, 'BraTS21ID']).zfill(5)\n    path_file = ''.join([path, 'train/', folder, '/', cat, '/'])\n    images = os.listdir(path_file)\n    \n    fig, axs = plt.subplots(1, 4, figsize=(20, 20))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    \n    for num in range(4):\n        data_file = dicom.dcmread(path_file+images[num])\n        img = data_file.pixel_array\n        axs[num].imshow(img, cmap='gray')\n        axs[num].set_title(cat+' '+images[num])\n        axs[num].set_xticklabels([])\n        axs[num].set_yticklabels([])","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:12:38.3225Z","iopub.execute_input":"2021-07-14T07:12:38.322878Z","iopub.status.idle":"2021-07-14T07:12:38.332064Z","shell.execute_reply.started":"2021-07-14T07:12:38.322847Z","shell.execute_reply":"2021-07-14T07:12:38.330947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.206115Z","iopub.execute_input":"2021-07-14T06:13:59.206559Z","iopub.status.idle":"2021-07-14T06:13:59.227781Z","shell.execute_reply.started":"2021-07-14T06:13:59.206514Z","shell.execute_reply":"2021-07-14T06:13:59.227017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train_labels.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.229664Z","iopub.execute_input":"2021-07-14T06:13:59.229999Z","iopub.status.idle":"2021-07-14T06:13:59.246564Z","shell.execute_reply.started":"2021-07-14T06:13:59.229967Z","shell.execute_reply":"2021-07-14T06:13:59.245411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Samples train:', len(train_data))\nprint('Samples test:', len(samp_subm))","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.248278Z","iopub.execute_input":"2021-07-14T06:13:59.248583Z","iopub.status.idle":"2021-07-14T06:13:59.254159Z","shell.execute_reply.started":"2021-07-14T06:13:59.248555Z","shell.execute_reply":"2021-07-14T06:13:59.253128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.255893Z","iopub.execute_input":"2021-07-14T06:13:59.256261Z","iopub.status.idle":"2021-07-14T06:13:59.273067Z","shell.execute_reply.started":"2021-07-14T06:13:59.256227Z","shell.execute_reply":"2021-07-14T06:13:59.272039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:13:59.274485Z","iopub.execute_input":"2021-07-14T06:13:59.274811Z","iopub.status.idle":"2021-07-14T06:13:59.287638Z","shell.execute_reply.started":"2021-07-14T06:13:59.274782Z","shell.execute_reply":"2021-07-14T06:13:59.286842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Dicom Files\nWe consider the first train sample.\n```\nTraining/Validation/Testing\n│\n└─── 00000\n│   │\n│   └─── FLAIR\n│   │   │ Image-1.dcm\n│   │   │ Image-2.dcm\n│   │   │ ...\n│   │   \n│   └─── T1w\n│   │   │ Image-1.dcm\n│   │   │ Image-2.dcm\n│   │   │ ...\n│   │   \n│   └─── T1wCE\n│   │   │ Image-1.dcm\n│   │   │ Image-2.dcm\n│   │   │ ...\n│   │   \n│   └─── T2w\n│   │   │ Image-1.dcm\n│   │   │ Image-2.dcm\n│   │   │ .....\n│   \n└─── 00001\n│   │ ...\n│   \n│ ...   \n│   \n└─── 00002\n│   │ ...\n```","metadata":{}},{"cell_type":"markdown","source":"Extract folder id of the first train sample:","metadata":{}},{"cell_type":"code","source":"folder = str(train_data.loc[0, 'BraTS21ID']).zfill(5)\nfolder","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:26:23.945614Z","iopub.execute_input":"2021-07-14T06:26:23.946017Z","iopub.status.idle":"2021-07-14T06:26:23.952469Z","shell.execute_reply.started":"2021-07-14T06:26:23.945985Z","shell.execute_reply":"2021-07-14T06:26:23.95172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Folder content","metadata":{}},{"cell_type":"code","source":"os.listdir(path+'train/'+folder)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:27:12.452886Z","iopub.execute_input":"2021-07-14T06:27:12.453235Z","iopub.status.idle":"2021-07-14T06:27:12.464077Z","shell.execute_reply.started":"2021-07-14T06:27:12.453206Z","shell.execute_reply":"2021-07-14T06:27:12.462625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of FLAIR images:', len(os.listdir(path+'train/'+folder+'/'+'FLAIR')))\nprint('Number of T1w images:', len(os.listdir(path+'train/'+folder+'/'+'T1w')))\nprint('Number of T1wCE images:', len(os.listdir(path+'train/'+folder+'/'+'T1wCE')))\nprint('Number of T2w images:', len(os.listdir(path+'train/'+folder+'/'+'T2w')))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-14T06:29:31.176726Z","iopub.execute_input":"2021-07-14T06:29:31.177089Z","iopub.status.idle":"2021-07-14T06:29:31.191384Z","shell.execute_reply.started":"2021-07-14T06:29:31.177057Z","shell.execute_reply":"2021-07-14T06:29:31.190629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read first FLAIR image","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:29:52.486362Z","iopub.execute_input":"2021-07-14T06:29:52.486712Z","iopub.status.idle":"2021-07-14T06:29:52.492418Z","shell.execute_reply.started":"2021-07-14T06:29:52.486664Z","shell.execute_reply":"2021-07-14T06:29:52.491077Z"}}},{"cell_type":"code","source":"path_file = ''.join([path, 'train/', folder, '/', 'FLAIR/'])\nimage = os.listdir(path_file)[0]\ndata_file = dicom.dcmread(path_file+image)\nimg = data_file.pixel_array","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:32:42.330297Z","iopub.execute_input":"2021-07-14T06:32:42.330708Z","iopub.status.idle":"2021-07-14T06:32:42.342293Z","shell.execute_reply.started":"2021-07-14T06:32:42.330655Z","shell.execute_reply":"2021-07-14T06:32:42.341162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Image shape","metadata":{}},{"cell_type":"code","source":"print('Image shape:', img.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:32:50.148287Z","iopub.execute_input":"2021-07-14T06:32:50.148651Z","iopub.status.idle":"2021-07-14T06:32:50.15425Z","shell.execute_reply.started":"2021-07-14T06:32:50.148619Z","shell.execute_reply":"2021-07-14T06:32:50.15318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot image","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:33:06.148165Z","iopub.execute_input":"2021-07-14T06:33:06.148541Z","iopub.status.idle":"2021-07-14T06:33:06.155534Z","shell.execute_reply.started":"2021-07-14T06:33:06.148509Z","shell.execute_reply":"2021-07-14T06:33:06.153734Z"}}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 6))\nax.imshow(img, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:36:05.087899Z","iopub.execute_input":"2021-07-14T06:36:05.088293Z","iopub.status.idle":"2021-07-14T06:36:05.280282Z","shell.execute_reply.started":"2021-07-14T06:36:05.088263Z","shell.execute_reply":"2021-07-14T06:36:05.279069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot Examples\nWe plot some images of the first train sample for every category","metadata":{}},{"cell_type":"code","source":"row = 0\nplot_examples(row = row, cat = 'FLAIR')\nplot_examples(row = row, cat = 'T1w')\nplot_examples(row = row, cat = 'T1wCE')\nplot_examples(row = row, cat = 'T2w')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:12:45.407833Z","iopub.execute_input":"2021-07-14T07:12:45.408248Z","iopub.status.idle":"2021-07-14T07:12:48.521143Z","shell.execute_reply.started":"2021-07-14T07:12:45.408215Z","shell.execute_reply":"2021-07-14T07:12:48.520256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator\nWe define a data generator to load the data on demand.\n\n*Coming Soon*","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:35:22.678681Z","iopub.execute_input":"2021-07-14T06:35:22.679243Z","iopub.status.idle":"2021-07-14T06:35:22.683243Z","shell.execute_reply.started":"2021-07-14T06:35:22.679189Z","shell.execute_reply":"2021-07-14T06:35:22.681987Z"}}},{"cell_type":"markdown","source":"# Export Data","metadata":{}},{"cell_type":"code","source":"samp_subm['MGMT_value'] = 1\nsamp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:14:01.581182Z","iopub.execute_input":"2021-07-14T07:14:01.581714Z","iopub.status.idle":"2021-07-14T07:14:01.588283Z","shell.execute_reply.started":"2021-07-14T07:14:01.581644Z","shell.execute_reply":"2021-07-14T07:14:01.587001Z"},"trusted":true},"execution_count":null,"outputs":[]}]}