def test_deprecated_hat_kernel(): # 'MexicanHat' was deprecated as a name for the models which are now # 'RickerWavelet'. This test ensures that the models are correctly # deprecated. with pytest.warns(AstropyDeprecationWarning): models.MexicanHat1D() with pytest.warns(AstropyDeprecationWarning): models.MexicanHat2D()
def fit_mex_hat_1d(data): """Fit a 1D Mexican Hat function to the data.""" ldata = len(data) x = np.arange(ldata) fixed_pars = {"x_0": True} # Fit model to data fit = fitting.LevMarLSQFitter() # Mexican Hat 1D model = models.MexicanHat1D(amplitude=np.max(data), x_0=ldata/2, sigma=2., fixed=fixed_pars) with warnings.catch_warnings(): # Ignore model linearity warning from the fitter warnings.simplefilter('ignore') results = fit(model, x, data) # previous yield amp, ycenter, xcenter, sigma, offset return results
# the astropy.modeling.Model registry. For now, we directly # store hard-coded instances. import astropy.modeling.models as models from astropy.modeling import Parameter, Fittable1DModel from astropy.modeling.polynomial import PolynomialModel registry = { 'Gaussian1D': models.Gaussian1D(1.0, 1.0, 1.0), 'GaussianAbsorption1D': models.GaussianAbsorption1D(1.0, 1.0, 1.0), 'Lorentz1D': models.Lorentz1D(1.0, 1.0, 1.0), 'MexicanHat1D': models.MexicanHat1D(1.0, 1.0, 1.0), 'Trapezoid1D': models.Trapezoid1D(1.0, 1.0, 1.0, 1.0), 'Moffat1D': models.Moffat1D(1.0, 1.0, 1.0, 1.0), 'ExponentialCutoffPowerLaw1D': models.ExponentialCutoffPowerLaw1D(1.0, 1.0, 1.0, 1.0), 'BrokenPowerLaw1D': models.BrokenPowerLaw1D(1.0, 1.0, 1.0, 1.0), 'LogParabola1D': models.LogParabola1D(1.0, 1.0, 1.0, 1.0), 'PowerLaw1D': models.PowerLaw1D(1.0, 1.0, 1.0), 'Linear1D': models.Linear1D(1.0, 0.0), 'Const1D':
def test_single_peak_estimate(): """ Single Peak fit. """ # Create the spectrum x_single, y_single = single_peak() s_single = Spectrum1D(flux=y_single*u.Jy, spectral_axis=x_single*u.um) # # Estimate parameter Gaussian1D # g_init = estimate_line_parameters(s_single, models.Gaussian1D()) assert np.isclose(g_init.amplitude.value, 3.354169257846847) assert np.isclose(g_init.mean.value, 6.218588636687762) assert np.isclose(g_init.stddev.value, 1.6339001193853715) assert g_init.amplitude.unit == u.Jy assert g_init.mean.unit == u.um assert g_init.stddev.unit == u.um # # Estimate parameter Lorentz1D # g_init = estimate_line_parameters(s_single, models.Lorentz1D()) assert np.isclose(g_init.amplitude.value, 3.354169257846847) assert np.isclose(g_init.x_0.value, 6.218588636687762) assert np.isclose(g_init.fwhm.value, 1.6339001193853715) assert g_init.amplitude.unit == u.Jy assert g_init.x_0.unit == u.um assert g_init.fwhm.unit == u.um # # Estimate parameter Voigt1D # g_init = estimate_line_parameters(s_single, models.Voigt1D()) assert np.isclose(g_init.amplitude_L.value, 3.354169257846847) assert np.isclose(g_init.x_0.value, 6.218588636687762) assert np.isclose(g_init.fwhm_L.value, 1.1553418541989058) assert np.isclose(g_init.fwhm_G.value, 1.1553418541989058) assert g_init.amplitude_L.unit == u.Jy assert g_init.x_0.unit == u.um assert g_init.fwhm_L.unit == u.um assert g_init.fwhm_G.unit == u.um # # Estimate parameter MexicanHat1D # mh = models.MexicanHat1D() estimators = { 'amplitude': lambda s: max(s.flux), 'x_0': lambda s: centroid(s, region=None), 'stddev': lambda s: fwhm(s) } mh._constraints['parameter_estimator'] = estimators g_init = estimate_line_parameters(s_single, mh) assert np.isclose(g_init.amplitude.value, 3.354169257846847) assert np.isclose(g_init.x_0.value, 6.218588636687762) assert np.isclose(g_init.stddev.value, 1.6339001193853715) assert g_init.amplitude.unit == u.Jy assert g_init.x_0.unit == u.um assert g_init.stddev.unit == u.um
def __init__(self, width, **kwargs): amplitude = 1.0 / (np.sqrt(2 * np.pi) * width**3) self._model = models.MexicanHat1D(amplitude, 0, width) self._default_size = _round_up_to_odd_integer(8 * width) super().__init__(**kwargs) self._truncation = np.abs(self._array.sum() / self._array.size)
def fit_mex_hat_1d(data, sigma_factor=0, center_at=None, weighted=False): """Fit a 1D Mexican Hat function to the data. Parameters ---------- data: 2D float array should be a 2d array, the initial center is used to estimate the fit center sigma_factor: float (optional) If sigma_factor > 0 then clipping will be performed on the data during the model fit center_at: float or None None by default, set to value to use as center weighted: bool if weighted is True, then weight the values by basic uncertainty hueristic Returns ------- The the fit model for mexican hat 1D function """ ldata = len(data) if center_at: x0 = 0 else: x0 = int(ldata / 2.) # assumes negligable background if weighted: z = np.nan_to_num(1. / np.sqrt(data)) # use as weight x = np.arange(ldata) fixed_pars = {"x_0": True} # Initialize the fitter fitter = fitting.LevMarLSQFitter() if sigma_factor > 0: fit = fitting.FittingWithOutlierRemoval(fitter, sigma_clip, niter=3, sigma=sigma_factor) else: fit = fitter # Mexican Hat 1D + constant model = (models.MexicanHat1D( amplitude=np.max(data), x_0=x0, sigma=2., fixed=fixed_pars) + models.Polynomial1D(c0=data.min(), degree=0)) with warnings.catch_warnings(): # Ignore model linearity warning from the fitter warnings.simplefilter('ignore') if weighted: results = fit(model, x, data, weights=z) else: results = fit(model, x, data) # previous yield amp, ycenter, xcenter, sigma, offset if sigma_factor > 0: return results[1] else: return results