Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_import_roundtrip(tmpdir, toplvl, basedir):
parsed_xml_before = pyhf.readxml.parse(toplvl, basedir)
spec = {
'channels': parsed_xml_before['channels'],
'parameters': parsed_xml_before['measurements'][0]['config']['parameters'],
}
pdf_before = pyhf.Model(spec, poiname='SigXsecOverSM')
tmpconfig = tmpdir.mkdir('config')
tmpdata = tmpdir.mkdir('data')
tmpxml = tmpdir.join('FitConfig.xml')
tmpxml.write(
pyhf.writexml.writexml(
parsed_xml_before,
tmpconfig.strpath,
tmpdata.strpath,
os.path.join(tmpdir.strpath, 'FitConfig'),
).decode('utf-8')
def test_import_histosys():
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input2/config/example.xml', 'validation/xmlimport_input2'
)
# build the spec, strictly checks properties included
spec = {
'channels': parsed_xml['channels'],
'parameters': parsed_xml['measurements'][0]['config']['parameters'],
}
pdf = pyhf.Model(spec, poiname='SigXsecOverSM')
data = [
binvalue
for k in pdf.spec['channels']
for binvalue in next(
obs for obs in parsed_xml['observations'] if obs['name'] == k['name']
)['data']
def test_import_filecache(mocker):
mocker.patch("pyhf.readxml.uproot.open", wraps=uproot.open)
pyhf.readxml.clear_filecache()
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
# call a second time (file should be cached now)
parsed_xml2 = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
# check if uproot.open was only called once with the expected root file
pyhf.readxml.uproot.open.assert_called_once_with(
os.path.join("validation/xmlimport_input", "./data/example.root")
)
assert_equal_dictionary(parsed_xml, parsed_xml2)
def test_import_normfactor_bounds():
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input2/config/example.xml', 'validation/xmlimport_input2'
)
ws = pyhf.Workspace(parsed_xml)
assert ('SigXsecOverSM', 'normfactor') in ws.modifiers
parameters = [
p
for p in ws.get_measurement(measurement_name='GaussExample')['config'][
'parameters'
]
if p['name'] == 'SigXsecOverSM'
]
assert len(parameters) == 1
parameter = parameters[0]
assert parameter['bounds'] == [[0, 10]]
def workspace_xml(request):
return pyhf.readxml.parse(*request.param)
def test_import_measurements():
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
measurements = parsed_xml['measurements']
assert len(measurements) == 4
measurement_configs = measurements[0]['config']
assert 'parameters' in measurement_configs
assert len(measurement_configs['parameters']) == 3
parnames = [p['name'] for p in measurement_configs['parameters']]
assert sorted(parnames) == sorted(['lumi', 'SigXsecOverSM', 'alpha_syst1'])
lumi_param_config = measurement_configs['parameters'][0]
assert 'auxdata' in lumi_param_config
assert lumi_param_config['auxdata'] == [1.0]
assert 'bounds' in lumi_param_config
def test_import_filecache(mocker):
mocker.patch("pyhf.readxml.uproot.open", wraps=uproot.open)
pyhf.readxml.clear_filecache()
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
# call a second time (file should be cached now)
parsed_xml2 = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
# check if uproot.open was only called once with the expected root file
pyhf.readxml.uproot.open.assert_called_once_with(
os.path.join("validation/xmlimport_input", "./data/example.root")
)
assert_equal_dictionary(parsed_xml, parsed_xml2)
def test_import_prepHistFactory():
parsed_xml = pyhf.readxml.parse(
'validation/xmlimport_input/config/example.xml', 'validation/xmlimport_input/'
)
# build the spec, strictly checks properties included
spec = {
'channels': parsed_xml['channels'],
'parameters': parsed_xml['measurements'][0]['config']['parameters'],
}
pdf = pyhf.Model(spec, poiname='SigXsecOverSM')
data = [
binvalue
for k in pdf.spec['channels']
for binvalue in next(
obs for obs in parsed_xml['observations'] if obs['name'] == k['name']
)['data']
'parameters': parsed_xml_before['measurements'][0]['config']['parameters'],
}
pdf_before = pyhf.Model(spec, poiname='SigXsecOverSM')
tmpconfig = tmpdir.mkdir('config')
tmpdata = tmpdir.mkdir('data')
tmpxml = tmpdir.join('FitConfig.xml')
tmpxml.write(
pyhf.writexml.writexml(
parsed_xml_before,
tmpconfig.strpath,
tmpdata.strpath,
os.path.join(tmpdir.strpath, 'FitConfig'),
).decode('utf-8')
)
parsed_xml_after = pyhf.readxml.parse(tmpxml.strpath, tmpdir.strpath)
spec = {
'channels': parsed_xml_after['channels'],
'parameters': parsed_xml_after['measurements'][0]['config']['parameters'],
}
pdf_after = pyhf.Model(spec, poiname='SigXsecOverSM')
data_before = [
binvalue
for k in pdf_before.config.channels
for binvalue in next(
obs for obs in parsed_xml_before['observations'] if obs['name'] == k
)['data']
] + pdf_before.config.auxdata
data_after = [
binvalue