Skip to content

Commit

Permalink
[Fix] Ruff rules and more precise Excpetion types (fjosw#248)
Browse files Browse the repository at this point in the history
* [Fix] Fix test for membership should be 'not in' (E713)

* [Fix] Fix module imported but unused (F401)

* [Fix] More precise Exception types in dirac, obs and correlator
  • Loading branch information
fjosw authored Dec 24, 2024
1 parent d908508 commit 3eac921
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 144 deletions.
18 changes: 9 additions & 9 deletions pyerrors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,12 @@ def func(a, x):
from .correlators import *
from .fits import *
from .misc import *
from . import dirac
from . import input
from . import linalg
from . import mpm
from . import roots
from . import integrate
from . import special

from .version import __version__
from . import dirac as dirac
from . import input as input
from . import linalg as linalg
from . import mpm as mpm
from . import roots as roots
from . import integrate as integrate
from . import special as special

from .version import __version__ as __version__
142 changes: 71 additions & 71 deletions pyerrors/correlators.py

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyerrors/dirac.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def epsilon_tensor(i, j, k):
"""
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)
raise ValueError("Unexpected input", i, j, k)

return (i - j) * (j - k) * (k - i) / 2

Expand All @@ -52,7 +52,7 @@ def epsilon_tensor_rank4(i, j, k, o):
"""
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)
raise ValueError("Unexpected input", i, j, k, o)

return (i - j) * (j - k) * (k - i) * (i - o) * (j - o) * (o - k) / 12

Expand Down Expand Up @@ -92,5 +92,5 @@ def Grid_gamma(gamma_tag):
elif gamma_tag == 'SigmaZT':
g = 0.5 * (gamma[2] @ gamma[3] - gamma[3] @ gamma[2])
else:
raise Exception('Unkown gamma structure', gamma_tag)
raise ValueError('Unkown gamma structure', gamma_tag)
return g
16 changes: 8 additions & 8 deletions pyerrors/input/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
For comparison with other analysis workflows `pyerrors` can also generate jackknife samples from an `Obs` object or import jackknife samples into an `Obs` object.
See `pyerrors.obs.Obs.export_jackknife` and `pyerrors.obs.import_jackknife` for details.
'''
from . import bdio
from . import dobs
from . import hadrons
from . import json
from . import misc
from . import openQCD
from . import pandas
from . import sfcf
from . import bdio as bdio
from . import dobs as dobs
from . import hadrons as hadrons
from . import json as json
from . import misc as misc
from . import openQCD as openQCD
from . import pandas as pandas
from . import sfcf as sfcf
4 changes: 2 additions & 2 deletions pyerrors/input/dobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _dict_to_xmlstring_spaces(d, space=' '):
o += space
o += li + '\n'
if li.startswith('<') and not cm:
if not '<%s' % ('/') in li:
if '<%s' % ('/') not in li:
c += 1
cm = False
return o
Expand Down Expand Up @@ -671,7 +671,7 @@ def _dobsdict_to_xmlstring_spaces(d, space=' '):
o += space
o += li + '\n'
if li.startswith('<') and not cm:
if not '<%s' % ('/') in li:
if '<%s' % ('/') not in li:
c += 1
cm = False
return o
Expand Down
4 changes: 2 additions & 2 deletions pyerrors/input/hadrons.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def read_hd5(filestem, ens_id, group, attrs=None, idl=None, part="real"):
infos = []
for hd5_file in files:
h5file = h5py.File(path + '/' + hd5_file, "r")
if not group + '/' + entry in h5file:
if group + '/' + entry not in h5file:
raise Exception("Entry '" + entry + "' not contained in the files.")
raw_data = h5file[group + '/' + entry + '/corr']
real_data = raw_data[:].view("complex")
Expand Down Expand Up @@ -186,7 +186,7 @@ def _extract_real_arrays(path, files, tree, keys):
for hd5_file in files:
h5file = h5py.File(path + '/' + hd5_file, "r")
for key in keys:
if not tree + '/' + key in h5file:
if tree + '/' + key not in h5file:
raise Exception("Entry '" + key + "' not contained in the files.")
raw_data = h5file[tree + '/' + key + '/data']
real_data = raw_data[:].astype(np.double)
Expand Down
2 changes: 1 addition & 1 deletion pyerrors/input/openQCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
Reweighting factors read
"""
known_oqcd_versions = ['1.4', '1.6', '2.0']
if not (version in known_oqcd_versions):
if version not in known_oqcd_versions:
raise Exception('Unknown openQCD version defined!')
print("Working with openQCD version " + version)
if 'postfix' in kwargs:
Expand Down
46 changes: 23 additions & 23 deletions pyerrors/obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _parse_kwarg(kwarg_name):
tmp = kwargs.get(kwarg_name)
if isinstance(tmp, (int, float)):
if tmp < 0:
raise Exception(kwarg_name + ' has to be larger or equal to 0.')
raise ValueError(kwarg_name + ' has to be larger or equal to 0.')
for e, e_name in enumerate(self.e_names):
getattr(self, kwarg_name)[e_name] = tmp
else:
Expand Down Expand Up @@ -291,7 +291,7 @@ def _compute_drho(i):
texp = self.tau_exp[e_name]
# Critical slowing down analysis
if w_max // 2 <= 1:
raise Exception("Need at least 8 samples for tau_exp error analysis")
raise ValueError("Need at least 8 samples for tau_exp error analysis")
for n in range(1, w_max // 2):
_compute_drho(n + 1)
if (self.e_rho[e_name][n] - self.N_sigma[e_name] * self.e_drho[e_name][n]) < 0 or n >= w_max // 2 - 2:
Expand Down Expand Up @@ -620,7 +620,7 @@ def plot_piechart(self, save=None):
if not hasattr(self, 'e_dvalue'):
raise Exception('Run the gamma method first.')
if np.isclose(0.0, self._dvalue, atol=1e-15):
raise Exception('Error is 0.0')
raise ValueError('Error is 0.0')
labels = self.e_names
sizes = [self.e_dvalue[name] ** 2 for name in labels] / self._dvalue ** 2
fig1, ax1 = plt.subplots()
Expand Down Expand Up @@ -659,7 +659,7 @@ def dump(self, filename, datatype="json.gz", description="", **kwargs):
with open(file_name + '.p', 'wb') as fb:
pickle.dump(self, fb)
else:
raise Exception("Unknown datatype " + str(datatype))
raise TypeError("Unknown datatype " + str(datatype))

def export_jackknife(self):
"""Export jackknife samples from the Obs
Expand All @@ -676,7 +676,7 @@ def export_jackknife(self):
"""

if len(self.names) != 1:
raise Exception("'export_jackknife' is only implemented for Obs defined on one ensemble and replicum.")
raise ValueError("'export_jackknife' is only implemented for Obs defined on one ensemble and replicum.")

name = self.names[0]
full_data = self.deltas[name] + self.r_values[name]
Expand Down Expand Up @@ -711,7 +711,7 @@ def export_bootstrap(self, samples=500, random_numbers=None, save_rng=None):
should agree with samples from a full bootstrap analysis up to O(1/N).
"""
if len(self.names) != 1:
raise Exception("'export_boostrap' is only implemented for Obs defined on one ensemble and replicum.")
raise ValueError("'export_boostrap' is only implemented for Obs defined on one ensemble and replicum.")

name = self.names[0]
length = self.N
Expand Down Expand Up @@ -1267,7 +1267,7 @@ def _compute_scalefactor_missing_rep(obs):
if 'man_grad' in kwargs:
deriv = np.asarray(kwargs.get('man_grad'))
if new_values.shape + data.shape != deriv.shape:
raise Exception('Manual derivative does not have correct shape.')
raise ValueError('Manual derivative does not have correct shape.')
elif kwargs.get('num_grad') is True:
if multi > 0:
raise Exception('Multi mode currently not supported for numerical derivative')
Expand Down Expand Up @@ -1333,7 +1333,7 @@ def __init__(self, N):
new_covobs = {name: Covobs(0, allcov[name], name, grad=new_grad[name]) for name in new_grad}

if not set(new_covobs.keys()).isdisjoint(new_deltas.keys()):
raise Exception('The same name has been used for deltas and covobs!')
raise ValueError('The same name has been used for deltas and covobs!')
new_samples = []
new_means = []
new_idl = []
Expand Down Expand Up @@ -1374,15 +1374,15 @@ def _reduce_deltas(deltas, idx_old, idx_new):
Has to be a subset of idx_old.
"""
if not len(deltas) == len(idx_old):
raise Exception('Length of deltas and idx_old have to be the same: %d != %d' % (len(deltas), len(idx_old)))
raise ValueError('Length of deltas and idx_old have to be the same: %d != %d' % (len(deltas), len(idx_old)))
if type(idx_old) is range and type(idx_new) is range:
if idx_old == idx_new:
return deltas
if _check_lists_equal([idx_old, idx_new]):
return deltas
indices = np.intersect1d(idx_old, idx_new, assume_unique=True, return_indices=True)[1]
if len(indices) < len(idx_new):
raise Exception('Error in _reduce_deltas: Config of idx_new not in idx_old')
raise ValueError('Error in _reduce_deltas: Config of idx_new not in idx_old')
return np.array(deltas)[indices]


Expand All @@ -1404,12 +1404,12 @@ def reweight(weight, obs, **kwargs):
result = []
for i in range(len(obs)):
if len(obs[i].cov_names):
raise Exception('Error: Not possible to reweight an Obs that contains covobs!')
raise ValueError('Error: Not possible to reweight an Obs that contains covobs!')
if not set(obs[i].names).issubset(weight.names):
raise Exception('Error: Ensembles do not fit')
raise ValueError('Error: Ensembles do not fit')
for name in obs[i].names:
if not set(obs[i].idl[name]).issubset(weight.idl[name]):
raise Exception('obs[%d] has to be defined on a subset of the configs in weight.idl[%s]!' % (i, name))
raise ValueError('obs[%d] has to be defined on a subset of the configs in weight.idl[%s]!' % (i, name))
new_samples = []
w_deltas = {}
for name in sorted(obs[i].names):
Expand Down Expand Up @@ -1446,14 +1446,14 @@ def correlate(obs_a, obs_b):
"""

if sorted(obs_a.names) != sorted(obs_b.names):
raise Exception(f"Ensembles do not fit {set(sorted(obs_a.names)) ^ set(sorted(obs_b.names))}")
raise ValueError(f"Ensembles do not fit {set(sorted(obs_a.names)) ^ set(sorted(obs_b.names))}")
if len(obs_a.cov_names) or len(obs_b.cov_names):
raise Exception('Error: Not possible to correlate Obs that contain covobs!')
raise ValueError('Error: Not possible to correlate Obs that contain covobs!')
for name in obs_a.names:
if obs_a.shape[name] != obs_b.shape[name]:
raise Exception('Shapes of ensemble', name, 'do not fit')
raise ValueError('Shapes of ensemble', name, 'do not fit')
if obs_a.idl[name] != obs_b.idl[name]:
raise Exception('idl of ensemble', name, 'do not fit')
raise ValueError('idl of ensemble', name, 'do not fit')

if obs_a.reweighted is True:
warnings.warn("The first observable is already reweighted.", RuntimeWarning)
Expand Down Expand Up @@ -1555,7 +1555,7 @@ def invert_corr_cov_cholesky(corr, inverrdiag):

condn = np.linalg.cond(corr)
if condn > 0.1 / np.finfo(float).eps:
raise Exception(f"Cannot invert correlation matrix as its condition number exceeds machine precision ({condn:1.2e})")
raise ValueError(f"Cannot invert correlation matrix as its condition number exceeds machine precision ({condn:1.2e})")
if condn > 1e13:
warnings.warn("Correlation matrix may be ill-conditioned, condition number: {%1.2e}" % (condn), RuntimeWarning)
chol = np.linalg.cholesky(corr)
Expand Down Expand Up @@ -1636,7 +1636,7 @@ def _smooth_eigenvalues(corr, E):
Number of eigenvalues to be left substantially unchanged
"""
if not (2 < E < corr.shape[0] - 1):
raise Exception(f"'E' has to be between 2 and the dimension of the correlation matrix minus 1 ({corr.shape[0] - 1}).")
raise ValueError(f"'E' has to be between 2 and the dimension of the correlation matrix minus 1 ({corr.shape[0] - 1}).")
vals, vec = np.linalg.eigh(corr)
lambda_min = np.mean(vals[:-E])
vals[vals < lambda_min] = lambda_min
Expand Down Expand Up @@ -1768,9 +1768,9 @@ def merge_obs(list_of_obs):
"""
replist = [item for obs in list_of_obs for item in obs.names]
if (len(replist) == len(set(replist))) is False:
raise Exception('list_of_obs contains duplicate replica: %s' % (str(replist)))
raise ValueError('list_of_obs contains duplicate replica: %s' % (str(replist)))
if any([len(o.cov_names) for o in list_of_obs]):
raise Exception('Not possible to merge data that contains covobs!')
raise ValueError('Not possible to merge data that contains covobs!')
new_dict = {}
idl_dict = {}
for o in list_of_obs:
Expand Down Expand Up @@ -1821,7 +1821,7 @@ def covobs_to_obs(co):
for i in range(len(means)):
ol.append(covobs_to_obs(Covobs(means[i], cov, name, pos=i, grad=grad)))
if ol[0].covobs[name].N != len(means):
raise Exception('You have to provide %d mean values!' % (ol[0].N))
raise ValueError('You have to provide %d mean values!' % (ol[0].N))
if len(ol) == 1:
return ol[0]
return ol
Expand All @@ -1837,7 +1837,7 @@ def _determine_gap(o, e_content, e_name):

gap = min(gaps)
if not np.all([gi % gap == 0 for gi in gaps]):
raise Exception(f"Replica for ensemble {e_name} do not have a common spacing.", gaps)
raise ValueError(f"Replica for ensemble {e_name} do not have a common spacing.", gaps)

return gap

Expand Down
16 changes: 8 additions & 8 deletions tests/correlators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_m_eff():
with pytest.warns(RuntimeWarning):
my_corr.m_eff('sinh')

with pytest.raises(Exception):
with pytest.raises(ValueError):
my_corr.m_eff('unkown_variant')


Expand All @@ -140,7 +140,7 @@ def test_m_eff_negative_values():
assert m_eff_log[padding + 1] is None
m_eff_cosh = my_corr.m_eff('cosh')
assert m_eff_cosh[padding + 1] is None
with pytest.raises(Exception):
with pytest.raises(ValueError):
my_corr.m_eff('logsym')


Expand All @@ -155,7 +155,7 @@ def test_correlate():
my_corr = pe.correlators.Corr([pe.pseudo_Obs(10, 0.1, 't'), pe.pseudo_Obs(0, 0.05, 't')])
corr1 = my_corr.correlate(my_corr)
corr2 = my_corr.correlate(my_corr[0])
with pytest.raises(Exception):
with pytest.raises(TypeError):
corr3 = my_corr.correlate(7.3)


Expand All @@ -176,9 +176,9 @@ def f(a, x):
assert fit_res[0] == my_corr[0]
assert fit_res[1] == my_corr[1] - my_corr[0]

with pytest.raises(Exception):
with pytest.raises(TypeError):
my_corr.fit(f, "from 0 to 3")
with pytest.raises(Exception):
with pytest.raises(ValueError):
my_corr.fit(f, [0, 2, 3])


Expand Down Expand Up @@ -256,11 +256,11 @@ def test_prange():
corr = pe.correlators.Corr(corr_content)

corr.set_prange([2, 4])
with pytest.raises(Exception):
with pytest.raises(ValueError):
corr.set_prange([2])
with pytest.raises(Exception):
with pytest.raises(TypeError):
corr.set_prange([2, 2.3])
with pytest.raises(Exception):
with pytest.raises(ValueError):
corr.set_prange([4, 1])


Expand Down
6 changes: 3 additions & 3 deletions tests/dirac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_grid_dirac():
'SigmaYZ',
'SigmaZT']:
pe.dirac.Grid_gamma(gamma)
with pytest.raises(Exception):
with pytest.raises(ValueError):
pe.dirac.Grid_gamma('Not a gamma matrix')


Expand All @@ -44,7 +44,7 @@ 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):
with pytest.raises(ValueError):
pe.dirac.epsilon_tensor(0, 1, 3)


Expand All @@ -59,5 +59,5 @@ def test_epsilon_tensor_rank4():
(1, 2, 3, 1) : 0.0}
for key, value in check.items():
assert pe.dirac.epsilon_tensor_rank4(*key) == value
with pytest.raises(Exception):
with pytest.raises(ValueError):
pe.dirac.epsilon_tensor_rank4(0, 1, 3, 4)
Loading

0 comments on commit 3eac921

Please sign in to comment.