{"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 tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Model, Sequential\nfrom keras import metrics,layers\nimport pydicom\nimport cv2\nimport glob\nimport os\nimport gc\nimport matplotlib.pyplot as plt\n%matplotlib inline\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#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\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-16T13:49:41.211101Z","iopub.execute_input":"2021-07-16T13:49:41.211530Z","iopub.status.idle":"2021-07-16T13:49:47.660501Z","shell.execute_reply.started":"2021-07-16T13:49:41.211439Z","shell.execute_reply":"2021-07-16T13:49:47.659347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_array(batch_x,SERIES='FLAIR',kind='train'):\n    ARRAY = np.empty((0,5,64,64,1))\n    SERIES= SERIES\n    bs = len(batch_x)\n    for i in range(bs):\n        study = batch_x[i]\n        array = np.empty((0,64,64,1))\n        file_names = glob.glob(f\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/{kind}/\" + str(study) + '/' + str(SERIES) + '/*')\n        file_names.sort()\n        file_names = file_names[-5:]\n        \n        for fn in file_names:\n            img = pydicom.dcmread(fn).pixel_array\n            img = ((img - img.min())/(img.max()+1e-4)).astype(float)\n            img = cv2.resize(img,(64,64))\n            array = np.append(array,img.reshape(1,64,64,1),axis=0)\n        ARRAY = np.append(ARRAY,array.reshape(1,5,64,64,1),axis=0)\n    \n    return ARRAY","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.662208Z","iopub.execute_input":"2021-07-16T13:49:47.662616Z","iopub.status.idle":"2021-07-16T13:49:47.671519Z","shell.execute_reply.started":"2021-07-16T13:49:47.662571Z","shell.execute_reply":"2021-07-16T13:49:47.670586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RSNADataset(tf.keras.utils.Sequence):\n    def __init__(self, study, target, batch_size=8,test=0):\n        self.study, self.target = study, target\n        self.batch_size = batch_size\n        self.test = test\n\n    def __len__(self):\n        return int(np.ceil(len(self.study) / float(self.batch_size)))\n\n    def __getitem__(self, idx):\n        \n        if self.test==0:\n            batch_x = self.study[idx * self.batch_size:(idx + 1) * self.batch_size]\n            FLAIR_ARRAY = get_array(batch_x,'FLAIR','train')\n            T1W_ARRAY = get_array(batch_x,'T1w','train')\n            T1WCE_ARRAY = get_array(batch_x,'T1wCE','train')\n            T2W_ARRAY = get_array(batch_x,'T2w','train')\n            batch_y = self.target[idx * self.batch_size:(idx + 1) * self.batch_size]\n            \n            return [FLAIR_ARRAY,T1W_ARRAY,T1WCE_ARRAY,T2W_ARRAY],[np.array(batch_y)]\n        \n        \n        else:\n            batch_x = self.study[idx * self.batch_size:(idx + 1) * self.batch_size]\n            FLAIR_ARRAY = get_array(batch_x,'FLAIR','test')\n            T1W_ARRAY = get_array(batch_x,'T1w','test')\n            T1WCE_ARRAY = get_array(batch_x,'T1wCE','test')\n            T2W_ARRAY = get_array(batch_x,'T2w','test')\n            \n            return [FLAIR_ARRAY,T1W_ARRAY,T1WCE_ARRAY,T2W_ARRAY]","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.673987Z","iopub.execute_input":"2021-07-16T13:49:47.674368Z","iopub.status.idle":"2021-07-16T13:49:47.687438Z","shell.execute_reply.started":"2021-07-16T13:49:47.674326Z","shell.execute_reply":"2021-07-16T13:49:47.686378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.689273Z","iopub.execute_input":"2021-07-16T13:49:47.689845Z","iopub.status.idle":"2021-07-16T13:49:47.747894Z","shell.execute_reply.started":"2021-07-16T13:49:47.689802Z","shell.execute_reply":"2021-07-16T13:49:47.746782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['study'] = test['BraTS21ID'].apply(lambda x: '0'*(5-len(str(x))) + str(x))\ntest.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.749368Z","iopub.execute_input":"2021-07-16T13:49:47.749800Z","iopub.status.idle":"2021-07-16T13:49:47.767326Z","shell.execute_reply.started":"2021-07-16T13:49:47.749756Z","shell.execute_reply":"2021-07-16T13:49:47.765971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = RSNADataset(test.study.values,None,test=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.769352Z","iopub.execute_input":"2021-07-16T13:49:47.769877Z","iopub.status.idle":"2021-07-16T13:49:47.777607Z","shell.execute_reply.started":"2021-07-16T13:49:47.769829Z","shell.execute_reply":"2021-07-16T13:49:47.776340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"../input/rsna-radiogenomic-starter/model.h5\")\n\npreds = model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:49:47.779644Z","iopub.execute_input":"2021-07-16T13:49:47.780140Z","iopub.status.idle":"2021-07-16T13:51:21.201937Z","shell.execute_reply.started":"2021-07-16T13:49:47.780090Z","shell.execute_reply":"2021-07-16T13:51:21.200728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['MGMT_value'] = preds\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:51:21.205161Z","iopub.execute_input":"2021-07-16T13:51:21.205601Z","iopub.status.idle":"2021-07-16T13:51:21.221190Z","shell.execute_reply.started":"2021-07-16T13:51:21.205552Z","shell.execute_reply":"2021-07-16T13:51:21.219747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[['BraTS21ID','MGMT_value']].to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:51:21.223462Z","iopub.execute_input":"2021-07-16T13:51:21.224090Z","iopub.status.idle":"2021-07-16T13:51:21.245712Z","shell.execute_reply.started":"2021-07-16T13:51:21.224030Z","shell.execute_reply":"2021-07-16T13:51:21.244413Z"},"trusted":true},"execution_count":null,"outputs":[]}]}