muon-spectroscopy-computational-project / muspinsim

Full quantum simulation of muon experiments
https://muon-spectroscopy-computational-project.github.io/muspinsim/
MIT License
1 stars 0 forks source link

Improve fitting of experimental data #77

Closed joelvdavies closed 11 months ago

joelvdavies commented 1 year ago

We want to allow scaling and offsetting of the generated data to better match experiments. Ideally this would be fitted at the same time as any of the other fitting parameters so I think right now the most obvious solution is to add parameters to the input file that allow arbitrary scales/offsets that can be given fitting parameters. Longer term we may also want to allow more complex fits such as a slow moving Gaussian for background effects but I don't see a very obvious solution to do this without further expansion of the range of functions we can take in the input file. Alternatively we could see if its possible to do something like

fitting_function
    m * y_value + c

and try to parse this function in a way we can pass the output of muspinsim as the y_value just before passing it to the minimize function. This would be much more general, but perhaps too complicated.

joelvdavies commented 1 year ago

In the end we went with the full method. Allowing any fitting parameters e.g.

fitting_data
    load("experiment.dat")
results_function
    A*y+B*exp(-(x*C)^2)+D
fitting_variables
    A
    B
    C
    D

(see #78)

However one place for improvement still remains: When we specify fitting_data we override the x_values in simconfig.py (here: https://github.com/muon-spectroscopy-computational-project/muspinsim/blob/3dc9fba6aa34853e0d49b58df7e79fa769cf78ea/muspinsim/simconfig.py#L177-L188). I have temporarily added a warning to notify the user of this, but ideally we would be able to specify the range separately with appropriate handling e.g. interpolating when the ranges overlap, erroring when they don't etc.

patrick-austin commented 1 year ago

On the topic of overwrite warning, this is logged even when no x_axis is provided in the input file, as the defaults are generated during evaluate, and the range overwritten later in MuSpinConfig (at which point the default values are indistinguishable from user defined values): https://github.com/muon-spectroscopy-computational-project/muspinsim/blob/e57dfd3420150ec749cba589e51f72f83d06e30a/muspinsim/fitting.py#L202

Ideally a user should not see this warning if they neither provided a value for x_axis nor the default time. However this is minor and may be superceded by wider reaching changes to the fitting.