{"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":"# 🧠Brain Tumor 3D blender ","metadata":{"papermill":{"duration":0.013172,"end_time":"2021-09-22T07:34:26.661617","exception":false,"start_time":"2021-09-22T07:34:26.648445","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Other notebooks in this competition\n\n* [Brain Tumor 3D [Training]](https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-training) \n* [Brain Tumor 3D [Inference]](https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-inference)\n* [Brain Tumor 3D [EDA]](https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-eda)\n\n\n## Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{"papermill":{"duration":0.012326,"end_time":"2021-09-22T07:34:26.687272","exception":false,"start_time":"2021-09-22T07:34:26.674946","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"#### <font color='red'>Important Note:</font> Please don't use this notebook as a final submit, that will lead you to get a low private score because I used a 0.5 as score for all points in the private data.\n### In this notebook, I just want to explain the blending and how it can improve the score.","metadata":{}},{"cell_type":"markdown","source":"# blending\nGenerally, blending uses many separate models to compute the initial prediction. the goal of mixing the predictions in some way is to achieve an even better final prediction.","metadata":{}},{"cell_type":"markdown","source":"\n# Solutions for blending\n\n1. https://www.kaggle.com/rluethy/efficientnet3d-with-one-mri-type (Score=0.684)\n1. https://www.kaggle.com/ammarnassanalhajali/brain-tumor-3d-inference (Score=0.683)\n1. https://www.kaggle.com/syerwin/efficientnet3d-with-one-mri-type (Score=0.674)\n1. https://www.kaggle.com/hijest/1-rsna-miccai-not-smart-ensemble  (Score=0.715)\n\n","metadata":{"papermill":{"duration":0.012318,"end_time":"2021-09-22T07:34:26.712234","exception":false,"start_time":"2021-09-22T07:34:26.699916","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport glob\nimport os\nimport matplotlib.pyplot as plt\nimport plotly.figure_factory as ff\nimport plotly.express as px\n\nimport plotly.graph_objects as go\nfrom plotly.offline import iplot\nimport seaborn as sns","metadata":{"papermill":{"duration":3.263001,"end_time":"2021-09-22T07:34:29.988255","exception":false,"start_time":"2021-09-22T07:34:26.725254","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:05.988424Z","iopub.execute_input":"2021-09-24T23:45:05.988831Z","iopub.status.idle":"2021-09-24T23:45:05.994534Z","shell.execute_reply.started":"2021-09-24T23:45:05.988796Z","shell.execute_reply":"2021-09-24T23:45:05.993727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_715 = pd.read_csv('../input/brain-tumor-3d-blender-csv/RSNA_Public/715.csv')\nsub_684 = pd.read_csv('../input/brain-tumor-3d-blender-csv/RSNA_Public/684.csv')\nsub_683 = pd.read_csv('../input/brain-tumor-3d-blender-csv/RSNA_Public/683.csv')\nsub_674 = pd.read_csv('../input/brain-tumor-3d-blender-csv/RSNA_Public/674.csv')","metadata":{"papermill":{"duration":0.078095,"end_time":"2021-09-22T07:34:30.079307","exception":false,"start_time":"2021-09-22T07:34:30.001212","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:05.996274Z","iopub.execute_input":"2021-09-24T23:45:05.9967Z","iopub.status.idle":"2021-09-24T23:45:06.032078Z","shell.execute_reply.started":"2021-09-24T23:45:05.996658Z","shell.execute_reply":"2021-09-24T23:45:06.030979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_715=sub_715.sort_values(by=['BraTS21ID'], ascending=True)\nsub_715=sub_715.reset_index(drop=True)\nsub_715.head(3)","metadata":{"papermill":{"duration":0.040047,"end_time":"2021-09-22T07:34:30.1323","exception":false,"start_time":"2021-09-22T07:34:30.092253","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.0333Z","iopub.execute_input":"2021-09-24T23:45:06.033529Z","iopub.status.idle":"2021-09-24T23:45:06.047453Z","shell.execute_reply.started":"2021-09-24T23:45:06.033501Z","shell.execute_reply":"2021-09-24T23:45:06.046853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_684=sub_684.sort_values(by=['BraTS21ID'], ascending=True)\nsub_684=sub_684.reset_index(drop=True)\nsub_684.head(3)","metadata":{"papermill":{"duration":0.023463,"end_time":"2021-09-22T07:34:30.169162","exception":false,"start_time":"2021-09-22T07:34:30.145699","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.049145Z","iopub.execute_input":"2021-09-24T23:45:06.049745Z","iopub.status.idle":"2021-09-24T23:45:06.061064Z","shell.execute_reply.started":"2021-09-24T23:45:06.049707Z","shell.execute_reply":"2021-09-24T23:45:06.060041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_683=sub_683.sort_values(by=['BraTS21ID'], ascending=True)\nsub_683=sub_683.reset_index(drop=True)\nsub_683.head(3)","metadata":{"papermill":{"duration":0.028383,"end_time":"2021-09-22T07:34:30.211198","exception":false,"start_time":"2021-09-22T07:34:30.182815","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.062212Z","iopub.execute_input":"2021-09-24T23:45:06.062605Z","iopub.status.idle":"2021-09-24T23:45:06.082102Z","shell.execute_reply.started":"2021-09-24T23:45:06.062575Z","shell.execute_reply":"2021-09-24T23:45:06.081193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_674=sub_674.sort_values(by=['BraTS21ID'], ascending=True)\nsub_674=sub_674.reset_index(drop=True)\nsub_674.head(3)","metadata":{"papermill":{"duration":0.024928,"end_time":"2021-09-22T07:34:30.25003","exception":false,"start_time":"2021-09-22T07:34:30.225102","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.08331Z","iopub.execute_input":"2021-09-24T23:45:06.083725Z","iopub.status.idle":"2021-09-24T23:45:06.102182Z","shell.execute_reply.started":"2021-09-24T23:45:06.083691Z","shell.execute_reply":"2021-09-24T23:45:06.101087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dict for the dataframes and their names\ndfs = {\"sub_715\" : sub_715, \"sub_684\": sub_684, \"sub_683\" : sub_683, \"sub_674\" : sub_674}\n\n# plot the data\nfig = go.Figure()\nfor i in dfs:\n    fig = fig.add_trace(go.Scatter(x = dfs[i][\"BraTS21ID\"],\n                                   y = dfs[i][\"MGMT_value\"], \n                                   mode=\"markers\",\n                                   name = i))\n    fig = fig.add_trace(\n        go.Scatter(\n            x=[0, 1010],\n            y=[0.5, 0.5],\n            mode=\"lines\",\n            line=go.scatter.Line(color=\"gray\"),\n            showlegend=False)\n    )\n\nfig.update_layout(\n    width = 700,\n    height = 500\n)\nfig.show()\n","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.046213,"end_time":"2021-09-22T07:34:30.488164","exception":false,"start_time":"2021-09-22T07:34:30.441951","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.151258Z","iopub.execute_input":"2021-09-24T23:45:06.151477Z","iopub.status.idle":"2021-09-24T23:45:06.179868Z","shell.execute_reply.started":"2021-09-24T23:45:06.151452Z","shell.execute_reply":"2021-09-24T23:45:06.178759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Finalsubmission = sub_715.copy()\nFinalsubmission['MGMT_value'] =\\\nsub_715['MGMT_value'].values*0.605+\\\nsub_684['MGMT_value'].values*0.185+\\\nsub_683['MGMT_value'].values*0.105+\\\nsub_674['MGMT_value'].values*0.105","metadata":{"papermill":{"duration":0.029834,"end_time":"2021-09-22T07:34:31.397379","exception":false,"start_time":"2021-09-22T07:34:31.367545","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.236553Z","iopub.execute_input":"2021-09-24T23:45:06.236881Z","iopub.status.idle":"2021-09-24T23:45:06.243986Z","shell.execute_reply.started":"2021-09-24T23:45:06.236843Z","shell.execute_reply":"2021-09-24T23:45:06.2431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Finalsubmission['BraTS21ID'] = Finalsubmission['BraTS21ID'].apply(lambda x: str(x).zfill(5))","metadata":{"papermill":{"duration":0.025142,"end_time":"2021-09-22T07:34:31.439804","exception":false,"start_time":"2021-09-22T07:34:31.414662","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.245699Z","iopub.execute_input":"2021-09-24T23:45:06.246015Z","iopub.status.idle":"2021-09-24T23:45:06.25864Z","shell.execute_reply.started":"2021-09-24T23:45:06.245977Z","shell.execute_reply":"2021-09-24T23:45:06.257939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Finalsubmission.head(3)","metadata":{"papermill":{"duration":0.026702,"end_time":"2021-09-22T07:34:31.484629","exception":false,"start_time":"2021-09-22T07:34:31.457927","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.259644Z","iopub.execute_input":"2021-09-24T23:45:06.259941Z","iopub.status.idle":"2021-09-24T23:45:06.275455Z","shell.execute_reply.started":"2021-09-24T23:45:06.25991Z","shell.execute_reply":"2021-09-24T23:45:06.274738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Fsubmission = Finalsubmission.set_index('BraTS21ID')\nFsubmissionDict = Fsubmission['MGMT_value'].to_dict()","metadata":{"papermill":{"duration":0.024567,"end_time":"2021-09-22T07:34:31.526963","exception":false,"start_time":"2021-09-22T07:34:31.502396","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.276504Z","iopub.execute_input":"2021-09-24T23:45:06.276748Z","iopub.status.idle":"2021-09-24T23:45:06.290051Z","shell.execute_reply.started":"2021-09-24T23:45:06.276721Z","shell.execute_reply":"2021-09-24T23:45:06.28914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listOfStudyPaths = glob.glob('../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/*')\nlistOfStudies = [eachPath.split('/')[-1] for eachPath in listOfStudyPaths]\n\npredList = []\nfor eachStudy in listOfStudies:\n    if eachStudy not in FsubmissionDict:\n        predList.append('0.500')\n    else:\n        score = float(FsubmissionDict[eachStudy])\n        predList.append(score)\n        \nsubmission = pd.DataFrame({'BraTS21ID':listOfStudies,'MGMT_value':predList})\n","metadata":{"papermill":{"duration":0.044541,"end_time":"2021-09-22T07:34:31.588226","exception":false,"start_time":"2021-09-22T07:34:31.543685","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.291197Z","iopub.execute_input":"2021-09-24T23:45:06.291411Z","iopub.status.idle":"2021-09-24T23:45:06.307857Z","shell.execute_reply.started":"2021-09-24T23:45:06.291388Z","shell.execute_reply":"2021-09-24T23:45:06.306815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=submission.sort_values(by=['BraTS21ID'], ascending=True)\nsubmission=submission.reset_index(drop=True)\nsubmission.head(5)","metadata":{"papermill":{"duration":0.030703,"end_time":"2021-09-22T07:34:31.636441","exception":false,"start_time":"2021-09-22T07:34:31.605738","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.309075Z","iopub.execute_input":"2021-09-24T23:45:06.309305Z","iopub.status.idle":"2021-09-24T23:45:06.322578Z","shell.execute_reply.started":"2021-09-24T23:45:06.309278Z","shell.execute_reply":"2021-09-24T23:45:06.321667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":0.029576,"end_time":"2021-09-22T07:34:31.693239","exception":false,"start_time":"2021-09-22T07:34:31.663663","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.323867Z","iopub.execute_input":"2021-09-24T23:45:06.324661Z","iopub.status.idle":"2021-09-24T23:45:06.335922Z","shell.execute_reply.started":"2021-09-24T23:45:06.324596Z","shell.execute_reply":"2021-09-24T23:45:06.335133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nplt.hist(submission[\"MGMT_value\"]);","metadata":{"papermill":{"duration":0.217005,"end_time":"2021-09-22T07:34:31.928299","exception":false,"start_time":"2021-09-22T07:34:31.711294","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-24T23:45:06.337267Z","iopub.execute_input":"2021-09-24T23:45:06.337773Z","iopub.status.idle":"2021-09-24T23:45:06.544002Z","shell.execute_reply.started":"2021-09-24T23:45:06.337742Z","shell.execute_reply":"2021-09-24T23:45:06.542912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{}}]}