OxIonics / ionics_fits

Small python fitting library with an emphasis on Atomic Molecular and Optical Physics
Apache License 2.0
0 stars 0 forks source link

Make using models standalone more ergonmic #128

Closed hartytp closed 7 months ago

hartytp commented 7 months ago

Currently it's not particularly ergonomic to use a model "standalone" (not as a fit). e.g. one has to do something like

import numpy as np
import ionics_fits as fits

x = np.linspace(0, 2*np.pi)
sinusoid = fits.models.Sinusoid()
y = sinusoid .func(x=x, params={
    "a": 1,
    "omega": 2,
    "phi": 0,
    "y0": 0,
    "x0": 0,
    "tau": np.inf,
})

This feels unergonomic because:

The fact that we programmatically modify the model's parameters during __init__ adds a few constraints to the problem. I'm not sure what the best option here, but maybe one is to do something like

class Model:
    def __call__(self, x, *, **kwargs):
    """ Evaluate the model.

        - keyword arguments specify values for model parameters (see model definition)
        - all model parameters which are not `fixed_to` a value by default must be specified
        - any parameters which are not specified default to their `fixed_to` values
      """
        args = {param.fixed_to for param in self.parameters.values() if param.fixed_to is not None}
        args.update(kwargs)
        return self.func(x, args)

That allows one to modify the above model to something like

import numpy as np
import ionics_fits as fits

x = np.linspace(0, 2*np.pi)
sinusoid = fits.models.Sinusoid()
y = sinusoid(x=x, a=1, omega=2, phi=0, y0=0)

Any thoughts @mbirtwell ?

cc @m-malinowski (I know you had some issues with this a while back)

mbirtwell commented 7 months ago

Yeah, that seems sensible as an approach to me.

This line:

args = {param.fixed_to for param in self.parameters.values() if param.fixed_to is not None}

Is constucting a set rather than the dictionary I think you need but that should be easy to fix.

hartytp commented 7 months ago

aah, yeah, the set syntax always gets me. Okay, I'll add that then. It seems like a simple quality of life improvement.