def average(norm_img: Stream, reset: Stream = None, **kwargs): """Perform a running average Parameters ---------- norm_img : Stream The data to be averaged as a stream reset : Stream, optional If provided, when data comes from this stream reset the averaging Returns ------- locals : dict The locals """ img_sum = norm_img.accumulate(op.add, reset_stream=reset) img_count = norm_img.accumulate(sum_state, start=0, reset_stream=reset) ave_img = img_sum.zip(img_count).starmap(op.truediv) return locals()
def test_accumulate(): a = Stream() b = a.accumulate(lambda x, y: x + y) L = b.sink_to_list() LL = [] for i in range(10): a.emit(i) if len(LL) == 0: LL.append(i) else: LL.append(i + LL[-1]) assert L == LL
def test_accumulate_reset(): a = Stream() rn = Stream() b = a.accumulate(lambda x, y: x + y, reset_stream=rn) L = b.sink_to_list() LL = [] for i in range(10): if i == 5: rn.emit('hi') a.emit(i) if len(LL) == 0 or i == 5: LL.append(i) else: LL.append(i + LL[-1]) assert L == LL
def test_accumulate_errors_raises(): a = Stream() b = a.accumulate(lambda x, y: x / y) with pytest.raises(ZeroDivisionError): a.emit(1) a.emit(0)