Skip to content

Instantly share code, notes, and snippets.

@nden
Last active September 5, 2018 18:56
Show Gist options
  • Save nden/5466946 to your computer and use it in GitHub Desktop.
Save nden/5466946 to your computer and use it in GitHub Desktop.
Example of creating a new fitter
from astropy.models.fitting import Fitter
import numpy as np
from scipy import optimize
class SLSQPFitter(Fitter):
def __init__(self):
super().__init__(optimizer=SLSQP, statistic=leastsquare)
def errorfunc(self, fps, *args):
model = args[0]
meas = args[-1]
model.parameters = fps
res = model(*args[1:-1]) - meas
return np.sum(res**2)
def __call__(self, model, x, y , maxiter=100, epsilon=10**(-12)):
model_copy = model.copy()
b = [model.bounds[key] for key in model.param_names]
fitpars = optimize.fmin_slsqp(self.errorfunc, x0=model_copy.parameters[:],
args=(model_copy, x,y), bounds=b)
model_copy.parameters = fitpars
return model_copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment