Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Use scikit-hep-testdata to provide probability models for regression tests #1711

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
+ extras_require['contrib']
+ extras_require['shellcomplete']
+ [
'scikit-hep-testdata>=0.4.11',
'pytest>=6.0',
'pytest-cov>=2.5.1',
'pytest-mock',
Expand Down
31 changes: 6 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
import pytest
import pyhf
import sys
import requests
import hashlib
import tarfile
import json
import os
import pathlib
import distutils.dir_util


@pytest.fixture(scope='module')
def sbottom_likelihoods_download():
"""Download the sbottom likelihoods tarball from HEPData"""
sbottom_HEPData_URL = "https://doi.org/10.17182/hepdata.89408.v1/r2"
targz_filename = "sbottom_workspaces.tar.gz"
response = requests.get(sbottom_HEPData_URL, stream=True)
assert response.status_code == 200
with open(targz_filename, "wb") as file:
file.write(response.content)
assert (
hashlib.sha256(open(targz_filename, "rb").read()).hexdigest()
== "9089b0e5fabba335bea4c94545ccca8ddd21289feeab2f85e5bcc8bada37be70"
)
# Open as a tarfile
yield tarfile.open(targz_filename, "r:gz")
os.remove(targz_filename)


# Factory as fixture pattern
@pytest.fixture
def get_json_from_tarfile():
def _get_json_from_tarfile(tarfile, json_name):
json_file = (
tarfile.extractfile(tarfile.getmember(json_name)).read().decode("utf8")
)
def _get_json_from_tarfile(archive_data_path, json_name):
with tarfile.open(archive_data_path, "r:gz") as archive:
json_file = (
archive.extractfile(archive.getmember(json_name)).read().decode("utf8")
)
return json.loads(json_file)

return _get_json_from_tarfile
Expand Down Expand Up @@ -150,6 +130,7 @@ def datadir(tmpdir, request):
if test_dir.is_dir():
distutils.dir_util.copy_tree(test_dir, tmpdir.strpath)
# shutil is nicer, but doesn't work: https://bugs.python.org/issue20849
# Once pyhf is Python 3.8+ only then the below can be used.
# shutil.copytree(test_dir, tmpdir)

return tmpdir
9 changes: 6 additions & 3 deletions tests/test_paramviewer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from skhep_testdata import data_path

import pyhf
from pyhf.parameters import ParamViewer

Expand All @@ -20,10 +22,11 @@ def test_paramviewer_simple_nonbatched(backend):
assert pyhf.tensorlib.tolist(par_slice) == [6, 7, 1, 2]


def test_paramviewer_order(sbottom_likelihoods_download, get_json_from_tarfile):
lhood = get_json_from_tarfile(sbottom_likelihoods_download, "RegionA/BkgOnly.json")
def test_paramviewer_order(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
lhood = get_json_from_tarfile(sbottom_archive, "RegionA/BkgOnly.json")
patch = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/patch.sbottom_1300_205_60.json"
sbottom_archive, "RegionA/patch.sbottom_1300_205_60.json"
)
workspace = pyhf.workspace.Workspace(lhood)
model = workspace.model(patches=[patch])
Expand Down
49 changes: 23 additions & 26 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest
from skhep_testdata import data_path

import pyhf
import numpy as np


def calculate_CLs(bkgonly_json, signal_patch_json):
Expand Down Expand Up @@ -31,14 +33,13 @@ def calculate_CLs(bkgonly_json, signal_patch_json):
return result[0].tolist(), result[-1]


def test_sbottom_regionA_1300_205_60(
sbottom_likelihoods_download, get_json_from_tarfile
):
def test_sbottom_regionA_1300_205_60(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
sbottom_regionA_bkgonly_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/BkgOnly.json"
sbottom_archive, "RegionA/BkgOnly.json"
)
sbottom_regionA_1300_205_60_patch_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/patch.sbottom_1300_205_60.json"
sbottom_archive, "RegionA/patch.sbottom_1300_205_60.json"
)
CLs_obs, CLs_exp = calculate_CLs(
sbottom_regionA_bkgonly_json, sbottom_regionA_1300_205_60_patch_json
Expand All @@ -61,14 +62,13 @@ def test_sbottom_regionA_1300_205_60(
)


def test_sbottom_regionA_1400_950_60(
sbottom_likelihoods_download, get_json_from_tarfile
):
def test_sbottom_regionA_1400_950_60(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
sbottom_regionA_bkgonly_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/BkgOnly.json"
sbottom_archive, "RegionA/BkgOnly.json"
)
sbottom_regionA_1400_950_60_patch_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/patch.sbottom_1400_950_60.json"
sbottom_archive, "RegionA/patch.sbottom_1400_950_60.json"
)
CLs_obs, CLs_exp = calculate_CLs(
sbottom_regionA_bkgonly_json, sbottom_regionA_1400_950_60_patch_json
Expand All @@ -91,14 +91,13 @@ def test_sbottom_regionA_1400_950_60(
)


def test_sbottom_regionA_1500_850_60(
sbottom_likelihoods_download, get_json_from_tarfile
):
def test_sbottom_regionA_1500_850_60(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
sbottom_regionA_bkgonly_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/BkgOnly.json"
sbottom_archive, "RegionA/BkgOnly.json"
)
sbottom_regionA_1500_850_60_patch_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionA/patch.sbottom_1500_850_60.json"
sbottom_archive, "RegionA/patch.sbottom_1500_850_60.json"
)
CLs_obs, CLs_exp = calculate_CLs(
sbottom_regionA_bkgonly_json, sbottom_regionA_1500_850_60_patch_json
Expand All @@ -121,14 +120,13 @@ def test_sbottom_regionA_1500_850_60(
)


def test_sbottom_regionB_1400_550_60(
sbottom_likelihoods_download, get_json_from_tarfile
):
def test_sbottom_regionB_1400_550_60(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
sbottom_regionB_bkgonly_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionB/BkgOnly.json"
sbottom_archive, "RegionB/BkgOnly.json"
)
sbottom_regionB_1400_550_60_patch_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionB/patch.sbottom_1400_550_60.json"
sbottom_archive, "RegionB/patch.sbottom_1400_550_60.json"
)
CLs_obs, CLs_exp = calculate_CLs(
sbottom_regionB_bkgonly_json, sbottom_regionB_1400_550_60_patch_json
Expand All @@ -151,14 +149,13 @@ def test_sbottom_regionB_1400_550_60(
)


def test_sbottom_regionC_1600_850_60(
sbottom_likelihoods_download, get_json_from_tarfile
):
def test_sbottom_regionC_1600_850_60(get_json_from_tarfile):
sbottom_archive = data_path("pyhf-ins1748602-probability-models.tar.gz")
sbottom_regionC_bkgonly_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionC/BkgOnly.json"
sbottom_archive, "RegionC/BkgOnly.json"
)
sbottom_regionC_1600_850_60_patch_json = get_json_from_tarfile(
sbottom_likelihoods_download, "RegionC/patch.sbottom_1600_850_60.json"
sbottom_archive, "RegionC/patch.sbottom_1600_850_60.json"
)
CLs_obs, CLs_exp = calculate_CLs(
sbottom_regionC_bkgonly_json, sbottom_regionC_1600_850_60_patch_json
Expand Down