2022-07-22 14:47:31 +08:00
|
|
|
import numpy as np
|
2022-07-19 17:17:45 +08:00
|
|
|
from astropy.modeling import models, fitting
|
|
|
|
|
|
|
|
from .utils import get_hist
|
|
|
|
|
|
|
|
|
2022-07-22 14:47:31 +08:00
|
|
|
def fit_line(model, x, y):
|
2022-07-19 17:17:45 +08:00
|
|
|
"""
|
|
|
|
Line fitting kx+b
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-07-22 14:47:31 +08:00
|
|
|
model : astropy.modeling.Model
|
|
|
|
fit model
|
|
|
|
x : array
|
|
|
|
x data, can have two columns representing two independent variables
|
|
|
|
y : array
|
|
|
|
y data
|
2022-07-19 17:17:45 +08:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
fitted_model : astropy fitting model
|
|
|
|
fitting gaussian model
|
|
|
|
"""
|
|
|
|
fitter = fitting.LevMarLSQFitter()
|
2022-07-22 14:47:31 +08:00
|
|
|
if np.ndim(x) == 1:
|
|
|
|
fitted_model = fitter(model, x, y)
|
|
|
|
else:
|
|
|
|
fitted_model = fitter(model, x[:, 0], x[:, 1], y)
|
2022-07-19 17:17:45 +08:00
|
|
|
|
|
|
|
return fitted_model
|
|
|
|
|
|
|
|
|
2022-09-06 13:45:45 +08:00
|
|
|
def fit_hist_gaussian(x, step=1):
|
2022-07-19 17:17:45 +08:00
|
|
|
"""
|
|
|
|
Gaussian fitting is performed on the histogram
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
x : array
|
|
|
|
data point
|
2022-09-06 13:45:45 +08:00
|
|
|
step : int, optional
|
|
|
|
Minimum bin width. The bin width is an integer multiple of step.
|
2022-07-19 17:17:45 +08:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
fitted_model : astropy fitting model
|
|
|
|
fitting gaussian model
|
|
|
|
"""
|
|
|
|
fitter = fitting.LMLSQFitter()
|
|
|
|
|
2022-09-06 13:45:45 +08:00
|
|
|
count, center = get_hist(x, step=step)
|
2022-07-19 17:17:45 +08:00
|
|
|
model = models.Gaussian1D(amplitude=count.max(), mean=x.mean(), stddev=x.std())
|
|
|
|
fitted_model = fitter(model, center, count)
|
|
|
|
|
|
|
|
return fitted_model
|