From c8cb0e4cb81ce28d24973693a96a5db2281a6e28 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Dec 2021 12:00:10 +0100 Subject: [PATCH 1/5] feat: epsilon tensors moved to dirac submodule, tests added --- pyerrors/dirac.py | 24 ++++++++++++++++++++++++ pyerrors/input/hadrons.py | 12 ------------ tests/dirac_test.py | 12 ++++++++++++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/pyerrors/dirac.py b/pyerrors/dirac.py index f5350b94..7f68cb22 100644 --- a/pyerrors/dirac.py +++ b/pyerrors/dirac.py @@ -22,6 +22,30 @@ identity = np.array( dtype=complex) +def epsilon_tensor(i, j, k): + """Rank-3 epsilon tensor + + Based on https://codegolf.stackexchange.com/a/160375 + """ + test_set = set((i, j, k)) + if not (test_set <= set((1, 2, 3)) or test_set <= set((0, 1, 2))): + raise Exception("Unexpected input", i, j, k) + + return (i - j) * (j - k) * (k - i) / 2 + + +def epsilon_tensor_rank4(i, j, k, o): + """Rank-4 epsilon tensor + + Extension of https://codegolf.stackexchange.com/a/160375 + """ + test_set = set((i, j, k, o)) + if not (test_set <= set((1, 2, 3, 4)) or test_set <= set((0, 1, 2, 3))): + raise Exception("Unexpected input", i, j, k, o) + + return (i - j) * (j - k) * (k - i) * (i - o) * (j - o) * (o - k) / 12 + + def Grid_gamma(gamma_tag): """Returns gamma matrix in Grid labeling.""" if gamma_tag == 'Identity': diff --git a/pyerrors/input/hadrons.py b/pyerrors/input/hadrons.py index e139f2b2..efe4feb1 100644 --- a/pyerrors/input/hadrons.py +++ b/pyerrors/input/hadrons.py @@ -298,18 +298,6 @@ def read_Fourquark_hd5(path, filestem, ens_id, idl=None, vertices=["VA", "AV"]): return result_dict -def _epsilon_tensor(i, j, k, o): - """Rank-4 epsilon tensor - - Extension of https://codegolf.stackexchange.com/a/160375 - """ - test_set = set((i, j, k, o)) - if not (test_set <= set((1, 2, 3, 4)) or test_set <= set((0, 1, 2, 3))): - raise Exception("Unexpected input", i, j, k, o) - - return (i - j) * (j - k) * (k - i) * (i - o) * (j - o) * (o - k) / 12 - - def _get_lorentz_names(name): assert len(name) == 2 diff --git a/tests/dirac_test.py b/tests/dirac_test.py index 0a2c0379..f36017a6 100644 --- a/tests/dirac_test.py +++ b/tests/dirac_test.py @@ -32,3 +32,15 @@ def test_grid_dirac(): pe.dirac.Grid_gamma(gamma) with pytest.raises(Exception): pe.dirac.Grid_gamma('Not a gamma matrix') + + +def test_epsilon_tensor(): + check = {(1, 2, 3) : 1.0, + (3, 1, 2) : 1.0, + (2, 3, 1) : 1.0, + (1, 1, 1) : 0.0, + (3, 2, 1) : -1.0, + (1, 3, 2) : -1.0, + (1, 1, 3) : 0.0} + for key, value in check.items(): + assert pe.dirac.epsilon_tensor(*key) == value From 3a57471ccf62d852dad80943691c7db4e31e12fc Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Dec 2021 12:06:28 +0100 Subject: [PATCH 2/5] test: tests for epsilon tensors extended --- tests/dirac_test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/dirac_test.py b/tests/dirac_test.py index f36017a6..44812397 100644 --- a/tests/dirac_test.py +++ b/tests/dirac_test.py @@ -44,3 +44,20 @@ def test_epsilon_tensor(): (1, 1, 3) : 0.0} for key, value in check.items(): assert pe.dirac.epsilon_tensor(*key) == value + with pytest.raises(Exception): + pe.dirac.epsilon_tensor(0, 1, 3) + + +def test_epsilon_tensor_rank4(): + check = {(1, 4, 3, 2) : -1.0, + (1, 2, 3, 4) : 1.0, + (2, 1, 3, 4) : -1.0, + (4, 3, 2, 1) : 1.0, + (3, 2, 4, 3) : 0.0, + (0, 1, 2, 3) : 1.0, + (1, 1, 1, 1) : 0.0, + (1, 2, 3, 1) : 0.0} + for key, value in check.items(): + assert pe.dirac.epsilon_tensor_rank4(*key) == value + with pytest.raises(Exception): + pe.dirac.epsilon_tensor_rank4(0, 1, 3, 4) From 2ccbd97b39191c0ee8597bce6e55a2809e8716a3 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Dec 2021 12:20:55 +0100 Subject: [PATCH 3/5] test: tests for reweighting, merging and correlate extended --- tests/obs_test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/obs_test.py b/tests/obs_test.py index 989a0064..9aeaf11d 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -428,6 +428,14 @@ def test_reweighting(): assert r_obs[0].reweighted r_obs2 = r_obs[0] * my_obs assert r_obs2.reweighted + my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') + with pytest.raises(Exception): + pe.reweight(my_obs, [my_covobs]) + my_obs2 = pe.Obs([np.random.rand(1000)], ['t2']) + with pytest.raises(Exception): + pe.reweight(my_obs, [my_obs + my_obs2]) + with pytest.raises(Exception): + pe.reweight(my_irregular_obs, [my_obs]) def test_merge_obs(): @@ -436,6 +444,12 @@ def test_merge_obs(): merged = pe.merge_obs([my_obs1, my_obs2]) diff = merged - my_obs2 - my_obs1 assert diff == -(my_obs1.value + my_obs2.value) / 2 + with pytest.raises(Exception): + pe.merge_obs([my_obs1, my_obs1]) + my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') + with pytest.raises(Exception): + pe.merge_obs([my_obs1, my_covobs]) + def test_merge_obs_r_values(): @@ -468,6 +482,17 @@ def test_correlate(): corr3 = pe.correlate(my_obs5, my_obs6) assert my_obs5.idl == corr3.idl + my_new_obs = pe.Obs([np.random.rand(100)], ['q3']) + with pytest.raises(Exception): + pe.correlate(my_obs1, my_new_obs) + my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') + with pytest.raises(Exception): + pe.correlate(my_covobs, my_covobs) + r_obs = pe.reweight(my_obs1, [my_obs1])[0] + with pytest.warns(RuntimeWarning): + pe.correlate(r_obs, r_obs) + + def test_irregular_error_propagation(): obs_list = [pe.Obs([np.random.rand(100)], ['t']), From 64a8bc690fe2d34ffecfba222d65429e51982ee8 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Dec 2021 12:21:40 +0100 Subject: [PATCH 4/5] refactor!: Obs.print method removed --- pyerrors/obs.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pyerrors/obs.py b/pyerrors/obs.py index edef264e..6724e346 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -434,10 +434,6 @@ class Obs: my_string_list.append(my_string) print('\n'.join(my_string_list)) - def print(self, level=1): - warnings.warn("Method 'print' renamed to 'details'", DeprecationWarning) - self.details(level > 1) - def is_zero_within_error(self, sigma=1): """Checks whether the observable is zero within 'sigma' standard errors. From b7da7f4b7e00861c894bed58bb2c4860e06c6947 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Dec 2021 12:29:42 +0100 Subject: [PATCH 5/5] refactor: unnecessary overloading of np.sinc removed, tests added --- pyerrors/obs.py | 3 --- tests/obs_test.py | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pyerrors/obs.py b/pyerrors/obs.py index 6724e346..e19cc617 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -795,9 +795,6 @@ class Obs: def arctanh(self): return derived_observable(lambda x: anp.arctanh(x[0]), [self]) - def sinc(self): - return derived_observable(lambda x: anp.sinc(x[0]), [self]) - class CObs: """Class for a complex valued observable.""" diff --git a/tests/obs_test.py b/tests/obs_test.py index 9aeaf11d..a5e72ec9 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -57,6 +57,7 @@ def test_dump(): value = np.random.normal(5, 10) dvalue = np.abs(np.random.normal(0, 1)) test_obs = pe.pseudo_Obs(value, dvalue, 't') + test_obs.dump('test_dump', path=".") test_obs.dump('test_dump') new_obs = pe.load_object('test_dump.p') os.remove('test_dump.p') @@ -105,6 +106,12 @@ def test_function_overloading(): assert np.sqrt(b ** 2) == b assert np.sqrt(b) ** 2 == b + np.arcsin(1 / b) + np.arccos(1 / b) + np.arctan(1 / b) + np.arctanh(1 / b) + np.sinc(1 / b) + def test_overloading_vectorization(): a = np.random.randint(1, 100, 10)