Example #1
0
def window_wise(seq, size):
    """Return all windows of `size` elements in `seq`.

       >>> _show (window_wise (range (5), 1))
       [(0,), (1,), (2,), (3,), (4,)]
       >>> _show (window_wise (range (5), 2))
       [(0, 1), (1, 2), (2, 3), (3, 4)]
       >>> _show (window_wise (range (5), 3))
       [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
       >>> _show (window_wise (range (5), 4))
       [(0, 1, 2, 3), (1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 5))
       [(0, 1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 6))
       []
    """
    from _TFL.DL_List import DL_Ring
    s = iter(seq)
    h = tuple((next(s) for i in range(size)))
    if len(h) == size:
        w = DL_Ring(h)
        yield tuple(w.values())
        while True:
            w.pop_front()
            w.append(next(s))
            yield tuple(w.values())
Example #2
0
def window_wise(seq, size):
    """Return all windows of `size` elements in `seq`.

       >>> _show (window_wise (range (5), 1))
       [(0,), (1,), (2,), (3,), (4,)]
       >>> _show (window_wise (range (5), 2))
       [(0, 1), (1, 2), (2, 3), (3, 4)]
       >>> _show (window_wise (range (5), 3))
       [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
       >>> _show (window_wise (range (5), 4))
       [(0, 1, 2, 3), (1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 5))
       [(0, 1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 6))
       []
    """
    from _TFL.DL_List import DL_Ring
    s = iter(seq)
    try:
        h = tuple((next(s) for i in range(size)))
    except RuntimeError:
        pass
    else:
        if len(h) == size:
            w = DL_Ring(h)
            yield tuple(w.values())
            while True:
                w.pop_front()
                try:
                    nxt = next(s)
                except StopIteration:
                    break
                else:
                    w.append(nxt)
                    yield tuple(w.values())
Example #3
0
def window_wise (seq, size) :
    """Return all windows of `size` elements in `seq`.

       >>> _show (window_wise (range (5), 1))
       [(0,), (1,), (2,), (3,), (4,)]
       >>> _show (window_wise (range (5), 2))
       [(0, 1), (1, 2), (2, 3), (3, 4)]
       >>> _show (window_wise (range (5), 3))
       [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
       >>> _show (window_wise (range (5), 4))
       [(0, 1, 2, 3), (1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 5))
       [(0, 1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 6))
       []
    """
    from _TFL.DL_List import DL_Ring
    s = iter  (seq)
    try :
        h = tuple ((next (s) for i in range (size)))
    except RuntimeError :
        pass
    else :
        if len (h) == size :
            w = DL_Ring (h)
            yield tuple (w.values ())
            while True:
                w.pop_front ()
                try :
                    nxt = next (s)
                except StopIteration :
                    break
                else :
                    w.append    (nxt)
                    yield tuple (w.values ())
Example #4
0
def window_wise (seq, size) :
    """Return all windows of `size` elements in `seq`.

       >>> _show (window_wise (range (5), 1))
       [(0,), (1,), (2,), (3,), (4,)]
       >>> _show (window_wise (range (5), 2))
       [(0, 1), (1, 2), (2, 3), (3, 4)]
       >>> _show (window_wise (range (5), 3))
       [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
       >>> _show (window_wise (range (5), 4))
       [(0, 1, 2, 3), (1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 5))
       [(0, 1, 2, 3, 4)]
       >>> _show (window_wise (range (5), 6))
       []
    """
    from _TFL.DL_List import DL_Ring
    s = iter  (seq)
    h = tuple ((next (s) for i in range (size)))
    if len (h) == size :
        w = DL_Ring (h)
        yield tuple (w.values ())
        while True:
            w.pop_front ()
            w.append    (next (s))
            yield tuple (w.values ())
Example #5
0
def moving_average(s, n, central=False):
    """Generator for moving average of `n` data points over sequence `s`.

       >>> list (moving_average (range (10), 3))
       [(2, 1.0), (3, 2.0), (4, 3.0), (5, 4.0), (6, 5.0), (7, 6.0), (8, 7.0), (9, 8.0)]
       >>> list (moving_average (range (10), 4))
       [(3, 1.5), (4, 2.5), (5, 3.5), (6, 4.5), (7, 5.5), (8, 6.5), (9, 7.5)]
       >>> list (moving_average (range (10), 5))
       [(4, 2.0), (5, 3.0), (6, 4.0), (7, 5.0), (8, 6.0), (9, 7.0)]
       >>> list (moving_average (range (10), 3, True))
       [(1, 1.0), (2, 2.0), (3, 3.0), (4, 4.0), (5, 5.0), (6, 6.0), (7, 7.0), (8, 8.0)]
       >>> list (moving_average (range (10), 5, True))
       [(2, 2.0), (3, 3.0), (4, 4.0), (5, 5.0), (6, 6.0), (7, 7.0)]
       >>> list (moving_average (range (10), 7, True))
       [(3, 3.0), (4, 4.0), (5, 5.0), (6, 6.0)]
       >>> list (moving_average (range (10), 9, True))
       [(4, 4.0), (5, 5.0)]
       >>> list (moving_average (range (10), 10, True))
       [(5, 4.5)]
       >>> list (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 3, True))
       [(1, 1.2933333333333332), (2, 1.2933333333333332), (3, 1.2899999999999998), (4, 1.2966666666666664), (5, 1.293333333333333)]
       >>> list (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 5, True))
       [(2, 1.292), (3, 1.298), (4, 1.29)]
       >>> list (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 7, True))
       [(3, 1.2914285714285714)]
    """
    if central:
        i = n // 2
    else:
        i = n - 1
    s = iter(s)
    w = DL_Ring(next(s) for k in range(n))
    m = float(n)
    v = sum(w.values()) / m
    yield i, v
    for x in s:
        i += 1
        v += (-w.pop_front() / m) + (x / m)
        w.append(x)
        yield i, v
Example #6
0
def moving_average (s, n) :
    """Generator for moving average of `n` data points over sequence `s`.

    >>> def show (ma_s, fmt = "%.1f") :
    ...     print ("[" + ", ".join (fmt % v for v in ma_s) + "]")

    >>> show (moving_average (range (10), 2))
    [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5]
    >>> show (moving_average (range (10), 3))
    [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    >>> show (moving_average (range (10), 4))
    [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5]
    >>> show (moving_average (range (10), 5))
    [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
    >>> fmt = "%.2f"
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 3), fmt)
    [1.29, 1.29, 1.29, 1.30, 1.29]
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 5), fmt)
    [1.29, 1.30, 1.29]
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 7), fmt)
    [1.29]
    """
    if n == 1 :
        for x in s :
            yield x
    else :
        m  = float   (n)
        s  = iter    (s)
        w  = DL_Ring (next (s) / m for k in range (n))
        ma = sum     (w.values ())
        yield ma
        for x in s :
            x_m  = x / m
            ma  -= w.pop_front ()
            ma  += x_m
            w.append (x_m)
            yield ma
Example #7
0
def moving_average (s, n) :
    """Generator for moving average of `n` data points over sequence `s`.

    >>> def show (ma_s, fmt = "%.1f") :
    ...     print ("[" + ", ".join (fmt % v for v in ma_s) + "]")

    >>> show (moving_average (range (10), 2))
    [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5]
    >>> show (moving_average (range (10), 3))
    [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    >>> show (moving_average (range (10), 4))
    [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5]
    >>> show (moving_average (range (10), 5))
    [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
    >>> fmt = "%.2f"
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 3), fmt)
    [1.29, 1.29, 1.29, 1.30, 1.29]
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 5), fmt)
    [1.29, 1.30, 1.29]
    >>> show (moving_average ((1.28, 1.31, 1.29, 1.28, 1.30, 1.31, 1.27), 7), fmt)
    [1.29]
    """
    if n == 1 :
        yield from s 
    else :
        m  = float   (n)
        s  = iter    (s)
        w  = DL_Ring (next (s) / m for k in range (n))
        ma = sum     (w.values ())
        yield ma
        for x in s :
            x_m  = x / m
            ma  -= w.pop_front ()
            ma  += x_m
            w.append (x_m)
            yield ma