Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
scarlehoff committed Jul 17, 2024
1 parent e182e66 commit 2caba59
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 130 deletions.
2 changes: 1 addition & 1 deletion validphys2/src/validphys/lhaindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def infofilename(name):
@lru_cache()
def parse_info(name):
with open(infofilename(name)) as infofile:
result=yaml.YAML(typ='safe', pure=True).load(infofile)
result = yaml.YAML(typ='safe', pure=True).load(infofile)
return result


Expand Down
2 changes: 1 addition & 1 deletion validphys2/src/validphys/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#
#
2 changes: 2 additions & 0 deletions validphys2/src/validphys/tests/test_arclengths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from validphys.tableloader import sane_load
from validphys.tests.test_regressions import make_table_comp


@make_table_comp(sane_load)
def test_arclength_mc(mc_pdf_config):
"""Integration test that ``arc_length_table`` action matches expected value
Expand All @@ -14,6 +15,7 @@ def test_arclength_mc(mc_pdf_config):
table = API.arc_length_table(**mc_pdf_config)
return table


@make_table_comp(sane_load)
def test_arclength_hessian(hessian_pdf_config):
"""Like ``test_arclength_mc`` except uses a hessian ``PDF``. Checks that
Expand Down
10 changes: 5 additions & 5 deletions validphys2/src/validphys/tests/test_calcutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

from validphys import calcutils

sane_floats = floats(
min_value=-1, max_value=1, allow_nan=False, allow_infinity=False)
sane_floats = floats(min_value=-1, max_value=1, allow_nan=False, allow_infinity=False)
diffs = arrays(dtype=float, shape=10, elements=sane_floats)
sqrtcov = arrays(dtype=float, shape=(10, 10), elements=sane_floats)


@given(sqrtcov, diffs)
def test_calc_chi2(s, d):
cov = s@s.T
#Handle zero matrices and so on
cov = s @ s.T
# Handle zero matrices and so on
np.fill_diagonal(cov, np.diag(cov) + 1)
chi2 = d@la.inv(cov)@d
chi2 = d @ la.inv(cov) @ d
chol = la.cholesky(cov, lower=True)
calc = calcutils.calc_chi2(chol, d)
assert np.allclose(chi2, calc)
Expand Down
43 changes: 16 additions & 27 deletions validphys2/src/validphys/tests/test_closuretest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,46 @@

from validphys.closuretest import bias_dataset, variance_dataset


class TestResult:
"""class for testing base level estimators which expect a results object"""

def __init__(self, central_value, rawdata=None):
self.central_value = central_value
self.rawdata = rawdata
self.error_members = rawdata
self.ndata = len(central_value)
self.sqrtcovmat = np.identity(self.ndata)

def __len__(self,):
def __len__(self):
return self.ndata


N_DATA = 5
N_REPLICAS = 10

#TODO: make these fixtures?
# TODO: make these fixtures?
# these are proxies for results tuples of data and theory
ones_results = 2*[TestResult(np.ones(N_DATA), np.ones((N_DATA, N_REPLICAS)))]
twos_results = 2*[TestResult(2*np.ones(N_DATA), 2*np.ones((N_DATA, N_REPLICAS)))]
ones_results = 2 * [TestResult(np.ones(N_DATA), np.ones((N_DATA, N_REPLICAS)))]
twos_results = 2 * [TestResult(2 * np.ones(N_DATA), 2 * np.ones((N_DATA, N_REPLICAS)))]

replicas = np.arange(N_REPLICAS)[np.newaxis, :] * np.ones((N_DATA, 1))
replicas_result = 2 * [TestResult(replicas.mean(axis=1), replicas)]

replicas = np.arange(N_REPLICAS)[np.newaxis, :]*np.ones((N_DATA, 1))
replicas_result = 2*[TestResult(replicas.mean(axis=1), replicas)]

def test_bias_function():
bias_ones = bias_dataset(
ones_results,
[ones_results], # need list of length one to emulate collect
None,
None,
ones_results, [ones_results], None, None # need list of length one to emulate collect
)
assert np.allclose(0, bias_ones.bias)
bias_one_two = bias_dataset(
ones_results,
[twos_results],
None,
None,
)
bias_one_two = bias_dataset(ones_results, [twos_results], None, None)
assert np.allclose(N_DATA, bias_one_two.bias)


def test_variance_function():
vardata = variance_dataset(
ones_results,
None,
None,
)
vardata = variance_dataset(ones_results, None, None)
assert np.allclose(0, vardata.variance)
var_reps = variance_dataset(
replicas_result,
None,
None,
)
var_reps = variance_dataset(replicas_result, None, None)
# calc explicitly what variance should be
expected = np.sum(((np.arange(N_REPLICAS) - 4.5)**2)*N_DATA/N_REPLICAS)
expected = np.sum(((np.arange(N_REPLICAS) - 4.5) ** 2) * N_DATA / N_REPLICAS)
assert np.allclose(expected, var_reps.variance)
1 change: 1 addition & 0 deletions validphys2/src/validphys/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from validphys import core
from validphys.tests.conftest import PDF, HESSIAN_PDF


@pytest.mark.parametrize("pdf_name", [PDF, HESSIAN_PDF])
def test_pdf(pdf_name):
"""Check that the given PDF and their relevant attributes can be read
Expand Down
35 changes: 23 additions & 12 deletions validphys2/src/validphys/tests/test_fitveto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import pytest
import itertools
from hypothesis import given
from hypothesis.strategies import (floats, integers, tuples, lists,
booleans)
from hypothesis.extra.numpy import arrays, array_shapes
from hypothesis.strategies import floats, integers, tuples, lists, booleans
from hypothesis.extra.numpy import arrays, array_shapes

from validphys.fitveto import distribution_veto, determine_vetoes
from validphys.fitveto import NSIGMA_DISCARD_ARCLENGTH, NSIGMA_DISCARD_CHI2, INTEG_THRESHOLD
Expand All @@ -14,15 +13,21 @@
nicefloats = floats(allow_nan=False, allow_infinity=False)
integ_floats = floats(allow_nan=False, max_value=0.4)

fitinfos = tuples(integers(min_value=1),
nicefloats, nicefloats, nicefloats,
booleans(),
arrays(float, shape=7, elements=nicefloats),
arrays(float, shape=5, elements=integ_floats)).map(FitInfo._make)
fitinfos = tuples(
integers(min_value=1),
nicefloats,
nicefloats,
nicefloats,
booleans(),
arrays(float, shape=7, elements=nicefloats),
arrays(float, shape=5, elements=integ_floats),
).map(FitInfo._make)


thresholds = floats(min_value=1, max_value=10)
distributions = arrays(float, shape=shape1d, elements=nicefloats)


# Ignore over- and underflow warnings.
@pytest.mark.filterwarnings("ignore")
@given(distributions, thresholds)
Expand All @@ -32,18 +37,24 @@ def test_distribution_veto(arr, threshold):
assert np.all(masked - np.mean(arr) <= threshold * np.std(arr))


#The case where the list is empty is handled in postfit
# The case where the list is empty is handled in postfit
@pytest.mark.filterwarnings('ignore')
@given(lists(fitinfos, min_size=1))
def test_determine_vetoes(fitinfos):
vetoes = determine_vetoes(fitinfos, NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD)
vetoes = determine_vetoes(
fitinfos, NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD
)
assert np.all(vetoes['Positivity'] == np.array([info.is_positive for info in fitinfos]))
tot = vetoes['Total']
assert all(np.all(tot & val == tot) for val in vetoes.values())
single_replica_veto = determine_vetoes([fitinfos[0]], NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD)
single_replica_veto = determine_vetoes(
[fitinfos[0]], NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD
)
assert single_replica_veto['Total'][0] == single_replica_veto['Positivity'][0]
# distribution_vetoes applied a second time should veto nothing
if sum(tot) > 0:
passing_fitinfos = list(itertools.compress(fitinfos, tot))
second_vetoes = determine_vetoes(passing_fitinfos, NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD)
second_vetoes = determine_vetoes(
passing_fitinfos, NSIGMA_DISCARD_CHI2, NSIGMA_DISCARD_ARCLENGTH, INTEG_THRESHOLD
)
assert sum(vetoes["Total"]) == sum(second_vetoes["Total"])
1 change: 1 addition & 0 deletions validphys2/src/validphys/tests/test_mc2hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

NEIG = 5


def test_mc2hessian(data_config, tmp):
"""Tests that the generated hessian PDF is indeed marked as such
and that the metadata is not obviously broken
Expand Down
8 changes: 2 additions & 6 deletions validphys2/src/validphys/tests/test_metaexps.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,5 @@ def test_no_systematic_overlaps():
# TODO: when we have dataset defaults it would be nice to check that none
# of the default systematics overlap - should be fine for now.
ds_inputs = [{"dataset": ds} for ds in ds_names]
res = API.print_systype_overlap(
dataset_inputs=ds_inputs, metadata_group="experiment"
)
assert isinstance(
res, str
), f"Overlap found between metadata experiments {res[0]} {res[1]}"
res = API.print_systype_overlap(dataset_inputs=ds_inputs, metadata_group="experiment")
assert isinstance(res, str), f"Overlap found between metadata experiments {res[0]} {res[1]}"
2 changes: 1 addition & 1 deletion validphys2/src/validphys/tests/test_overfit_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"t0pdfset": {"from_": "datacuts"},
"pdf": {"from_": "fit"},
"dataset_inputs": {"from_": "fit"},
"separate_multiplicative": True
"separate_multiplicative": True,
}


Expand Down
7 changes: 6 additions & 1 deletion validphys2/src/validphys/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ def test_plot_xq2_custom():
dataset_inputs = [
{'dataset': 'NMC_NC_NOTFIXED_P_EM-SIGMARED', 'variant': 'legacy', 'custom_group': 'one'},
{'dataset': 'ATLAS_TTBAR_7TEV_TOT_X-SEC', 'variant': 'legacy', 'custom_group': 'one'},
{'dataset': 'CMS_Z0J_8TEV_PT-Y', 'cfac': ['NRM'], 'variant': 'legacy', 'custom_group': 'two'},
{
'dataset': 'CMS_Z0J_8TEV_PT-Y',
'cfac': ['NRM'],
'variant': 'legacy',
'custom_group': 'two',
},
]

return API.plot_xq2(
Expand Down
5 changes: 1 addition & 4 deletions validphys2/src/validphys/tests/test_postfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ def test_postfit(tmp):
for x in range(1, nrep + 1):
repnos = set()
# [File in PDF set, file in fit]
files = [
pdfsetpath / f"{TMPFIT}_{x:04d}.dat",
postfitpath / f"replica_{x}/{TMPFIT}.dat",
]
files = [pdfsetpath / f"{TMPFIT}_{x:04d}.dat", postfitpath / f"replica_{x}/{TMPFIT}.dat"]
for file in files:
with open(file, "r") as f:
data = yaml.safe_load_all(f)
Expand Down
5 changes: 4 additions & 1 deletion validphys2/src/validphys/tests/test_pyfkdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def test_positivity(pdf_name):
assert_allclose(preds.values, core_predictions.rawdata)
# Now do the same with the API
api_predictions = API.positivity_predictions_data_result(
theoryid=THEORYID, use_cuts="internal", pdf=pdf_name, posdataset={"dataset": posset, "maxlambda": 1e6}
theoryid=THEORYID,
use_cuts="internal",
pdf=pdf_name,
posdataset={"dataset": posset, "maxlambda": 1e6},
)
assert_allclose(preds.values, api_predictions.rawdata)
# And now check that the results are correct for any kind of PDF
Expand Down
101 changes: 50 additions & 51 deletions validphys2/src/validphys/tests/test_tableloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,65 @@
def test_min_combination():
nan = np.nan
dfdicts = [
{('NNPDF31_nlo_as_0130_uncorr__combined',
'chi2'): {('NMC', 'NMC', 204, 394): nan, ('NMC',
'NMC',
204,
400): 679.78465060721771, ('NMC', 'NMCPD', 121, 394): nan, ('NMC',
'NMCPD',
121,
400): 226.73557951005108, ('NMC', 'Total', 325, 394): nan, ('NMC',
'Total',
325,
400): 906.52023011726862, ('SLAC', 'SLACD', 34, 394): nan, ('SLAC',
'SLACD',
34,
400): 55.48265988454024, ('SLAC', 'SLACP', 33, 394): nan, ('SLAC',
'SLACP',
33,
400): 56.765548408772041, ('SLAC', 'Total', 67, 394): nan, ('SLAC',
'Total',
67,
400): 110.50940462648715}},

{('NNPDF31_nlo_as_0130_uncorr__combined', 'chi2'):
{('NMC', 'NMC', 204, 394): 572.89990139102679, ('NMC','NMC', 204,
400): 643.10896957499369, ('NMC',
'NMCPD',
121,
394): 228.66185589823439, ('NMC', 'NMCPD', 121, 400): 232.87411189255329, ('NMC',
'Total',
325,
394): 801.56175728926098, ('NMC',
'Total',
325,
400): 875.98308146754709, ('SLAC', 'SLACD', 34, 394): 62.071957514383364, ('SLAC',
'SLACD',
34,
400): 44.278821773436725, ('SLAC',
'SLACP',
33,
394): 70.465557968855123, ('SLAC', 'SLACP', 33, 400): 57.213453384186352, ('SLAC',
'Total',
67,
394): 126.25943979466223, ('SLAC', 'Total', 67, 400): 101.69776217874313}},
{
('NNPDF31_nlo_as_0130_uncorr__combined', 'chi2'): {
('NMC', 'NMC', 204, 394): nan,
('NMC', 'NMC', 204, 400): 679.78465060721771,
('NMC', 'NMCPD', 121, 394): nan,
('NMC', 'NMCPD', 121, 400): 226.73557951005108,
('NMC', 'Total', 325, 394): nan,
('NMC', 'Total', 325, 400): 906.52023011726862,
('SLAC', 'SLACD', 34, 394): nan,
('SLAC', 'SLACD', 34, 400): 55.48265988454024,
('SLAC', 'SLACP', 33, 394): nan,
('SLAC', 'SLACP', 33, 400): 56.765548408772041,
('SLAC', 'Total', 67, 394): nan,
('SLAC', 'Total', 67, 400): 110.50940462648715,
}
},
{
('NNPDF31_nlo_as_0130_uncorr__combined', 'chi2'): {
('NMC', 'NMC', 204, 394): 572.89990139102679,
('NMC', 'NMC', 204, 400): 643.10896957499369,
('NMC', 'NMCPD', 121, 394): 228.66185589823439,
('NMC', 'NMCPD', 121, 400): 232.87411189255329,
('NMC', 'Total', 325, 394): 801.56175728926098,
('NMC', 'Total', 325, 400): 875.98308146754709,
('SLAC', 'SLACD', 34, 394): 62.071957514383364,
('SLAC', 'SLACD', 34, 400): 44.278821773436725,
('SLAC', 'SLACP', 33, 394): 70.465557968855123,
('SLAC', 'SLACP', 33, 400): 57.213453384186352,
('SLAC', 'Total', 67, 394): 126.25943979466223,
('SLAC', 'Total', 67, 400): 101.69776217874313,
}
},
]
dfs = [pd.DataFrame.from_dict(df) for df in dfdicts]
res = tableloader.combine_pseudorreplica_tables(dfs, ['NNPDF31_nlo_as_0130_uncorr__combined'], )
assert pd.isnull(res.loc[pd.IndexSlice[:,:,:,394],:]).all().all()
assert (res.loc[pd.IndexSlice[:,:,:,400],:] == dfs[1].loc[pd.IndexSlice[:,:,:,400],:]).all().all()
res = tableloader.combine_pseudorreplica_tables(dfs, ['NNPDF31_nlo_as_0130_uncorr__combined'])
assert pd.isnull(res.loc[pd.IndexSlice[:, :, :, 394], :]).all().all()
assert (
(res.loc[pd.IndexSlice[:, :, :, 400], :] == dfs[1].loc[pd.IndexSlice[:, :, :, 400], :])
.all()
.all()
)
res2 = tableloader.combine_pseudorreplica_tables(
dfs, ['NNPDF31_nlo_as_0130_uncorr__combined'], min_points_required=1)
assert not pd.isnull(res2.loc[pd.IndexSlice[:,:,:,394],:]).all().all()
assert (res2.loc[pd.IndexSlice[:,:,:,400],:] == dfs[1].loc[pd.IndexSlice[:,:,:,400],:]).all().all()
dfs, ['NNPDF31_nlo_as_0130_uncorr__combined'], min_points_required=1
)
assert not pd.isnull(res2.loc[pd.IndexSlice[:, :, :, 394], :]).all().all()
assert (
(res2.loc[pd.IndexSlice[:, :, :, 400], :] == dfs[1].loc[pd.IndexSlice[:, :, :, 400], :])
.all()
.all()
)


def test_extrasum_slice():
l = Loader()
f = l.check_vp_output_file('ljzWOixPQfmq5dA1-EUocg==/tables/fits_chi2_table.csv')
f = l.check_vp_output_file('ljzWOixPQfmq5dA1-EUocg==/tables/fits_chi2_table.csv')
d, l = tableloader.load_adapted_fits_chi2_table(f)
components = ['LHCb Total', 'ATLASTTBARTOT', 'LHCBZ940PB']
sliced = tableloader.get_extrasum_slice(d, components)
slicel = tableloader.get_extrasum_slice(d, components)
assert sliced.shape == (3,1)
assert sliced.loc[('LHCb', 'Total'),:].shape == (1,)
assert sliced.shape == (3, 1)
assert sliced.loc[('LHCb', 'Total'), :].shape == (1,)
assert slicel.size == 3
Loading

0 comments on commit 2caba59

Please sign in to comment.