{"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","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-25T03:13:14.064693Z","iopub.execute_input":"2021-09-25T03:13:14.065315Z","iopub.status.idle":"2021-09-25T03:13:14.073258Z","shell.execute_reply.started":"2021-09-25T03:13:14.065271Z","shell.execute_reply":"2021-09-25T03:13:14.071652Z"},"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-25T03:13:14.075398Z","iopub.execute_input":"2021-09-25T03:13:14.075942Z","iopub.status.idle":"2021-09-25T03:13:14.108579Z","shell.execute_reply.started":"2021-09-25T03:13:14.075897Z","shell.execute_reply":"2021-09-25T03:13:14.107907Z"},"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-25T03:13:14.109860Z","iopub.execute_input":"2021-09-25T03:13:14.110792Z","iopub.status.idle":"2021-09-25T03:13:14.124498Z","shell.execute_reply.started":"2021-09-25T03:13:14.110743Z","shell.execute_reply":"2021-09-25T03:13:14.123421Z"},"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-25T03:13:14.126082Z","iopub.execute_input":"2021-09-25T03:13:14.126470Z","iopub.status.idle":"2021-09-25T03:13:14.144051Z","shell.execute_reply.started":"2021-09-25T03:13:14.126412Z","shell.execute_reply":"2021-09-25T03:13:14.143166Z"},"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-25T03:13:14.146064Z","iopub.execute_input":"2021-09-25T03:13:14.146845Z","iopub.status.idle":"2021-09-25T03:13:14.163212Z","shell.execute_reply.started":"2021-09-25T03:13:14.146809Z","shell.execute_reply":"2021-09-25T03:13:14.162002Z"},"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-25T03:13:14.164734Z","iopub.execute_input":"2021-09-25T03:13:14.165303Z","iopub.status.idle":"2021-09-25T03:13:14.176219Z","shell.execute_reply.started":"2021-09-25T03:13:14.165268Z","shell.execute_reply":"2021-09-25T03:13:14.175432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nhist_data = [sub_715.MGMT_value, sub_684.MGMT_value, sub_683.MGMT_value, sub_674.MGMT_value]\ngroup_labels = ['sub_715', 'sub_684', 'sub_683', 'sub_674']\n\nfig = ff.create_distplot(hist_data, group_labels, bin_size=.2, show_hist=False, show_rug=False)\n\nfig.update_layout(\n    width = 700,\n    height = 400\n)\nfig.show()","metadata":{"papermill":{"duration":0.153437,"end_time":"2021-09-22T07:34:30.42511","exception":false,"start_time":"2021-09-22T07:34:30.271673","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-25T03:13:14.177693Z","iopub.execute_input":"2021-09-25T03:13:14.177950Z","iopub.status.idle":"2021-09-25T03:13:14.397254Z","shell.execute_reply.started":"2021-09-25T03:13:14.177910Z","shell.execute_reply":"2021-09-25T03:13:14.396580Z"},"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-25T03:13:14.398358Z","iopub.execute_input":"2021-09-25T03:13:14.398904Z","iopub.status.idle":"2021-09-25T03:13:14.422485Z","shell.execute_reply.started":"2021-09-25T03:13:14.398869Z","shell.execute_reply":"2021-09-25T03:13:14.421486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=np.corrcoef(hist_data)\nfig = px.imshow(data,x=group_labels,y=group_labels)\nfig.show()","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.844651,"end_time":"2021-09-22T07:34:31.349587","exception":false,"start_time":"2021-09-22T07:34:30.504936","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-25T03:13:14.423794Z","iopub.execute_input":"2021-09-25T03:13:14.424035Z","iopub.status.idle":"2021-09-25T03:13:14.483131Z","shell.execute_reply.started":"2021-09-25T03:13:14.424007Z","shell.execute_reply":"2021-09-25T03:13:14.482344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Finalsubmission = sub_715.copy()\nFinalsubmission['MGMT_value'] =\\\nsub_715['MGMT_value'].values*0.595+\\\nsub_684['MGMT_value'].values*0.2+\\\nsub_683['MGMT_value'].values*0.095+\\\nsub_674['MGMT_value'].values*0.11","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-25T03:13:14.485317Z","iopub.execute_input":"2021-09-25T03:13:14.485589Z","iopub.status.idle":"2021-09-25T03:13:14.492101Z","shell.execute_reply.started":"2021-09-25T03:13:14.485560Z","shell.execute_reply":"2021-09-25T03:13:14.491242Z"},"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-25T03:13:14.493521Z","iopub.execute_input":"2021-09-25T03:13:14.493752Z","iopub.status.idle":"2021-09-25T03:13:14.506230Z","shell.execute_reply.started":"2021-09-25T03:13:14.493725Z","shell.execute_reply":"2021-09-25T03:13:14.505348Z"},"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-25T03:13:14.507837Z","iopub.execute_input":"2021-09-25T03:13:14.508092Z","iopub.status.idle":"2021-09-25T03:13:14.523383Z","shell.execute_reply.started":"2021-09-25T03:13:14.508065Z","shell.execute_reply":"2021-09-25T03:13:14.522432Z"},"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-25T03:13:14.524860Z","iopub.execute_input":"2021-09-25T03:13:14.525160Z","iopub.status.idle":"2021-09-25T03:13:14.536543Z","shell.execute_reply.started":"2021-09-25T03:13:14.525130Z","shell.execute_reply":"2021-09-25T03:13:14.535323Z"},"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-25T03:13:14.537847Z","iopub.execute_input":"2021-09-25T03:13:14.538122Z","iopub.status.idle":"2021-09-25T03:13:14.554034Z","shell.execute_reply.started":"2021-09-25T03:13:14.538084Z","shell.execute_reply":"2021-09-25T03:13:14.552958Z"},"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-25T03:13:14.555594Z","iopub.execute_input":"2021-09-25T03:13:14.556499Z","iopub.status.idle":"2021-09-25T03:13:14.569220Z","shell.execute_reply.started":"2021-09-25T03:13:14.556430Z","shell.execute_reply":"2021-09-25T03:13:14.568123Z"},"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-25T03:13:14.570708Z","iopub.execute_input":"2021-09-25T03:13:14.570944Z","iopub.status.idle":"2021-09-25T03:13:14.584407Z","shell.execute_reply.started":"2021-09-25T03:13:14.570917Z","shell.execute_reply":"2021-09-25T03:13:14.583478Z"},"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-25T03:13:14.585920Z","iopub.execute_input":"2021-09-25T03:13:14.586679Z","iopub.status.idle":"2021-09-25T03:13:14.776093Z","shell.execute_reply.started":"2021-09-25T03:13:14.586611Z","shell.execute_reply":"2021-09-25T03:13:14.775216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{}}]}