{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":164364362,"sourceType":"kernelVersion"},{"sourceId":164831366,"sourceType":"kernelVersion"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Purpose\n\nIt will be interesting to know which part of spectrogram is the key for diagnosis.\n\nFirst, I created CNN model in another notebook [HMS-CNN-Spectrogram-Short\n](https://www.kaggle.com/code/hidetaketakahashi/hms-cnn-spectrogram-short/notebook). This model is trained by only 48rows of Spectrogram of LL, RL, LP or RP (randomly selected). \n\nThen, I predicted probability of diagnosis from Spectrogram. ","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport time\nimport math","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-29T14:58:12.385596Z","iopub.execute_input":"2024-02-29T14:58:12.385964Z","iopub.status.idle":"2024-02-29T14:58:13.595818Z","shell.execute_reply.started":"2024-02-29T14:58:12.385935Z","shell.execute_reply":"2024-02-29T14:58:13.594604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras import backend as K\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:13.598063Z","iopub.execute_input":"2024-02-29T14:58:13.598577Z","iopub.status.idle":"2024-02-29T14:58:29.742200Z","shell.execute_reply.started":"2024-02-29T14:58:13.598545Z","shell.execute_reply":"2024-02-29T14:58:29.741109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#output of other notebook\ntrain_df_all = pd.read_csv(\"/kaggle/input/hms-eda-confidence-of-diagnosis/hms_train_eda.csv\")\n\ntest_df  = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/test.csv\")\nsample_df = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:29.743736Z","iopub.execute_input":"2024-02-29T14:58:29.744454Z","iopub.status.idle":"2024-02-29T14:58:30.187668Z","shell.execute_reply.started":"2024-02-29T14:58:29.744410Z","shell.execute_reply":"2024-02-29T14:58:30.185838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_spectro_dir = \"/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/\"\ntrain_spectro_files = os.listdir(train_spectro_dir)\nlen(train_spectro_files)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.191009Z","iopub.execute_input":"2024-02-29T14:58:30.191488Z","iopub.status.idle":"2024-02-29T14:58:30.423655Z","shell.execute_reply.started":"2024-02-29T14:58:30.191451Z","shell.execute_reply":"2024-02-29T14:58:30.422037Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split data for train and validation\n\nData should not split randomly, because there are multiple columns for a patient. First, unique patients IDs are identified. Then, patient IDs are splitted for train and validation.","metadata":{}},{"cell_type":"markdown","source":"**Note**\n\nThis split is consistent with notebook for modeling [HMS-CNN-Spectrogram-Short](https://www.kaggle.com/code/hidetaketakahashi/hms-cnn-spectrogram-short)","metadata":{}},{"cell_type":"code","source":"patient_all = train_df_all[\"patient_id\"].unique()\nnp.random.seed(1) #Same as another notebook\nnp.random.shuffle(patient_all)\n\nn_patient = patient_all.shape[0]\nn_patient_train = int(n_patient*0.85)\nn_patient_val = n_patient - n_patient_train\nn_patient, n_patient_train, n_patient_val\n\npatient_train = set(patient_all[:n_patient_train])\npatient_val = set(patient_all[n_patient_train:])\n\n\nfilter1 = train_df_all[\"patient_id\"].apply(lambda x: x in patient_train)\ntrain_df = train_df_all.loc[filter1]\nval_df = train_df_all.loc[~filter1]","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.425362Z","iopub.execute_input":"2024-02-29T14:58:30.425793Z","iopub.status.idle":"2024-02-29T14:58:30.526537Z","shell.execute_reply.started":"2024-02-29T14:58:30.425761Z","shell.execute_reply":"2024-02-29T14:58:30.525055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Cleaning\n\nData which satisfy following conditions are selected for developing ML model.\n\n- total vote is greater than or equal to 3\n- This vote is 100% confident. (all votes has the same diagnosis)\n\nThose preprocess are described in a notebook [HMS-EDA_Confidence_Of_Diagnosis](https://www.kaggle.com/code/hidetaketakahashi/hms-eda-confidence-of-diagnosis).","metadata":{}},{"cell_type":"code","source":"filter1 = (train_df[\"total_vote\"] >= 3) & (train_df[\"max_vote_rate\"] >= 1) #& (train_df[\"SpectrogramWithSingleSymtom\"])\n\ntrain_df_clean = train_df.loc[filter1] \ntrain_df_mess = train_df.loc[~filter1] \n\nfilter1 = (val_df[\"total_vote\"] >= 3) & (val_df[\"max_vote_rate\"] >= 1)# & (val_df[\"SpectrogramWithSingleSymtom\"])\n\nval_df_clean = val_df.loc[filter1] \nval_df_mess = val_df.loc[~filter1] ","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.528129Z","iopub.execute_input":"2024-02-29T14:58:30.528552Z","iopub.status.idle":"2024-02-29T14:58:30.555045Z","shell.execute_reply.started":"2024-02-29T14:58:30.528519Z","shell.execute_reply":"2024-02-29T14:58:30.553753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_clean.shape, val_df_clean.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.556578Z","iopub.execute_input":"2024-02-29T14:58:30.557082Z","iopub.status.idle":"2024-02-29T14:58:30.564470Z","shell.execute_reply.started":"2024-02-29T14:58:30.557034Z","shell.execute_reply":"2024-02-29T14:58:30.563170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"select = [\"spectrogram_id\", \"expert_consensus\"]\ntrain_df_clean_unique = train_df_clean[select].value_counts().reset_index()      \nval_df_clean_unique = val_df_clean[select].value_counts().reset_index()      ","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:25:01.765431Z","iopub.execute_input":"2024-02-29T15:25:01.765879Z","iopub.status.idle":"2024-02-29T15:25:01.793616Z","shell.execute_reply.started":"2024-02-29T15:25:01.765844Z","shell.execute_reply":"2024-02-29T15:25:01.791646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_clean_unique.shape, val_df_clean_unique.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:25:00.261391Z","iopub.execute_input":"2024-02-29T15:25:00.261816Z","iopub.status.idle":"2024-02-29T15:25:00.270584Z","shell.execute_reply.started":"2024-02-29T15:25:00.261783Z","shell.execute_reply":"2024-02-29T15:25:00.269277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_index_dict = {\"Seizure\":0, \"LPD\":1, \"GPD\":2, \"LRDA\":3, \"GRDA\":4, \"Other\":5}\nlabel_index_dict\n\nlabel_name_dict = {}\nfor key in label_index_dict.keys():\n    val = label_index_dict[key]\n    label_name_dict[val] = key\n    \nlabel_name_dict","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.615825Z","iopub.execute_input":"2024-02-29T14:58:30.616357Z","iopub.status.idle":"2024-02-29T14:58:30.629166Z","shell.execute_reply.started":"2024-02-29T14:58:30.616323Z","shell.execute_reply":"2024-02-29T14:58:30.627844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def readSpectrogram(fileID, test = False, dropTime = True):\n    fid = str(fileID)\n    \n    if test:\n        spectro_dir = \"/kaggle/input/hms-harmful-brain-activity-classification/test_spectrograms/\"\n        data = pd.read_parquet(spectro_dir + fid + \".parquet\")\n        \n    else:\n        spectro_dir = \"/kaggle/input/hms-harmful-brain-activity-classification/train_spectrograms/\"\n        data = pd.read_parquet(spectro_dir + fid + \".parquet\")\n    \n    if dropTime:\n        return data.drop(\"time\", axis = 1).to_numpy()\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.630770Z","iopub.execute_input":"2024-02-29T14:58:30.631116Z","iopub.status.idle":"2024-02-29T14:58:30.641961Z","shell.execute_reply.started":"2024-02-29T14:58:30.631088Z","shell.execute_reply":"2024-02-29T14:58:30.640798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SpectrogramLoader:\n    \n    def __init__(self):\n        self.shuffle = True\n        self.scaling = True\n        self.L = 48\n        self.H = 96\n        self.impute_value = 0\n        self.selectChannel = True\n        \n    def imputation(self, data):\n        filter1 = np.isnan(data)\n        \n        if np.mean(filter1) > 0.1:\n            return False\n        data[filter1] = self.impute_value\n        return True\n        \n    def SecToIndex(self, sec):\n        \n        return max(int(int(sec)/2), 0)\n    \n    def cropData300(self, data, sec):\n        \n        idxOffset = self.SecToIndex(sec)\n        data = data[idxOffset:(idxOffset+300)]\n        \n        L1 = min(data.shape[0], 300)\n        \n        if L1 < 300 - 10:\n            return data, False\n        \n        return data, True\n        \n    \n    def cropDataShort(self, data):\n        \n        L1 = data.shape[0]\n        start = 0\n        if L1 > self.L:\n            start = np.random.randint(L1 - self.L)\n\n        end = start + self.L\n        data =  data[start:end]\n        \n        return data\n    \n    def reshapeTo4Channels(self, data):\n        \n        mat = np.zeros((data.shape[0], self.H, 4))\n        \n        starts = [0,100,200,300] \n        for i in range(4):\n            mat[:,:,i] = data[:,starts[i]:(starts[i] + self.H)]\n\n        return mat\n        \n    def shuffleChannel(self, mat):\n        \n        idx_array = np.arange(4)\n        np.random.shuffle(idx_array)\n        \n        return mat[:,:,idx_array]\n    \n    def selectChannelRandom(self, mat):\n        \n        channel = np.random.randint(4)\n        \n        return mat[:,:,channel].reshape((self.L, self.H, 1))\n        \n            \n    def loadSpectrogram(self, spectID, offsetSec):\n        \n        data = readSpectrogram(spectID)\n        \n        data, crop_result = self.cropData300(data, offsetSec)\n        if crop_result is False:\n            return None\n\n        if self.scaling:\n            #data = data/np.max(data)\n            #data = data/np.quantile(data, 0.995)\n            data = data/1000\n            filter1 = data > 1\n            data[filter1] = 1\n                \n        data = self.cropDataShort(data)\n                \n        data = self.reshapeTo4Channels(data)\n\n        impute_result = self.imputation(data)\n        \n        if impute_result is False:\n            return None\n        \n        \n        if self.selectChannel:\n            return self.selectChannelRandom(data)\n            \n        return data\n        \n    \n    def loadSpectrogramForAnalysis(self, spectID, offsetSec):\n        \n        data = readSpectrogram(spectID)\n        \n        data, crop_result = self.cropData300(data, offsetSec)\n        \n        if crop_result is False:\n            return None\n\n        if self.scaling:\n            #data = data/np.max(data)\n            #data = data/np.quantile(data, 0.995)\n            data = data/1000\n            filter1 = data > 1\n            data[filter1] = 1\n                \n        data = self.reshapeTo4Channels(data)\n\n        impute_result = self.imputation(data)\n        \n        if impute_result is False:\n            return None\n\n        return data","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.643676Z","iopub.execute_input":"2024-02-29T14:58:30.644196Z","iopub.status.idle":"2024-02-29T14:58:30.671340Z","shell.execute_reply.started":"2024-02-29T14:58:30.644160Z","shell.execute_reply":"2024-02-29T14:58:30.669864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ConvBlock(X, channel, ksizes, psizes, pstrides, max_pool = True):\n    \n    X = layers.Conv2D(channel, kernel_size = ksizes, strides = 1, padding = \"same\")(X)        \n    X = layers.BatchNormalization()(X)\n    X = layers.ReLU()(X)\n    if max_pool:\n        X =  layers.MaxPooling2D(pool_size = psizes, strides = pstrides)(X)    \n    \n    return X","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.672777Z","iopub.execute_input":"2024-02-29T14:58:30.673116Z","iopub.status.idle":"2024-02-29T14:58:30.694476Z","shell.execute_reply.started":"2024-02-29T14:58:30.673089Z","shell.execute_reply":"2024-02-29T14:58:30.692808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ConvLine(X, n_output = 6):\n    \n#    X = layers.Rescaling(scale = 1./100., offset= 0, )(X)\n\n    X = ConvBlock(X, 64, (3,3), (2,2), (2,2)) # 48 x 96\n    X = ConvBlock(X, 64*2, (3,3), (2,2), (2,2)) #24 x 48\n    X = ConvBlock(X, 64*4, (3,3), (2,2), (2,2)) #12 x 24\n    X = ConvBlock(X, 64*8, (3,3), (2,2), (2,2)) #6 x 12\n    X = ConvBlock(X, 64*8, (3,3), (2,2), (2,2)) #3 x 6\n    \n    return X","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.696066Z","iopub.execute_input":"2024-02-29T14:58:30.696462Z","iopub.status.idle":"2024-02-29T14:58:30.713136Z","shell.execute_reply.started":"2024-02-29T14:58:30.696422Z","shell.execute_reply":"2024-02-29T14:58:30.711623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def createModel(n_output = 6):\n\n    Input =  layers.Input(shape=(48, 96,1))\n    X = ConvLine(Input, n_output)\n    X = layers.Flatten()(X)\n    X = layers.Dropout(0.3)(X)\n    X = layers.Dense(n_output)(X)\n    \n\n    model = Model(inputs = Input, outputs = X)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.715621Z","iopub.execute_input":"2024-02-29T14:58:30.716860Z","iopub.status.idle":"2024-02-29T14:58:30.725324Z","shell.execute_reply.started":"2024-02-29T14:58:30.716809Z","shell.execute_reply":"2024-02-29T14:58:30.724485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_Spect = createModel()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:30.727106Z","iopub.execute_input":"2024-02-29T14:58:30.727929Z","iopub.status.idle":"2024-02-29T14:58:31.095609Z","shell.execute_reply.started":"2024-02-29T14:58:30.727884Z","shell.execute_reply":"2024-02-29T14:58:31.094539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_Spect.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:31.097048Z","iopub.execute_input":"2024-02-29T14:58:31.097495Z","iopub.status.idle":"2024-02-29T14:58:31.185330Z","shell.execute_reply.started":"2024-02-29T14:58:31.097458Z","shell.execute_reply":"2024-02-29T14:58:31.183854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read model from another notebook","metadata":{}},{"cell_type":"code","source":"fpath_model = \"/kaggle/input/hms-cnn-spectrogram-short/HMS_CNN1/ckpt1\"\nmodel_Spect.load_weights(fpath_model)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:31.188574Z","iopub.execute_input":"2024-02-29T14:58:31.188982Z","iopub.status.idle":"2024-02-29T14:58:31.432318Z","shell.execute_reply.started":"2024-02-29T14:58:31.188949Z","shell.execute_reply":"2024-02-29T14:58:31.430832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_probability(X, model):\n\n    pred = model.predict(X, verbose = 0).astype(np.float64)\n    odd = np.exp(pred)\n    prob = odd/(1+odd)\n    \n    prob = prob/(np.sum(prob, axis = 1).reshape(-1, 1))\n    \n    return prob","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:31.900355Z","iopub.execute_input":"2024-02-29T14:58:31.901356Z","iopub.status.idle":"2024-02-29T14:58:31.907514Z","shell.execute_reply.started":"2024-02-29T14:58:31.901316Z","shell.execute_reply":"2024-02-29T14:58:31.906324Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SL = SpectrogramLoader()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:33.188687Z","iopub.execute_input":"2024-02-29T14:58:33.189056Z","iopub.status.idle":"2024-02-29T14:58:33.193930Z","shell.execute_reply.started":"2024-02-29T14:58:33.189027Z","shell.execute_reply":"2024-02-29T14:58:33.192202Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def readDataForAnalysis(df, label):\n    \n    label_name = label_name_dict[label]\n    \n    filter1 = df[\"expert_consensus\"] == label_name\n    \n    df_sample = df.loc[filter1].sample(1)\n    sid = df_sample[\"spectrogram_id\"].iloc[0]\n    secOffset = df_sample[\"spectrogram_label_offset_seconds\"].iloc[0]\n    \n    return SL.loadSpectrogramForAnalysis(sid, secOffset)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:33.487543Z","iopub.execute_input":"2024-02-29T14:58:33.487985Z","iopub.status.idle":"2024-02-29T14:58:33.494981Z","shell.execute_reply.started":"2024-02-29T14:58:33.487949Z","shell.execute_reply":"2024-02-29T14:58:33.493739Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"code","source":"def predictVal(label_id, allchannel = False):\n\n    spect = None\n    while spect is None:\n        spect = readDataForAnalysis(val_df_clean, label_id)\n\n    yhat_channel = []\n    for channel in range(4):\n        X_val = spect[:,:,channel].reshape((1, 300, 96, 1))\n        yhat_list = []\n        for i in range(12):\n            start = 24*i + 0\n            end = start + 48\n            yhat_val = predict_probability(X_val[:,start:end], model_Spect)[0]\n            \n            if allchannel:\n                yhat_list.append(yhat_val)\n            else:\n                yhat_list.append(yhat_val[label_id])\n\n        yhat_channel.append(yhat_list)\n        \n    \n    return spect, yhat_channel","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:06:18.423547Z","iopub.execute_input":"2024-02-29T15:06:18.423929Z","iopub.status.idle":"2024-02-29T15:06:18.433660Z","shell.execute_reply.started":"2024-02-29T15:06:18.423897Z","shell.execute_reply":"2024-02-29T15:06:18.432399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = np.random.randint(train_df.shape[0])\nspect_id = train_df[\"spectrogram_id\"].iloc[idx]\ntrain_spect = readSpectrogram(spect_id, False, False)\ntrain_spect.drop(\"time\", axis = 1, inplace = True)\nHz = list(map(lambda x: float(x.split(\"_\")[-1]), list(train_spect.columns[0:100])))","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:35.186417Z","iopub.execute_input":"2024-02-29T14:58:35.186802Z","iopub.status.idle":"2024-02-29T14:58:35.386586Z","shell.execute_reply.started":"2024-02-29T14:58:35.186766Z","shell.execute_reply":"2024-02-29T14:58:35.385612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_Spect(spect, type1):\n    title_list = [\"LL\", \"RL\", \"LP\", \"RP\"]\n    fig, ax = plt.subplots(4, figsize = (10, 10))\n    for i in range(4):\n        ax[i].pcolormesh(np.arange(spect.shape[0]), Hz[0:96],np.transpose(spect[:,:,i]), shading='gouraud')\n        ax[i].set_title(type1 + \"  \" + title_list[i] )\n        \n        for j in range(6):\n            v_linex = 48*j + 6\n            ax[i].axvline(x = v_linex, color = \"red\", ls = \"-.\", alpha = 0.7)\n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:26:14.898757Z","iopub.execute_input":"2024-02-29T15:26:14.899205Z","iopub.status.idle":"2024-02-29T15:26:14.909154Z","shell.execute_reply.started":"2024-02-29T15:26:14.899176Z","shell.execute_reply":"2024-02-29T15:26:14.907645Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_Result(spect, yhat_channel, label): #NO USE\n    \n    title_list = [\"LL\", \"RL\", \"LP\", \"RP\"]\n    label_name = label_name_dict[label]\n    fig, ax = plt.subplots(8, figsize = (10, 15))\n    \n    \n    for k in range(4):\n        \n        i = 2*k\n        j = 2*k+1\n        ax[i].pcolormesh(np.arange(spect.shape[0]), Hz[0:96],np.transpose(spect[:,:,k]), shading='gouraud')\n        ax[j].plot(yhat_channel[k], marker = \"x\")\n        ax[j].set_ylim(0, 1)\n        \n        ax[i].set_ylabel(title_list[k])\n        ax[j].set_ylabel(label_name + \" prob\")\n        \n        for i2 in range(6):\n            v_linex = 24*i2 + 0\n            ax[i].axvline(x = v_linex, color = \"red\", ls = \"-.\", alpha = 0.7)\n        \n    ax[0].set_title(label_name)\n        \n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:35.985487Z","iopub.execute_input":"2024-02-29T14:58:35.985880Z","iopub.status.idle":"2024-02-29T14:58:35.999540Z","shell.execute_reply.started":"2024-02-29T14:58:35.985852Z","shell.execute_reply":"2024-02-29T14:58:35.997284Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_Result(spect, yhat_channel, label):\n    \n    title_list = [\"LL\", \"RL\", \"LP\", \"RP\"]\n    label_name = label_name_dict[label]\n    fig, ax = plt.subplots(5, figsize = (10, 10))\n    \n    \n    for i in range(4):\n        ax[i].pcolormesh(np.arange(spect.shape[0]), Hz[0:96],np.transpose(spect[:,:,i]), shading='gouraud')\n        ax[i].set_ylabel(title_list[i])\n        for i2 in range(12):\n            v_linex = 24*i2 + 0\n            ax[i].axvline(x = v_linex, color = \"red\", ls = \"dotted\", alpha = 0.5, lw = 1)\n        \n    ax[0].set_title(label_name)\n    \n    for i in range(4):\n        ax[4].plot(yhat_channel[i], marker = \"x\", label = title_list[i])\n        \n    ax[4].legend()\n    ax[4].set_ylabel(label_name + \" prob(pred)\")\n    ax[4].set_ylim(0, 1)\n    ax[4].grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:36.318839Z","iopub.execute_input":"2024-02-29T14:58:36.319457Z","iopub.status.idle":"2024-02-29T14:58:36.338459Z","shell.execute_reply.started":"2024-02-29T14:58:36.319409Z","shell.execute_reply":"2024-02-29T14:58:36.335335Z"},"_kg_hide-input":true,"_kg_hide-output":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotSamplePrediction(label1):\n    np.random.seed(2)\n    for i in range(5):\n        spect1, yhat_channel1 = predictVal(label1)\n        plot_Result(spect1, yhat_channel1, label1)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T14:58:36.820586Z","iopub.execute_input":"2024-02-29T14:58:36.820988Z","iopub.status.idle":"2024-02-29T14:58:36.826703Z","shell.execute_reply.started":"2024-02-29T14:58:36.820957Z","shell.execute_reply":"2024-02-29T14:58:36.825434Z"},"_kg_hide-input":true,"scrolled":true,"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_Result_all(spect, yhat_channel, label): #NO USE\n    \n    title_list = [\"LL\", \"RL\", \"LP\", \"RP\"]\n    label_name = label_name_dict[label]\n    fig, ax = plt.subplots(8, figsize = (10, 20))\n    \n    \n    for k in range(4):\n        \n        i = 2*k\n        j = 2*k+1\n        ax[i].pcolormesh(np.arange(spect.shape[0]), Hz[0:96],np.transpose(spect[:,:,k]), shading='gouraud')\n        \n        for m in range(6):\n            ax[j].plot(np.array(yhat_channel[k]).T[m], marker = \"x\", label = label_name_dict[m])\n            \n        ax[j].set_ylim(0, 1)\n        ax[j].grid()\n        ax[i].set_ylabel(title_list[k])\n        ax[j].set_ylabel(\"Prediction\")\n        \n        for i2 in range(12):\n            v_linex = 24*i2 + 0\n            ax[i].axvline(x = v_linex, color = \"red\", ls = \"dotted\", alpha = 0.6, lw = 1)\n        \n    ax[0].set_title(label_name)\n    ax[1].legend()\n    ax[5].legend()\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:26:54.796624Z","iopub.execute_input":"2024-02-29T15:26:54.797036Z","iopub.status.idle":"2024-02-29T15:26:54.810523Z","shell.execute_reply.started":"2024-02-29T15:26:54.797006Z","shell.execute_reply":"2024-02-29T15:26:54.809284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotSamplePrediction_all(label1):\n    np.random.seed(2)\n    for i in range(3):\n        spect1, yhat_channel1 = predictVal(label1, True)\n        plot_Result_all(spect1, yhat_channel1, label1)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:26:55.298300Z","iopub.execute_input":"2024-02-29T15:26:55.298702Z","iopub.status.idle":"2024-02-29T15:26:55.306172Z","shell.execute_reply.started":"2024-02-29T15:26:55.298673Z","shell.execute_reply":"2024-02-29T15:26:55.304336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Seizure","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(0)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:26:56.173945Z","iopub.execute_input":"2024-02-29T15:26:56.174392Z","iopub.status.idle":"2024-02-29T15:27:16.341804Z","shell.execute_reply.started":"2024-02-29T15:26:56.174360Z","shell.execute_reply":"2024-02-29T15:27:16.340523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LPD","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(1)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:27:16.344186Z","iopub.execute_input":"2024-02-29T15:27:16.345515Z","iopub.status.idle":"2024-02-29T15:27:37.430521Z","shell.execute_reply.started":"2024-02-29T15:27:16.345466Z","shell.execute_reply":"2024-02-29T15:27:37.429279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GPD","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(2)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:27:37.432013Z","iopub.execute_input":"2024-02-29T15:27:37.432411Z","iopub.status.idle":"2024-02-29T15:27:58.057970Z","shell.execute_reply.started":"2024-02-29T15:27:37.432379Z","shell.execute_reply":"2024-02-29T15:27:58.057032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LRDA","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(3)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:27:58.060349Z","iopub.execute_input":"2024-02-29T15:27:58.061297Z","iopub.status.idle":"2024-02-29T15:28:18.552607Z","shell.execute_reply.started":"2024-02-29T15:27:58.061260Z","shell.execute_reply":"2024-02-29T15:28:18.551262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GRDA","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(4)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:28:18.554260Z","iopub.execute_input":"2024-02-29T15:28:18.554714Z","iopub.status.idle":"2024-02-29T15:28:39.152287Z","shell.execute_reply.started":"2024-02-29T15:28:18.554679Z","shell.execute_reply":"2024-02-29T15:28:39.150846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Other","metadata":{}},{"cell_type":"code","source":"plotSamplePrediction_all(5)","metadata":{"execution":{"iopub.status.busy":"2024-02-29T15:28:39.153721Z","iopub.execute_input":"2024-02-29T15:28:39.154194Z","iopub.status.idle":"2024-02-29T15:28:59.875661Z","shell.execute_reply.started":"2024-02-29T15:28:39.154157Z","shell.execute_reply":"2024-02-29T15:28:59.874203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"jupyter":{"source_hidden":true}},"execution_count":null,"outputs":[]}]}