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

Add additional Model helper methods #1235

Open
matthewfeickert opened this issue Dec 22, 2020 · 2 comments
Open

Add additional Model helper methods #1235

matthewfeickert opened this issue Dec 22, 2020 · 2 comments
Labels
API Changes the public API feat/enhancement New feature or request question Further information is requested

Comments

@matthewfeickert
Copy link
Member

matthewfeickert commented Dec 22, 2020

Description

Does it makes sense to add some helper methods to pyhf.pdf.Model that would do some common things that users seem to want? In particular I'm thinking

def get_par_slice(self, par_name):
    return self.config.par_map[par_name]["slice"]

and

def to_json(self):
    return json.dumps(self.spec)

which would allow things like

>>> import pyhf
>>> pyhf.set_backend("numpy")
>>> model = pyhf.simplemodels.hepdata_like(
...     signal_data=[12.0, 11.0], bkg_data=[50.0, 52.0], bkg_uncerts=[3.0, 7.0]
... )
>>> model.config.parameters
['mu', 'uncorr_bkguncrt']
>>> data = [51, 48] + model.config.auxdat
>>> bestfit_pars = pyhf.infer.mle.fit(data, model)
>>> bestfit_pars[model.get_par_slice("uncorr_bkguncrt")]
array([1.0030512 , 0.96266961])
>>> for name in model.config.parameters:
...     print(f"best fit {name}: {bestfit_pars[model.get_par_slice(name)]}")
... 
best fit mu: [0.]
best fit uncorr_bkguncrt: [1.0030512  0.96266961]
>>> json_str = model.to_json()

Admittedly model.to_json() might be of limited use as if you want to write that out to disk you don't save any steps.

Thoughts @lukasheinrich and @kratsg?

@matthewfeickert matthewfeickert added feat/enhancement New feature or request question Further information is requested API Changes the public API labels Dec 22, 2020
@matthewfeickert matthewfeickert changed the title Model helper methods Add additional Model helper methods Dec 22, 2020
@alexander-held
Copy link
Member

related: #994

@alexander-held
Copy link
Member

Related to get_par_slice above, model.config.par_slice(par_name) already exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes the public API feat/enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants