Example #1
0
#!/usr/bin/env python

import operator

from random import choice
from stream import repeatcall, apply, fold, takei, takewhile, drop, item


vectoradd = lambda u, v: zip(u, v) >> apply(operator.add) >> list

Origin = [0, 0]
Directions = [[1,0], [0,1], [-1,0], [0,-1]]

randwalk = lambda: repeatcall(choice, Directions) >> fold(vectoradd, initval=Origin)

def returned(n):
	"""Generate a random walk and return True if the walker has returned to
	the origin after taking `n` steps.
	"""
	## `takei` yield lazily so we can short-circuit and avoid computing the rest of the walk
	for pos in randwalk() >> drop(1) >> takei(xrange(n-1)):
		if pos == Origin:
			return True
	return False

def first_return():
	"""Generate a random walk and return its length upto the moment
	that the walker first returns to the origin.

	It is mathematically provable that the walker will eventually return,
	meaning that the function call will halt, although it may take
Example #2
0
        print("couldn't open file; using stdout")
        fp = sys.stdout
print(str(fp), file=sys.stderr)

#define what we need to do moving averages
weights = [1.0/win_len for i in range(win_len)]
def inner(window):
    """ Computes the inner product of window and weights.
    weights must be defined outside to avoid a useless rezipping 
    when using this in a stream.
    """
    acc =  sum((i*w for i,w in zip(window, weights)))
    return acc

#get an infinite stream of uniform random floats
zsource   = repeatcall(rand.random)

# WIRING
# make our moving average window
winstream = ( zsource >> chop(win_len)  )
# compute the windowed average
xstream   = ( winstream >> stream.map(inner) )

# EXECUTING
if view_len > 0:
    ts = time()
    for i in range(view_len):
        fp.write(str(next(xstream.iterator))+'\n')
    print("time: %f" % (time()-ts), file=sys.stderr)
    print("items_per_sec: %f" % (view_len/(time()-ts)), file=sys.stderr)
if view_len < 0:
Example #3
0
        accept_branch, reject_branch = itertools.tee(iter(inpipe))
	accept = lambda x: abs(x) <  self.thresh
	reject = lambda x: abs(x) >= self.thresh
	self.iterator = itertools.ifilter(accept, accept_branch)
	reject_branch = itertools.ifilter(reject, reject_branch)
	Stream.pipe(reject_branch, self.named_stream)
	return self
    
    def __call__(self, iterator):
        return itertools.imap(push, iter(inpipe))
    

if __name__ == "__main__":
    varstream = zscore()
    #repeatcall(rand.normalvariate, 0,1) >> varstream
    repeatcall(rand.random) >> varstream
    x = (varstream >> item[:500])
    print(varstream)
    #hist(x)
    thresh = 1
    #normal_accept = filter(lambda x : abs(x)<=thresh)
    #normal_reject = filter(lambda x : abs(x)>thresh)
    #varstream >> tee(normal_accept)
    #varstream >> normal_reject
    #hist(normal_accept >> item[:500])
    #hist(normal_reject >> item[:500])

    extremes = Stream()
    typicals = (varstream >> threshold_tee(extremes, 2))
    print(typicals >> item[:500])
    print(extremes >> item[:50])