Learning Similarities between Biomedical Signals with Deep Siamese Network

An automated neural network framework for signal matching problem

Ajay Arunachalam
Towards Data Science

--

TL;DR: full code

Hi friends.

Today, I will walk you through the electrocardiogram (ECG) biomedical signal data with the aim of learning similarity representations between the two recorded signal data events. ECG is one of the most commonly heard types of signal data in context to human medical recordings. So, let’s first simply understand what exactly is “Signal” in layman terms, what is an ECG signal, why is it needed, what exactly is Siamese Neural Network, how it can be useful towards comparing two vectors, and finally we will see an use-case starting with the ECG data analysis including uni/multivariate plotting, rolling window sum plots, data profiling, filtering outliers, detecting r-signal-to-signal peaks, and finally identifying the ECG signal similarities with Siamese Network model.

Signal

The fundamental quantity of representing some information is called a “signal” in simple engineering terms. While in context to mathematical world, a signal is just a function that simply conveys some information, where the information could be a function of time [y = y (t) ] or it could be function of spatial coordinates [ y = y (x, y) ] or it could be a function of distance from source [ y = y (r) ], etc. as an example.

Medical Signals (ECG, EEG,EOG, EMG, etc.)

The biomedical signals are basically electrical signals collected from our human body mostly in an non-invasive manner. For example electrocardiogram (ECG), electroencephalogram (EEG), electro-oculography (EOG), Electromyography (EMG), surface electromyogram (sEMG), etc. are the most common ones. These signals are very useful & are of very great value because then further it can be used for diagnostic purposes.

Electrocardiogram (ECG or EKG)

An electrocardiogram (ECG or EKG) measures the heart’s electrical conductivity for diagnosis purpose to test the functioning of our heart, and identify if any problems. It helps depict the rate and regularity of heartbeats, the size and position of the heart’s chambers, and whether there is any damage or not, and so on. The heartbeat is usually monitored by a small tape recorder attached to stickers called ‘electrodes’ that are attached to our chest.

The diagram below depicts a QRS complex schematic representation of normal ECG signal trace. As the name simply implies, the QRS complex includes the Q wave, R wave, and S wave. These three waves generally occur in rapid succession. The different waves that comprise the ECG represent the sequence of depolarization and repolarization of the atria and ventricles. The ECG is recorded at a speed of 25 mm/sec (5 large squares/sec), and the voltages are calibrated so that 1 mV = 10 mm (2 large squares) in the vertical direction. The QRS complex represents the electrical impulse as it spreads through the ventricles, and indicates ventricular depolarization. As with the P wave, the QRS complex starts just before ventricular contraction.

Diagram of the Human Heart and An example of Normal ECG Trace — Image Copyright [N. H. Kamarudin et al.]

Siamese Neural Network

Siamese neural networks or Siamese networks was developed at AT & T Bell Laboratories by Jane Bromley, and team long way back in the year 1993. It includes twin identical neural networks, and the units in these networks share their weights. These networks accept distinct inputs, and the outputs are combined by a simple comparative energy function. The energy function can be a distance function, most commonly the Euclidean distance. This type of network maps a high dimensional input to a target space, and uses the energy function to compare difficult to define & interpret semantic features of the output space.

Hands-on Demo

Now that we have seen some basic theory about ECG signals and Siamese networks, let’s quickly get started with an example. Here, we will use the data that I had received from one of the leading smart garment and textile companies that manufactures Sports Infrared Performance and Recovery Sportswear for fitness enthusiasts aimed towards monitoring altheles performance. The data provided includes ECG recordings logged from the sportwear jackets tested for different activities such as Sitting, Walking, Standing, etc with the west worne over the subjects/participants.

Please Note:- For data privacy and security reasons, I would like to keep the actual data source anonymous, and will also just provide readers with a subset of actual data (sample data) from the whole dataset just for sake of following this tutorial.

We will see how “Deep-XF” a python library can also be used intuitively for learning signal similarity. For quick introduction & all the other use-case applications that is supported by the deep-xf package in general is listed in this blog post here. Also, take a look at the Forecasting, Nowcasting blogs with hands-on demo tutorials from here & here which are services provided within the same package. If keen to filter out noise from your raw signal dataset, do check out this blog here.

For library installation follow these steps here & for manual prerequisites installation check here.

Data Analysis

Let us understand one thing very clearly that, before we talk about AI or to that matter any machine learning stuff, Data Analysis plays a very important key role in the entire Data Science Workflow. In fact, it takes most of the time within the entire pipeline.

Exploratory Data Analysis (EDA) is the initial, and an important phase of the workflow. It helps, to get a first look of the data, and help generate relevant hypothesis and decide next steps. However, the EDA process could be a hassle most of the times.

The snippet code given below is used for EDA with utility functions for checking missing values, plotting raw signals, sumplots of rolling window, profiling the data & generating corresponding reports, removing outliers, detecting r-signal-to-signal peaks (R-peaks) using the available detector types, such as i.e., swt detector, christov detector, two average detector, engzee detector, hamilton detector, pan tompkins detector, matched filter detector, etc.

Image by author: ECG1 — ECG5 raw signal plot
Image by author: Rolling window plot for ECG1 — ECG5 signals
Image by author: Plotting detected R-peaks in the raw signals with Christov Detector as the input argument

Signal Similarity Comparisons

The diagram below provides an intuitive representation of using siamese neural networks to derive a similarity score for our signal matching usecase. As seen in the figure below it includes twin identical neural networks, and the units in these networks share their weights. The final derived similarity score is outputted that estimates the match between the input vectors.

Image by author: SigSimNet Architecture

The Siamese_Model function takes the input vectors with number of features & performs a bunch of mathematical operations followed by concatenations through a feed-forward network architecture to get the desired ouput score as seen in the given snippet code below.

Next, we create the Siamese Network architecture with the Siamese_Model function taking the number of input features as the argument, followed by compiling the model with ‘Mean Square Error’ as our loss function as seen in the given snippet below with Adam optimizer & different evaluation metrics such as Mean Square Error (MSE), Mean Absolute Error (MAE), Mean Absolute Percentage Error (MAPE), and Cosine Similarity. The estimated similarity score for each of the signal instance is finally outputted that takes in the two input vectors as the argument. Our siamese model summary with subsequent information is as shown in the figure given below.

Image by author: Model Summary

Conclusion

Through this post, we saw how siamese neural network model (a.k.a., twin neural network) can be useful for our example of signal matching problem. This certainly can generalized to any context (Image/Video/Text/Point Cloud/etc.) with building of any corresponding network architectures like (Siamese CNN/RNN/Point Cloud/etc.), as the principle behind these artificial neural network it simply that it uses the same weights while working in parallel on two different input vectors to compute comparable output vectors. We also briefly discussed about the biomedical signals, and performed data analysis with bunch of different utility functions like detecting R-peaks, profiling, removing outliers, visualization plots, etc. We saw how the package deep-xf can be handy for signal data in terms of exploratory data analysis, preprocessing, learning representations, filtering, etc.

Let’s get connected

Contact via Linkedin; Alternative, you can also reach me at ajay.arunachalam08@gmail.com

About Author

I am an AWS Certified Machine Learning Specialist & Cloud Solution Architect. From my experience working on real-world problems, I fully acknowledge that finding good representations is the key in designing the system that can solve interesting challenging real-world problems, that go beyond human-level intelligence, and ultimately explain complicated data for us that we don’t understand. In order to achieve this, I envision learning algorithms that can learn feature representations from both unlabelled and labelled data, be guided with and/or without human interaction, and that are on different levels of abstractions in order to bridge the gap between low-level data and high-level abstract concepts. I also truly believe that Opacity in AI Systems is the need of the hour. With this in mind, I have always strived to democratize AI, and be more inclined towards building Interpretable/Explainable Models. My interest is in building real-time AI Solutions, Machine Learning/Deep Learning at scale, Productionalising Explainable Models, Deep Reinforcement Learning, Computer Vision, and Natural Language Processing, specifically learning good representations.

References

--

--