amplitude = Parameter('amplitude', amplitude, min=0) phase = Parameter('phase', phase) period = Parameter('period', period, min=0, max=2 * np.pi) return cls(amplitude, phase, period) def fit_fun(self, x, *args, **kwargs): # In the real case we would gust call the evaluation fn without reference to the BaseObj return self.amplitude.raw_value * np.sin( (x + self.phase.raw_value) / self.period.raw_value) b = Wavey.from_params() bb = Wavey.from_params(1.1, 0.1, 1.9 * np.pi) f = Fitter() f.initialize(b, b.fit_fun) nx = 1E3 x_min = 0 x_max = 100 x = np.linspace(x_min, x_max, num=int(nx)) y1 = bb.fit_fun(x) + .75 * (0.5 - np.random.random(size=x.shape)) x2 = x + 20 y2 = bb.fit_fun(x2) + .75 * (0.5 - np.random.random(size=x2.shape)) d.easyCore.add_coordinate('x1', x) d.easyCore.add_variable('y1', ['x1'], y1, auto_sigma=True) d.easyCore.add_coordinate('x2', x2) d.easyCore.add_variable('y2', ['x2'], y2, auto_sigma=True)
from easyCore.Fitting.Fitting import Fitter import matplotlib.pyplot as plt d = xr.Dataset() b = BaseObj('line', m=Parameter('m', 1), c=Parameter('c', 1)) def fit_fun(x, *args, **kwargs): # In the real case we would gust call the evaluation fn without reference to the BaseObj return b.c.raw_value + b.m.raw_value * x f = Fitter() f.initialize(b, fit_fun) nx = 1E3 x_min = 0 x_max = 100 x = np.linspace(x_min, x_max, num=int(nx)) y1 = 2 * x - 1 + 5 * (np.random.random(size=x.shape) - 0.5) x2 = x + 20 y2 = 2 * x2 - 1 + 5 * (np.random.random(size=x2.shape) - 0.5) d.easyCore.add_coordinate('x1', x) d.easyCore.add_variable('y1', ['x1'], y1, auto_sigma=True) d.easyCore.add_coordinate('x2', x2) d.easyCore.add_variable('y2', ['x2'], y2, auto_sigma=True)
_defaults = [Parameter('m', 1), Parameter('c', 0)] def __init__(self): super().__init__(self.__class__.__name__, *self._defaults) @property def gradient(self): return self.m.raw_value @property def intercept(self): return self.c.raw_value def fit_func(self, x: np.ndarray) -> np.ndarray: return self.gradient * x + self.intercept def __repr__(self): return f'Line: m={self.m}, c={self.c}' l = Line() f = Fitter() f.initialize(l, l.fit_func) x = np.array([1, 2, 3]) y = np.array([2, 4, 6]) - 1 f_res = f.fit(x, y) print(f_res.fit_report()) print(l)