diff --git a/fretbursts/burstlib.py b/fretbursts/burstlib.py index 2a14cdca..2989618e 100644 --- a/fretbursts/burstlib.py +++ b/fretbursts/burstlib.py @@ -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): @@ -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()} diff --git a/fretbursts/tests/test_burstlib.py b/fretbursts/tests/test_burstlib.py index a924a735..c6a4e964 100644 --- a/fretbursts/tests/test_burstlib.py +++ b/fretbursts/tests/test_burstlib.py @@ -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): @@ -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')]