{"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 json\nimport glob\nimport random\nimport collections\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:25.601100Z","iopub.execute_input":"2023-06-20T20:08:25.601482Z","iopub.status.idle":"2023-06-20T20:08:33.871910Z","shell.execute_reply.started":"2023-06-20T20:08:25.601450Z","shell.execute_reply":"2023-06-20T20:08:33.870996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the training data from the CSV file\ntrain_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\n\n# Display the training data\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:33.873861Z","iopub.execute_input":"2023-06-20T20:08:33.874485Z","iopub.status.idle":"2023-06-20T20:08:33.900152Z","shell.execute_reply.started":"2023-06-20T20:08:33.874452Z","shell.execute_reply":"2023-06-20T20:08:33.899113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the figure size and create a count plot of the MGMT_value variable\nplt.figure(figsize=(5, 5))\nsns.countplot(data=train_df, x=\"MGMT_value\")","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:33.901779Z","iopub.execute_input":"2023-06-20T20:08:33.902144Z","iopub.status.idle":"2023-06-20T20:08:34.127241Z","shell.execute_reply.started":"2023-06-20T20:08:33.902109Z","shell.execute_reply":"2023-06-20T20:08:34.126328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a function to load DICOM files\ndef load_dicom_file(path):\n    dicom_file = pydicom.read_file(path)\n    pixel_array = dicom_file.pixel_array\n    pixel_array = pixel_array - np.min(pixel_array)\n    if np.max(pixel_array) != 0:\n        pixel_array = pixel_array / np.max(pixel_array)\n    pixel_array = (pixel_array * 255).astype(np.uint8)\n    return pixel_array\n\n# Define a function to visualize a sample of brain tumor radiogenomic data\ndef visualize_sample(brats21id, slice_i, mgmt_value, types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")):\n    plt.figure(figsize=(16, 5))\n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\", \n        str(brats21id).zfill(5),\n    )\n    for i, t in enumerate(types, 1):\n        t_paths = sorted(\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),\n        )\n        pixel_array = load_dicom_file(t_paths[int(len(t_paths) * slice_i)])\n        plt.subplot(1, 4, i)\n        plt.imshow(pixel_array, cmap=\"gray\")\n        plt.title(f\"{t}\", fontsize=16)\n        plt.axis(\"off\")\n\n    plt.suptitle(f\"MGMT_value: {mgmt_value}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:34.128904Z","iopub.execute_input":"2023-06-20T20:08:34.129561Z","iopub.status.idle":"2023-06-20T20:08:34.139995Z","shell.execute_reply.started":"2023-06-20T20:08:34.129526Z","shell.execute_reply":"2023-06-20T20:08:34.139119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select a random sample of 10 brain tumor radiogenomic images from the training dataset\nrandom_indices = random.sample(range(train_df.shape[0]), 10)\n\n# Visualize each sample using the `visualize_sample()` function\nfor i in random_indices:\n    brats21id = train_df.iloc[i][\"BraTS21ID\"]\n    mgmt_value = train_df.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=brats21id, mgmt_value=mgmt_value, slice_i=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:34.143619Z","iopub.execute_input":"2023-06-20T20:08:34.143973Z","iopub.status.idle":"2023-06-20T20:08:40.689459Z","shell.execute_reply.started":"2023-06-20T20:08:34.143923Z","shell.execute_reply":"2023-06-20T20:08:40.688605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap=\"gray\")\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:40.690821Z","iopub.execute_input":"2023-06-20T20:08:40.691866Z","iopub.status.idle":"2023-06-20T20:08:40.703511Z","shell.execute_reply.started":"2023-06-20T20:08:40.691830Z","shell.execute_reply":"2023-06-20T20:08:40.702612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_images(path):\n    \"\"\"\n    Loads a sequence of DICOM images from a directory.\n\n    Args:\n    - path: The path to the directory containing the DICOM files.\n\n    Returns:\n    - A list of 2D arrays representing the DICOM images.\n    \"\"\"\n    # Get the paths to the DICOM files and sort them by slice number\n    dicom_paths = sorted(\n        glob.glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n\n    # Load the DICOM images into a list\n    images = []\n    for path in dicom_paths:\n        image = load_dicom_file(path)\n        if image.max() != 0:\n            images.append(image)\n\n    return images","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:40.704885Z","iopub.execute_input":"2023-06-20T20:08:40.705381Z","iopub.status.idle":"2023-06-20T20:08:40.714475Z","shell.execute_reply.started":"2023-06-20T20:08:40.705347Z","shell.execute_reply":"2023-06-20T20:08:40.713527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:08:40.716024Z","iopub.execute_input":"2023-06-20T20:08:40.716512Z","iopub.status.idle":"2023-06-20T20:09:04.924308Z","shell.execute_reply.started":"2023-06-20T20:08:40.716480Z","shell.execute_reply":"2023-06-20T20:09:04.923098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:04.926265Z","iopub.execute_input":"2023-06-20T20:09:04.927019Z","iopub.status.idle":"2023-06-20T20:09:07.555421Z","shell.execute_reply.started":"2023-06-20T20:09:04.926970Z","shell.execute_reply":"2023-06-20T20:09:07.554515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = load_dicom_images(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE\")\ncreate_animation(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:07.557177Z","iopub.execute_input":"2023-06-20T20:09:07.557817Z","iopub.status.idle":"2023-06-20T20:09:14.830019Z","shell.execute_reply.started":"2023-06-20T20:09:07.557769Z","shell.execute_reply":"2023-06-20T20:09:14.829062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\ndef plot_brain_3d(directory):\n    # Load all DICOM files in the directory\n    files = sorted([os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.dcm')], key=lambda path: int(os.path.splitext(os.path.basename(path))[0].split('-')[-1]))\n\n    print(\"File count: {}\".format(len(files)))\n\n    # Load the DICOM files into memory and skip those with no SliceLocation attribute\n    slices = []\n    skipcount = 0\n    for fname in files:\n        ds = pydicom.dcmread(fname)\n        if hasattr(ds, 'SliceLocation'):\n            slices.append(ds)\n        else:\n            skipcount += 1\n    print(\"Skipped, no SliceLocation: {}\".format(skipcount))\n\n    # Sort the slices based on their SliceLocation attribute\n    slices = sorted(slices, key=lambda s: s.SliceLocation)\n\n    # Calculate pixel aspect ratios for each view\n    ps = slices[0].PixelSpacing\n    ss = slices[0].SliceThickness\n    ax_aspect = ps[1]/ps[0]\n    sag_aspect = ps[1]/ss\n    cor_aspect = ss/ps[0]\n\n    # Create a 3D array to store the image data\n    img_shape = list(slices[0].pixel_array.shape)\n    img_shape.append(len(slices))\n    img3d = np.zeros(img_shape)\n\n    # Fill the 3D array with the pixel data from each slice\n    for i, s in enumerate(slices):\n        img2d = s.pixel_array\n        img3d[:, :, i] = img2d\n\n    # Plot the 3 orthogonal views\n    fig, axes = plt.subplots(1, 3, figsize=(25, 25))\n    fig.suptitle(\"Brain CT Scan in three different views (Horizontal, Sagittal, Coronal)\", fontsize=24)\n\n    axes[0].imshow(img3d[:, :, img_shape[2]//2], cmap='hot')\n    axes[0].set_aspect(ax_aspect)\n\n    axes[1].imshow(img3d[:, img_shape[1]//2, :], cmap='hot')\n    axes[1].set_aspect(sag_aspect)\n\n    axes[2].imshow(img3d[img_shape[0]//2, :, :].T, cmap='hot')\n    axes[2].set_aspect(cor_aspect)\n\n    plt.show()\n\n# Example usage: plot 3D brain scans in the directory '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR/'\ndirectory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00006/FLAIR/'\nplot_brain_3d(directory)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:14.831898Z","iopub.execute_input":"2023-06-20T20:09:14.832544Z","iopub.status.idle":"2023-06-20T20:09:17.248262Z","shell.execute_reply.started":"2023-06-20T20:09:14.832509Z","shell.execute_reply":"2023-06-20T20:09:17.247255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/')\nmri_types = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nthreshold = 10\nexc = [109, 123, 709]","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.249774Z","iopub.execute_input":"2023-06-20T20:09:17.250422Z","iopub.status.idle":"2023-06-20T20:09:17.256160Z","shell.execute_reply.started":"2023-06-20T20:09:17.250375Z","shell.execute_reply":"2023-06-20T20:09:17.255177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.257659Z","iopub.execute_input":"2023-06-20T20:09:17.258300Z","iopub.status.idle":"2023-06-20T20:09:17.278040Z","shell.execute_reply.started":"2023-06-20T20:09:17.258265Z","shell.execute_reply":"2023-06-20T20:09:17.277075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\ntest_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.283154Z","iopub.execute_input":"2023-06-20T20:09:17.284858Z","iopub.status.idle":"2023-06-20T20:09:17.301009Z","shell.execute_reply.started":"2023-06-20T20:09:17.284832Z","shell.execute_reply":"2023-06-20T20:09:17.300191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[~train_df.BraTS21ID.isin(exc)]\ntrain_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.302125Z","iopub.execute_input":"2023-06-20T20:09:17.302455Z","iopub.status.idle":"2023-06-20T20:09:17.313700Z","shell.execute_reply.started":"2023-06-20T20:09:17.302422Z","shell.execute_reply":"2023-06-20T20:09:17.312588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dicom image, standardizes so that pixel values are between 0 and 1\n# Rescale to 0 and 255\ndef dicomImage(path, size = 224):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\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))\n\n# Returns an arry of all the images of a particular type for a particular patient ID\ndef allImagePath(brats21id, image_type, folder='train'): \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 [dicomImage(path, size) for path in allImagePath(brats21id, image_type, folder)]","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.315323Z","iopub.execute_input":"2023-06-20T20:09:17.315976Z","iopub.status.idle":"2023-06-20T20:09:17.327662Z","shell.execute_reply.started":"2023-06-20T20:09:17.315942Z","shell.execute_reply":"2023-06-20T20:09:17.326622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgaeSize = 128\n\ndef get_all_data_for_train(image_type):\n    global train_df\n    var1 = []\n    var2 = []\n    train_ids = []\n\n    for i in tqdm(train_df.index):\n        x = train_df.loc[i]\n        allImages = get_all_images(int(x['BraTS21ID']), image_type, 'train', imgaeSize)\n        label = x['MGMT_value']\n        var1 += allImages\n        var2 += [label] * len(allImages)\n        train_ids += [int(x['BraTS21ID'])] * len(allImages)\n        assert(len(var1) == len(var2))\n    return np.array(var1), np.array(var2), np.array(train_ids)\n\ndef get_all_data_for_test(image_type):\n    global test_df\n    var1 = []\n    test_ids = []\n\n    for i in tqdm(test_df.index):\n        x = test_df.loc[i]\n        allImages = get_all_images(int(x['BraTS21ID']), image_type, 'test', imgaeSize)\n        var1 += allImages\n        test_ids += [int(x['BraTS21ID'])] * len(allImages)\n\n    return np.array(var1), np.array(test_ids)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.331299Z","iopub.execute_input":"2023-06-20T20:09:17.331598Z","iopub.status.idle":"2023-06-20T20:09:17.343429Z","shell.execute_reply.started":"2023-06-20T20:09:17.331573Z","shell.execute_reply":"2023-06-20T20:09:17.341978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, Y, train_idt = get_all_data_for_train('T1wCE')\nX_test, test_idt = get_all_data_for_test('T1wCE')\nX.shape, Y.shape, train_idt.shape, test_idt.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:09:17.344778Z","iopub.execute_input":"2023-06-20T20:09:17.345941Z","iopub.status.idle":"2023-06-20T20:11:40.897051Z","shell.execute_reply.started":"2023-06-20T20:09:17.345865Z","shell.execute_reply":"2023-06-20T20:11:40.895906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, Y.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:40.898578Z","iopub.execute_input":"2023-06-20T20:11:40.899077Z","iopub.status.idle":"2023-06-20T20:11:40.906631Z","shell.execute_reply.started":"2023-06-20T20:11:40.899039Z","shell.execute_reply":"2023-06-20T20:11:40.905357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid, trainidt_train, trainidt_valid = train_test_split(X, Y, train_idt, test_size=0.1, random_state=140)\nsplit = int(X.shape[0] * 0.9)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:40.908257Z","iopub.execute_input":"2023-06-20T20:11:40.908679Z","iopub.status.idle":"2023-06-20T20:11:40.996664Z","shell.execute_reply.started":"2023-06-20T20:11:40.908593Z","shell.execute_reply":"2023-06-20T20:11:40.995688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = tf.expand_dims(X_train, axis = -1)\nX_valid = tf.expand_dims(X_valid, axis = -1)\n\ny_train = to_categorical(y_train)\ny_valid = to_categorical(y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:40.998111Z","iopub.execute_input":"2023-06-20T20:11:40.998480Z","iopub.status.idle":"2023-06-20T20:11:43.789256Z","shell.execute_reply.started":"2023-06-20T20:11:40.998445Z","shell.execute_reply":"2023-06-20T20:11:43.788208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape, trainidt_train.shape, trainidt_valid.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:43.790518Z","iopub.execute_input":"2023-06-20T20:11:43.790868Z","iopub.status.idle":"2023-06-20T20:11:43.799111Z","shell.execute_reply.started":"2023-06-20T20:11:43.790836Z","shell.execute_reply":"2023-06-20T20:11:43.798118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(0)\nrandom.seed(12)\ntf.random.set_seed(12)\n\ninputShape = keras.Input(shape = X_train.shape[1:])\n\npreProcessing = keras.layers.experimental.preprocessing.Rescaling(1./255)(inputShape)\n# h = augmentation(h)\n\n# convolutional layer!\nl = keras.layers.Conv2D(32, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_1\", padding=\"valid\")(preProcessing) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.Conv2D(32, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_1b\", padding=\"same\")(l) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.MaxPool2D(pool_size=(2,2))(l)\nl = keras.layers.Conv2D(64, kernel_size=(3, 3),strides=(1,1), activation=\"relu\", name=\"Conv_2\", padding=\"valid\")(l) \nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\n\n# pooling layer\nl = keras.layers.MaxPool2D(pool_size=(2,2))(l)\nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\n\n# convolutional layer!\nl = keras.layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\", name=\"Conv_3\",padding =\"same\")(l)\n\n# pooling layer\nl = tf.keras.layers.BatchNormalization(axis=-1)(l)\nl = keras.layers.Dropout(0.4)(l)\nl = keras.layers.Flatten()(l)\nl1 = keras.layers.experimental.preprocessing.Rescaling(1./255)(preProcessing)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Conv2D(32, kernel_size=(3, 3), activation=\"swish\", name=\"Conv_4b\")(l1)\nl1 = tf.keras.layers.BatchNormalization(axis=-1)(l1)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Dropout(0.3)(l1)\nl1 = keras.layers.Conv2D(32, kernel_size=(3, 3), activation=\"swish\", name=\"Conv_5b\")(l1)\nl1 = tf.keras.layers.BatchNormalization(axis=-1)(l1)\nl1 =  keras.layers.MaxPool2D(strides=(5,5))(l1)\nl1 = keras.layers.Dropout(0.3)(l1)\nl1 = keras.layers.Flatten()(l1)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:43.800378Z","iopub.execute_input":"2023-06-20T20:11:43.801471Z","iopub.status.idle":"2023-06-20T20:11:44.044715Z","shell.execute_reply.started":"2023-06-20T20:11:43.801435Z","shell.execute_reply":"2023-06-20T20:11:44.043819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge = keras.layers.Concatenate()([l,l1])\ndropOut = keras.layers.Dropout(0.4)(merge)\ndropOut = keras.layers.Dense(150, activation='relu')(dropOut)  \noutPut = keras.layers.Dense(2, activation=\"softmax\")(dropOut)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:44.045938Z","iopub.execute_input":"2023-06-20T20:11:44.046254Z","iopub.status.idle":"2023-06-20T20:11:44.087759Z","shell.execute_reply.started":"2023-06-20T20:11:44.046224Z","shell.execute_reply":"2023-06-20T20:11:44.086936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Model(inputShape, outPut)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:44.089129Z","iopub.execute_input":"2023-06-20T20:11:44.089454Z","iopub.status.idle":"2023-06-20T20:11:44.102101Z","shell.execute_reply.started":"2023-06-20T20:11:44.089423Z","shell.execute_reply":"2023-06-20T20:11:44.101081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\n\nfilepathCheckpoint = 'best_model.h5'\n\nmodelCallbackCheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath = filepathCheckpoint,\n    save_weights_only = False,\n    monitor = 'val_auc',\n    mode = 'max',\n    save_best_only = True,\n    save_freq = 'epoch'\n)\n\nmodel.compile(\n    loss = 'categorical_crossentropy',\n    optimizer = tf.keras.optimizers.SGD(learning_rate =0.0001),\n    metrics = [tf.keras.metrics.AUC()]\n)\n\nhistory = model.fit(x = X_train, y = y_train, epochs = 100, callbacks = [modelCallbackCheckpoint], validation_data = (X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:11:44.103363Z","iopub.execute_input":"2023-06-20T20:11:44.103788Z","iopub.status.idle":"2023-06-20T20:35:34.618721Z","shell.execute_reply.started":"2023-06-20T20:11:44.103756Z","shell.execute_reply":"2023-06-20T20:35:34.617608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = tf.keras.models.load_model(filepath = filepathCheckpoint)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:35:34.620388Z","iopub.execute_input":"2023-06-20T20:35:34.621274Z","iopub.status.idle":"2023-06-20T20:35:35.248529Z","shell.execute_reply.started":"2023-06-20T20:35:34.621234Z","shell.execute_reply":"2023-06-20T20:35:35.247527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model1.predict(X_valid)\n\npred = np.argmax(y_pred, axis = 1)\n\nresult = pd.DataFrame(trainidt_valid)\nresult[1] = pred\n\nresult.columns = ['BraTS21ID','MGMT_value']\nresult2 = result.groupby('BraTS21ID', as_index = False).mean()\n\nresult2 = result2.merge(train_df, on = 'BraTS21ID')\nroc_auc_score(result2.MGMT_value_y, result2.MGMT_value_x,)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:35:35.249958Z","iopub.execute_input":"2023-06-20T20:35:35.250320Z","iopub.status.idle":"2023-06-20T20:35:36.175588Z","shell.execute_reply.started":"2023-06-20T20:35:35.250284Z","shell.execute_reply":"2023-06-20T20:35:36.174586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(result2)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:35:36.176857Z","iopub.execute_input":"2023-06-20T20:35:36.177192Z","iopub.status.idle":"2023-06-20T20:35:36.185950Z","shell.execute_reply.started":"2023-06-20T20:35:36.177159Z","shell.execute_reply":"2023-06-20T20:35:36.184650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import SGD\n\nfilepathCheckpoint = 'best_model.h5'\n\nmodelCallbackCheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    filepath = filepathCheckpoint,\n    save_weights_only = False,\n    monitor = 'val_auc',\n    mode = 'max',\n    save_best_only = True,\n    save_freq = 'epoch'\n)\n\nmodel.compile(\n    loss = 'categorical_crossentropy',\n    optimizer = tf.keras.optimizers.SGD(learning_rate =0.0001),\n    metrics = [tf.keras.metrics.AUC()]\n)\nmodel.summary()\n#history = model.fit(x = X_train, y = y_train, epochs = 100, callbacks = [modelCallbackCheckpoint], validation_data = (X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:38:39.157843Z","iopub.execute_input":"2023-06-20T20:38:39.158588Z","iopub.status.idle":"2023-06-20T20:38:39.235518Z","shell.execute_reply.started":"2023-06-20T20:38:39.158552Z","shell.execute_reply":"2023-06-20T20:38:39.234826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nplot_model(model, to_file='model.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T20:39:21.524583Z","iopub.execute_input":"2023-06-20T20:39:21.525217Z","iopub.status.idle":"2023-06-20T20:39:21.876464Z","shell.execute_reply.started":"2023-06-20T20:39:21.525182Z","shell.execute_reply":"2023-06-20T20:39:21.875626Z"},"trusted":true},"execution_count":null,"outputs":[]}]}