{"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 pydicom\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport math\ndata_dir = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"\npatients = os.listdir(data_dir)\npatients.sort()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.082374Z","iopub.execute_input":"2021-10-14T19:42:38.082981Z","iopub.status.idle":"2021-10-14T19:42:38.090475Z","shell.execute_reply.started":"2021-10-14T19:42:38.082942Z","shell.execute_reply":"2021-10-14T19:42:38.089734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def average(l):\n    return sum(l)/len(l)\n    \ndef layers(l,n):\n    for i in range(0, len(l), n):\n        yield l[i:i + n]\n\nm2 = [22, 23, 24, 36, 37, 38, 39, 40, 50, 51, 52, 53, 54, 55, 56, 65, 66, 67, 68, 69, 70, 81, 82, 83, 84, 97, 98]\nm3 = [19, 20, 21, 25, 26, 33, 34, 35, 41, 42, 49]\nm4 = [18, 27, 28]\nm5 = [17]\n\ndef adjuster(file):\n    if len(file) in m5:\n        n = 5\n    elif len(file) in m4:\n        n = 4\n    elif len(file) in m3:\n        n = 3\n    elif len(file) in m2:\n        n = 2\n    else:\n        n = 1\n    new_file = []\n    for i in range(len(file)):\n        for j in range(n):\n            new_file.append(file[i])\n    return new_file       \n\ndef decider(length, layer_number):\n    return math.ceil(length/layer_number)\n   \ndef adjuster2(layers, layer_number):\n    if len(layers) == layer_number - 1:\n        layers.append(layers[-1])","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.092399Z","iopub.execute_input":"2021-10-14T19:42:38.092913Z","iopub.status.idle":"2021-10-14T19:42:38.104826Z","shell.execute_reply.started":"2021-10-14T19:42:38.092876Z","shell.execute_reply":"2021-10-14T19:42:38.104014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''layer_number=16\nc=1\nInput_Values = []\nfor p in patients[:]:\n    if p != '00109' and p != '00123' and p != '00709':\n        scan=[]\n        \n        flair_list = os.listdir(data_dir + p + '/FLAIR/')\n        flair_list.sort(key = lambda x : int(pydicom.read_file(data_dir + p + '/FLAIR/'+ x).ImagePositionPatient[2]))\n        flair = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/FLAIR/'+ layer).pixel_array,(64,64)) \n            for layer in flair_list ]\n\n        new_flair=[]\n        flair = adjuster(flair)\n        layer_size = decider(len(flair) , layer_number)\n        for layer_chunk in layers(flair, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_flair.append(layer_chunk)\n        adjuster2(new_flair, layer_number)\n\n        T1w_list = os.listdir(data_dir + p + '/T1w/')\n        T1w_list.sort(key = lambda x : int(pydicom.read_file(data_dir + p + '/T1w/'+ x).ImagePositionPatient[2]))\n        T1w = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T1w/'+ layer).pixel_array,(64,64)) \n            for layer in T1w_list]\n\n        new_T1w = []\n        T1w=adjuster(T1w)\n        layer_size = decider(len(T1w) , layer_number)\n        for layer_chunk in layers(T1w, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T1w.append(layer_chunk)\n        adjuster2(new_T1w, layer_number)\n\n        T1wCE_list = os.listdir(data_dir + p + '/T1wCE/')\n        T1wCE_list.sort(key = lambda x : int(pydicom.read_file(data_dir + p + '/T1wCE/'+ x).ImagePositionPatient[2]))\n        T1wCE = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T1wCE/'+ layer).pixel_array,(64,64)) \n            for layer in T1wCE_list]\n\n        new_T1wCE = []\n        T1wCE=adjuster(T1wCE)\n        layer_size = decider(len(T1wCE) , layer_number)\n        for layer_chunk in layers(T1wCE, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T1wCE.append(layer_chunk)\n\n        adjuster2(new_T1wCE, layer_number)\n\n        T2w_list = os.listdir(data_dir + p + '/T2w/')\n        T2w_list.sort(key = lambda x : int(pydicom.read_file(data_dir + p + '/T2w/'+ x).ImagePositionPatient[2]))\n        T2w = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T2w/'+ layer).pixel_array,(64,64)) \n            for layer in T2w_list]\n\n        new_T2w=[]\n        T2w=adjuster(T2w)\n        layer_size = decider(len(T2w) , layer_number)\n        for layer_chunk in layers(T2w, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T2w.append(layer_chunk)\n\n        adjuster2(new_T2w, layer_number)\n\n        for i in range(0,layer_number):\n            x=[]\n            for j in range(64):\n                y =[]\n                for k in range(64):\n                    channel=[]\n                    channel.append(new_flair[i][j][k])\n                    channel.append(new_T1w[i][j][k])\n                    channel.append(new_T1wCE[i][j][k])\n                    channel.append(new_T2w[i][j][k])\n                    y.append(channel)\n                x.append(y)\n            scan.append(np.array(x))\n        Input_Values.append(scan)\n        print(str(c))\n        c+=1\n\nnp.shape(Input_Values)'''","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.106078Z","iopub.execute_input":"2021-10-14T19:42:38.106592Z","iopub.status.idle":"2021-10-14T19:42:38.118450Z","shell.execute_reply.started":"2021-10-14T19:42:38.106557Z","shell.execute_reply":"2021-10-14T19:42:38.117680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\ndf = df.drop([71,81,488])\nOutput = df['MGMT_value']\n\nOutput_Values = []\nfor i in range(585):\n    if i != 71 and i != 81 and i != 488:\n        Output_Values.append(Output[i])\nOutput_Values = np.reshape(Output_Values,(582,1))\nprint(np.shape(Output_Values))","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.119789Z","iopub.execute_input":"2021-10-14T19:42:38.120208Z","iopub.status.idle":"2021-10-14T19:42:38.138216Z","shell.execute_reply.started":"2021-10-14T19:42:38.120176Z","shell.execute_reply":"2021-10-14T19:42:38.137426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Input_Values = np.load('../input/rsna-competition-2/Input_Values.npy')\n#np.save('./Input_Values',Input_Values)\nInput_Values = np.array(Input_Values)\nOutput_Values = np.array(Output_Values)\nprint(np.shape(Input_Values))\nprint(np.shape(Output_Values))","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.140030Z","iopub.execute_input":"2021-10-14T19:42:38.140279Z","iopub.status.idle":"2021-10-14T19:42:38.997318Z","shell.execute_reply.started":"2021-10-14T19:42:38.140239Z","shell.execute_reply":"2021-10-14T19:42:38.996569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Conv3D, MaxPooling3D, BatchNormalization, Activation, Dropout, Flatten, Dense, GlobalAveragePooling3D, LeakyReLU\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.regularizers import l2, l1\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:38.998524Z","iopub.execute_input":"2021-10-14T19:42:38.999198Z","iopub.status.idle":"2021-10-14T19:42:43.439688Z","shell.execute_reply.started":"2021-10-14T19:42:38.999162Z","shell.execute_reply":"2021-10-14T19:42:43.438992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv3D(32, (3, 3, 3), activation = \"relu\", input_shape=(16, 64, 64, 4), kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))\nmodel.add(LeakyReLU(alpha=0.1))\nmodel.add(MaxPooling3D(pool_size=(2, 3, 3)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Conv3D(64, (3, 3, 3), activation = \"relu\", kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))\nmodel.add(LeakyReLU(alpha=0.1))\nmodel.add(MaxPooling3D(pool_size=(1, 2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n#model.add(Conv3D(64, (3, 3, 3), activation = \"relu\", kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))\n#model.add(LeakyReLU(alpha=0.1))\n#model.add(MaxPooling3D(pool_size=(1, 2, 2)))\n#model.add(BatchNormalization())\n#model.add(Dropout(0.5))\n\n#model.add(Conv3D(64, (2, 3, 3), activation = \"relu\",kernel_regularizer=l2(0.001), bias_regularizer=l2(0.001)))\n#model.add(MaxPooling3D(pool_size=(1, 2, 2)))\n#model.add(BatchNormalization())\n#model.add(Dropout(0.5))\n#model.add(Conv3D(128, (2, 3, 3), activation = \"relu\",kernel_regularizer=l2(0.001), bias_regularizer=l2(0.001)))\n#model.add(MaxPooling3D(pool_size=(1, 2, 2)))\n#model.add(BatchNormalization())\n#model.add(Dropout(0.5))\nmodel.add(GlobalAveragePooling3D())\nmodel.add(Dense(64, activation = \"relu\" ,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation = \"sigmoid\" ))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:43.443097Z","iopub.execute_input":"2021-10-14T19:42:43.443307Z","iopub.status.idle":"2021-10-14T19:42:45.535095Z","shell.execute_reply.started":"2021-10-14T19:42:43.443283Z","shell.execute_reply":"2021-10-14T19:42:45.534417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile( loss='binary_crossentropy' , metrics=['accuracy'], optimizer=opt)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:45.536968Z","iopub.execute_input":"2021-10-14T19:42:45.537476Z","iopub.status.idle":"2021-10-14T19:42:45.551948Z","shell.execute_reply.started":"2021-10-14T19:42:45.537438Z","shell.execute_reply":"2021-10-14T19:42:45.551241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(Input_Values,Output_Values, epochs=45, batch_size=32, shuffle = 'true')\n\nprint(history.history.keys())\n\nplt.plot(history.history['accuracy'])\n#plt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\n#plt.legend(['Training set', 'Validation set'], loc='upper left')\nplt.show()\n\nplt.plot(history.history['loss'])\n#plt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\n#plt.legend(['Training set', 'Validation set'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:43:00.206061Z","iopub.execute_input":"2021-10-14T19:43:00.206912Z","iopub.status.idle":"2021-10-14T19:44:51.823507Z","shell.execute_reply.started":"2021-10-14T19:43:00.206864Z","shell.execute_reply":"2021-10-14T19:44:51.822786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.save('./Model6')","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:55.096025Z","iopub.status.idle":"2021-10-14T19:42:55.096477Z","shell.execute_reply.started":"2021-10-14T19:42:55.096220Z","shell.execute_reply":"2021-10-14T19:42:55.096258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/\"\ntest = os.listdir(test_dir)\ntest.sort()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:45:52.872318Z","iopub.execute_input":"2021-10-14T19:45:52.872577Z","iopub.status.idle":"2021-10-14T19:45:52.890660Z","shell.execute_reply.started":"2021-10-14T19:45:52.872551Z","shell.execute_reply":"2021-10-14T19:45:52.889986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer_number=16\nc=1\nTest_Values = []\nfor p in test[:]:\n    scan=[]\n\n    flair_list = os.listdir(test_dir + p + '/FLAIR/')\n    flair_list.sort(key = lambda x : int(pydicom.read_file(test_dir + p + '/FLAIR/'+ x).ImagePositionPatient[2]))\n    flair = [\n        cv2.resize(pydicom.read_file(test_dir + p + '/FLAIR/'+ layer).pixel_array,(64,64)) \n        for layer in flair_list ]\n\n    new_flair=[]\n    flair = adjuster(flair)\n    layer_size = decider(len(flair) , layer_number)\n    for layer_chunk in layers(flair, layer_size):\n        layer_chunk = list(map(average, zip(*layer_chunk)))\n        new_flair.append(layer_chunk)\n    adjuster2(new_flair, layer_number)\n\n    T1w_list = os.listdir(test_dir + p + '/T1w/')\n    T1w_list.sort(key = lambda x : int(pydicom.read_file(test_dir + p + '/T1w/'+ x).ImagePositionPatient[2]))\n    T1w = [\n        cv2.resize(pydicom.read_file(test_dir + p + '/T1w/'+ layer).pixel_array,(64,64)) \n        for layer in T1w_list]\n\n    new_T1w = []\n    T1w=adjuster(T1w)\n    layer_size = decider(len(T1w) , layer_number)\n    for layer_chunk in layers(T1w, layer_size):\n        layer_chunk = list(map(average, zip(*layer_chunk)))\n        new_T1w.append(layer_chunk)\n    adjuster2(new_T1w, layer_number)\n\n    T1wCE_list = os.listdir(test_dir + p + '/T1wCE/')\n    T1wCE_list.sort(key = lambda x : int(pydicom.read_file(test_dir + p + '/T1wCE/'+ x).ImagePositionPatient[2]))\n    T1wCE = [\n        cv2.resize(pydicom.read_file(test_dir + p + '/T1wCE/'+ layer).pixel_array,(64,64)) \n        for layer in T1wCE_list]\n\n    new_T1wCE = []\n    T1wCE=adjuster(T1wCE)\n    layer_size = decider(len(T1wCE) , layer_number)\n    for layer_chunk in layers(T1wCE, layer_size):\n        layer_chunk = list(map(average, zip(*layer_chunk)))\n        new_T1wCE.append(layer_chunk)\n\n    adjuster2(new_T1wCE, layer_number)\n\n    T2w_list = os.listdir(test_dir + p + '/T2w/')\n    T2w_list.sort(key = lambda x : int(pydicom.read_file(test_dir + p + '/T2w/'+ x).ImagePositionPatient[2]))\n    T2w = [\n        cv2.resize(pydicom.read_file(test_dir + p + '/T2w/'+ layer).pixel_array,(64,64)) \n        for layer in T2w_list]\n\n    new_T2w=[]\n    T2w=adjuster(T2w)\n    layer_size = decider(len(T2w) , layer_number)\n    for layer_chunk in layers(T2w, layer_size):\n        layer_chunk = list(map(average, zip(*layer_chunk)))\n        new_T2w.append(layer_chunk)\n\n    adjuster2(new_T2w, layer_number)\n\n    for i in range(0,layer_number):\n        x=[]\n        for j in range(64):\n            y =[]\n            for k in range(64):\n                channel=[]\n                channel.append(new_flair[i][j][k])\n                channel.append(new_T1w[i][j][k])\n                channel.append(new_T1wCE[i][j][k])\n                channel.append(new_T2w[i][j][k])\n                y.append(channel)\n            x.append(y)\n        scan.append(np.array(x))\n    Test_Values.append(scan)\n    print(str(c))\n    c+=1\n\nTest_Values = np.array(Test_Values)\nnp.shape(Test_Values)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:42:55.100015Z","iopub.status.idle":"2021-10-14T19:42:55.100589Z","shell.execute_reply.started":"2021-10-14T19:42:55.100361Z","shell.execute_reply":"2021-10-14T19:42:55.100383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#np.save('./Test_Values',Test_Values)\n#Test_Values = np.load('../input/rsna-competition-2/Test_Values.npy')","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:45:56.288492Z","iopub.execute_input":"2021-10-14T19:45:56.288764Z","iopub.status.idle":"2021-10-14T19:45:58.922972Z","shell.execute_reply.started":"2021-10-14T19:45:56.288735Z","shell.execute_reply":"2021-10-14T19:45:58.922191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Results = model.predict(Test_Values)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:46:01.463252Z","iopub.execute_input":"2021-10-14T19:46:01.463755Z","iopub.status.idle":"2021-10-14T19:46:01.822544Z","shell.execute_reply.started":"2021-10-14T19:46:01.463694Z","shell.execute_reply":"2021-10-14T19:46:01.821791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(columns=['BraTS21ID','MGMT_value'])\nsub.loc[:,'BraTS21ID'] = test\nsub.loc[:,'MGMT_value'] = Results\nsub.set_index('BraTS21ID', inplace = True)\nsub.to_csv('./submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-14T19:46:03.939010Z","iopub.execute_input":"2021-10-14T19:46:03.939545Z","iopub.status.idle":"2021-10-14T19:46:03.954343Z","shell.execute_reply.started":"2021-10-14T19:46:03.939508Z","shell.execute_reply":"2021-10-14T19:46:03.953503Z"},"trusted":true},"execution_count":null,"outputs":[]}]}