dkriegner / xrayutilities

xrayutilities - a package with useful scripts for X-ray diffraction physicists
http://xrayutilities.sourceforge.io
GNU General Public License v2.0
81 stars 29 forks source link

Positional arguments in KinematicalModel.simulate() #175

Closed rugfri closed 4 months ago

rugfri commented 8 months ago

Hi, with the latest version 1.7.6 I'm getting the error:

KinematicalModel.simulate() missing 1 required positional argument: 'hkl'

the code snippet is mk = xu.simpack.KinematicalModel(ls_s, energy=en, resolution_width=resol) fitmkin = xu.simpack.FitModel(mk) fitmkin.set_param_hint(f'{material}_a', vary=True) fitmkin.set_param_hint(f'{material}_thickness', vary=True) fitmkin.set_param_hint('I0', vary=True, value=0.6e8) params = fitmkin.make_params() result = fitmkin.fit(ydata, params, qarray, hkl=(0,0,2))

I obtain same error passing the "hkl" through **kwargs.

The code above used to work in v. 1.7.4. Maybe I am doing something wrong?

Thanks

dkriegner commented 8 months ago

I am afraid I screws this keyword arguments somehow up.

Can you try the following (with v. 1.7.6):

result = fitmkin.fit(ydata, params, qarray, lmfit_kws=dict(hkl=(0,0,2)), hkl=(0,0,2))

(When I test this with some dummy code I get a warning, but it seems to run: lmfit/model.py:1053: UserWarning: The keyword argument hkl does not match any arguments of the model function. It will be ignored.)

of course this is a real issue and giving these arguments twice can not stay for the future. I need to investigate a proper fix. but if you can tell me that this dirty hack works for you it would help me.

rugfri commented 8 months ago

Thanks,

yes, it works (with UserWarning). FYI, the UserWarning was present also in version 1.7.4.

dkriegner commented 8 months ago

thanks for the feedback. I will look into it.

As a quick fix you could add the following patch.

Add lmfit_kws.update(kwargs) before the super().fit-call

         # perform fitting
+        lmfit_kws.update(kwargs)  # add kwargs for function call
         res = super().fit(data[mask], params, x=x[mask], weights=mweights,
                           fit_kws=fit_kws, iter_cb=cb_func, **lmfit_kws)

issue175_patch.txt

I am not sure this will be the permanent solution because there are still differences to the behavior of lmfit which I would like to sort out. but with this change your original code should work again (with the warning).