{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image \nimport seaborn as sns\nimport os\nimport pydicom as dicom\nfrom pympler import asizeof\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers\nfrom keras.layers.experimental import preprocessing\nfrom keras.preprocessing.image import ImageDataGenerator\nimport cv2\nfrom skimage.transform import resize\nfrom random import randrange\nfrom sklearn.metrics import roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:06.737346Z","iopub.execute_input":"2021-10-19T05:03:06.737846Z","iopub.status.idle":"2021-10-19T05:03:10.499668Z","shell.execute_reply.started":"2021-10-19T05:03:06.737743Z","shell.execute_reply":"2021-10-19T05:03:10.498484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_T2 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_114_epochs_T2W_7k_imgs.h5\")\n\n#model_T2_2 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_200_epochs_T2W_7k_imgs.h5\")\n\n#model_T2_3 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_15_b400_flair_5k_0.73auc_imgs.h5\")\n\n#model_T2_4 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_20_b600_t1wce_7k_0.73auc_imgs.h5\")# last\n\nmodel_t2 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_10_b600_T2w_7k_0.62auc_imgs.h5\")\n\n#model_2 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_15_b600_T2w_7k_0.74auc_imgs.h5\")\n\nmodel_flair = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_10_b600_flair_5.5k_0.70auc_imgs.h5\")\n\n#model_T2_8 = keras.models.load_model(\"../input/trained-model-for-rsnamiccai/rsna_miccai_20_b700_t1wce_7k_0.77auc_imgs.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:10.501250Z","iopub.execute_input":"2021-10-19T05:03:10.501768Z","iopub.status.idle":"2021-10-19T05:03:11.557052Z","shell.execute_reply.started":"2021-10-19T05:03:10.501729Z","shell.execute_reply":"2021-10-19T05:03:11.555597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_t2w_images(path_test):\n    array_1 = []  \n    array_2 = []  \n    array_3 = []  \n    array_4 = []  \n    array_5 = []\n    array_6 = []\n    IMG_PX_SIZE = 150\n    path_cases = sorted([f.path for f in os.scandir(path_test)])\n    for i in range(len(path_cases)):\n        count=0\n        mri_type = sorted([f.path for f in os.scandir(path_cases[i])])\n        img_path = sorted([f.path for f in os.scandir(mri_type[3])])\n        for k in range(len(img_path)): \n            img = dicom.dcmread(img_path[k])\n            if (img.pixel_array.sum()>100000):\n                    resized_img = resize(img.pixel_array, (IMG_PX_SIZE, IMG_PX_SIZE))\n                    img = np.array(resized_img)\n                    stacked_img = np.stack((img,)*3, axis=-1)\n                    stacked_img_normalize = stacked_img/np.max(stacked_img)\n                    if stacked_img_normalize.sum()>2000:\n                        if count==0:\n                            array_1.append(stacked_img_normalize)\n                            #array_1.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==1:\n                            array_2.append(stacked_img_normalize)\n                            #array_2.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==2:\n                            array_3.append(stacked_img_normalize)\n                            #array_3.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==3:\n                            array_4.append(stacked_img_normalize)\n                            #array_4.append(img_path[k])\n                            count+=1\n                            continue \n                        if count==4:\n                            array_5.append(stacked_img_normalize)\n                            #array_5.append(img_path[k])\n                            count+=1\n                            continue \n                        if count==5:\n                            array_6.append(stacked_img_normalize)\n                            #array_6.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==6:\n                            break\n                            \n    array_1 = array_1/np.max(array_1)\n    array_2 = array_2/np.max(array_2)\n    array_3 = array_3/np.max(array_3)\n    array_4 = array_4/np.max(array_4)\n    array_5 = array_5/np.max(array_5)\n    array_6 = array_6/np.max(array_6)\n    \n    print(\"Number of t2w images loaded are \", len(array_1), \",\", len(array_2), \",\", len(array_3),\",\", len(array_4)\n          , \",\", len(array_5), \",\", len(array_6)\n         )\n    \n    \n    return array_1, array_2, array_3, array_4, array_5, array_6","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:11.562504Z","iopub.execute_input":"2021-10-19T05:03:11.562940Z","iopub.status.idle":"2021-10-19T05:03:11.582614Z","shell.execute_reply.started":"2021-10-19T05:03:11.562895Z","shell.execute_reply":"2021-10-19T05:03:11.581569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test_flair_images(path_test):\n    array_1 = []  \n    array_2 = []  \n    array_3 = []  \n    array_4 = []  \n    array_5 = []\n    array_6 = []\n    IMG_PX_SIZE = 150\n    path_cases = sorted([f.path for f in os.scandir(path_test)])\n    for i in range(len(path_cases)):\n        count=0\n        mri_type = sorted([f.path for f in os.scandir(path_cases[i])])\n        img_path = sorted([f.path for f in os.scandir(mri_type[0])])\n        for k in range(len(img_path)): \n            img = dicom.dcmread(img_path[k])\n            if (img.pixel_array.sum()>100000):\n                    resized_img = resize(img.pixel_array, (IMG_PX_SIZE, IMG_PX_SIZE))\n                    img = np.array(resized_img)\n                    stacked_img = np.stack((img,)*3, axis=-1)\n                    stacked_img_normalize = stacked_img/np.max(stacked_img)\n                    if stacked_img_normalize.sum()>2000:\n                        if count==0:\n                            array_1.append(stacked_img_normalize)\n                            #array_1.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==1:\n                            array_2.append(stacked_img_normalize)\n                            #array_2.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==2:\n                            array_3.append(stacked_img_normalize)\n                            #array_3.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==3:\n                            array_4.append(stacked_img_normalize)\n                            #array_4.append(img_path[k])\n                            count+=1\n                            continue \n                        if count==4:\n                            array_5.append(stacked_img_normalize)\n                            #array_5.append(img_path[k])\n                            count+=1\n                            continue \n                        if count==5:\n                            array_6.append(stacked_img_normalize)\n                            #array_6.append(img_path[k])\n                            count+=1\n                            continue\n                        if count==6:\n                            break\n                            \n    array_1 = array_1/np.max(array_1)\n    array_2 = array_2/np.max(array_2)\n    array_3 = array_3/np.max(array_3)\n    array_4 = array_4/np.max(array_4)\n    array_5 = array_5/np.max(array_5)\n    array_6 = array_6/np.max(array_6)\n    \n    print(\"Number of flair images loaded are \", len(array_1), \",\", len(array_2), \",\", len(array_3),\",\", len(array_4)\n          , \",\", len(array_5), \",\", len(array_6)\n         )\n    \n    \n    return array_1, array_2, array_3, array_4, array_5, array_6","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:11.584757Z","iopub.execute_input":"2021-10-19T05:03:11.585227Z","iopub.status.idle":"2021-10-19T05:03:11.606688Z","shell.execute_reply.started":"2021-10-19T05:03:11.585119Z","shell.execute_reply":"2021-10-19T05:03:11.605158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/test\"","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:11.608397Z","iopub.execute_input":"2021-10-19T05:03:11.608887Z","iopub.status.idle":"2021-10-19T05:03:11.625367Z","shell.execute_reply.started":"2021-10-19T05:03:11.608844Z","shell.execute_reply":"2021-10-19T05:03:11.623850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixels_1, pixels_2, pixels_3, pixels_4, pixels_5, pixels_6 = load_test_flair_images(test)\n\npixels_7, pixels_8, pixels_9, pixels_10, pixels_11, pixels_12 = load_test_t2w_images(test)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:11.627525Z","iopub.execute_input":"2021-10-19T05:03:11.628015Z","iopub.status.idle":"2021-10-19T05:03:46.764850Z","shell.execute_reply.started":"2021-10-19T05:03:11.627970Z","shell.execute_reply":"2021-10-19T05:03:46.763540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_1 = model_flair.predict(pixels_1)\nprediction_1 = preds_1[:,1]\n#-----------------------------------------\npreds_2 = model_flair.predict(pixels_2)\nprediction_2 = preds_2[:,1]\n#-----------------------------------------\npreds_3 = model_flair.predict(pixels_3)\nprediction_3 = preds_3[:,1]\n#-----------------------------------------\npreds_4 = model_flair.predict(pixels_4)\nprediction_4 = preds_4[:,1]\n#-----------------------------------------\npreds_5 = model_flair.predict(pixels_5)\nprediction_5 = preds_5[:,1]\n#-----------------------------------------\npreds_6 = model_flair.predict(pixels_6)\nprediction_6 = preds_6[:,1]\n#-----------------------------------------\n\n\npreds_7 = model_t2.predict(pixels_7)\nprediction_7 = preds_7[:,1]\n#-----------------------------------------\npreds_8 = model_t2.predict(pixels_8)\nprediction_8 = preds_8[:,1]\n#-----------------------------------------\npreds_9 = model_t2.predict(pixels_9)\nprediction_9 = preds_9[:,1]\n#-----------------------------------------\npreds_10 = model_t2.predict(pixels_10)\nprediction_10 = preds_10[:,1]\n#-----------------------------------------\npreds_11 = model_t2.predict(pixels_11)\nprediction_11 = preds_11[:,1]\n#-----------------------------------------\npreds_12 = model_t2.predict(pixels_12)\nprediction_12 = preds_12[:,1]\n#-----------------------------------------","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:46.766590Z","iopub.execute_input":"2021-10-19T05:03:46.766961Z","iopub.status.idle":"2021-10-19T05:03:51.135403Z","shell.execute_reply.started":"2021-10-19T05:03:46.766925Z","shell.execute_reply":"2021-10-19T05:03:51.133995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" def create_sub(path_test, \n                p1, p2, p3, p4, p5, p6,\n                p7, p8, p9, p10, p11, p12\n               ):\n    cases = []\n    path_cases = sorted([f.path for f in os.scandir(path_test)])\n    for i in range(len(path_cases)):\n        \n        case_number = path_cases[i][-5:]\n        final_case_no = case_number.lstrip(\"0\")\n        cases.append(int(final_case_no))\n        \n        prediction = (\n            \n            p1.astype(float)\n            +p2.astype(float)\n            +p3.astype(float)\n            +p4.astype(float)\n            +p5.astype(float)\n            +p6.astype(float)\n            \n            +p7.astype(float)\n            +p8.astype(float)\n            +p9.astype(float)\n            +p10.astype(float)\n            +p11.astype(float)\n            +p12.astype(float)\n        )/12\n        \n        \n    df = pd.DataFrame({\"BraTS21ID\":cases, \"MGMT_value\":prediction})\n    return df","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:51.137601Z","iopub.execute_input":"2021-10-19T05:03:51.137976Z","iopub.status.idle":"2021-10-19T05:03:51.148642Z","shell.execute_reply.started":"2021-10-19T05:03:51.137941Z","shell.execute_reply":"2021-10-19T05:03:51.147418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = create_sub(test,\n                    \n                    prediction_1,\n                    prediction_2, \n                    prediction_3,\n                    prediction_4,\n                    prediction_5,\n                    prediction_6,\n                    \n                    prediction_7,\n                    prediction_8, \n                    prediction_9,\n                    prediction_10,\n                    prediction_11,\n                    prediction_12,\n                   )","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:51.151075Z","iopub.execute_input":"2021-10-19T05:03:51.151965Z","iopub.status.idle":"2021-10-19T05:03:51.173543Z","shell.execute_reply.started":"2021-10-19T05:03:51.151899Z","shell.execute_reply":"2021-10-19T05:03:51.172428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(sub_df.MGMT_value)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T05:03:51.175008Z","iopub.execute_input":"2021-10-19T05:03:51.175603Z","iopub.status.idle":"2021-10-19T05:03:51.513188Z","shell.execute_reply.started":"2021-10-19T05:03:51.175561Z","shell.execute_reply":"2021-10-19T05:03:51.511903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}