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

feat: Add pyhf.workspace.build #1101

Merged
merged 28 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8e74de5
pyhf.workspace.build
lukasheinrich Oct 13, 2020
b71daa4
pyhf.workspace.build
lukasheinrich Oct 13, 2020
fa71322
pyhf.workspace.build
lukasheinrich Oct 13, 2020
982d8bb
pyhf.workspace.build
lukasheinrich Oct 13, 2020
6f48523
Apply suggestions from code review
lukasheinrich Oct 13, 2020
6af2f85
Use correct namespace
matthewfeickert Oct 13, 2020
7df0c31
cls method
lukasheinrich Oct 13, 2020
e23b7d4
build
lukasheinrich Oct 13, 2020
4f98bf7
build
lukasheinrich Oct 13, 2020
6846c28
pyhf.workspace.build
lukasheinrich Oct 13, 2020
3adc931
pyhf.workspace.build
lukasheinrich Oct 13, 2020
2a536ee
pyhf.workspace.build
lukasheinrich Oct 13, 2020
1d35b85
pyhf.workspace.build
lukasheinrich Oct 13, 2020
fefa350
Apply suggestions from code review
lukasheinrich Oct 13, 2020
356a106
Use correct namespace
matthewfeickert Oct 13, 2020
5f7beef
cls method
lukasheinrich Oct 13, 2020
c75d949
Fix typo
matthewfeickert Oct 14, 2020
0ed3a2d
Add missing args to docstring
matthewfeickert Oct 14, 2020
485a7e5
build
lukasheinrich Oct 14, 2020
04e0324
build
lukasheinrich Oct 14, 2020
a3b7eeb
build
lukasheinrich Oct 14, 2020
c6aba08
Merge branch 'master' into workspace_maker
lukasheinrich Oct 14, 2020
969a15a
lint
lukasheinrich Oct 14, 2020
f951465
Update src/pyhf/workspace.py
lukasheinrich Oct 14, 2020
84f98fd
Merge branch 'master' into workspace_maker
lukasheinrich Oct 14, 2020
a520488
doi url
lukasheinrich Oct 14, 2020
194baa0
Update src/pyhf/exceptions/__init__.py
lukasheinrich Oct 14, 2020
ac5e2a4
Remove left behind )
matthewfeickert Oct 14, 2020
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
7 changes: 7 additions & 0 deletions src/pyhf/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ def __init__(self, *args, **kwargs):
self.samples = sorted(list(set(self.samples)))
self.parameters = sorted(list(set(self.parameters)))
self.modifiers = sorted(list(set(self.modifiers)))

self.channel_slices = {}
begin = 0
for c in self.channels:
end = begin + self.channel_nbins[c]
self.channel_slices[c] = slice(begin, end)
begin = end
25 changes: 25 additions & 0 deletions src/pyhf/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,28 @@ def sorted(cls, workspace):
newspec['observations'].sort(key=lambda e: e['name'])

return cls(newspec)

@classmethod
def build(cls, model, data, name='measurement'):
"""
Build a workspace from model and data.

Args:
model (~pyhf.pdf.Model): A model to store into a workspace
data (tensor): A array holding observations to store into a workspace
name (str): The name of the workspace measurement

Returns:
~pyhf.workspace.Workspace: A new workspace object

"""
workspace = copy.deepcopy(model.spec)
workspace['version'] = utils.SCHEMA_VERSION
workspace['measurements'] = [
{'name': name, 'config': {'poi': model.config.poi_name, 'parameters': []}}
]
workspace['observations'] = [
{'name': k, 'data': data[model.config.channel_slices[k]]}
for k in model.config.channels
]
return cls(workspace)
18 changes: 18 additions & 0 deletions tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,21 @@ def test_sorted(workspace_factory):
for channel in new_ws['channels']:
# check sort
assert channel['samples'][-1]['name'] == 'zzzzlast'


def test_closure_over_workspace_build():
model = pyhf.simplemodels.hepdata_like(
signal_data=[12.0, 11.0], bkg_data=[50.0, 52.0], bkg_uncerts=[3.0, 7.0]
)
data = [51, 48]
one = pyhf.infer.hypotest(1.0, data + model.config.auxdata, model)

workspace = pyhf.Workspace.build(model, data)

assert json.dumps(workspace)

newmodel = workspace.model()
newdata = workspace.data(newmodel)
two = pyhf.infer.hypotest(1.0, newdata, newmodel)

assert one == two
kratsg marked this conversation as resolved.
Show resolved Hide resolved