scikit-hep / probfit

Cost function builder. For fitting distributions.
http://probfit.readthedocs.io/
MIT License
51 stars 30 forks source link

Error while trying to access draw or show methods #126

Open tosaric opened 2 years ago

tosaric commented 2 years ago

I tried to access draw or show methods just like from the tutorial, but I get an error. Here is minimal reproducible example:

from probfit import gaussian, UnbinnedLH from iminuit import Minuit unbinned_likelihood = UnbinnedLH(gaussian, xdata.values) minuit = Minuit(unbinned_likelihood, mean=0, sigma=5) unbinned_likelihood.draw(minuit)

And this is the errror I am getting:

_AttributeError Traceback (most recent call last) /var/folders/gj/xpkrsnhd2zl8gs7vsxy72bs40000gn/T/ipykernel_41791/1198878356.py in 1 minuit = Minuit(unbinned_likelihood, mean=0, sigma=5) ----> 2 unbinnedlikelihood.draw(minuit) probfit/costfunc.pyx in probfit.costfunc.UnbinnedLH.draw()

_/opt/miniconda3/envs/astrometry/lib/python3.9/site-packages/probfit/plotting.py in draw_ulh(self, minuit, bins, ax, bound, parmloc, nfbins, print_par, grid, args, errors, parts, show_errbars, noplot) _94 ax = plt.gca() if ax is None and not noplot else ax 95 _---> 96 arg, error = _get_args_anderrors(self, minuit, args, errors) 97 98 n, e = np.histogram(self.data, bins=bins, range=bound, weights=self.weights)_

_/opt/miniconda3/envs/astrometry/lib/python3.9/site-packages/probfit/plotting.py in _get_args_anderrors(self, minuit, args, errors) _40 reterror = None 41 if minuit is not None: # case 1 _---> 42 retarg = minuit.args _ 43 reterror = minuit.errors _44 return ret_arg, reterror

AttributeError: 'Minuit' object has no attribute 'args'

nrad commented 2 years ago

Also, just came across this issue. It looks like this is because probfit requires an older version of iminuit (1.5.4). So downgrading the iminuit version should solve this, but then you might end up missing some of the more recent features of iminuit.

The workaround is to pass minuit.values and minuit.errors to the draw function instead of minuit, i.e unbinned_likelihood.draw(args=minuit.values, errors=minuit.errors)

jonas-eschle commented 2 years ago

Hi @nrad , thanks for reporting this! However, probfit is not actively maintained anymore and won't get updates. As you seem to do unbinned likelihood fits, the recommendation would be to either use iminuit, which offers minimization capabilities and a few cost functions or zfit, which allows additionally to minimization (using iminuit) and cost functions - similarly to probfit - building your model with out-of-the-box components.

If any of the probfit functionality is missing, please let us know!