-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_window.py
More file actions
27 lines (22 loc) · 1.05 KB
/
moving_window.py
File metadata and controls
27 lines (22 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def moving_window(sequence,winsize,step):
"""Returns a python generator that will iterate through defined chunks ('winsize') a given distance ('step')
apart for, an input list ('sequence'). The input sequence must be iterable and the window size must reflect
the underlying distribution to give meaningful indices.
If you want to do a moving-window average quickly, call this to make the window."""
#import
import itertools
# Verify the inputs
try: it = iter(sequence)
except TypeError:
raise Exception("**ERROR** sequence must be iterable.")
if not ((type(winsize) == type(0)) and (type(step) == type(0))):
raise Exception("**ERROR** type(winsize) and type(step) must be int.")
if step > winsize:
raise Exception("**ERROR** step must not be larger than winsize.")
if winsize > len(sequence):
raise Exception("**ERROR** winsize must not be larger than sequence length.")
#pre-compute number of chunks to emit
numOfChunks=((len(sequence)-winsize)/step)+1
#do work
for i in range(0,numOfChunks*step,step):
yield sequence[i:i+winsize]