{"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":"## 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-28T16:08:16.394226Z","iopub.execute_input":"2021-09-28T16:08:16.395009Z","iopub.status.idle":"2021-09-28T16:08:19.925439Z","shell.execute_reply.started":"2021-09-28T16:08:16.394918Z","shell.execute_reply":"2021-09-28T16:08:19.924756Z"},"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-28T16:08:19.926658Z","iopub.execute_input":"2021-09-28T16:08:19.927019Z","iopub.status.idle":"2021-09-28T16:08:19.9715Z","shell.execute_reply.started":"2021-09-28T16:08:19.926993Z","shell.execute_reply":"2021-09-28T16:08:19.970688Z"},"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-28T16:08:19.972482Z","iopub.execute_input":"2021-09-28T16:08:19.972687Z","iopub.status.idle":"2021-09-28T16:08:20.000011Z","shell.execute_reply.started":"2021-09-28T16:08:19.972663Z","shell.execute_reply":"2021-09-28T16:08:19.999282Z"},"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-28T16:09:16.509729Z","iopub.execute_input":"2021-09-28T16:09:16.510011Z","iopub.status.idle":"2021-09-28T16:09:16.520886Z","shell.execute_reply.started":"2021-09-28T16:09:16.50998Z","shell.execute_reply":"2021-09-28T16:09:16.520288Z"},"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-28T16:09:01.597825Z","iopub.execute_input":"2021-09-28T16:09:01.598083Z","iopub.status.idle":"2021-09-28T16:09:01.608897Z","shell.execute_reply.started":"2021-09-28T16:09:01.598057Z","shell.execute_reply":"2021-09-28T16:09:01.608051Z"},"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-28T16:09:30.403419Z","iopub.execute_input":"2021-09-28T16:09:30.404099Z","iopub.status.idle":"2021-09-28T16:09:30.415391Z","shell.execute_reply.started":"2021-09-28T16:09:30.404063Z","shell.execute_reply":"2021-09-28T16:09:30.41439Z"},"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-28T16:09:32.801763Z","iopub.execute_input":"2021-09-28T16:09:32.802244Z","iopub.status.idle":"2021-09-28T16:09:32.845631Z","shell.execute_reply.started":"2021-09-28T16:09:32.802213Z","shell.execute_reply":"2021-09-28T16:09:32.844946Z"},"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-28T16:16:33.187125Z","iopub.execute_input":"2021-09-28T16:16:33.187464Z","iopub.status.idle":"2021-09-28T16:16:33.214774Z","shell.execute_reply.started":"2021-09-28T16:16:33.187432Z","shell.execute_reply":"2021-09-28T16:16:33.214227Z"},"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-28T16:17:03.402309Z","iopub.execute_input":"2021-09-28T16:17:03.403006Z","iopub.status.idle":"2021-09-28T16:17:04.181461Z","shell.execute_reply.started":"2021-09-28T16:17:03.402956Z","shell.execute_reply":"2021-09-28T16:17:04.180805Z"},"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-28T16:17:49.699463Z","iopub.execute_input":"2021-09-28T16:17:49.700129Z","iopub.status.idle":"2021-09-28T16:17:49.709593Z","shell.execute_reply.started":"2021-09-28T16:17:49.700091Z","shell.execute_reply":"2021-09-28T16:17:49.708613Z"},"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-28T16:19:28.929597Z","iopub.execute_input":"2021-09-28T16:19:28.929864Z","iopub.status.idle":"2021-09-28T16:19:28.936264Z","shell.execute_reply.started":"2021-09-28T16:19:28.929838Z","shell.execute_reply":"2021-09-28T16:19:28.935425Z"},"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-28T16:19:47.391009Z","iopub.execute_input":"2021-09-28T16:19:47.391829Z","iopub.status.idle":"2021-09-28T16:19:47.400031Z","shell.execute_reply.started":"2021-09-28T16:19:47.391769Z","shell.execute_reply":"2021-09-28T16:19:47.399484Z"},"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-28T16:19:51.932914Z","iopub.execute_input":"2021-09-28T16:19:51.933426Z","iopub.status.idle":"2021-09-28T16:19:51.938486Z","shell.execute_reply.started":"2021-09-28T16:19:51.933392Z","shell.execute_reply":"2021-09-28T16:19:51.937721Z"},"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-28T16:20:06.791058Z","iopub.execute_input":"2021-09-28T16:20:06.791926Z","iopub.status.idle":"2021-09-28T16:20:06.809114Z","shell.execute_reply.started":"2021-09-28T16:20:06.791888Z","shell.execute_reply":"2021-09-28T16:20:06.808469Z"},"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-28T16:22:40.257637Z","iopub.execute_input":"2021-09-28T16:22:40.258311Z","iopub.status.idle":"2021-09-28T16:22:40.270018Z","shell.execute_reply.started":"2021-09-28T16:22:40.258262Z","shell.execute_reply":"2021-09-28T16:22:40.269225Z"},"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-28T16:22:43.049398Z","iopub.execute_input":"2021-09-28T16:22:43.04965Z","iopub.status.idle":"2021-09-28T16:22:43.056601Z","shell.execute_reply.started":"2021-09-28T16:22:43.049624Z","shell.execute_reply":"2021-09-28T16:22:43.055844Z"},"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-28T16:22:48.954768Z","iopub.execute_input":"2021-09-28T16:22:48.955036Z","iopub.status.idle":"2021-09-28T16:22:49.206065Z","shell.execute_reply.started":"2021-09-28T16:22:48.955009Z","shell.execute_reply":"2021-09-28T16:22:49.20532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Please if this kernel is useful, <font color='red'>please upvote !!</font>","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}