fix: Check if configs appears multiple times when creating an obs (#216)

This commit is contained in:
s-kuberski 2023-11-10 19:13:11 +01:00 committed by GitHub
parent eb83ff2091
commit d689959b1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View file

@ -104,7 +104,9 @@ 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 ValueError("Unsorted idx for idl[%s]" % (name)) raise ValueError("Unsorted idx for idl[%s] at position %s" % (name, ' '.join(['%s' % (pos + 1) for pos in np.where(np.diff(idx) < 0)[0]])))
elif np.any(dc == 0):
raise ValueError("Duplicate entries in idx for idl[%s] at position %s" % (name, ' '.join(['%s' % (pos + 1) for pos in np.where(np.diff(idx) == 0)[0]])))
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:

View file

@ -40,6 +40,10 @@ def test_Obs_exceptions():
pe.Obs([np.random.rand(4)], ['name']) pe.Obs([np.random.rand(4)], ['name'])
with pytest.raises(ValueError): 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(ValueError):
pe.Obs([np.random.rand(5)], ['1'], idl=[[1, 2, 3, 3, 5]])
with pytest.raises(ValueError):
pe.Obs([np.random.rand(5)], ['1'], idl=[[1, 1, 3, 1, 5]])
with pytest.raises(TypeError): 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(ValueError): with pytest.raises(ValueError):