Skip to content

filtering

Set of filtering functions.

Todo

  • Correct border effects of running mean

runningmean(array, window)

Compute centered running average with given window size.

This function returns the centered based running average of the given data. The output of this function is of the same length as the input, by assuming that the given data is zero before and after the given series. Hence, there are border affects which are not corrected.

Warning

If the given window is even (not symmetric) it will be shifted towards the beginning of the current value. So for window=4, it will consider the current position \(i\), the two to the left \(i-2\) and \(i-1\) and one to the right \(i+1\).

Function is taken from lapis: https://stackoverflow.com/questions/13728392/moving-average-or-running-mean

Parameters:

  • array (ndarray) –

    One dimensional numpy array.

  • window (int) –

    Integer which specifies window-width.

Returns:

  • array_rmean ( ndarray ) –

    Data which is time-averaged over the specified window.

Source code in src/msmhelper/utils/filtering.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def runningmean(array, window):
    r"""Compute centered running average with given window size.

    This function returns the centered based running average of the given
    data. The output of this function is of the same length as the input,
    by assuming that the given data is zero before and after the given series.
    Hence, there are border affects which are not corrected.

    !!! warning
        If the given window is even (not symmetric) it will be shifted towards
        the beginning of the current value. So for `window=4`, it will consider
        the current position \(i\), the two to the left \(i-2\) and \(i-1\) and
        one to the right \(i+1\).

    Function is taken from lapis:
    https://stackoverflow.com/questions/13728392/moving-average-or-running-mean

    Parameters
    ----------
    array : ndarray
        One dimensional numpy array.
    window : int
        Integer which specifies window-width.

    Returns
    -------
    array_rmean : ndarray
        Data which is time-averaged over the specified window.

    """
    ndim = _np.asarray(array).ndim
    if ndim > 1:
        raise ValueError(
            'Runningmean is only defined for 1D data, but'
            f'{ndim:.0f}D data were provided.'
        )

    # Calculate running mean
    return _np.convolve(
        array,
        _np.ones(window) / window,
        mode='same',
    )

gaussian_filter(array, sigma)

Compute Gaussian filter along axis=0.

Parameters:

  • array (ndarray) –

    One dimensional numpy array.

  • sigma (float) –

    Float which specifies the standard deviation of the Gaussian kernel (window-width).

Returns:

  • array_filtered ( ndarray ) –

    Data which is time-averaged with the specified Gaussian kernel.

Source code in src/msmhelper/utils/filtering.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def gaussian_filter(array, sigma):
    r"""Compute Gaussian filter along axis=0.

    Parameters
    ----------
    array : ndarray
        One dimensional numpy array.
    sigma : float
        Float which specifies the standard deviation of the Gaussian kernel
        (window-width).

    Returns
    -------
    array_filtered : ndarray
        Data which is time-averaged with the specified Gaussian kernel.

    """
    array = _np.asarray(array, dtype=_np.float64)
    ndim = array.ndim
    if ndim > 2:
        raise ValueError(
            'Gaussian filtering is only defined for 1D and 2D data, but'
            f'{ndim:.0f}D data were provided.'
        )

    # Calculate running mean
    if ndim == 1:
        return _gaussian_filter_1d(
            array,
            sigma=sigma,
            mode='nearest',
        )
    return _gaussian_filter(
        array,
        sigma=(sigma, 0),
        mode='nearest',
    )