{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport random as rd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tensorflow import keras\nimport tensorflow as tf\nprint(\"Tensorflow version \" + tf.__version__)\nimport cv2\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-21T15:09:55.49648Z","iopub.execute_input":"2021-07-21T15:09:55.496878Z","iopub.status.idle":"2021-07-21T15:10:00.168523Z","shell.execute_reply.started":"2021-07-21T15:09:55.496795Z","shell.execute_reply":"2021-07-21T15:10:00.167037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training\n\nThis notebooke was used for training:\n\nhttps://www.kaggle.com/lucamtb/brain-tumer-train-class-flair","metadata":{}},{"cell_type":"markdown","source":"## Parameters","metadata":{}},{"cell_type":"code","source":"IM_SIZE = 256","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:00.169897Z","iopub.execute_input":"2021-07-21T15:10:00.170169Z","iopub.status.idle":"2021-07-21T15:10:00.176459Z","shell.execute_reply.started":"2021-07-21T15:10:00.170142Z","shell.execute_reply":"2021-07-21T15:10:00.175569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"model = keras.models.load_model('../input/effect0-brain/Brain_flair_model_effect.h5',custom_objects={\"FixedDropout\": keras.layers.Dropout})","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:00.178288Z","iopub.execute_input":"2021-07-21T15:10:00.178645Z","iopub.status.idle":"2021-07-21T15:10:04.901451Z","shell.execute_reply.started":"2021-07-21T15:10:00.17861Z","shell.execute_reply":"2021-07-21T15:10:04.900616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Some function","metadata":{}},{"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    #data = (data * 255).astype(np.uint8)\n        \n    return data\n\ndef get_prediction_per_case(patient):\n    \n    path = f'../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{patient}/FLAIR/'\n\n    list_subfolders_with_paths = [f for f in os.listdir(path)]\n    \n    prediction = []\n    \n    for images in list_subfolders_with_paths:\n        \n            \n        img = read_xray(path+images)\n                       \n        if np.max(img) > 0 and np.mean(img)>= 0.015:\n                \n                \n            img =  cv2.resize(img,(IM_SIZE,IM_SIZE))\n            \n            img = cv2.merge((img,img,img))\n            img = tf.reshape(img, (-1, IM_SIZE, IM_SIZE, 3))\n            \n            pred = model.predict(img)\n            \n            prediction.append(pred)\n    \n    return np.mean(prediction,axis=0)[0][0]","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:04.903212Z","iopub.execute_input":"2021-07-21T15:10:04.903547Z","iopub.status.idle":"2021-07-21T15:10:04.915257Z","shell.execute_reply.started":"2021-07-21T15:10:04.903514Z","shell.execute_reply":"2021-07-21T15:10:04.913894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_prediction_per_case('00047')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:04.91675Z","iopub.execute_input":"2021-07-21T15:10:04.917105Z","iopub.status.idle":"2021-07-21T15:10:21.795548Z","shell.execute_reply.started":"2021-07-21T15:10:04.917071Z","shell.execute_reply":"2021-07-21T15:10:21.794646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv',dtype=\"string\")","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:21.796906Z","iopub.execute_input":"2021-07-21T15:10:21.797208Z","iopub.status.idle":"2021-07-21T15:10:21.810729Z","shell.execute_reply.started":"2021-07-21T15:10:21.797177Z","shell.execute_reply":"2021-07-21T15:10:21.810017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['MGMT_value'] = df['BraTS21ID'].apply(get_prediction_per_case)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T15:10:21.811901Z","iopub.execute_input":"2021-07-21T15:10:21.812293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df['MGMT_value'] = df['MGMT_value'].apply(lambda x: rd.uniform(0, 1))\ndf[['BraTS21ID', 'MGMT_value']].to_csv('submission.csv', index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}