scipy-svn@scip... scipy-svn@scip...
Thu Apr 12 09:13:54 CDT 2007

```Author: mattknox_ca
Date: 2007-04-12 09:13:39 -0500 (Thu, 12 Apr 2007)
New Revision: 2906

Modified:
Log:
moved functions into moving_funcs

===================================================================
--- trunk/Lib/sandbox/timeseries/addons/filters.py	2007-04-12 14:12:43 UTC (rev 2905)
+++ trunk/Lib/sandbox/timeseries/addons/filters.py	2007-04-12 14:13:39 UTC (rev 2906)
@@ -21,140 +21,8 @@
marray = MA.array

+from moving_funcs import mov_average_expw, cmov_average, cmov_mean, \
+                         cmov_window

-__all__ = ['expmave'
-           'running_window', 'running_mean'
-           ]
-
-#####---------------------------------------------------------------------------
-#---- --- Moving average functions ---
-#####---------------------------------------------------------------------------
-def expmave(data, n, tol=1e-6):
-    """Calculates the exponential moving average of a series.
-
-:Parameters:
-    data : ndarray
-        Data as a valid (subclass of) ndarray or MaskedArray. In particular,
-        TimeSeries objects are valid here.
-    n : int
-        Time periods. The smoothing factor is 2/(n + 1)
-    tol : float, *[1e-6]*
-        Tolerance for the definition of the mask. When data contains masked
-        values, this parameter determinea what points in the result should be masked.
-        Values in the result that would not be "significantly" impacted (as
-        determined by this parameter) by the masked values are left unmasked.
-"""
-    data = marray(data, copy=True, subok=True)
-    _data = data._data
-    #
-    k = 2./float(n + 1)
-    def expmave_sub(a, b):
-        return b + k * (a - b)
-    #
-    data._data.flat = N.frompyfunc(expmave_sub, 2, 1).accumulate(_data)
-        marker = 1. - N.frompyfunc(expmave_sub, 2, 1).accumulate(_unmasked)
-        data._mask[marker > tol] = True
-    #
-    return data
-
-def weightmave(data, n):
-    data = marray(data, subok=True, copy=True)
-    # Set the data
-    _data = data._data
-    tmp = N.empty_like(_data)
-    tmp[:n] = _data[:n]
-    s = 0
-    for i in range(n, len(data)):
-        s += _data[i] - _data[i-n]
-        tmp[i] = n*_data[i] + tmp[i-1] - s
-    tmp *= 2./(n*(n+1))
-    data._data.flat = tmp
-        msk += range(n)
-    return data
-
-
-#...............................................................................
-def running_window(data, window_type, window_size):
-    """Applies a running window of type window_type and size window_size on the
-    data.
-
-    Returns a (subclass of) MaskedArray. The k first and k last data are always
-    masked (with k=window_size//2). When data has a missing value at position i,
-    the result has missing values in the interval [i-k:i+k+1].
-
-
-:Parameters:
-    data : ndarray
-        Data to process. The array should be at most 2D. On 2D arrays, the window
-        is applied recursively on each column.
-    window_type : string/tuple/float
-        Window type (see Notes)
-    window_size : integer
-        The width of the window.
-
-Notes
------
-
-The recognized window types are: boxcar, triang, blackman, hamming, hanning,
-bartlett, parzen, bohman, blackmanharris, nuttall, barthann, kaiser (needs beta),
-gaussian (needs std), general_gaussian (needs power, width), slepian (needs width).
-If the window requires parameters, the window_type argument should be a tuple
-with the first argument the string name of the window, and the next arguments
-the needed parameters. If window_type is a floating point number, it is interpreted
-as the beta parameter of the kaiser window.
-
-Note also that only boxcar has been thoroughly tested.
-    """
-    #
-    data = marray(data, copy=True, subok=True)
-    window = get_window(window_type, window_size, fftbins=False)
-    (n, k) = (len(data), window_size//2)
-    #
-    if data.ndim == 1:
-        data._data.flat = convolve(data._data, window)[k:n+k] / float(window_size)
-    elif data.ndim == 2:
-        for i in range(data.shape[-1]):
-            _data = data._data[:,i]
-            _data.flat = convolve(_data, window)[k:n+k] / float(window_size)
-    else:
-        raise ValueError, "Data should be at most 2D"
-    return data
-
-def running_mean(data, width):
-    """Computes the running mean of size width on the data.
-
-    Returns a (subclass of) MaskedArray. The k first and k last data are always
-    masked (with k=window_size//2). When data has a missing value at position i,
-    the result has missing values in the interval [i-k:i+k+1].
-
-:Parameters:
-    data : ndarray
-        Data to process. The array should be at most 2D. On 2D arrays, the window
-        is applied recursively on each column.
-    window_size : integer
-        The width of the window.
-    """
-    return running_window(data, 'boxcar', width)
-
-################################################################################
-if __name__ == '__main__':
-    from maskedarray.testutils import assert_equal, assert_almost_equal
-    from timeseries import time_series, thisday
-    #
-    data = MA.arange(100)
+__all__ = ['mov_average_expw'
+           'cmov_average', 'cmov_mean', 'cmov_window']

```