1
0
Fork 0
mirror of https://github.com/fjosw/pyerrors.git synced 2025-03-16 15:20:24 +01:00

chore: Exceptions in Obs.__init__ made more explicit.

This commit is contained in:
Fabian Joswig 2023-03-02 18:54:08 +00:00
parent dc7033e51f
commit 9ef7e504a0
No known key found for this signature in database
2 changed files with 16 additions and 16 deletions
pyerrors
tests

View file

@ -73,21 +73,21 @@ class Obs:
if kwargs.get("means") is None and len(samples): if kwargs.get("means") is None and len(samples):
if len(samples) != len(names): if len(samples) != len(names):
raise Exception('Length of samples and names incompatible.') raise ValueError('Length of samples and names incompatible.')
if idl is not None: if idl is not None:
if len(idl) != len(names): if len(idl) != len(names):
raise Exception('Length of idl incompatible with samples and names.') raise ValueError('Length of idl incompatible with samples and names.')
name_length = len(names) name_length = len(names)
if name_length > 1: if name_length > 1:
if name_length != len(set(names)): if name_length != len(set(names)):
raise Exception('names are not unique.') raise ValueError('Names are not unique.')
if not all(isinstance(x, str) for x in names): if not all(isinstance(x, str) for x in names):
raise TypeError('All names have to be strings.') raise TypeError('All names have to be strings.')
else: else:
if not isinstance(names[0], str): if not isinstance(names[0], str):
raise TypeError('All names have to be strings.') raise TypeError('All names have to be strings.')
if min(len(x) for x in samples) <= 4: if min(len(x) for x in samples) <= 4:
raise Exception('Samples have to have at least 5 entries.') raise ValueError('Samples have to have at least 5 entries.')
self.names = sorted(names) self.names = sorted(names)
self.shape = {} self.shape = {}
@ -105,13 +105,13 @@ class Obs:
elif isinstance(idx, (list, np.ndarray)): elif isinstance(idx, (list, np.ndarray)):
dc = np.unique(np.diff(idx)) dc = np.unique(np.diff(idx))
if np.any(dc < 0): if np.any(dc < 0):
raise Exception("Unsorted idx for idl[%s]" % (name)) raise ValueError("Unsorted idx for idl[%s]" % (name))
if len(dc) == 1: if len(dc) == 1:
self.idl[name] = range(idx[0], idx[-1] + dc[0], dc[0]) self.idl[name] = range(idx[0], idx[-1] + dc[0], dc[0])
else: else:
self.idl[name] = list(idx) self.idl[name] = list(idx)
else: else:
raise Exception('incompatible type for idl[%s].' % (name)) raise TypeError('incompatible type for idl[%s].' % (name))
else: else:
for name, sample in sorted(zip(names, samples)): for name, sample in sorted(zip(names, samples)):
self.idl[name] = range(1, len(sample) + 1) self.idl[name] = range(1, len(sample) + 1)
@ -127,7 +127,7 @@ class Obs:
self.shape[name] = len(self.idl[name]) self.shape[name] = len(self.idl[name])
self.N += self.shape[name] self.N += self.shape[name]
if len(sample) != self.shape[name]: if len(sample) != self.shape[name]:
raise Exception('Incompatible samples and idx for %s: %d vs. %d' % (name, len(sample), self.shape[name])) raise ValueError('Incompatible samples and idx for %s: %d vs. %d' % (name, len(sample), self.shape[name]))
self.r_values[name] = np.mean(sample) self.r_values[name] = np.mean(sample)
self.deltas[name] = sample - self.r_values[name] self.deltas[name] = sample - self.r_values[name]
self._value += self.shape[name] * self.r_values[name] self._value += self.shape[name] * self.r_values[name]

View file

@ -25,23 +25,23 @@ def test_sin2_cos2(value):
def test_Obs_exceptions(): def test_Obs_exceptions():
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(10)], ['1', '2']) pe.Obs([np.random.rand(10)], ['1', '2'])
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(10)], ['1'], idl=[]) pe.Obs([np.random.rand(10)], ['1'], idl=[])
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(10), np.random.rand(10)], ['1', '1']) pe.Obs([np.random.rand(10), np.random.rand(10)], ['1', '1'])
with pytest.raises(Exception): with pytest.raises(TypeError):
pe.Obs([np.random.rand(10), np.random.rand(10)], ['1', 1]) pe.Obs([np.random.rand(10), np.random.rand(10)], ['1', 1])
with pytest.raises(Exception): with pytest.raises(TypeError):
pe.Obs([np.random.rand(10)], [1]) pe.Obs([np.random.rand(10)], [1])
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(4)], ['name']) pe.Obs([np.random.rand(4)], ['name'])
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(5)], ['1'], idl=[[5, 3, 2 ,4 ,1]]) pe.Obs([np.random.rand(5)], ['1'], idl=[[5, 3, 2 ,4 ,1]])
with pytest.raises(Exception): with pytest.raises(TypeError):
pe.Obs([np.random.rand(5)], ['1'], idl=['t']) pe.Obs([np.random.rand(5)], ['1'], idl=['t'])
with pytest.raises(Exception): with pytest.raises(ValueError):
pe.Obs([np.random.rand(5)], ['1'], idl=[range(1, 8)]) pe.Obs([np.random.rand(5)], ['1'], idl=[range(1, 8)])
my_obs = pe.Obs([np.random.rand(6)], ['name']) my_obs = pe.Obs([np.random.rand(6)], ['name'])