57 lines
1.2 KiB
Python
57 lines
1.2 KiB
Python
import numpy as np
|
|
from astropy.modeling import models, fitting
|
|
|
|
from .utils import get_hist
|
|
|
|
|
|
def fit_line(model, x, y):
|
|
"""
|
|
Line fitting kx+b
|
|
|
|
Parameters
|
|
----------
|
|
model : astropy.modeling.Model
|
|
fit model
|
|
x : array
|
|
x data, can have two columns representing two independent variables
|
|
y : array
|
|
y data
|
|
|
|
Returns
|
|
-------
|
|
fitted_model : astropy fitting model
|
|
fitting gaussian model
|
|
"""
|
|
fitter = fitting.LevMarLSQFitter()
|
|
if np.ndim(x) == 1:
|
|
fitted_model = fitter(model, x, y)
|
|
else:
|
|
fitted_model = fitter(model, x[:, 0], x[:, 1], y)
|
|
|
|
return fitted_model
|
|
|
|
|
|
def fit_hist_gaussian(x, delta=1):
|
|
"""
|
|
Gaussian fitting is performed on the histogram
|
|
|
|
Parameters
|
|
----------
|
|
x : array
|
|
data point
|
|
delta : int, optional
|
|
Minimum bin width. The bin width is an integer multiple of delta.
|
|
|
|
Returns
|
|
-------
|
|
fitted_model : astropy fitting model
|
|
fitting gaussian model
|
|
"""
|
|
fitter = fitting.LMLSQFitter()
|
|
|
|
count, center = get_hist(x, delta=delta)
|
|
model = models.Gaussian1D(amplitude=count.max(), mean=x.mean(), stddev=x.std())
|
|
fitted_model = fitter(model, center, count)
|
|
|
|
return fitted_model
|