elephant.signal_processing.zscore

elephant.signal_processing.zscore(signal, inplace=True)[source]

Apply a z-score operation to one or several neo.AnalogSignal objects.

The z-score operation subtracts the mean \(\mu\) of the signal, and divides by its standard deviation \(\sigma\):

\[Z(x(t)) = \frac{x(t)-\mu}{\sigma}\]

If a neo.AnalogSignal object containing multiple signals is provided, the z-transform is always calculated for each signal individually.

If a list of neo.AnalogSignal objects is supplied, the mean and standard deviation are calculated across all objects of the list. Thus, all list elements are z-transformed by the same values of \(\\mu\) and \(\sigma\). For a neo.AnalogSignal that contains multiple signals, each signal of the array is treated separately across list elements. Therefore, the number of signals must be identical for each neo.AnalogSignal object of the list.

Parameters:
signalneo.AnalogSignal or list of neo.AnalogSignal

Signals for which to calculate the z-score.

inplacebool, optional

If True, the contents of the input signal is replaced by the z-transformed signal, if possible, i.e when the signal type is float. If the signal type is not float, an error is raised. If False, a copy of the original signal is returned. Default: True

Returns:
signal_ztransformedneo.AnalogSignal or list of neo.AnalogSignal

The output format matches the input format: for each input neo.AnalogSignal, a corresponding neo.AnalogSignal is returned, containing the z-transformed signal with dimensionless unit.

Raises:
ValueError

If inplace is True and the type of signal is not float.

Notes

You may supply a list of neo.AnalogSignal objects, where each object in the list contains the data of one trial of the experiment, and each signal of the neo.AnalogSignal corresponds to the recordings from one specific electrode in a particular trial. In this scenario, you will z-transform the signal of each electrode separately, but transform all trials of a given electrode in the same way.

Examples

Z-transform a single neo.AnalogSignal, containing only a single signal.

>>> import neo
>>> import numpy as np
>>> import quantities as pq
>>> from elephant.signal_processing import zscore
>>> a = neo.AnalogSignal(
...       np.array([1, 2, 3, 4, 5, 6]).reshape(-1,1) * pq.mV,
...       t_start=0*pq.s, sampling_rate=1000*pq.Hz)
>>> zscore(a).as_quantity()
array([[-1.46385011],
       [-0.87831007],
       [-0.29277002],
       [ 0.29277002],
       [ 0.87831007],
       [ 1.46385011]]) * dimensionless

Z-transform a single neo.AnalogSignal containing multiple signals.

>>> b = neo.AnalogSignal(
...       np.transpose([[1, 2, 3, 4, 5, 6],
...                     [11, 12, 13, 14, 15, 16]]) * pq.mV,
...       t_start=0*pq.s, sampling_rate=1000*pq.Hz)
>>> zscore(b, inplace=False).as_quantity()
array([[-1.46385011, -1.46385011],
       [-0.87831007, -0.87831007],
       [-0.29277002, -0.29277002],
       [ 0.29277002,  0.29277002],
       [ 0.87831007,  0.87831007],
       [ 1.46385011,  1.46385011]]) * dimensionless

Z-transform a list of neo.AnalogSignal, each one containing more than one signal:

>>> c = neo.AnalogSignal(
...       np.transpose([[21, 22, 23, 24, 25, 26],
...                     [31, 32, 33, 34, 35, 36]]) * pq.mV,
...       t_start=0*pq.s, sampling_rate=1000*pq.Hz)
>>> zscore([b, c])  
[<AnalogSignal(array([[-1.11669108, -1.08361877],
   [-1.0672076 , -1.04878252],
   [-1.01772411, -1.01394628],
   [-0.96824063, -0.97911003],
   [-0.91875714, -0.94427378],
   [-0.86927366, -0.90943753]]) * dimensionless, [0.0 s, 0.006 s],
   sampling rate: 1000.0 Hz)>,
   <AnalogSignal(array([[ 0.78170952,  0.84779261],
   [ 0.86621866,  0.90728682],
   [ 0.9507278 ,  0.96678104],
   [ 1.03523694,  1.02627526],
   [ 1.11974608,  1.08576948],
   [ 1.20425521,  1.1452637 ]]) * dimensionless, [0.0 s, 0.006 s],
   sampling rate: 1000.0 Hz)>]