Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Created July 13, 2019 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewfeickert/f03a7ce33b70de2e21df371a54903884 to your computer and use it in GitHub Desktop.
Save matthewfeickert/f03a7ce33b70de2e21df371a54903884 to your computer and use it in GitHub Desktop.
Testing script for Numba pyhf tests
#!/usr/bin/env python3
import sys
import json
import argparse
import time
import pyhf
def main(args):
if args.config_file is not None:
if ".json" in args.config_file:
config_file = args.config_file
else:
print("A JSON configuration file is required")
sys.exit()
parsed = None
with open(config_file, "r") as f:
parsed = json.load(f)
if not parsed:
print("ERROR could not load JSON file")
sys.exit()
workspace = pyhf.Workspace(parsed)
pdf = workspace.model(
modifier_settings={
'normsys': {'interpcode': 'code4'},
'histosys': {'interpcode': 'code4p'},
}
)
time_start = time.time()
n_tests = 5000
for _ in range(n_tests):
pyhf.utils.hypotest(
1.0, workspace.data(pdf), pdf, qtilde='qtilde', return_expected_set=True
)
time_end = time.time()
total_time = time_end - time_start
mean_time = total_time / n_tests
result = pyhf.utils.hypotest(
1.0, workspace.data(pdf), pdf, qtilde='qtilde', return_expected_set=True
)
result = {'CLs_obs': result[0].tolist()[0], 'CLs_exp': result[-1].ravel().tolist()}
print(f"Expected CL_s: {result['CLs_exp']}")
print(f"Observed CL_s: {result['CLs_obs']}")
print(f'\nTotal run time: {total_time} seconds')
print(f'Mean time over {n_tests} tests: {mean_time} seconds')
if __name__ == '__main__':
cli_parser = argparse.ArgumentParser(
description="configuration arguments provided at run time from the CLI"
)
cli_parser.add_argument(
"-c",
"--config",
dest="config_file",
type=str,
default=None,
help="config file path",
)
args, unknown = cli_parser.parse_known_args()
parser = argparse.ArgumentParser(parents=[cli_parser], add_help=False)
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment