Table Of Contents

Previous topic

pyrem.polygram module

Next topic

pyrem.visualization module

This Page

pyrem.univariate module

Feature computation for univariate time series

This sub-module provides routines for computing features on univariate time series. Many functions are improved version of PyEEG [PYEEG] functions. Be careful, some functions will give different results compared to PyEEG as the maths have been changed to match original definitions. Have a look at the documentation notes/ source code to know more.

Here a list of the functions that were reimplemented:

[PET95](1, 2) A. Petrosian, Kolmogorov complexity of finite sequences and recognition of different preictal EEG patterns, in , Proceedings of the Eighth IEEE Symposium on Computer-Based Medical Systems, 1995, 1995, pp. 212-217.
[PYEEG](1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) F. S. Bao, X. Liu, and C. Zhang, PyEEG: An Open Source Python Module for EEG/MEG Feature Extraction, Computational Intelligence and Neuroscience, vol. 2011, p. e406391, Mar. 2011.
[HJO70](1, 2) B. Hjorth, EEG analysis based on time domain properties, Electroencephalography and Clinical Neurophysiology, vol. 29, no. 3, pp. 306-310, Sep. 1970.
[COS05]
  1. Costa, A. L. Goldberger, and C.-K. Peng, “Multiscale entropy analysis of biological signals,” Phys. Rev. E, vol. 71, no. 2, p. 021906, Feb. 2005.
[RIC00](1, 2, 3) J. S. Richman and J. R. Moorman, “Physiological time-series analysis using approximate entropy and sample entropy,” American Journal of Physiology - Heart and Circulatory Physiology, vol. 278, no. 6, pp. H2039-H2049, Jun. 2000.
[HIG88](1, 2, 3)
  1. Higuchi, “Approach to an irregular time series on the basis of the fractal theory,” Physica D: Nonlinear Phenomena, vol. 31, no. 2, pp. 277-283, Jun. 1988.
pyrem.univariate.ap_entropy(a, m, R)[source]

Compute the approximate entropy of a signal with embedding dimension “de” and delay “tau” [PYEEG]. Vectorised version of the PyEEG function. Faster than PyEEG, but still critically slow.

Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • m (int) – the scale
  • R (float`) – The tolerance
Returns:

the approximate entropy, a scalar

Return type:

float

pyrem.univariate.dfa(X, Ave=None, L=None, sampling=1)[source]

WIP on this function. It is basically copied and pasted from [PYEEG], without verification of the maths or unittests.

pyrem.univariate.fisher_info(a, tau, de)[source]

Compute the Fisher information of a signal with embedding dimension “de” and delay “tau” [PYEEG]. Vectorised (i.e. faster) version of the eponymous PyEEG function.

Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • tau (int) – the delay
  • de (int) – the embedding dimension
Returns:

the Fisher information, a scalar

Return type:

float

pyrem.univariate.hfd(a, k_max)[source]

Compute Higuchi Fractal Dimension of a time series. Vectorised version of the eponymous [PYEEG] function.

Note

Difference with PyEEG:

Results is different from [PYEEG] which appears to have implemented an erroneous formulae. [HIG88] defines the normalisation factor as:

\[\frac{N-1}{[\frac{N-m}{k} ]\dot{} k}\]

[PYEEG] implementation uses:

\[\frac{N-1}{[\frac{N-m}{k}]}\]

The latter does not give the expected fractal dimension of approximately 1.50 for brownian motion (see example bellow).

Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • k_max (int) – the maximal value of k
Returns:

Higuchi’s fractal dimension; a scalar

Return type:

float

Example from [HIG88]. This should produce a result close to 1.50:

>>> import numpy as np
>>> import pyrem as pr
>>> i = np.arange(2 ** 15) +1001
>>> z = np.random.normal(size=int(2 ** 15) + 1001)
>>> y = np.array([np.sum(z[1:j]) for j in i])
>>> pr.univariate.hfd(y,2**8)
pyrem.univariate.hjorth(a)[source]

Compute Hjorth parameters [HJO70].

\[Activity = m_0 = \sigma_{a}^2\]
\[Complexity = m_2 = \sigma_{d}/ \sigma_{a}\]
\[Morbidity = m_4 = \frac{\sigma_{dd}/ \sigma_{d}}{m_2}\]

Where:

\(\sigma_{x}^2\) is the mean power of a signal \(x\). That is, its variance, if it’s mean is zero.

\(a\), \(d\) and \(dd\) represent the original signal, its first and second derivatives, respectively.

Note

Difference with PyEEG:

Results is different from [PYEEG] which appear to uses a non normalised (by the length of the signal) definition of the activity:

\[\sigma_{a}^2 = \sum{\mathbf{x}[i]^2}\]

As opposed to

\[\sigma_{a}^2 = \frac{1}{n}\sum{\mathbf{x}[i]^2}\]
Parameters:a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
Returns:activity, complexity and morbidity
Return type:tuple(float, float, float)

Example:

>>> import pyrem as pr
>>> import numpy as np
>>> # generate white noise:
>>> noise = np.random.normal(size=int(1e4))
>>> activity, complexity, morbidity = pr.univariate.hjorth(noise)
pyrem.univariate.hurst(signal)[source]

Experimental/untested implementation taken from: http://drtomstarke.com/index.php/calculation-of-the-hurst-exponent-to-test-for-trend-and-mean-reversion/

Use at your own risks.

pyrem.univariate.pfd(a)[source]

Compute Petrosian Fractal Dimension of a time series [PET95].

It is defined by:

\[\frac{log(N)}{log(N) + log(\frac{N}{N+0.4N_{\delta}})}\]

Note

Difference with PyEEG:

Results is different from [PYEEG] which implemented an apparently erroneous formulae:

\[\frac{log(N)}{log(N) + log(\frac{N}{N}+0.4N_{\delta})}\]

Where:

\(N\) is the length of the time series, and

\(N_{\delta}\) is the number of sign changes.

Parameters:a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
Returns:the Petrosian Fractal Dimension; a scalar.
Return type:float

Example:

>>> import pyrem as pr
>>> import numpy as np
>>> # generate white noise:
>>> noise = np.random.normal(size=int(1e4))
>>> pr.univariate.pdf(noise)
pyrem.univariate.samp_entropy(a, m, r, tau=1, relative_r=True)[source]

Compute the sample entropy [RIC00] of a signal with embedding dimension de and delay tau [PYEEG]. Vectorised version of the eponymous PyEEG function. In addition, this function can also be used to vary tau and therefore compute Multi-Scale Entropy(MSE) [COS05] by coarse grainning the time series (see example bellow). By default, r is expressed as relatively to the standard deviation of the signal.

Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • m (int) – the scale
  • r (float) – The tolerance
  • tau (int) – The scale for coarse grainning.
  • relative_r (true) – whether the argument r is relative to the standard deviation. If false, an absolute value should be given for r.
Returns:

the approximate entropy, a scalar

Return type:

float

Example:

>>> import pyrem as pr
>>> import numpy as np
>>> # generate white noise:
>>> noise = np.random.normal(size=int(1e4))
>>> pr.univariate.samp_entropy(noise, m=2, r=1.5)
>>> # now we can do that for multiple scales (MSE):
>>> [pr.univariate.samp_entropy(noise, m=2, r=1.5, tau=tau) for tau in range(1, 5)]
pyrem.univariate.spectral_entropy(a, sampling_freq, bands=None)[source]

Compute spectral entropy of a signal with respect to frequency bands. The power spectrum is computed through fft. Then, it is normalised and assimilated to a probability density function. The entropy of the signal \(x\) can be expressed by:

\[H(x) = -\sum_{f=0}^{f = f_s/2} PSD(f) log_2[PSD(f)]\]

Where:

\(PSD\) is the normalised power spectrum (Power Spectrum Density), and

\(f_s\) is the sampling frequency

Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • sampling_freq (float) – the sampling frequency
  • bands – a list of numbers delimiting the bins of the frequency bands. If None the entropy is computed over the whole range of the DFT (from 0 to \(f_s/2\))
Returns:

the spectral entropy; a scalar

pyrem.univariate.svd_entropy(a, tau, de)[source]

Compute the Singular Value Decomposition entropy of a signal with embedding dimension “de” and delay “tau” [PYEEG].

Note

Difference with PyEEG:

The result differs from PyEEG implementation because \(log_2\) is used (as opposed to natural logarithm in PyEEG code), according to the definition in their paper [PYEEG] (eq. 9):

\[H_{SVD} = -\sum{\bar\sigma{}_i log_2 \bar\sigma{}_i}\]
Parameters:
  • a (ndarray or Signal) – a one dimensional floating-point array representing a time series.
  • tau (int) – the delay
  • de (int) – the embedding dimension
Returns:

the SVD entropy, a scalar

Return type:

float