ECG Analysis Through Deep Learning
ECG Analysis Through Deep Learning
ECG Analysis Through Deep Learning
Develop and evaluate a deep learning-based system for detection and classification of cardiovascular and
respiratory diseases from treadmill test ECG recordings with the goal of improving diagnostic accuracy and
can reduce False positive rate and invasive procedures.
PROCEDURE
Data Collection: Gather a large dataset of ECG recordings obtained during treadmill tests, including records
from individuals with various cardiovascular conditions such as coronary artery disease (CAD), arrhythmias,
myocardial ischemia, and other relevant diseases.
Data Preprocessing: Preprocess the ECG data to ensure consistency and remove noise. This may involve
filtering, resampling, and normalization techniques to standardize the data for analysis.
Feature Extraction: Extract relevant features from the ECG signals that are indicative of different diseases.
Features could include waveform morphology, heart rate variability, ST-segment changes, and other
characteristic patterns associated with specific conditions.
Model Selection: Convolutional neural networks (CNNs), recurrent neural networks (RNNs), or hybrid
models.
Model Training: Train the selected deep learning model using the preprocessed ECG data. Utilize
techniques such as data augmentation and regularization to improve model generalization and prevent
overfitting.
Model Evaluation: Evaluate the trained model's performance on its accuracy, sensitivity, specificity, and
other performance metrics.
Disease Detection: Deploy the trained deep learning model to analyze new ECG recordings obtained during
treadmill tests.
Integration with Clinical Workflow: Integrate the deep learning-based disease detection system into the
clinical workflow, allowing healthcare providers to interpret treadmill test results more efficiently.
Cardiovascular Diseases have unique features that exhibit presence of that disease.These features include:-
1. Coronary Artery Disease (CAD): Extract features such as ST-segment deviations, T-wave
abnormalities, and QRS complex morphology from the ECG signals.
2. Arrhythmias: Extract features such as RR interval variability, P-wave morphology, and QRS
duration from the ECG signals.
3. Myocardial Ischemia:Extract features such as ST-segment deviations, T-wave changes, and heart
rate variability from the ECG signals.
To Analyse these Features from the ECG signal obtained from a treadmill test, multiple neural network
models can be used, for which i have created a ‘ECG Analysing Deep learning model’ (attatched) in which
has ECG data has been imported from an external source and also is in .csv file format.
ECG Analysis Through Deep Learning
-By Aaditya Balakrishnan
-Analysing ECG signals through Time series data analysis and Feature extraction process.
Importing Libraries
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import plotly.graph_objs as go
import plotly.express as px
from scipy.signal import medfilt, butter, filtfilt
import pywt
from sklearn.model_selection import train_test_split
import scipy.signal
from keras.models import Sequential
from keras.layers import LSTM, Dense, Reshape
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report
from sklearn.metrics import roc_auc_score
OUTPUT :
Plotting Normal and Aberrant ECG signals
#plot graphs of normal and abnormal ECG to visualise the trends
abnormal = df[df.loc[:,140] ==0][:10]
normal = df[df.loc[:,140] ==1][:10]
# Create the figure
fig = go.Figure()
#create a list to display only a single legend
leg = [False] * abnormal.shape[0]
leg[0] = True
# Plot training and validation error
for i in range(abnormal.shape[0]):
fig.add_trace(go.Scatter( x=np.arange(abnormal.shape[1]),y=abnormal.iloc[i,:],name='Abnormal ECG',
mode='lines', marker_color='rgba(255, 0, 0, 0.9)', showlegend= leg[i]))
for j in range(normal.shape[0]):
fig.add_trace(go.Scatter( x=np.arange(normal.shape[1]),y=normal.iloc[j,:],name='Normal ECG', mode='lines',
marker_color='rgba(0, 255, 0, 1)', showlegend= leg[j]))
fig.update_layout(xaxis_title="time", yaxis_title="Signal", title= {'text': 'Difference between different ECG',
'xanchor': 'center', 'yanchor': 'top', 'x':0.5} , bargap=0,)
fig.update_traces(opacity=0.5)
fig.show()
OUTPUT :
Data Preprocessing
OUTPUT :
Filter the dataset
Wavelet filtering. ( To Eliminate Noise)
#filtering the raw signals
# Median filtering
ecg_medfilt = medfilt(ecg_data, kernel_size=3)
# Low-pass filtering
lowcut = 0.05
highcut = 20.0
nyquist = 0.5 * 360.0
low = lowcut / nyquist
high = highcut / nyquist
b, a = butter(4, [low, high], btype='band')
ecg_lowpass = filtfilt(b, a, ecg_data)
# Wavelet filtering
coeffs = pywt.wavedec(ecg_data, 'db4', level=1)
threshold = np.std(coeffs[-1]) * np.sqrt(2*np.log(len(ecg_data)))
coeffs[1:] = (pywt.threshold(i, value=threshold, mode='soft') for i in coeffs[1:])
ecg_wavelet = pywt.waverec(coeffs, 'db4')
OUTPUT :
Filtering techniques
OUTPUT :
MSE value of Median Filtering: 0.017260298402611125
MSE value of Low-pass Filtering: 0.36750805414756493
MSE value of Wavelet Filtering: 0.0010818752598698714
//The dataset is divided into 80% for training and 20% for testing and validation purposes.
Feature Extraction
Four Features used in the ECG signal are : T Amplitude , R Amplitude , RR Interval , QRS Duration
//We first calculate the R & T peaks, the R amplitude, the RR interval, etc., using the scipy.signals library. Then,
we calculate each feature’s mean, median, sum, and other statistical metrics to capture its characteristics. All of
these features are then stored in an array.
//Building a Recurrent Neural Network LSTM model. First, we will reshape the data to make it compatible with
the model. Then, we will create an LSTM model with only 2 layers. Then, we will train it on the features extracted
from the data. Finally, we will make the predictions on the validation/test set.
Model Evaluation
//Evaluate the model’s performance using metrics, e.g., accuracy, AUC score precision, etc. Furthermore, we will
visualize the confusion matrix in Plotly.
fig = go.Figure()
fig.add_trace(go.Scatter( y=history.history['loss'], mode='lines', name='Training'))
fig.add_trace(go.Scatter( y=history.history['val_loss'], mode='lines', name='Validation'))
fig.update_layout(xaxis_title="Epoch", yaxis_title="Error", title= {'text': 'Model Error', 'xanchor': 'center', 'yanchor':
'top', 'x':0.5} , bargap=0)
fig.show()
The model achieved a recall value of 0.92 and an AUC score of 0.93