Last active
September 5, 2018 18:56
-
-
Save nden/5466946 to your computer and use it in GitHub Desktop.
Example of creating a new fitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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