wonambi.trans.math module

Convenient module to convert data based on simple mathematical operations.

wonambi.trans.math.dB(x)[source]
wonambi.trans.math.get_descriptives(data)[source]

Get mean, SD, and mean and SD of log values.

Parameters:

data (ndarray) – Data with segment as first dimension and all other dimensions raveled into second dimension.

Returns:

dict of ndarray – each entry is a 1-D vector of descriptives over segment dimension

wonambi.trans.math.math(data, operator=None, operator_name=None, axis=None)[source]

Apply mathematical operation to each trial and channel individually.

Parameters:
  • data (instance of DataTime, DataFreq, or DataTimeFreq) –

  • operator (function or tuple of functions, optional) – function(s) to run on the data.

  • operator_name (str or tuple of str, optional) – name of the function(s) to run on the data.

  • axis (str, optional) – for functions that accept it, which axis you should run it on.

Returns:

instance of Data – data where the trials underwent operator.

Raises:
  • TypeError – If you pass both operator and operator_name.

  • ValueError – When you try to operate on an axis that has already been removed.

Notes

operator and operator_name are mutually exclusive. operator_name is given as shortcut for most common operations.

If a function accepts an ‘axis’ argument, you need to pass ‘axis’ to the constructor. In this way, it’ll apply the function to the correct dimension.

The possible point-wise operator_name are: ‘absolute’, ‘angle’, ‘dB’ (=10 * log10), ‘exp’, ‘log’, ‘sqrt’, ‘square’, ‘unwrap’

The operator_name’s that need an axis, but do not remove it: ‘hilbert’, ‘diff’, ‘detrend’

The operator_name’s that need an axis and remove it: ‘mean’, ‘gmean’ (geometric mean), ‘median’, ‘mode’, ‘std’

Examples

You can pass a single value or a tuple. The order starts from left to right, so abs of the hilbert transform, should be:

>>> rms = math(data, operator_name=('hilbert', 'abs'), axis='time')

If you want to pass the power of three, use lambda (or partial):

>>> p3 = lambda x: power(x, 3)
>>> data_p3 = math(data, operator=p3)

Note that lambdas are fine with point-wise operation, but if you want them to operate on axis, you need to pass ‘’axis’’ as well, so that:

>>> std_ddof = lambda x, axis: std(x, axis, ddof=1)
>>> data_std = math(data, operator=std_ddof)

If you don’t pass ‘axis’ in lambda, it’ll never know on which axis the function should be applied and you’ll get unpredictable results.

If you want to pass a function that operates on an axis and removes it (for example, if you want the max value over time), you need to add an argument in your function called ‘’keepdims’’ (the values won’t be used):

>>> def func(x, axis, keepdims=None):
>>>     return nanmax(x, axis=axis)