{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Sahel-Eskandar/Project-Notebooks/blob/main/3_nonEEG_Psychological_State_Classification.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "BfUJk0wHwdy4"
      },
      "outputs": [],
      "source": [
        "#!pip install --target=$nb_path hmmlearn --upgrade\n",
        "#!pip install --target=$nb_path hyperopt --upgrade\n",
        "#!pip install --target=$nb_path wfdb --upgrade\n",
        "#!pip install --target=$nb_path seaborn --upgrade"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "rZOZkXf2bEpJ"
      },
      "outputs": [],
      "source": [
        "OUTPUT_PATH='/Users/saheleskandar/0Research/Output'\n",
        "INPUT_PATH1='/Users/saheleskandar/0Research/1609976651_A02F0E'\n",
        "pp='/Users/saheleskandar/0Research/'\n",
        "Training_Logs_File =pp+ 'training_logs_file.csv'\n",
        "Model_Summary_File =pp+ 'model_summary.txt'\n",
        "Model_File =pp+ 'DetectStatus.h5'\n",
        "log_dir=pp+'logs/fit/'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "id": "IYOJGtsw_z0j",
        "outputId": "a5a5aceb-5b90-4012-f01e-a01d7eec9a6b"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
            "  import pandas.util.testing as tm\n"
          ]
        }
      ],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "import tqdm\n",
        "from tqdm.notebook import tqdm_notebook as tqdm\n",
        "import time\n",
        "import pickle\n",
        "import datetime\n",
        "sns.set_style('whitegrid') # style\n",
        "# render plots inline\n",
        "%matplotlib inline\n",
        "import logging\n",
        "import itertools as it\n",
        "import os, sys\n",
        "import wfdb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZAzNwnWWwdzI"
      },
      "outputs": [],
      "source": [
        "from hmmlearn.hmm import GaussianHMM\n",
        "from statsmodels.graphics.tsaplots import plot_acf\n",
        "from scipy import fftpack, signal\n",
        "from sklearn.preprocessing import LabelEncoder, StandardScaler\n",
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.naive_bayes import GaussianNB\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.feature_selection import SelectKBest, f_classif\n",
        "from sklearn.model_selection import train_test_split, GridSearchCV\n",
        "from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, mean_squared_error\n",
        "from sklearn.decomposition import PCA\n",
        "from sklearn.preprocessing import MinMaxScaler, normalize\n",
        "from sklearn import preprocessing"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "id": "VqnglM_ewdzL",
        "outputId": "391f5c23-577d-43b2-ff42-0b5611732dcf"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "Using TensorFlow backend.\n"
          ]
        }
      ],
      "source": [
        "import tensorflow as tf\n",
        "import keras\n",
        "from keras.models import Sequential, load_model\n",
        "from keras.layers import Conv1D, LSTM, TimeDistributed, Dropout, Dense, Flatten, MaxPooling1D, Embedding\n",
        "from keras.callbacks import Callback, ModelCheckpoint, CSVLogger, EarlyStopping\n",
        "from keras import optimizers\n",
        "from keras.wrappers.scikit_learn import KerasClassifier\n",
        "from keras.utils.np_utils import to_categorical"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "zx6YiMKgniDb"
      },
      "outputs": [],
      "source": [
        "min_max_scaler = preprocessing.MinMaxScaler()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VNkkP11nZGuu"
      },
      "outputs": [],
      "source": [
        "for i in range(0,19):\n",
        "    #Testdata files\n",
        "    DataXt = pd.DataFrame()\n",
        "    Datayt = pd.DataFrame()\n",
        "    filename1 = './'+listAccTempEDA[i]\n",
        "    filename2 = './'+listSpO2HR[i]\n",
        "    filename3 = './'+Subject[i]+'.csv'\n",
        "    #Reading Data\n",
        "    ann = wfdb.rdann(filename1, extension='atr', summarize_labels=True)\n",
        "    record_acc_temp_eda = wfdb.rdrecord(filename1)\n",
        "    data_acc_temp_eda = record_acc_temp_eda.p_signal[:18000]\n",
        "    data_acc_temp_eda = min_max_scaler.fit_transform(data_acc_temp_eda)\n",
        "    record_spo2_hr = wfdb.rdrecord(filename2)\n",
        "    data_spo2_hr = record_spo2_hr.p_signal[:2250]\n",
        "    data_spo2_hr = min_max_scaler.fit_transform(data_spo2_hr)\n",
        "    \n",
        "    #Upsample data of Spo2_hr to matches frequency of Acc_Temp_EDA\n",
        "    per_second_index = pd.period_range(start='2020-04-01', periods=len(data_spo2_hr), freq='S')\n",
        "    df_spO2_hr = pd.DataFrame(data_spo2_hr, index=per_second_index, columns=record_spo2_hr.sig_name)\n",
        "    upsampled = df_spO2_hr.resample('125ms')\n",
        "    df_upsampled = upsampled.interpolate()\n",
        "       \n",
        "    #Indexing Acc_Temp_EDA\n",
        "    df_acc_temp_eda = pd.DataFrame(data_acc_temp_eda, columns=record_acc_temp_eda.sig_name)\n",
        "    df_acc_temp_eda.index = df_upsampled.index[:len(df_acc_temp_eda)]\n",
        "    df_upsampled.index = df_acc_temp_eda.index[:len(df_upsampled)]\n",
        "    df = pd.concat([df_acc_temp_eda, df_upsampled], axis=1).dropna() \n",
        "    \n",
        "    # align the labels with the row offsets    \n",
        "    df['label'] = ''\n",
        "    start = ann.sample - 1\n",
        "    end = ann.sample[1:] - 1\n",
        "    end[-1] = -1 \n",
        "    for s, e, label in zip(start, end, ann.aux_note):\n",
        "        df.loc[s:e, 'label'] = label\n",
        "    # set the final row's label to the second-last row's label\n",
        "    df.loc[df.index[-1], 'label'] = df.loc[df.index[-2], 'label']\n",
        "    # Label Encode\n",
        "    le = LabelEncoder()\n",
        "    df['label_enc'] = le.fit_transform(df['label'])\n",
        "    candidate_features = df.columns.drop(['ax', 'ay','az', 'label', 'label_enc'])\n",
        "    y_feature = df.columns.drop(['ax','ay','az','temp','EDA','SpO2','hr','label'])\n",
        "    label = df.columns.drop(['ax','ay','az','temp','EDA','SpO2','hr','label_enc'])\n",
        "        \n",
        "    DataXt = DataXt.append(df.loc[df.index[:],candidate_features], ignore_index=True)\n",
        "    Datayt  =Datayt.append(df.loc[df.index[:],label], ignore_index=True)\n",
        "    NEEGdatat = pd.concat([DataXt, Datayt], axis=1).dropna()\n",
        "    NEEGdatat.to_csv(filename3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MF8v_enTwdzh"
      },
      "source": [
        "# Hyper Parameter Tunning"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UH0u10VIG6QW"
      },
      "outputs": [],
      "source": [
        "def data(batch_size, time_steps):\n",
        "    global mat\n",
        "    BATCH_SIZE = batch_size\n",
        "    TIME_STEPS = time_steps\n",
        "    datawin=[]\n",
        "    labelwin=[]\n",
        "    datalabelwin=[]\n",
        "    for i in range(0,19):\n",
        "       filename3 = './'+Subject[i]+'.csv'  \n",
        "       dataS=pd.read_csv(filename3)\n",
        "       datax=dataS.iloc[:,1:-1].values\n",
        "       datay=dataS.iloc[:,-1].values\n",
        "       datax=np.array(datax)\n",
        "       datay=np.array(datay)\n",
        "       for i in range(0, 17900, TIME_STEPS):\n",
        "        datawin.append(datax[i:i+TIME_STEPS,:])\n",
        "        coe=int(TIME_STEPS/2)\n",
        "        labelwin.append(datay[i+coe])\n",
        "    datawin= np.array(datawin)\n",
        "    labelwin= np.array(labelwin)\n",
        "    labelwin=np.reshape(labelwin,(len(labelwin),1))\n",
        "    #Building dataframe of labels call dflabel for processing\n",
        "    dfy=pd.DataFrame(labelwin,columns=['label'])\n",
        "    count=dfy.iloc[:,0].value_counts()\n",
        "    minlabels=min(count)\n",
        "\n",
        "    num_of_categories = minlabels\n",
        "    Relax = datawin[dfy['label'] == 'Relax'][:num_of_categories]\n",
        "    LRelax = labelwin[dfy['label'] == 'Relax'][:num_of_categories]\n",
        "    EmotionalStress = datawin[dfy['label'] == 'EmotionalStress'][:num_of_categories]\n",
        "    LEmotionalStress = labelwin[dfy['label'] == 'EmotionalStress'][:num_of_categories]\n",
        "    CognitiveStress = datawin[dfy['label'] == 'CognitiveStress'][:num_of_categories]\n",
        "    LCognitiveStress = labelwin[dfy['label'] == 'CognitiveStress'][:num_of_categories]\n",
        "    PhysicalStress =datawin[dfy['label'] == 'PhysicalStress'][:num_of_categories]\n",
        "    LPhysicalStress =labelwin[dfy['label'] == 'PhysicalStress'][:num_of_categories]\n",
        "    ReEm=np.concatenate((Relax,EmotionalStress))\n",
        "    CoPh=np.concatenate((CognitiveStress,PhysicalStress))\n",
        "    data_X=np.concatenate((ReEm,CoPh))\n",
        "    LReEm=np.concatenate((LRelax,LEmotionalStress))\n",
        "    LCoPh=np.concatenate((LCognitiveStress,LPhysicalStress))\n",
        "    data_y=np.concatenate((LReEm,LCoPh))\n",
        "    perm = np.arange(data_X.shape[0])\n",
        "    np.random.shuffle(perm)\n",
        "    X = data_X[perm]\n",
        "    y =data_y[perm]\n",
        "    le = LabelEncoder()\n",
        "    label_enc = le.fit_transform(y)\n",
        "    label_enc=np.reshape(label_enc,(len(label_enc),1))\n",
        "    yall= to_categorical(label_enc, num_classes=4)\n",
        "    Xall=X\n",
        "    x_train_ts, x_test_ts, y_train_ts, y_test_ts = train_test_split(Xall, yall, test_size=0.2)\n",
        "    print(y[:5],label_enc[:5],yall[:5],yall.shape,Xall.shape)\n",
        "\n",
        "    return x_train_ts, y_train_ts, x_test_ts, y_test_ts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qHyLbGepTs9E"
      },
      "outputs": [],
      "source": [
        "batch_size= 30 \n",
        "epochs= 240\n",
        "time_steps= 20\n",
        "lstm1_dropouts= 0.026440582088985087\n",
        "lstm1_nodes= 20\n",
        "lstm2_dropouts= 0.217370653429103\n",
        "lstm2_nodes= 30\n",
        "lstm3_dropouts= 0.217370653429103\n",
        "lstm3_nodes= 30\n",
        "optimizer= 'adam'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 289
        },
        "id": "UQ3cc6Hnw6j-",
        "outputId": "f0331680-1177-4669-85b9-8c3603ec84f9"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[['Relax']\n",
            " ['Relax']\n",
            " ['CognitiveStress']\n",
            " ['EmotionalStress']\n",
            " ['EmotionalStress']] [[3]\n",
            " [3]\n",
            " [0]\n",
            " [1]\n",
            " [1]] [[0. 0. 0. 1.]\n",
            " [0. 0. 0. 1.]\n",
            " [1. 0. 0. 0.]\n",
            " [0. 1. 0. 0.]\n",
            " [0. 1. 0. 0.]] (9920, 4) (9920, 20, 4)\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/content/notebooks/sklearn/utils/validation.py:73: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  return f(**kwargs)\n"
          ]
        }
      ],
      "source": [
        "x_train_ts, y_train_ts, x_test_ts, y_test_ts= data(batch_size, time_steps)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "Z6zyeKVAUsS3",
        "outputId": "ace6abcc-3885-4b81-f6ea-a5cd7877b072"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Train on 6348 samples, validate on 1588 samples\n",
            "Epoch 1/240\n",
            " - 10s - loss: 1.0925 - categorical_accuracy: 0.5153 - val_loss: 0.8907 - val_categorical_accuracy: 0.6341\n",
            "Epoch 2/240\n",
            " - 9s - loss: 0.8645 - categorical_accuracy: 0.6572 - val_loss: 0.7775 - val_categorical_accuracy: 0.6940\n",
            "Epoch 3/240\n",
            " - 7s - loss: 0.8083 - categorical_accuracy: 0.6755 - val_loss: 0.7393 - val_categorical_accuracy: 0.7040\n",
            "Epoch 4/240\n",
            " - 7s - loss: 0.7817 - categorical_accuracy: 0.6905 - val_loss: 0.7260 - val_categorical_accuracy: 0.7380\n",
            "Epoch 5/240\n",
            " - 7s - loss: 0.7605 - categorical_accuracy: 0.6999 - val_loss: 0.7322 - val_categorical_accuracy: 0.7267\n",
            "Epoch 6/240\n",
            " - 7s - loss: 0.7447 - categorical_accuracy: 0.7072 - val_loss: 0.6678 - val_categorical_accuracy: 0.7519\n",
            "Epoch 7/240\n",
            " - 7s - loss: 0.7283 - categorical_accuracy: 0.7142 - val_loss: 0.6789 - val_categorical_accuracy: 0.7374\n",
            "Epoch 8/240\n",
            " - 7s - loss: 0.7274 - categorical_accuracy: 0.7135 - val_loss: 0.6660 - val_categorical_accuracy: 0.7361\n",
            "Epoch 9/240\n",
            " - 8s - loss: 0.7106 - categorical_accuracy: 0.7169 - val_loss: 0.6475 - val_categorical_accuracy: 0.7450\n",
            "Epoch 10/240\n",
            " - 9s - loss: 0.6980 - categorical_accuracy: 0.7224 - val_loss: 0.6487 - val_categorical_accuracy: 0.7506\n",
            "Epoch 11/240\n",
            " - 9s - loss: 0.6957 - categorical_accuracy: 0.7226 - val_loss: 0.6511 - val_categorical_accuracy: 0.7355\n",
            "Epoch 12/240\n",
            " - 9s - loss: 0.6994 - categorical_accuracy: 0.7234 - val_loss: 0.6338 - val_categorical_accuracy: 0.7500\n",
            "Epoch 13/240\n",
            " - 9s - loss: 0.6837 - categorical_accuracy: 0.7240 - val_loss: 0.6385 - val_categorical_accuracy: 0.7469\n",
            "Epoch 14/240\n",
            " - 9s - loss: 0.6824 - categorical_accuracy: 0.7257 - val_loss: 0.6030 - val_categorical_accuracy: 0.7664\n",
            "Epoch 15/240\n",
            " - 8s - loss: 0.6733 - categorical_accuracy: 0.7297 - val_loss: 0.6060 - val_categorical_accuracy: 0.7620\n",
            "Epoch 16/240\n",
            " - 8s - loss: 0.6722 - categorical_accuracy: 0.7341 - val_loss: 0.6223 - val_categorical_accuracy: 0.7714\n",
            "Epoch 17/240\n",
            " - 9s - loss: 0.6741 - categorical_accuracy: 0.7300 - val_loss: 0.6315 - val_categorical_accuracy: 0.7563\n",
            "Epoch 18/240\n",
            " - 7s - loss: 0.6701 - categorical_accuracy: 0.7284 - val_loss: 0.5984 - val_categorical_accuracy: 0.7733\n",
            "Epoch 19/240\n",
            " - 9s - loss: 0.6616 - categorical_accuracy: 0.7388 - val_loss: 0.6475 - val_categorical_accuracy: 0.7494\n",
            "Epoch 20/240\n",
            " - 9s - loss: 0.6611 - categorical_accuracy: 0.7360 - val_loss: 0.6488 - val_categorical_accuracy: 0.7399\n",
            "Epoch 21/240\n",
            " - 8s - loss: 0.6552 - categorical_accuracy: 0.7361 - val_loss: 0.5961 - val_categorical_accuracy: 0.7676\n",
            "Epoch 22/240\n",
            " - 9s - loss: 0.6517 - categorical_accuracy: 0.7417 - val_loss: 0.6060 - val_categorical_accuracy: 0.7607\n",
            "Epoch 23/240\n",
            " - 8s - loss: 0.6484 - categorical_accuracy: 0.7461 - val_loss: 0.5790 - val_categorical_accuracy: 0.7702\n",
            "Epoch 24/240\n",
            " - 8s - loss: 0.6433 - categorical_accuracy: 0.7431 - val_loss: 0.5890 - val_categorical_accuracy: 0.7733\n",
            "Epoch 25/240\n",
            " - 9s - loss: 0.6385 - categorical_accuracy: 0.7475 - val_loss: 0.6065 - val_categorical_accuracy: 0.7620\n",
            "Epoch 26/240\n",
            " - 8s - loss: 0.6400 - categorical_accuracy: 0.7451 - val_loss: 0.5634 - val_categorical_accuracy: 0.7853\n",
            "Epoch 27/240\n",
            " - 8s - loss: 0.6216 - categorical_accuracy: 0.7530 - val_loss: 0.5699 - val_categorical_accuracy: 0.7878\n",
            "Epoch 28/240\n",
            " - 9s - loss: 0.6294 - categorical_accuracy: 0.7468 - val_loss: 0.5669 - val_categorical_accuracy: 0.7645\n",
            "Epoch 29/240\n",
            " - 8s - loss: 0.6365 - categorical_accuracy: 0.7465 - val_loss: 0.5676 - val_categorical_accuracy: 0.7783\n",
            "Epoch 30/240\n",
            " - 8s - loss: 0.6291 - categorical_accuracy: 0.7522 - val_loss: 0.5766 - val_categorical_accuracy: 0.7771\n",
            "Epoch 31/240\n",
            " - 9s - loss: 0.6346 - categorical_accuracy: 0.7448 - val_loss: 0.5771 - val_categorical_accuracy: 0.7739\n",
            "Epoch 32/240\n",
            " - 8s - loss: 0.6182 - categorical_accuracy: 0.7560 - val_loss: 0.5457 - val_categorical_accuracy: 0.7821\n",
            "Epoch 33/240\n",
            " - 8s - loss: 0.6121 - categorical_accuracy: 0.7571 - val_loss: 0.5722 - val_categorical_accuracy: 0.7764\n",
            "Epoch 34/240\n",
            " - 7s - loss: 0.6103 - categorical_accuracy: 0.7588 - val_loss: 0.5399 - val_categorical_accuracy: 0.7928\n",
            "Epoch 35/240\n",
            " - 7s - loss: 0.6000 - categorical_accuracy: 0.7615 - val_loss: 0.5211 - val_categorical_accuracy: 0.8016\n",
            "Epoch 36/240\n",
            " - 7s - loss: 0.5981 - categorical_accuracy: 0.7675 - val_loss: 0.5424 - val_categorical_accuracy: 0.7997\n",
            "Epoch 37/240\n",
            " - 7s - loss: 0.5911 - categorical_accuracy: 0.7637 - val_loss: 0.5132 - val_categorical_accuracy: 0.8054\n",
            "Epoch 38/240\n",
            " - 7s - loss: 0.6056 - categorical_accuracy: 0.7595 - val_loss: 0.5263 - val_categorical_accuracy: 0.8004\n",
            "Epoch 39/240\n",
            " - 7s - loss: 0.6000 - categorical_accuracy: 0.7651 - val_loss: 0.5323 - val_categorical_accuracy: 0.8029\n",
            "Epoch 40/240\n",
            " - 8s - loss: 0.5957 - categorical_accuracy: 0.7637 - val_loss: 0.5556 - val_categorical_accuracy: 0.7903\n",
            "Epoch 41/240\n",
            " - 7s - loss: 0.5844 - categorical_accuracy: 0.7689 - val_loss: 0.5366 - val_categorical_accuracy: 0.7909\n",
            "Epoch 42/240\n",
            " - 8s - loss: 0.5811 - categorical_accuracy: 0.7733 - val_loss: 0.5151 - val_categorical_accuracy: 0.8098\n",
            "Epoch 43/240\n",
            " - 9s - loss: 0.5803 - categorical_accuracy: 0.7711 - val_loss: 0.5127 - val_categorical_accuracy: 0.8004\n",
            "Epoch 44/240\n",
            " - 9s - loss: 0.5761 - categorical_accuracy: 0.7750 - val_loss: 0.5066 - val_categorical_accuracy: 0.8073\n",
            "Epoch 45/240\n",
            " - 8s - loss: 0.5749 - categorical_accuracy: 0.7777 - val_loss: 0.5065 - val_categorical_accuracy: 0.8060\n",
            "Epoch 46/240\n",
            " - 8s - loss: 0.5800 - categorical_accuracy: 0.7765 - val_loss: 0.5746 - val_categorical_accuracy: 0.7702\n",
            "Epoch 47/240\n",
            " - 9s - loss: 0.5818 - categorical_accuracy: 0.7710 - val_loss: 0.5432 - val_categorical_accuracy: 0.7953\n",
            "Epoch 48/240\n",
            " - 8s - loss: 0.5748 - categorical_accuracy: 0.7739 - val_loss: 0.5081 - val_categorical_accuracy: 0.8029\n",
            "Epoch 49/240\n",
            " - 8s - loss: 0.5682 - categorical_accuracy: 0.7804 - val_loss: 0.5071 - val_categorical_accuracy: 0.8117\n",
            "Epoch 50/240\n",
            " - 8s - loss: 0.5643 - categorical_accuracy: 0.7815 - val_loss: 0.5095 - val_categorical_accuracy: 0.8149\n",
            "Epoch 51/240\n",
            " - 8s - loss: 0.5617 - categorical_accuracy: 0.7798 - val_loss: 0.5277 - val_categorical_accuracy: 0.7935\n",
            "Epoch 52/240\n",
            " - 8s - loss: 0.5602 - categorical_accuracy: 0.7873 - val_loss: 0.4821 - val_categorical_accuracy: 0.8230\n",
            "Epoch 53/240\n",
            " - 6s - loss: 0.5601 - categorical_accuracy: 0.7784 - val_loss: 0.5041 - val_categorical_accuracy: 0.8079\n",
            "Epoch 54/240\n",
            " - 7s - loss: 0.5666 - categorical_accuracy: 0.7761 - val_loss: 0.4975 - val_categorical_accuracy: 0.8079\n",
            "Epoch 55/240\n",
            " - 7s - loss: 0.5558 - categorical_accuracy: 0.7839 - val_loss: 0.4981 - val_categorical_accuracy: 0.8142\n",
            "Epoch 56/240\n",
            " - 7s - loss: 0.5606 - categorical_accuracy: 0.7826 - val_loss: 0.5021 - val_categorical_accuracy: 0.8073\n",
            "Epoch 57/240\n",
            " - 7s - loss: 0.5575 - categorical_accuracy: 0.7807 - val_loss: 0.5182 - val_categorical_accuracy: 0.8161\n",
            "Epoch 58/240\n",
            " - 7s - loss: 0.5451 - categorical_accuracy: 0.7892 - val_loss: 0.4815 - val_categorical_accuracy: 0.8293\n",
            "Epoch 59/240\n",
            " - 7s - loss: 0.5501 - categorical_accuracy: 0.7840 - val_loss: 0.4989 - val_categorical_accuracy: 0.8086\n",
            "Epoch 60/240\n",
            " - 7s - loss: 0.5426 - categorical_accuracy: 0.7851 - val_loss: 0.5035 - val_categorical_accuracy: 0.8136\n",
            "Epoch 61/240\n",
            " - 7s - loss: 0.5506 - categorical_accuracy: 0.7829 - val_loss: 0.4836 - val_categorical_accuracy: 0.8168\n",
            "Epoch 62/240\n",
            " - 7s - loss: 0.5501 - categorical_accuracy: 0.7876 - val_loss: 0.4864 - val_categorical_accuracy: 0.8149\n",
            "Epoch 63/240\n",
            " - 7s - loss: 0.5504 - categorical_accuracy: 0.7859 - val_loss: 0.4937 - val_categorical_accuracy: 0.8174\n",
            "Epoch 64/240\n",
            " - 7s - loss: 0.5410 - categorical_accuracy: 0.7848 - val_loss: 0.4784 - val_categorical_accuracy: 0.8230\n",
            "Epoch 65/240\n",
            " - 7s - loss: 0.5377 - categorical_accuracy: 0.7889 - val_loss: 0.4908 - val_categorical_accuracy: 0.8073\n",
            "Epoch 66/240\n",
            " - 7s - loss: 0.5442 - categorical_accuracy: 0.7869 - val_loss: 0.4673 - val_categorical_accuracy: 0.8275\n",
            "Epoch 67/240\n",
            " - 7s - loss: 0.5376 - categorical_accuracy: 0.7875 - val_loss: 0.4798 - val_categorical_accuracy: 0.8268\n",
            "Epoch 68/240\n",
            " - 7s - loss: 0.5397 - categorical_accuracy: 0.7875 - val_loss: 0.4858 - val_categorical_accuracy: 0.8275\n",
            "Epoch 69/240\n",
            " - 8s - loss: 0.5430 - categorical_accuracy: 0.7888 - val_loss: 0.5033 - val_categorical_accuracy: 0.8117\n",
            "Epoch 70/240\n",
            " - 7s - loss: 0.5301 - categorical_accuracy: 0.7913 - val_loss: 0.4879 - val_categorical_accuracy: 0.8035\n",
            "Epoch 71/240\n",
            " - 8s - loss: 0.5465 - categorical_accuracy: 0.7820 - val_loss: 0.5006 - val_categorical_accuracy: 0.8004\n",
            "Epoch 72/240\n",
            " - 8s - loss: 0.5278 - categorical_accuracy: 0.7954 - val_loss: 0.5108 - val_categorical_accuracy: 0.8079\n",
            "Epoch 73/240\n",
            " - 7s - loss: 0.5248 - categorical_accuracy: 0.7891 - val_loss: 0.4731 - val_categorical_accuracy: 0.8344\n",
            "Epoch 74/240\n",
            " - 7s - loss: 0.5246 - categorical_accuracy: 0.7932 - val_loss: 0.4631 - val_categorical_accuracy: 0.8256\n",
            "Epoch 75/240\n",
            " - 7s - loss: 0.5249 - categorical_accuracy: 0.7919 - val_loss: 0.4632 - val_categorical_accuracy: 0.8262\n",
            "Epoch 76/240\n",
            " - 7s - loss: 0.5257 - categorical_accuracy: 0.7895 - val_loss: 0.4608 - val_categorical_accuracy: 0.8344\n",
            "Epoch 77/240\n",
            " - 7s - loss: 0.5279 - categorical_accuracy: 0.7946 - val_loss: 0.4730 - val_categorical_accuracy: 0.8168\n",
            "Epoch 78/240\n",
            " - 7s - loss: 0.5237 - categorical_accuracy: 0.7922 - val_loss: 0.4781 - val_categorical_accuracy: 0.8205\n",
            "Epoch 79/240\n",
            " - 7s - loss: 0.5317 - categorical_accuracy: 0.7899 - val_loss: 0.4770 - val_categorical_accuracy: 0.8300\n",
            "Epoch 80/240\n",
            " - 7s - loss: 0.5172 - categorical_accuracy: 0.7968 - val_loss: 0.4611 - val_categorical_accuracy: 0.8268\n",
            "Epoch 81/240\n",
            " - 7s - loss: 0.5282 - categorical_accuracy: 0.7941 - val_loss: 0.4685 - val_categorical_accuracy: 0.8300\n",
            "Epoch 82/240\n",
            " - 7s - loss: 0.5064 - categorical_accuracy: 0.8004 - val_loss: 0.4588 - val_categorical_accuracy: 0.8249\n",
            "Epoch 83/240\n",
            " - 7s - loss: 0.5160 - categorical_accuracy: 0.7946 - val_loss: 0.4395 - val_categorical_accuracy: 0.8445\n",
            "Epoch 84/240\n",
            " - 7s - loss: 0.5166 - categorical_accuracy: 0.7952 - val_loss: 0.4558 - val_categorical_accuracy: 0.8275\n",
            "Epoch 85/240\n",
            " - 7s - loss: 0.5211 - categorical_accuracy: 0.7980 - val_loss: 0.4739 - val_categorical_accuracy: 0.8130\n",
            "Epoch 86/240\n",
            " - 7s - loss: 0.5224 - categorical_accuracy: 0.7924 - val_loss: 0.4426 - val_categorical_accuracy: 0.8363\n",
            "Epoch 87/240\n",
            " - 7s - loss: 0.5122 - categorical_accuracy: 0.7985 - val_loss: 0.4663 - val_categorical_accuracy: 0.8230\n",
            "Epoch 88/240\n",
            " - 7s - loss: 0.5062 - categorical_accuracy: 0.8026 - val_loss: 0.4819 - val_categorical_accuracy: 0.8142\n",
            "Epoch 89/240\n",
            " - 8s - loss: 0.5205 - categorical_accuracy: 0.7962 - val_loss: 0.4459 - val_categorical_accuracy: 0.8401\n",
            "Epoch 90/240\n",
            " - 7s - loss: 0.5054 - categorical_accuracy: 0.7965 - val_loss: 0.4793 - val_categorical_accuracy: 0.8230\n",
            "Epoch 91/240\n",
            " - 7s - loss: 0.5091 - categorical_accuracy: 0.8025 - val_loss: 0.4556 - val_categorical_accuracy: 0.8312\n",
            "Epoch 92/240\n",
            " - 8s - loss: 0.5063 - categorical_accuracy: 0.8010 - val_loss: 0.4639 - val_categorical_accuracy: 0.8382\n",
            "Epoch 93/240\n",
            " - 7s - loss: 0.5071 - categorical_accuracy: 0.8039 - val_loss: 0.4394 - val_categorical_accuracy: 0.8388\n",
            "Epoch 94/240\n",
            " - 7s - loss: 0.5082 - categorical_accuracy: 0.8017 - val_loss: 0.4569 - val_categorical_accuracy: 0.8300\n",
            "Epoch 95/240\n",
            " - 7s - loss: 0.5154 - categorical_accuracy: 0.7996 - val_loss: 0.4560 - val_categorical_accuracy: 0.8375\n",
            "Epoch 96/240\n",
            " - 7s - loss: 0.5064 - categorical_accuracy: 0.7985 - val_loss: 0.4662 - val_categorical_accuracy: 0.8243\n",
            "Epoch 97/240\n",
            " - 9s - loss: 0.4973 - categorical_accuracy: 0.8058 - val_loss: 0.4451 - val_categorical_accuracy: 0.8344\n",
            "Epoch 98/240\n",
            " - 8s - loss: 0.5059 - categorical_accuracy: 0.7932 - val_loss: 0.4354 - val_categorical_accuracy: 0.8463\n",
            "Epoch 99/240\n",
            " - 7s - loss: 0.5004 - categorical_accuracy: 0.8047 - val_loss: 0.4396 - val_categorical_accuracy: 0.8369\n",
            "Epoch 100/240\n",
            " - 7s - loss: 0.4869 - categorical_accuracy: 0.8047 - val_loss: 0.4221 - val_categorical_accuracy: 0.8438\n",
            "Epoch 101/240\n",
            " - 8s - loss: 0.5057 - categorical_accuracy: 0.7990 - val_loss: 0.4284 - val_categorical_accuracy: 0.8558\n",
            "Epoch 102/240\n",
            " - 8s - loss: 0.4894 - categorical_accuracy: 0.8037 - val_loss: 0.4397 - val_categorical_accuracy: 0.8300\n",
            "Epoch 103/240\n",
            " - 7s - loss: 0.4923 - categorical_accuracy: 0.8023 - val_loss: 0.4543 - val_categorical_accuracy: 0.8331\n",
            "Epoch 104/240\n",
            " - 7s - loss: 0.4966 - categorical_accuracy: 0.8026 - val_loss: 0.4546 - val_categorical_accuracy: 0.8388\n",
            "Epoch 105/240\n",
            " - 7s - loss: 0.4910 - categorical_accuracy: 0.8092 - val_loss: 0.4392 - val_categorical_accuracy: 0.8356\n",
            "Epoch 106/240\n",
            " - 7s - loss: 0.4822 - categorical_accuracy: 0.8089 - val_loss: 0.4250 - val_categorical_accuracy: 0.8476\n",
            "Epoch 107/240\n",
            " - 7s - loss: 0.4944 - categorical_accuracy: 0.8042 - val_loss: 0.4543 - val_categorical_accuracy: 0.8287\n",
            "Epoch 108/240\n",
            " - 7s - loss: 0.4959 - categorical_accuracy: 0.8064 - val_loss: 0.4549 - val_categorical_accuracy: 0.8249\n",
            "Epoch 109/240\n",
            " - 7s - loss: 0.4961 - categorical_accuracy: 0.8097 - val_loss: 0.4409 - val_categorical_accuracy: 0.8369\n",
            "Epoch 110/240\n",
            " - 7s - loss: 0.4731 - categorical_accuracy: 0.8118 - val_loss: 0.4256 - val_categorical_accuracy: 0.8413\n",
            "Epoch 111/240\n",
            " - 7s - loss: 0.4839 - categorical_accuracy: 0.8081 - val_loss: 0.4304 - val_categorical_accuracy: 0.8394\n",
            "Epoch 112/240\n",
            " - 8s - loss: 0.4857 - categorical_accuracy: 0.8094 - val_loss: 0.4391 - val_categorical_accuracy: 0.8413\n",
            "Epoch 113/240\n",
            " - 7s - loss: 0.4923 - categorical_accuracy: 0.8067 - val_loss: 0.4445 - val_categorical_accuracy: 0.8375\n",
            "Epoch 114/240\n",
            " - 7s - loss: 0.4754 - categorical_accuracy: 0.8174 - val_loss: 0.4490 - val_categorical_accuracy: 0.8300\n",
            "Epoch 115/240\n",
            " - 7s - loss: 0.4868 - categorical_accuracy: 0.8061 - val_loss: 0.4291 - val_categorical_accuracy: 0.8394\n"
          ]
        }
      ],
      "source": [
        "model = Sequential()\n",
        "model.add(Conv1D(filters=20,\n",
        "                 kernel_size=2,\n",
        "                 strides=1,\n",
        "                 input_shape=(x_train_ts.shape[1], x_train_ts.shape[2]),\n",
        "                 padding=\"same\",\n",
        "                 activation=\"relu\"))\n",
        "model.add(LSTM(lstm1_nodes,\n",
        "               dropout=lstm1_dropouts,\n",
        "               recurrent_dropout=lstm1_dropouts, \n",
        "               return_sequences=True,\n",
        "               kernel_initializer='random_uniform'))  \n",
        "model.add(LSTM(lstm2_nodes, \n",
        "               dropout=lstm2_dropouts, \n",
        "               return_sequences=True))\n",
        "model.add(LSTM(lstm3_nodes, \n",
        "               dropout=lstm3_dropouts))\n",
        "model.add(Dense(5, activation='relu'))\n",
        "model.add(Dense(4, activation='softmax'))\n",
        "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'])  \n",
        "history = model.fit(x_train_ts, y_train_ts, epochs=epochs, verbose=2, batch_size=batch_size, validation_split=0.2,\n",
        "                    callbacks=[EarlyStopping(monitor='val_loss',patience=15, min_delta=0.002)])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 545
        },
        "id": "nzsy2SqUS8Lb",
        "outputId": "6464b790-ad93-4d0e-c17e-4e5f89855bca"
      },
      "outputs": [
        {
          "data": {
            "image/png": "[encoded data, 25256 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light",
            "tags": []
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "image/png": "[encoded data, 22492 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light",
            "tags": []
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "acc = history.history['categorical_accuracy']\n",
        "val_acc = history.history['val_categorical_accuracy']\n",
        "loss = history.history['loss']\n",
        "val_loss = history.history['val_loss']\n",
        "\n",
        "epochs = range(1, len(acc) + 1)\n",
        "\n",
        "plt.plot(epochs, acc, 'bo', label='Training acc')\n",
        "plt.plot(epochs, val_acc, 'r', label='Validation acc')\n",
        "plt.title('Training and validation accuracy')\n",
        "plt.legend()\n",
        "\n",
        "plt.figure()\n",
        "\n",
        "plt.plot(epochs, loss, 'bo', label='Training loss')\n",
        "plt.plot(epochs, val_loss, 'r', label='Validation loss')\n",
        "plt.title('Training and validation loss')\n",
        "plt.legend()\n",
        "\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "id": "do4IxV8_wdzz",
        "outputId": "fe0df572-28ef-44b6-f90a-223a3b36d7ec"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "1984/1984 [==============================] - 0s 207us/step\n",
            "Test set\n",
            "  Loss: 0.408\n",
            "  Accuracy: 0.838\n"
          ]
        }
      ],
      "source": [
        "# evaluate model\n",
        "accuracy = model.evaluate(x_test_ts, y_test_ts)\n",
        "print('Test set\\n  Loss: {:0.3f}\\n  Accuracy: {:0.3f}'.format(accuracy[0],accuracy[1]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 370
        },
        "id": "9dLCp2gw5Ulv",
        "outputId": "022d9429-1cbc-4527-be8e-b8b8ebceea44"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Confusion Matrix\n",
            "[[465  13  17  22]\n",
            " [ 77 397   6  21]\n",
            " [ 24   6 441  15]\n",
            " [ 85  28   8 359]]\n",
            "[[0.89941973 0.02514507 0.03288201 0.04255319]\n",
            " [0.15369261 0.79241517 0.01197605 0.04191617]\n",
            " [0.04938272 0.01234568 0.90740741 0.0308642 ]\n",
            " [0.17708333 0.05833333 0.01666667 0.74791667]]\n",
            "Classification Report\n",
            "                 precision    recall  f1-score   support\n",
            "\n",
            "CognitiveStress       0.71      0.90      0.80       517\n",
            "EmotionalStress       0.89      0.79      0.84       501\n",
            " PhysicalStress       0.93      0.91      0.92       486\n",
            "          Relax       0.86      0.75      0.80       480\n",
            "\n",
            "       accuracy                           0.84      1984\n",
            "      macro avg       0.85      0.84      0.84      1984\n",
            "   weighted avg       0.85      0.84      0.84      1984\n",
            "\n"
          ]
        }
      ],
      "source": [
        "#Confution Matrix and Classification Report\n",
        "Y_pred = model.predict(x_test_ts,verbose=0)\n",
        "y_pred = np.argmax(Y_pred, axis=1)\n",
        "ytest=np.argmax(y_test_ts, axis=1)\n",
        "print('Confusion Matrix')\n",
        "matrix=confusion_matrix(ytest, y_pred)\n",
        "print(confusion_matrix(ytest, y_pred))\n",
        "normalize=confusion_matrix(ytest, y_pred, normalize='true')\n",
        "print(normalize)\n",
        "print('Classification Report')\n",
        "target_names = ['CognitiveStress','EmotionalStress', 'PhysicalStress','Relax']\n",
        "print(classification_report(ytest, y_pred, target_names=target_names))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 353
        },
        "id": "vaCPYjOd-vZL",
        "outputId": "4a6776c0-1b88-41a7-e5fd-cad1bb1f09b4"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fa4d628f588>"
            ]
          },
          "execution_count": 31,
          "metadata": {
            "tags": []
          },
          "output_type": "execute_result"
        },
        {
          "data": {
            "image/png": "[encoded data, 19988 characters]\n",
            "text/plain": [
              "<Figure size 360x288 with 2 Axes>"
            ]
          },
          "metadata": {
            "needs_background": "light",
            "tags": []
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "import seaborn as sn\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "array = matrix\n",
        "matrixlabel=['CognitiveStress', 'EmotionalStress','PhysicalStress','Relax']\n",
        "df_cm = pd.DataFrame(array, index = matrixlabel,\n",
        "                  columns = matrixlabel)\n",
        "plt.figure(figsize = (5,4))\n",
        "sn.heatmap(df_cm, annot=True,fmt='d')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "C0g4OYzeuRUr"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}