Skip to content

Commit

Permalink
Merge pull request #70 from CoffeaTeam/fix-accumulator
Browse files Browse the repository at this point in the history
Don't assign in-place!
  • Loading branch information
lgray authored Apr 16, 2019
2 parents 002575b + 21bcb10 commit 94da063
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions fnal_column_analysis_tools/processor/accumulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def identity(self):

def add(self, other):
if isinstance(other, accumulator):
self.value += other.value
self.value = self.value + other.value
else:
self.value += other
self.value = self.value + other


class set_accumulator(set, AccumulatorABC):
Expand Down
11 changes: 10 additions & 1 deletion tests/test_accumulators.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from __future__ import print_function, division
from fnal_column_analysis_tools import processor
import numpy as np


def test_accumulators():
a = processor.accumulator(0.)
a += 3.
a += processor.accumulator(2)
assert a.value == 5.
assert a.identity().value == 0.

a = processor.accumulator(np.array([0.]))
a += 3.
a += processor.accumulator(2)
assert a.value == np.array([5.])
assert a.identity().value == np.array([0.])

b = processor.set_accumulator({'apples', 'oranges'})
b += {'pears'}
Expand All @@ -24,7 +32,8 @@ def test_accumulators():
assert c['fruit'] == {'apples', 'oranges', 'pears', 'grapes', 'cherries'}

d = processor.defaultdict_accumulator(lambda: processor.accumulator(0.))
d['x'] = processor.accumulator(0.) + 4
d['x'] = processor.accumulator(0.)
d['x'] += 4.
d['y'] += 5.
d['z'] += d['x']
d['x'] += d['y']
Expand Down

0 comments on commit 94da063

Please sign in to comment.