Skip to content

Commit

Permalink
FIX: attribute access for the obsolete background attributes.
Browse files Browse the repository at this point in the history
- Do not raise AttributeError inside a property getter (use RuntimeError)
- Add test
  • Loading branch information
tritemio committed Mar 25, 2017
1 parent 21ae92b commit b850a59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 9 additions & 5 deletions fretbursts/burstlib.py
Expand Up @@ -1355,10 +1355,14 @@ def _obsolete_bg_attr(self, attrname, ph_sel):
bg_field = 'bg'
elif attrname in bg_mean_attrs:
bg_field = 'bg_mean'
if bg_field in self:
return getattr(self, bg_field)[ph_sel]
else:
raise AttributeError('No attribute `%s` found in Data.' % bg_field)
try:
value = getattr(self, bg_field)[ph_sel]
except AttributeError as e:
# This only happens when trying to access 'bg' because
# 'bg_mean' raises RuntimeError when missing.
msg = 'No attribute `%s` found. Please compute background first.'
raise RuntimeError(msg % bg_field) from e
return value

@property
def rate_m(self):
Expand Down Expand Up @@ -1629,7 +1633,7 @@ def nperiods(self):
@property
def bg_mean(self):
if 'bg' not in self:
raise AttributeError('No background found, compute it first.')
raise RuntimeError('No background found, compute it first.')
if not hasattr(self, '_bg_mean'):
self._bg_mean = {k: [bg_ch.mean() for bg_ch in bg_ph_sel]
for k, bg_ph_sel in self.bg.items()}
Expand Down
15 changes: 14 additions & 1 deletion fretbursts/tests/test_burstlib.py
Expand Up @@ -97,6 +97,19 @@ def list_array_allclose(list1, list2):
# Test functions
#


def test_bg_compatlayer_for_obsolete_attrs():
d = load_dataset_1ch(process=False)
attrs = ('bg_dd', 'bg_ad', 'bg_da', 'bg_aa',
'rate_m', 'rate_dd', 'rate_ad', 'rate_da', 'rate_aa')
for attr in attrs:
with pytest.raises(RuntimeError):
getattr(d, attr)
_alex_process(d)
for attr in attrs:
assert isinstance(getattr(d, attr), list)


def test_ph_times_compact(data_1ch):
"""Test calculation of ph_times_compact."""
def isinteger(x):
Expand Down Expand Up @@ -156,7 +169,7 @@ def test_bg_calc(data):
assert 'bg_auto_th_us0' in data
assert 'bg_auto_F_bg' in data
assert 'bg_th_us_user' not in data

data.calc_bg(bg.exp_fit, time_s=30, tail_min_us='auto', F_bg=1.7,
fit_allph=False)
streams = [s for s in data.ph_streams if s != Ph_sel('all')]
Expand Down

0 comments on commit b850a59

Please sign in to comment.