Skip to content

Commit

Permalink
Apply black to tests/ after rebase against master
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfeickert committed Oct 18, 2018
1 parent 4fdee9a commit bb386f7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
40 changes: 20 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ def isolate_modules():
yield isolate_modules
sys.modules.update(CACHE_MODULES)

'''
This fixture is automatically run to clear out the events registered before and after a test function runs.
'''

@pytest.fixture(scope='function', autouse=True)
def reset_events():
"""
This fixture is automatically run to clear out the events registered before and after a test function runs.
"""
pyhf.events.__events.clear()
pyhf.events.__disabled_events.clear()
yield reset_events
pyhf.events.__events.clear()
pyhf.events.__disabled_events.clear()

'''
This fixture is automatically run to reset the backend before and after a test function runs.
'''

@pytest.fixture(scope='function', autouse=True)
def reset_backend():
"""
Expand All @@ -38,20 +37,21 @@ def reset_backend():
yield reset_backend
pyhf.set_backend(pyhf.default_backend)

@pytest.fixture(scope='function', params=[
(pyhf.tensor.numpy_backend(), None),
(pyhf.tensor.tensorflow_backend(session=tf.Session()), None),
(pyhf.tensor.pytorch_backend(), None),
(pyhf.tensor.mxnet_backend(), None),
(pyhf.tensor.numpy_backend(poisson_from_normal=True), pyhf.optimize.minuit_optimizer()),
],
ids=[
'numpy',
'tensorflow',
'pytorch',
'mxnet',
'numpy_minuit',
])

@pytest.fixture(
scope='function',
params=[
(pyhf.tensor.numpy_backend(), None),
(pyhf.tensor.tensorflow_backend(session=tf.Session()), None),
(pyhf.tensor.pytorch_backend(), None),
(pyhf.tensor.mxnet_backend(), None),
(
pyhf.tensor.numpy_backend(poisson_from_normal=True),
pyhf.optimize.minuit_optimizer(),
),
],
ids=['numpy', 'tensorflow', 'pytorch', 'mxnet', 'numpy_minuit'],
)
def backend(request):
param = request.param
# a better way to get the id? all the backends we have so far for testing
Expand Down
7 changes: 4 additions & 3 deletions tests/test_constraints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import pyhf


Expand Down Expand Up @@ -91,10 +92,10 @@ def slow(self, auxdata, pars):
return tensorlib.sum(summands) if summands is not None else 0

def fast(self, auxdata, pars):
return self.constraint_logpdf(auxdata,pars)
return self.constraint_logpdf(auxdata, pars)

auxd = pyhf.tensorlib.astensor(m.config.auxdata)
pars = pyhf.tensorlib.astensor(m.config.suggested_init())
slow_result = pyhf.tensorlib.tolist(slow(m,auxd,pars))
fast_result = pyhf.tensorlib.tolist(fast(m,auxd,pars))
slow_result = pyhf.tensorlib.tolist(slow(m, auxd, pars))
fast_result = pyhf.tensorlib.tolist(fast(m, auxd, pars))
assert pytest.approx(slow_result) == fast_result
4 changes: 3 additions & 1 deletion tests/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def filled_shapes(histogramssets, alphasets):
def test_interpolator(backend, interpcode, random_histosets_alphasets_pair):
histogramssets, alphasets = random_histosets_alphasets_pair

interpolator = getattr(pyhf.interpolate, '_hfinterpolator_code{}'.format(interpcode))(histogramssets.tolist())
interpolator = getattr(
pyhf.interpolate, '_hfinterpolator_code{}'.format(interpcode)
)(histogramssets.tolist())
assert interpolator.alphasets_shape == (histogramssets.shape[0], 1)
interpolator(pyhf.tensorlib.astensor(alphasets.tolist()))
assert interpolator.alphasets_shape == alphasets.shape
Expand Down
19 changes: 13 additions & 6 deletions tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import json


@pytest.mark.fail_mxnet
def test_pdf_inputs(backend):
source = {
Expand All @@ -18,12 +19,15 @@ def test_pdf_inputs(backend):
pars = pdf.config.suggested_init()
data = source['bindata']['data'] + pdf.config.auxdata


tensorlib, _ = backend
assert tensorlib.shape(tensorlib.astensor(data)) == (2,)
assert tensorlib.shape(tensorlib.astensor(pars)) == (2,)
assert tensorlib.tolist(pdf.pdf(pars,data)) == pytest.approx([0.002417160663753748], abs=1e-4)
assert tensorlib.tolist(pdf.logpdf(pars,data)) == pytest.approx([-6.025179228209936], abs=1e-4)
assert tensorlib.tolist(pdf.pdf(pars, data)) == pytest.approx(
[0.002417160663753748], abs=1e-4
)
assert tensorlib.tolist(pdf.logpdf(pars, data)) == pytest.approx(
[-6.025179228209936], abs=1e-4
)


@pytest.mark.only_numpy
Expand Down Expand Up @@ -52,6 +56,7 @@ def test_core_pdf_broadcasting(backend):
assert broadcasted.shape == np.array(data).shape
assert np.all(naive_python == broadcasted)


def test_pdf_integration_staterror(backend):
spec = {
'channels': [
Expand Down Expand Up @@ -96,11 +101,13 @@ def test_pdf_integration_staterror(backend):
par = pdf.config.par_slice('stat_firstchannel')
par_set = pdf.config.param_set('stat_firstchannel')
tensorlib, _ = backend
uncerts = tensorlib.astensor([[12.,12.],[5.,5.]])
nominal = tensorlib.astensor([[50.,70.],[30.,20.]])
uncerts = tensorlib.astensor([[12.0, 12.0], [5.0, 5.0]])
nominal = tensorlib.astensor([[50.0, 70.0], [30.0, 20.0]])
quad = tensorlib.sqrt(tensorlib.sum(tensorlib.power(uncerts, 2), axis=0))
totals = tensorlib.sum(nominal, axis=0)
assert pytest.approx(tensorlib.tolist(par_set.sigmas)) == tensorlib.tolist(tensorlib.divide(quad, totals))
assert pytest.approx(tensorlib.tolist(par_set.sigmas)) == tensorlib.tolist(
tensorlib.divide(quad, totals)
)


@pytest.mark.only_numpy
Expand Down
9 changes: 4 additions & 5 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,12 @@ def validate_runOnePoint(pdf, data, mu_test, expected_result, tolerance=1e-6):
par_bounds = pdf.config.suggested_bounds()

CLs_obs, CLs_exp = pyhf.utils.runOnePoint(
mu_test, data, pdf, init_pars, par_bounds)[-2:]
mu_test, data, pdf, init_pars, par_bounds
)[-2:]

assert abs(CLs_obs - expected_result['obs']) / \
expected_result['obs'] < tolerance
assert abs(CLs_obs - expected_result['obs']) / expected_result['obs'] < tolerance
for result, expected_result in zip(CLs_exp, expected_result['exp']):
assert abs(result - expected_result) / \
expected_result < tolerance
assert abs(result - expected_result) / expected_result < tolerance


@pytest.mark.parametrize(
Expand Down

0 comments on commit bb386f7

Please sign in to comment.