In this second tutorial we will see how to use the class Algorithm
to create signal processing pipelines.
A signal processing step is a computational function $F$ that operates on input data (a signal) to produce a result. It is characterized by a set of parameters p which regulate its behavior.
Figure 1: Abstract representation of a processing step.
In pyphysio each processing step is represented by an instance of a class derived from the generic class Algorithm
.
The type of function or algorithm is given by the class name (e.g. BeatFromECG
extracts the heartbeats from an ECG signal, PeakDetection
detects the peaks in the input signal).
The parameters of the function/algorithm are the attributes of the created instance.
Therefore, a processing step is defined by creating a new instance of the Class, which is initialized with the given parameters:
processing_step = ph.BeatFromECG(parameters)
To execute the processing step we need to give as input an instance of the class Signal
:
output = processing_step(input)
Algorithms in pyphysio are grouped in four categories (see also the tutorial '3-pipelines'):
In [1]:
# import packages
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# import data from included examples
from pyphysio.tests import TestData
from pyphysio import EvenlySignal
ecg_data = TestData.ecg()
eda_data = TestData.eda()
# create two signals
fsamp = 2048
tstart_ecg = 15
tstart_eda = 5
ecg = EvenlySignal(values = ecg_data,
sampling_freq = fsamp,
signal_type = 'ecg',
start_time = tstart_ecg)
eda = EvenlySignal(values = eda_data,
sampling_freq = fsamp,
signal_type = 'eda',
start_time = tstart_eda)
In [2]:
# create a Filter
import pyphysio.filters.Filters as flt
lowpass_50 = flt.IIRFilter(fp=50, fs=75, ftype='ellip')
In [3]:
# help inline
#?flt.IIRFilter
In [4]:
# check parameters
print(lowpass_50)
# OR
print(lowpass_50.get())
In [5]:
# apply a Filter
ecg_filtered = lowpass_50(ecg)
In [6]:
#plot
ecg.plot()
ecg_filtered.plot()
Out[6]:
In [7]:
# check output type
ecg.get_signal_type()
Out[7]:
Estimators are algorithms which aim at extracting the information of interest from the input signal, thus returning a new signal which has a different signal_nature
.
The name Estimators
recalls the fact that the information extraction depends on the value of the algorithm parameters which might not be known a-priori. Thus the result should be considered as an estimate of the real content of information of the input signal.
In [8]:
# create an Estimator
import pyphysio.estimators.Estimators as est
ibi_ecg = est.BeatFromECG()
In [9]:
# check parameters
ibi_ecg
Out[9]:
In [10]:
# apply an Estimator
ibi = ibi_ecg(ecg_filtered)
In [11]:
# plot
ax1 = plt.subplot(211)
ecg.plot()
plt.subplot(212, sharex=ax1)
ibi.plot()
Out[11]:
In [12]:
# check output type
ibi.get_signal_type()
Out[12]:
Indicators are algorithm which extract a metrics (scalar value) from the input signal, for instance a statistic (average).
Three types of indicators are provided in pyphysio
:
In [13]:
# create an Indicator
import pyphysio.indicators.TimeDomain as td_ind
import pyphysio.indicators.FrequencyDomain as fd_ind
In [14]:
rmssd = td_ind.RMSSD()
HF = fd_ind.PowerInBand(interp_freq=4, freq_max=0.4, freq_min=0.15, method = 'ar')
In [15]:
# check parameters
print(rmssd)
print(HF)
In [16]:
# apply an Indicator
rmssd_ = rmssd(ibi)
HF_ = HF(ibi.resample(4)) #resampling is needed to compute the Power Spectrum Density
print(rmssd_)
print(HF_)
In [17]:
# check output type
print(type(rmssd_))
print(type(HF_))
In [18]:
# create a Tool
import pyphysio.tools.Tools as tll
compute_psd = tll.PSD(method='ar', interp_freq = 4)
In [19]:
# check parameters
compute_psd
Out[19]:
In [20]:
# apply a Tool
frequencies, power = compute_psd(ibi.resample(4))
plt.plot(frequencies, power)
plt.show()
In [ ]: