{"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 os\nimport glob\n\nimport pandas as pd\nimport numpy as np\nfrom pathlib import Path\n\nimport random\nfrom tqdm.notebook import tqdm\nimport pydicom # Handle MRI images\n\nimport cv2  # OpenCV - https://docs.opencv.org/master/d6/d00/tutorial_py_root.html\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import model_selection\nfrom sklearn.metrics import roc_auc_score\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-08T15:44:12.274069Z","iopub.execute_input":"2021-10-08T15:44:12.274356Z","iopub.status.idle":"2021-10-08T15:44:13.199587Z","shell.execute_reply.started":"2021-10-08T15:44:12.274315Z","shell.execute_reply":"2021-10-08T15:44:13.198872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras.layers as tkl","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:13.201059Z","iopub.execute_input":"2021-10-08T15:44:13.201404Z","iopub.status.idle":"2021-10-08T15:44:13.205673Z","shell.execute_reply.started":"2021-10-08T15:44:13.201361Z","shell.execute_reply":"2021-10-08T15:44:13.204832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:13.328728Z","iopub.execute_input":"2021-10-08T15:44:13.328961Z","iopub.status.idle":"2021-10-08T15:44:13.334082Z","shell.execute_reply.started":"2021-10-08T15:44:13.328935Z","shell.execute_reply":"2021-10-08T15:44:13.331616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\ndata_dir = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/')\ntest_df = pd.read_csv(data_dir / \"sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:13.697372Z","iopub.execute_input":"2021-10-08T15:44:13.697902Z","iopub.status.idle":"2021-10-08T15:44:13.707084Z","shell.execute_reply.started":"2021-10-08T15:44:13.697868Z","shell.execute_reply":"2021-10-08T15:44:13.706377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mri_types = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nexcluded_images = [109, 123, 709] # Bad images\n\nSEED_LENGTH = 10 # Number of seeds to use for the model ensemble","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:14.134142Z","iopub.execute_input":"2021-10-08T15:44:14.134894Z","iopub.status.idle":"2021-10-08T15:44:14.139501Z","shell.execute_reply.started":"2021-10-08T15:44:14.134855Z","shell.execute_reply":"2021-10-08T15:44:14.138754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('../input/bestmodel/best_model_121.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:14.441859Z","iopub.execute_input":"2021-10-08T15:44:14.442476Z","iopub.status.idle":"2021-10-08T15:44:14.605053Z","shell.execute_reply.started":"2021-10-08T15:44:14.442437Z","shell.execute_reply":"2021-10-08T15:44:14.604347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testlist=os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/test')","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:14.808501Z","iopub.execute_input":"2021-10-08T15:44:14.809445Z","iopub.status.idle":"2021-10-08T15:44:14.815993Z","shell.execute_reply.started":"2021-10-08T15:44:14.809405Z","shell.execute_reply":"2021-10-08T15:44:14.815224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path, size = 224):\n    ''' \n    Reads a DICOM image, standardizes so that the pixel values are between 0 and 1, then rescales to 0 and 255\n    \n    Not super sure if this kind of scaling is appropriate, but everyone seems to do it. \n    '''\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    # transform data into black and white scale / grayscale\n#     data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return cv2.resize(data, (size, size))\ndef get_all_image_paths(brats21id, image_type, folder='train'): \n    '''\n    Returns an arry of all the images of a particular type for a particular patient ID\n    '''\n    assert(image_type in mri_types)\n    \n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/%s/\" % folder, \n        str(brats21id).zfill(5),\n    )\n\n    paths = sorted(\n        glob.glob(os.path.join(patient_path, image_type, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    \n    num_images = len(paths)\n    \n    start = int(num_images * 0.25)\n    end = int(num_images * 0.75)\n\n    interval = 3\n    \n    if num_images < 10: \n        interval = 1\n    \n    return np.array(paths[start:end:interval])\n\ndef get_all_images(brats21id, image_type, folder='train', size=225):\n    return [load_dicom(path, size) for path in get_all_image_paths(brats21id, image_type, folder)]","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:15.128102Z","iopub.execute_input":"2021-10-08T15:44:15.128713Z","iopub.status.idle":"2021-10-08T15:44:15.138721Z","shell.execute_reply.started":"2021-10-08T15:44:15.128667Z","shell.execute_reply":"2021-10-08T15:44:15.137933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ndef get_all_data_for_test(image_type, image_size=32):\n    global test_df\n    \n    X = []\n    test_ids = []\n\n    for i in tqdm(test_df.index):\n        x = test_df.loc[i]\n        images = get_all_images(int(x['BraTS21ID']), image_type, 'test', image_size)\n        X += images\n        test_ids += [int(x['BraTS21ID'])] * len(images)\n\n    return np.array(X), np.array(test_ids)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:15.452269Z","iopub.execute_input":"2021-10-08T15:44:15.452549Z","iopub.status.idle":"2021-10-08T15:44:15.459124Z","shell.execute_reply.started":"2021-10-08T15:44:15.452519Z","shell.execute_reply":"2021-10-08T15:44:15.457988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test, testidt = get_all_data_for_test('FLAIR', image_size=32)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:44:16.042157Z","iopub.execute_input":"2021-10-08T15:44:16.042415Z","iopub.status.idle":"2021-10-08T15:44:32.435521Z","shell.execute_reply.started":"2021-10-08T15:44:16.042388Z","shell.execute_reply":"2021-10-08T15:44:32.434856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2021-10-08T15:46:22.619956Z","iopub.execute_input":"2021-10-08T15:46:22.620581Z","iopub.status.idle":"2021-10-08T15:46:22.637816Z","shell.execute_reply.started":"2021-10-08T15:46:22.620541Z","shell.execute_reply":"2021-10-08T15:46:22.637074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:03:18.023250Z","iopub.execute_input":"2021-10-08T16:03:18.023517Z","iopub.status.idle":"2021-10-08T16:03:18.116452Z","shell.execute_reply.started":"2021-10-08T16:03:18.023489Z","shell.execute_reply":"2021-10-08T16:03:18.115726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:04:49.887144Z","iopub.execute_input":"2021-10-08T16:04:49.887492Z","iopub.status.idle":"2021-10-08T16:04:49.895034Z","shell.execute_reply.started":"2021-10-08T16:04:49.887454Z","shell.execute_reply":"2021-10-08T16:04:49.894148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:04:56.014344Z","iopub.execute_input":"2021-10-08T16:04:56.015433Z","iopub.status.idle":"2021-10-08T16:04:56.020505Z","shell.execute_reply.started":"2021-10-08T16:04:56.015381Z","shell.execute_reply":"2021-10-08T16:04:56.019465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['BraTS21ID']=testidt\nsubmission['MGMT_value']=pred\nfinsub=submission.sort_values(by=['BraTS21ID'])\nfinsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:05:21.565632Z","iopub.execute_input":"2021-10-08T16:05:21.566437Z","iopub.status.idle":"2021-10-08T16:05:21.584737Z","shell.execute_reply.started":"2021-10-08T16:05:21.566395Z","shell.execute_reply":"2021-10-08T16:05:21.583921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}