fab.preprocessing.filters

 1from .preprocessor import preprocessor
 2import numpy as np
 3
 4@preprocessor
 5def integrate(data, dims=None): 
 6    """ Returns sum of data along dimension dims"""         
 7    return data.sum(dim=dims)
 8
 9@preprocessor
10def mean(data, dims=None):      
11    """ Returns mean of data along dimension dims"""  
12    return data.mean(dim=dims)
13
14@preprocessor
15def normalize(data, method='L2', dims=None): 
16    """ Normalizes data according to method
17        
18        Args:
19            method: one of "L2", "area" or "max". Defaults to "L2".
20                Determines how the normalization factor is calculated
21                - "L2": square root of sum of squared values
22                - "area": area under the curve 
23                - "max": largest absolute value
24    """
25    match method:
26        case 'L2':
27            factor = np.sqrt((data**2).sum(dim=dims))
28        case 'area':
29            factor = np.abs(data).sum(dim=dims)
30        case 'max':
31            factor = np.abs(data).max(dim=dims)
32    return data / factor
33
34@preprocessor
35def moving_avg(data, dim=None, window=10):      
36    """ Returns moving average of data along dimension dims.
37        The window parameter determines the window size in samples."""
38    return data.rolling(**{dim: window}).mean()
39    
40@preprocessor
41def rescale(data, factor=1, offset=0):   
42    """ returnns data*factor + offset """     
43    return data*factor + offset
44
45@preprocessor
46def mathexpr(data, expr):
47    ''' Evaluates a string as a mathematical expression
48    
49        Args: 
50            expr: A string representing a mathematical expression
51                to be evaluated on the data. To refer to the 
52                loaded data in the expression, use the symbol `x`.
53                Numpy and xarray can be accessed as `np` and `xr`
54                respectively.
55        
56        Examples:
57            To calculate the the inverse of the data, use:
58            `__preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "1/x"`
59
60            The maximum value can be calcluded by:
61            `__preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "x.max()"`
62    '''
63    import asteval
64    import numpy as np
65    import xarray as xr
66
67    interp = asteval.Interpreter(minimal=True, 
68                                 usersyms={'x': data,
69                                           'np' : np,
70                                           'xr' : xr})
71    output = interp.eval(expr, raise_errors=True)
72    
73    if not (isinstance(output, xr.DataArray) or isinstance(output, xr.Dataset)):
74        raise ValueError("mathexpr preprocessor must return a xr.DataArray or xr.Dataset")
75    return output
@preprocessor
def integrate(data, dims=None):
5@preprocessor
6def integrate(data, dims=None): 
7    """ Returns sum of data along dimension dims"""         
8    return data.sum(dim=dims)

Returns sum of data along dimension dims

@preprocessor
def mean(data, dims=None):
10@preprocessor
11def mean(data, dims=None):      
12    """ Returns mean of data along dimension dims"""  
13    return data.mean(dim=dims)

Returns mean of data along dimension dims

@preprocessor
def normalize(data, method='L2', dims=None):
15@preprocessor
16def normalize(data, method='L2', dims=None): 
17    """ Normalizes data according to method
18        
19        Args:
20            method: one of "L2", "area" or "max". Defaults to "L2".
21                Determines how the normalization factor is calculated
22                - "L2": square root of sum of squared values
23                - "area": area under the curve 
24                - "max": largest absolute value
25    """
26    match method:
27        case 'L2':
28            factor = np.sqrt((data**2).sum(dim=dims))
29        case 'area':
30            factor = np.abs(data).sum(dim=dims)
31        case 'max':
32            factor = np.abs(data).max(dim=dims)
33    return data / factor

Normalizes data according to method

Arguments:
  • method: one of "L2", "area" or "max". Defaults to "L2". Determines how the normalization factor is calculated
    • "L2": square root of sum of squared values
    • "area": area under the curve
    • "max": largest absolute value
@preprocessor
def moving_avg(data, dim=None, window=10):
35@preprocessor
36def moving_avg(data, dim=None, window=10):      
37    """ Returns moving average of data along dimension dims.
38        The window parameter determines the window size in samples."""
39    return data.rolling(**{dim: window}).mean()

Returns moving average of data along dimension dims. The window parameter determines the window size in samples.

@preprocessor
def rescale(data, factor=1, offset=0):
41@preprocessor
42def rescale(data, factor=1, offset=0):   
43    """ returnns data*factor + offset """     
44    return data*factor + offset

returnns data*factor + offset

@preprocessor
def mathexpr(data, expr):
46@preprocessor
47def mathexpr(data, expr):
48    ''' Evaluates a string as a mathematical expression
49    
50        Args: 
51            expr: A string representing a mathematical expression
52                to be evaluated on the data. To refer to the 
53                loaded data in the expression, use the symbol `x`.
54                Numpy and xarray can be accessed as `np` and `xr`
55                respectively.
56        
57        Examples:
58            To calculate the the inverse of the data, use:
59            `__preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "1/x"`
60
61            The maximum value can be calcluded by:
62            `__preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "x.max()"`
63    '''
64    import asteval
65    import numpy as np
66    import xarray as xr
67
68    interp = asteval.Interpreter(minimal=True, 
69                                 usersyms={'x': data,
70                                           'np' : np,
71                                           'xr' : xr})
72    output = interp.eval(expr, raise_errors=True)
73    
74    if not (isinstance(output, xr.DataArray) or isinstance(output, xr.Dataset)):
75        raise ValueError("mathexpr preprocessor must return a xr.DataArray or xr.Dataset")
76    return output

Evaluates a string as a mathematical expression

Args: expr: A string representing a mathematical expression to be evaluated on the data. To refer to the loaded data in the expression, use the symbol x. Numpy and xarray can be accessed as np and xr respectively.

Examples:

To calculate the the inverse of the data, use: __preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "1/x"

The maximum value can be calcluded by: __preprocess__ = [ {__name__='fab.preprocessing.mathexpr', expr= "x.max()"