This repository contains an AI-based extraction tool for deriving Heart Rate Variability (HRV) directly from raw Magnetoencephalography (MEG) data. The purpose of this tool is to leverage the cardiac signals in MEG data, which are traditionally treated as artifacts and discarded, but have been recently found to carry significant health information about a patient.
## primary workflows
## Abstract
### Train model
Magnetoencephalography (MEG) measures magnetic fluctuations in the brain generated by neural processes. Among these processes, cardiac signals are generally removed as artifacts. Recent studies indicate Heart Rate Variability (HRV) as a potential biomarker related to autonomic function, thus implying the cardiac signal in MEG contains useful supplemental health information. This repository presents an automated extraction tool developed to derive HRV directly from raw MEG data using artificial intelligence. Five scans were performed with simultaneous MEG and Electrocardiogram (ECG) acquisition, with the ECG providing a ground-truth metric for evaluating our algorithms and data processing pipeline.
## Repository Structure
### Optimize hyperparameters
This repository is primarily divided into four parts, each represented by a Python script:
1.`preprocess_ecgs.py`: Script to preprocess ECG data. This involves signal cleaning, normalization and the detection of the QRS complex.
2.`preprocess_megs.py`: Script to preprocess MEG data. This involves filtering the data, removing noise and other artifacts.
3.`label_cardiac_ic.py`: Script to extract the cardiac signal from the preprocessed MEG data using artificial intelligence techniques.
4.`compare_signals.py`: Script to compare the HRV derived from MEG data with the ground truth from ECG. It computes the root mean square error (RMSE) between the RR-intervals of the two signals.
### Predict a new observation
## Usage
For each of the scripts, you can run them via the command line with Python 3.8 or higher.
# Use model as a package
For example:
## Virtual environment (optional)
```
python3 -m venv venv
source venv/bin/activate
```
## Installation
From the src/ directory...
python3 hrv_meg/main.py preprocess_ecg
```
python3 -m pip install pip setuptools wheel
python3 -m pip install -e .
```
Details of the function calls are available in the
If you are developing this package,
```
python3 -m pip install pip setuptools wheel
python3 -m pip install -e ".[dev]"
```
## Results
## Primary workflows
After removing outlier intervals and aligning the ECG and derived cardiac MEG signals, the RMSE between the RR-intervals of each was RMSE1 = 2ms, RMSE2 = 2ms, RMSE3 = 9ms, RMSE4 = 4ms, RMSE5 = 12ms. Our findings indicate that it is feasible to use cardiac artifacts from MEG data to approximate an individual’s HRV metrics.
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
## Acknowledgments
If you use this code in your work, please use the following citation: