pyhf icon indicating copy to clipboard operation
pyhf copied to clipboard

Add additional Model helper methods

Open matthewfeickert opened this issue 5 years ago • 2 comments

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 avatar Dec 22 '20 05:12 matthewfeickert

related: #994

alexander-held avatar Dec 22 '20 17:12 alexander-held

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

alexander-held avatar Oct 14 '21 10:10 alexander-held