1. Getting Started

1.1. Installation

Use the pip tool to download pyHRV from the Python Package Index (PyPi). If you are using macOS or a Linux-based operating system, open the Terminal and type in the command below. Windows users may use the Command Prompt.

$ pip install pyhrv

pyHRV depends on the following third-party packages, which will be automatically installed using the pip tool, if these are not already on your machine:

Note

This has been primarily developed for the Python 2.7 programming language. Running the pip command above may cause errors when trying to install the package using Python 3.

In this case, try to install the pyHRV dependencies first:

$ pip install biosppy
$ pip install matplotlib
$ pip install numpy
$ pip install scipy
$ pip install nolds
$ pip install spectrum

Note

Alternatively, it is recommended to install the Anaconda software, which comes with a compatible Python 2.7 distribution and all the necessary (and more) third-party packages for scientific computing.

1.2. R-Peak Detection with BioSPPy

BioSPPy is a toolbox for biosignal processing, and comes with built-in ECG processing and R-peak detection algorithms. These can be used to compute the NNI series upon which the HRV parameters can be computed.

An example of this procedure is demonstrated below, using ECG data acquired with the BITalino (r)evolution hardware and the OpenSignals (r)evolution software. The ECG signals are imported and converted to mV using the opensignalsreader package.

import biosppy
import numpy as np
import pyhrv.tools as tools
from opensignalsreader import OpenSignalsReader

# Load sample ECG signal & extract R-peaks using BioSppy
signal = OpenSignalsReader('./samples/SampleECG.txt').signal('ECG')
signal, rpeaks = biosppy.signals.ecg.ecg(signal, show=False)[1:3]

# Compute NNI
nni = tools.nn_intervals(rpeaks)

Note

pyHRV can of course be used with any ECG - or ECG Lead I like - signal and is not limited to signals acquired with specific devices or software. The instructions above are merely an example.

1.3. Sample NNI Series

pyHRV comes with sample data available here folder.

1.4. The biosppy.utils.ReturnTuple Object

The results of the pyHRV parameter functions, wrapped and returned as biosppy.utils.ReturnTuple objects. This package-specific class combines the advantages of Python dictionaries (indexing using keywords) and Python tuples (immutable). Parameter values stored in the ReturnTuple object can be accessed as follows:

from biosppy import utils

# Store sample data in a ReturnTuple object
args = (500, 600, )
names = ('parameter1', 'parameter2', )
results = utils.ReturnTuple(args, names)

# Get and print 'parameter1'
print(results['parameter1'])

1.5. What may help when matplotlib blocks your code from being executed

The plots generated by the functions of pyHRV use matplotlib as the fundamental plotting library. The default backend configuration of this library can cause some unwanted behaviour, where your Python scripts are interrupted whenever a plot is shown.

Important

This issue can be solved by switching the matplotlib backend to a backend that supports the matplotlib .interactive() mode. This mode allows you to show the generated plots without interrupting your Python script. The Qt4Agg has shown to be a suitable backend to solve this issue on Windows and macOS.

Add the following lines of code at the top of your script, before importing the other Python packages to try to solve this issue:

# Import matplotlib and set the 'Qt4Agg' backend to support interactive mode on Windows and macOS
import matplotlib
matplotlib.use('Qt4Agg')

# Activate interactive mode
import matploltib.pyplot as plt
plt.ion()