From d689959b1f34a359f37f3cb6c39e1c6985e2135f Mon Sep 17 00:00:00 2001 From: s-kuberski Date: Fri, 10 Nov 2023 19:13:11 +0100 Subject: [PATCH] fix: Check if configs appears multiple times when creating an obs (#216) --- pyerrors/obs.py | 4 +++- tests/obs_test.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pyerrors/obs.py b/pyerrors/obs.py index df287a10..3f86a6b3 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -104,7 +104,9 @@ class Obs: elif isinstance(idx, (list, np.ndarray)): dc = np.unique(np.diff(idx)) 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: self.idl[name] = range(idx[0], idx[-1] + dc[0], dc[0]) else: diff --git a/tests/obs_test.py b/tests/obs_test.py index 02d539ef..0631effe 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -40,6 +40,10 @@ def test_Obs_exceptions(): pe.Obs([np.random.rand(4)], ['name']) with pytest.raises(ValueError): 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): pe.Obs([np.random.rand(5)], ['1'], idl=['t']) with pytest.raises(ValueError):