{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Multi_lead_1dCNN.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPt71+F1bF6jL/rFq0ohH7N",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "code",
"metadata": {
"id": "g4s82uBJWlj7",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6427ca10-0c18-4602-9c3c-29eb31b101bc"
},
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib as plt\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sn\n",
"%matplotlib inline\n",
"%pylab inline\n",
"%config InlineBackend.figure_formats = ['retina']\n",
"from imutils import paths\n",
"import time # time1 = time.time(); print('Time taken: {:.1f} sec'.format(time.time() - time1))\n",
"import cv2\n",
"import pickle\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"import pickle\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pluEuqpFYD0V"
},
"source": [
"\n",
"import keras\n",
"import keras.utils\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Flatten\n",
"from keras.layers import Conv2D, MaxPooling2D\n",
"from keras.optimizers import Adam\n",
"\n",
"from tensorflow.keras import utils as np_utils\n",
"\n",
"from tensorflow.keras.utils import to_categorical\n",
"from keras.preprocessing import image\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"from tqdm import tqdm\n",
"from sklearn.metrics import plot_confusion_matrix\n",
"from sklearn.model_selection import train_test_split\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "cwJWpDYHJ0Fs"
},
"source": [
"from tensorflow import keras \n",
"from tensorflow.keras.models import Model\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D\n",
"from tensorflow.keras.layers import Convolution1D, ZeroPadding1D, MaxPooling1D, BatchNormalization, Activation, Dropout, Flatten, Dense"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JSElyJujYOGQ",
"outputId": "58e7bedf-02d3-4a7c-8552-1547517f36fa"
},
"source": [
"from google.colab import drive\n",
"drive.mount._DEBUG = True\n",
"drive.mount('/content/MyDrive', force_remount=True)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"unset HISTFILE; export PS1=\"root@8853f9368014-e8bef2a5298943548333554ce401e648: \"\n",
"bash: cannot set terminal process group (-1): Inappropriate ioctl for device\n",
"bash: no job control in this shell\n",
"\u001b[01;34m/content\u001b[00m# root@8853f9368014-e8bef2a5298943548333554ce401e648: umount -f /content/MyDrive || umount /content/MyDrive; pkill -9 -x drive\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: pkill -9 -f /opt/google/drive/directoryprefetcher_binary\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: ( while `sleep 0.5`; do if [[ -d \"/content/MyDrive\" && \"$(ls -A /content/MyDrive)\" != \"\" ]]; then echo \"google.colab.drive MOUNTED\"; break; fi; done ) &\n",
"[1] 786\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: cat /tmp/tmp99cd79x9/drive.fifo | head -1 | ( /opt/google/drive/drive --features=fuse_max_background:1000,max_read_qps:1000,max_write_qps:1000,max_operation_batch_size:15,max_parallel_push_task_instances:10,opendir_timeout_ms:120000,virtual_folders_omit_spaces:true --inet_family=IPV4_ONLY --preferences=trusted_root_certs_file_path:/opt/google/drive/roots.pem,mount_point_path:/content/MyDrive --console_auth 2>&1 | grep --line-buffered -E \"(Go to this URL in a browser: https://.*)$|Drive File Stream encountered a problem and has stopped|Authorization failed|The domain policy has disabled Drive File Stream\"; echo \"drive EXITED\"; ) &\n",
"[2] 790\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: google.colab.drive MOUNTED\n",
"fuser -kw \"/root/.config/Google/DriveFS/Logs/timeouts.txt\" ; rm -rf \"/root/.config/Google/DriveFS/Logs/timeouts.txt\"\n",
"/root/.config/Google/DriveFS/Logs/timeouts.txt: 238\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: nohup bash -c 'tail -n +0 -F \"/root/.config/Google/DriveFS/Logs/drive_fs.txt\" | python3 /opt/google/drive/drive-filter.py > \"/root/.config/Google/DriveFS/Logs/timeouts.txt\" ' < /dev/null > /dev/null 2>&1 &\n",
"[3] 839\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: disown -a\n",
"root@8853f9368014-e8bef2a5298943548333554ce401e648: exit\n",
"Mounted at /content/MyDrive\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "OneI2FHXYF8H"
},
"source": [
"# Enter the directory\n",
"!cd /content/MyDrive/MyDrive/ECG/\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "jALJ2MIEH3ib"
},
"source": [
"# Importing any of these files is fine.\n",
"They differ by methods of preprocessing of the CSV files"
]
},
{
"cell_type": "code",
"metadata": {
"id": "DlqItrd6XNnX"
},
"source": [
"\n",
"with open('/content/MyDrive/MyDrive/ECG/dataset_dict_moise.pickle', 'rb') as file:\n",
" dataset_dict = pickle.load(file)\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NhNlh62VjOfY",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f99fb725-a5a9-4408-dddb-7a6e0f3f73ac"
},
"source": [
"dataset_dict.keys()"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict_keys(['X_train', 'X_test', 'y_train', 'y_test'])"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "OAKau2s0iv40"
},
"source": [
"x_train1=dataset_dict['X_train']\n",
"x_test1=dataset_dict['X_test']\n",
"\n",
"y_train1=dataset_dict['y_train']\n",
"y_test1=dataset_dict['y_test']\n",
"\n",
"\n",
"## For Moise data: \n",
"\n",
"x_train=x_train1\n",
"x_test=x_test1\n",
"\n",
"y_test = to_categorical(y_test1) #[:,1:5] # Since labeling is [1,2,3,4] instead \\\n",
"# of [0,1,2,3]\n",
"y_train = to_categorical(y_train1) #[:,1:5] #one-hot encoding"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 606
},
"id": "7Lhkt_ZjMiA0",
"outputId": "c6417c54-3f08-47dd-ae8a-007b981cdad0"
},
"source": [
"x_train"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"
\n", " | Lead1 | \n", "Lead2 | \n", "Lead3 | \n", "Lead4 | \n", "Lead5 | \n", "Lead6 | \n", "Lead7 | \n", "Lead8 | \n", "Lead9 | \n", "Lead10 | \n", "Lead11 | \n", "Lead12 | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
309 | \n", "0 138.0\n", "1 140.5\n", "2 143.5\n", "3 ... | \n", "0 100.0\n", "1 89.5\n", "2 88.5\n", "3 ... | \n", "0 96.5\n", "1 96.0\n", "2 101.0\n", "3 ... | \n", "0 40.0\n", "1 33.0\n", "2 66.5\n", "3 ... | \n", "0 108.50\n", "1 98.00\n", "2 113.00\n", "3 ... | \n", "0 156.0\n", "1 155.0\n", "2 156.5\n", "3 ... | \n", "0 125.535714\n", "1 147.666667\n", "2 139... | \n", "0 133.0\n", "1 135.5\n", "2 131.5\n", "3 ... | \n", "0 107.5\n", "1 97.5\n", "2 123.0\n", "3 ... | \n", "0 140.5\n", "1 139.5\n", "2 146.0\n", "3 ... | \n", "0 153.0\n", "1 150.5\n", "2 162.5\n", "3 ... | \n", "0 101.0\n", "1 100.5\n", "2 110.0\n", "3 ... | \n", "
1108 | \n", "0 124.5\n", "1 115.0\n", "2 116.5\n", "3 ... | \n", "0 101.0\n", "1 101.5\n", "2 102.5\n", "3 ... | \n", "0 107.0\n", "1 107.0\n", "2 109.0\n", "3 ... | \n", "0 25.5\n", "1 22.5\n", "2 40.5\n", "3 ... | \n", "0 106.5\n", "1 112.5\n", "2 117.5\n", "3 ... | \n", "0 135.0\n", "1 145.0\n", "2 138.5\n", "3 ... | \n", "0 145.148148\n", "1 6.000000\n", "2 6... | \n", "0 123.5\n", "1 111.0\n", "2 114.0\n", "3 ... | \n", "0 165.428571\n", "1 165.875000\n", "2 154... | \n", "0 130.0\n", "1 132.0\n", "2 133.0\n", "3 ... | \n", "0 89.5\n", "1 92.5\n", "2 102.5\n", "3 ... | \n", "0 135.0\n", "1 128.5\n", "2 125.0\n", "3 ... | \n", "
647 | \n", "0 65.000000\n", "1 56.000000\n", "2 58.85... | \n", "0 80.0\n", "1 80.0\n", "2 80.0\n", "3 81.... | \n", "0 45.000000\n", "1 44.500000\n", "2 46.00... | \n", "0 40.500000\n", "1 51.914286\n", "2 45.92... | \n", "0 77.000000\n", "1 78.555556\n", "2 78... | \n", "0 45.500000\n", "1 50.200000\n", "2 46.00... | \n", "0 78.000000\n", "1 82.000000\n", "2 76... | \n", "0 54.636364\n", "1 58.500000\n", "2 62.12... | \n", "0 133.500000\n", "1 133.500000\n", "2 133... | \n", "0 73.000000\n", "1 75.000000\n", "2 75... | \n", "0 68.333333\n", "1 57.500000\n", "2 59.09... | \n", "0 74.631579\n", "1 76.466667\n", "2 79... | \n", "
863 | \n", "0 157.666667\n", "1 157.666667\n", "2 149... | \n", "0 105.0\n", "1 109.5\n", "2 102.5\n", "3 ... | \n", "0 104.0\n", "1 112.5\n", "2 104.5\n", "3 ... | \n", "0 52.0\n", "1 76.0\n", "2 58.0\n", "3 ... | \n", "0 124.5\n", "1 120.5\n", "2 128.5\n", "3 ... | \n", "0 118.333333\n", "1 125.500000\n", "2 128... | \n", "0 113.5\n", "1 120.0\n", "2 151.0\n", "3 ... | \n", "0 92.5\n", "1 94.0\n", "2 109.5\n", "3 ... | \n", "0 120.0\n", "1 120.0\n", "2 126.5\n", "3 ... | \n", "0 150.0\n", "1 153.5\n", "2 145.5\n", "3 ... | \n", "0 144.0\n", "1 137.5\n", "2 145.5\n", "3 ... | \n", "0 127.888889\n", "1 126.962963\n", "2 133... | \n", "
207 | \n", "0 122.0\n", "1 130.0\n", "2 130.0\n", "3 ... | \n", "0 97.5\n", "1 92.0\n", "2 93.0\n", "3 ... | \n", "0 85.000000\n", "1 80.500000\n", "2 86... | \n", "0 34.0\n", "1 33.0\n", "2 63.0\n", "3 ... | \n", "0 83.50\n", "1 85.00\n", "2 94.04\n", "3 ... | \n", "0 117.5\n", "1 113.5\n", "2 122.5\n", "3 ... | \n", "0 157.818182\n", "1 144.714286\n", "2 122... | \n", "0 104.5\n", "1 93.0\n", "2 113.5\n", "3 ... | \n", "0 116.5\n", "1 106.5\n", "2 128.5\n", "3 ... | \n", "0 147.0\n", "1 145.0\n", "2 149.0\n", "3 ... | \n", "0 153.781022\n", "1 141.024793\n", "2 22... | \n", "0 106.0\n", "1 101.5\n", "2 113.5\n", "3 ... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
449 | \n", "0 40.625\n", "1 39.500\n", "2 40.500\n", "3 ... | \n", "0 86.500000\n", "1 86.833333\n", "2 87.60... | \n", "0 97.875\n", "1 92.250\n", "2 80.000\n", "3 ... | \n", "0 15.000000\n", "1 55.625000\n", "2 15.50... | \n", "0 71.500000\n", "1 80.166667\n", "2 68... | \n", "0 52.500000\n", "1 55.000000\n", "2 54.50... | \n", "0 125.000000\n", "1 137.666667\n", "2 124... | \n", "0 43.0\n", "1 46.0\n", "2 51.5\n", "3 46.... | \n", "0 104.5\n", "1 104.5\n", "2 103.5\n", "3 ... | \n", "0 72.5\n", "1 72.0\n", "2 73.2\n", "3 72.... | \n", "0 61.521739\n", "1 55.219512\n", "2 63.29... | \n", "0 62.500000\n", "1 63.000000\n", "2 65.00... | \n", "
411 | \n", "0 39.666667\n", "1 38.500000\n", "2 39.50... | \n", "0 79.500000\n", "1 86.500000\n", "2 83.50... | \n", "0 103.833333\n", "1 77.555556\n", "2 71... | \n", "0 67.0\n", "1 66.5\n", "2 67.5\n", "3 68.... | \n", "0 96.400000\n", "1 86.736842\n", "2 86... | \n", "0 51.000000\n", "1 52.000000\n", "2 51.00... | \n", "0 73.500000\n", "1 83.642857\n", "2 85... | \n", "0 14.000000\n", "1 55.171429\n", "2 13.00... | \n", "0 102.5\n", "1 104.5\n", "2 111.0\n", "3 ... | \n", "0 78.125000\n", "1 87.000000\n", "2 100... | \n", "0 86.416667\n", "1 94.962963\n", "2 88... | \n", "0 52.000000\n", "1 52.000000\n", "2 62.38... | \n", "
686 | \n", "0 130.5\n", "1 132.5\n", "2 136.5\n", "3 ... | \n", "0 99.250000\n", "1 90.352941\n", "2 88... | \n", "0 104.5\n", "1 109.0\n", "2 106.5\n", "3 ... | \n", "0 58.0\n", "1 80.0\n", "2 69.0\n", "3 ... | \n", "0 116.0\n", "1 102.0\n", "2 108.0\n", "3 ... | \n", "0 157.5\n", "1 160.5\n", "2 165.5\n", "3 ... | \n", "0 142.000000\n", "1 140.333333\n", "2 110... | \n", "0 107.0\n", "1 119.0\n", "2 116.0\n", "3 ... | \n", "0 135.285714\n", "1 159.142857\n", "2 159... | \n", "0 156.5\n", "1 156.5\n", "2 156.0\n", "3 ... | \n", "0 130.0\n", "1 124.5\n", "2 147.0\n", "3 ... | \n", "0 109.5\n", "1 128.5\n", "2 120.5\n", "3 ... | \n", "
461 | \n", "0 68.5\n", "1 54.0\n", "2 60.5\n", "3 65.... | \n", "0 61.000000\n", "1 52.000000\n", "2 56.27... | \n", "0 84.0\n", "1 84.0\n", "2 83.5\n", "3 84.... | \n", "0 40.500\n", "1 43.500\n", "2 48.625\n", "3 ... | \n", "0 65.086957\n", "1 69.357143\n", "2 67.50... | \n", "0 44.50000\n", "1 38.60000\n", "2 41.0800... | \n", "0 63.823529\n", "1 57.500000\n", "2 63... | \n", "0 70.235294\n", "1 69.000000\n", "2 78.13... | \n", "0 68.5\n", "1 70.5\n", "2 69.5\n", "3 71.... | \n", "0 98.0\n", "1 101.5\n", "2 99.0\n", "3 ... | \n", "0 77.500000\n", "1 79.000000\n", "2 85... | \n", "0 60.5\n", "1 37.0\n", "2 65.0\n", "3 ... | \n", "
727 | \n", "0 119.5\n", "1 105.5\n", "2 112.0\n", "3 ... | \n", "0 101.111111\n", "1 107.000000\n", "2 106... | \n", "0 104.5\n", "1 104.5\n", "2 104.5\n", "3 ... | \n", "0 51.5\n", "1 48.0\n", "2 66.5\n", "3 ... | \n", "0 114.375000\n", "1 142.965517\n", "2 138... | \n", "0 119.0\n", "1 118.0\n", "2 119.0\n", "3 ... | \n", "0 83.0\n", "1 75.0\n", "2 102.0\n", "3 ... | \n", "0 110.0\n", "1 97.5\n", "2 104.5\n", "3 ... | \n", "0 140.875000\n", "1 143.500000\n", "2 140... | \n", "0 155.5\n", "1 161.5\n", "2 151.0\n", "3 ... | \n", "0 128.0\n", "1 132.5\n", "2 136.5\n", "3 ... | \n", "0 135.0\n", "1 125.5\n", "2 130.0\n", "3 ... | \n", "
883 rows × 12 columns
\n", "\n", " | precision | \n", "recall | \n", "f1-score | \n", "support | \n", "
---|---|---|---|---|
Covid | \n", "0.980000 | \n", "0.980000 | \n", "0.980000 | \n", "50.0 | \n", "
MI | \n", "0.905660 | \n", "1.000000 | \n", "0.950495 | \n", "48.0 | \n", "
MI-Hist | \n", "0.756757 | \n", "0.823529 | \n", "0.788732 | \n", "34.0 | \n", "
Abnormal | \n", "0.939394 | \n", "0.659574 | \n", "0.775000 | \n", "47.0 | \n", "
Normal | \n", "0.809524 | \n", "0.894737 | \n", "0.850000 | \n", "57.0 | \n", "