From 07d947d5b1ddf797230ad66cfc86f189696c779b Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 23 Jun 2022 14:06:45 +0100 Subject: [PATCH 1/3] docs: CHANGELOG updated. --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f900f57..5d4415b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. +## [2.x.x] - 2022-xx-xx +### Added +- `Obs.reweight` method added in analogy to `Corr.reweight` which allows for a more convenient reweighting of individual observables. +- `Corr.show` now has the additional argument `title` which allows to add a title to the figure. Figures are now saved with `bbox_inches='tight'`. + +### Fixed +- `Corr.m_eff` can now deal with correlator entries which are exactly zero. +- Minor bugs in `input.dobs` fixed. + ## [2.1.3] - 2022-06-13 ### Fixed - Further bugs in connection with correlator objects which have arrays with None entries as content fixed. From 338bf8906a4097aab6acb840c708fde598356183 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Fri, 24 Jun 2022 12:28:49 +0100 Subject: [PATCH 2/3] refactor: maintainability issues in tests fixed. --- tests/correlators_test.py | 4 ++-- tests/fits_test.py | 7 +++---- tests/linalg_test.py | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/correlators_test.py b/tests/correlators_test.py index 3cfa230b..c1c79975 100644 --- a/tests/correlators_test.py +++ b/tests/correlators_test.py @@ -249,9 +249,9 @@ def test_matrix_corr(): def test_corr_none_entries(): a = pe.pseudo_Obs(1.0, 0.1, 'a') - l = np.asarray([[a, a], [a, a]]) + la = np.asarray([[a, a], [a, a]]) n = np.asarray([[None, None], [None, None]]) - x = [l, n] + x = [la, n] matr = pe.Corr(x) matr.projected(np.asarray([1.0, 0.0])) diff --git a/tests/fits_test.py b/tests/fits_test.py index 8dd6677d..6cb806d2 100644 --- a/tests/fits_test.py +++ b/tests/fits_test.py @@ -194,7 +194,7 @@ def test_linear_fit_guesses(): lin_func = lambda a, x: a[0] + a[1] * x with pytest.raises(Exception): pe.least_squares(xvals, yvals, lin_func) - [o.gamma_method() for o in yvals]; + [o.gamma_method() for o in yvals] with pytest.raises(Exception): pe.least_squares(xvals, yvals, lin_func, initial_guess=[5]) @@ -414,7 +414,7 @@ def test_fit_vs_jackknife(): for i, cov in enumerate([cov1, cov2, cov3]): dat = pe.misc.gen_correlated_data(np.arange(1, 4), cov, 'test', 0.5, samples=samples) - [o.gamma_method(S=0) for o in dat]; + [o.gamma_method(S=0) for o in dat] func = lambda a, x: a[0] + a[1] * x fr = pe.least_squares(np.arange(1, 4), dat, func) fr.gamma_method(S=0) @@ -448,8 +448,7 @@ def test_correlated_fit_vs_jackknife(): x_val = np.arange(1, 6, 2) for i, cov in enumerate([cov1, cov2, cov3]): dat = pe.misc.gen_correlated_data(x_val + x_val ** 2 + np.random.normal(0.0, 0.1, 3), cov, 'test', 0.5, samples=samples) - [o.gamma_method(S=0) for o in dat]; - dat + [o.gamma_method(S=0) for o in dat] func = lambda a, x: a[0] * x + a[1] * x ** 2 fr = pe.least_squares(x_val, dat, func, correlated_fit=True, silent=True) [o.gamma_method(S=0) for o in fr] diff --git a/tests/linalg_test.py b/tests/linalg_test.py index 09db0ac5..59dc898c 100644 --- a/tests/linalg_test.py +++ b/tests/linalg_test.py @@ -258,8 +258,8 @@ def test_complex_matrix_inverse(): inverse_matrix = np.linalg.inv(matrix) inverse_obs_matrix = pe.linalg.inv(obs_matrix) for (n, m), entry in np.ndenumerate(inverse_matrix): - assert np.isclose(inverse_matrix[n, m].real, inverse_obs_matrix[n, m].real.value) - assert np.isclose(inverse_matrix[n, m].imag, inverse_obs_matrix[n, m].imag.value) + assert np.isclose(inverse_matrix[n, m].real, inverse_obs_matrix[n, m].real.value) + assert np.isclose(inverse_matrix[n, m].imag, inverse_obs_matrix[n, m].imag.value) def test_matrix_functions(): From 3e29cf9ca8bb4b7cd6745085bb0af3d24e89488f Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Fri, 24 Jun 2022 12:50:26 +0100 Subject: [PATCH 3/3] fix: detection of invalid fit functions extended. --- pyerrors/fits.py | 16 +++++++++++----- tests/fits_test.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pyerrors/fits.py b/pyerrors/fits.py index 997c7f6f..eef16518 100644 --- a/pyerrors/fits.py +++ b/pyerrors/fits.py @@ -177,13 +177,15 @@ def total_least_squares(x, y, func, silent=False, **kwargs): if not callable(func): raise TypeError('func has to be a function.') - for i in range(25): + for i in range(42): try: func(np.arange(i), x.T[0]) except Exception: - pass + continue else: break + else: + raise RuntimeError("Fit function is not valid.") n_parms = i if not silent: @@ -321,9 +323,11 @@ def _prior_fit(x, y, func, priors, silent=False, **kwargs): try: func(np.arange(i), 0) except Exception: - pass + continue else: break + else: + raise RuntimeError("Fit function is not valid.") n_parms = i @@ -442,13 +446,15 @@ def _standard_fit(x, y, func, silent=False, **kwargs): if not callable(func): raise TypeError('func has to be a function.') - for i in range(25): + for i in range(42): try: func(np.arange(i), x.T[0]) except Exception: - pass + continue else: break + else: + raise RuntimeError("Fit function is not valid.") n_parms = i diff --git a/tests/fits_test.py b/tests/fits_test.py index 6cb806d2..c578a86d 100644 --- a/tests/fits_test.py +++ b/tests/fits_test.py @@ -495,6 +495,31 @@ def test_fit_no_autograd(): pe.total_least_squares(oy, oy, func) +def test_invalid_fit_function(): + def func1(a, x): + return a[0] + a[1] * x + a[2] * anp.sinh(x) + a[199] + + def func2(a, x, y): + return a[0] + a[1] * x + + def func3(x): + return x + + xvals =[] + yvals =[] + err = 0.1 + + for x in range(1, 8, 2): + xvals.append(x) + yvals.append(pe.pseudo_Obs(x + np.random.normal(0.0, err), err, 'test1') + pe.pseudo_Obs(0, err / 100, 'test2', samples=87)) + [o.gamma_method() for o in yvals] + for func in [func1, func2, func3]: + with pytest.raises(Exception): + pe.least_squares(xvals, yvals, func) + with pytest.raises(Exception): + pe.total_least_squares(yvals, yvals, func) + + def test_singular_correlated_fit(): obs1 = pe.pseudo_Obs(1.0, 0.1, 'test') with pytest.raises(Exception):