From e62a957d3c8ff130822d020a3f8f9c3d702eaa61 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 16 Nov 2021 11:58:27 +0000 Subject: [PATCH] feat: Obs.details does not output zero error anymore in case the gamma_method had not been applied. Obs.plot* function now correctly throw an exception in case the gamma_method had not been run. docs adjusted accordingly. --- pyerrors/__init__.py | 17 +++++++++++++++-- pyerrors/obs.py | 22 ++++++++++++---------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pyerrors/__init__.py b/pyerrors/__init__.py index 22efa8f4..8753c154 100644 --- a/pyerrors/__init__.py +++ b/pyerrors/__init__.py @@ -125,7 +125,7 @@ obs2 = pe.Obs([samples2], ['ensemble2']) my_sum = obs1 + obs2 my_sum.details() -> Result 2.00697958e+00 +/- 0.00000000e+00 +/- 0.00000000e+00 (0.000%) +> Result 2.00697958e+00 > 1500 samples in 2 ensembles: > · Ensemble 'ensemble1' : 1000 configurations (from 1 to 1000) > · Ensemble 'ensemble2' : 500 configurations (from 1 to 500) @@ -140,7 +140,7 @@ obs2 = pe.Obs([samples2], ['ensemble1|r02']) > my_sum = obs1 + obs2 > my_sum.details() -> Result 2.00697958e+00 +/- 0.00000000e+00 +/- 0.00000000e+00 (0.000%) +> Result 2.00697958e+00 > 1500 samples in 1 ensemble: > · Ensemble 'ensemble1' > · Replicum 'r01' : 1000 configurations (from 1 to 1000) @@ -170,12 +170,25 @@ Example: ```python # Observable defined on configurations 20 to 519 obs1 = pe.Obs([samples1], ['ensemble1'], idl=[range(20, 520)]) +obs1.details() +> Result 9.98319881e-01 +> 500 samples in 1 ensemble: +> · Ensemble 'ensemble1' : 500 configurations (from 20 to 519) # Observable defined on every second configuration between 5 and 1003 obs2 = pe.Obs([samples2], ['ensemble1'], idl=[range(5, 1005, 2)]) +obs2.details() +> Result 9.99100712e-01 +> 500 samples in 1 ensemble: +> · Ensemble 'ensemble1' : 500 configurations (from 5 to 1003 in steps of 2) # Observable defined on configurations 2, 9, 28, 29 and 501 obs3 = pe.Obs([samples3], ['ensemble1'], idl=[[2, 9, 28, 29, 501]]) +obs3.details() +> Result 1.01718064e+00 +> 5 samples in 1 ensemble: +> · Ensemble 'ensemble1' : 5 configurations (irregular range) + ``` **Warning:** Irregular Monte Carlo chains can result in odd patterns in the autocorrelation functions. diff --git a/pyerrors/obs.py b/pyerrors/obs.py index 24fa91ca..e4a9340d 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -348,12 +348,14 @@ class Obs: """ if self.tag is not None: print("Description:", self.tag) - if self.value == 0.0: - percentage = np.nan + if not hasattr(self, 'e_dvalue'): + print('Result\t %3.8e' % (self.value)) else: - percentage = np.abs(self.dvalue / self.value) * 100 - print('Result\t %3.8e +/- %3.8e +/- %3.8e (%3.3f%%)' % (self.value, self.dvalue, self.ddvalue, percentage)) - if hasattr(self, 'e_dvalue'): + if self.value == 0.0: + percentage = np.nan + else: + percentage = np.abs(self.dvalue / self.value) * 100 + print('Result\t %3.8e +/- %3.8e +/- %3.8e (%3.3f%%)' % (self.value, self.dvalue, self.ddvalue, percentage)) if len(self.e_names) > 1: print(' Ensemble errors:') for e_name in self.e_names: @@ -420,7 +422,7 @@ class Obs: save : str saves the figure to a file named 'save' if. """ - if not hasattr(self, 'e_names'): + if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') fig = plt.figure() @@ -453,7 +455,7 @@ class Obs: def plot_rho(self): """Plot normalized autocorrelation function time for each ensemble.""" - if not hasattr(self, 'e_names'): + if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') for e, e_name in enumerate(self.e_names): plt.xlabel('W') @@ -475,7 +477,7 @@ class Obs: def plot_rep_dist(self): """Plot replica distribution for each ensemble with more than one replicum.""" - if not hasattr(self, 'e_names'): + if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') for e, e_name in enumerate(self.e_names): if len(self.e_content[e_name]) == 1: @@ -503,7 +505,7 @@ class Obs: expand : bool show expanded history for irregular Monte Carlo chains (default: True). """ - if not hasattr(self, 'e_names'): + if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') for e, e_name in enumerate(self.e_names): @@ -527,7 +529,7 @@ class Obs: def plot_piechart(self): """Plot piechart which shows the fractional contribution of each ensemble to the error and returns a dictionary containing the fractions.""" - if not hasattr(self, 'e_names'): + if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') if self.dvalue == 0.0: raise Exception('Error is 0.0')