Exemplo n.º 1
0
def create_poly(coeff):
    """Create a polynomial model from coefficients.

    Parameters
    ----------
    coeff: list of float
        The coefficients of the polynomial, constant term first, highest
        order term last.

    Returns
    -------
    astropy.modeling.polynomial.Polynomial1D object, or None if `coeff`
        is empty.
    """

    n = len(coeff)
    if n < 1:
        return None

    coeff_dict = {}
    for i in range(n):
        key = "c{}".format(i)
        coeff_dict[key] = coeff[i]

    return polynomial.Polynomial1D(degree=n - 1, **coeff_dict)
Exemplo n.º 2
0
def test_g_average():
    """Assign an array of values to tab_wl; these are strictly increasing,
    but they're not uniformly spaced.
    The abscissas and weights in combine_fast_slow are for three-point
    Gaussian integration, which will (in principle) integrate a fifth-order
    polynomial exactly.  So for a test, we use a set of six polynomial
    coefficients to create the tab_flat array by evaluating the polynomial
    with those coefficients over the tab_wl array.
    """

    # Generate an array (tab_wl) of wavelengths, not uniformly spaced.
    wl_coeff = {'c0': 5., 'c1': 1., 'c2': -0.05}
    wl_poly = polynomial.Polynomial1D(degree=2, **wl_coeff)
    tab_index = np.arange(6000, dtype=np.float64) / 1000.
    tab_wl = wl_poly(tab_index)
    del tab_index

    coeff = {
        'c0': -41.9,
        'c1': 30.7,
        'c2': -8.3,
        'c3': 1.15,
        'c4': -7.8e-2,
        'c5': 2.0e-3
    }
    poly = polynomial.Polynomial1D(degree=5, **coeff)
    tab_flat = poly(tab_wl)

    wl0 = 7.37
    dwl0 = 2.99
    lower = wl0 - dwl0 / 2.
    upper = wl0 + dwl0 / 2.
    # Compute the integral of the polynomial over wavelength.
    # This function returns a tuple, the integral and an error estimate.
    integral = quad(poly, lower, upper)[0]
    # We want the average over the interval, not the integral itself.
    correct_value = integral / (upper - lower)

    # These three lines were copied from combine_fast_slow in flat_field.py
    d = math.sqrt(0.6) / 2.
    dx = np.array([-d, 0., d])
    wgt = np.array([5., 8., 5.]) / 18.

    value = g_average(wl0, dwl0, tab_wl, tab_flat, dx, wgt)
    assert math.isclose(value, correct_value, rel_tol=1.e-8, abs_tol=1.e-8)
def fit_gaussians_to_spectrum(obs_flx, obs_wvl, idx_peaks):
    print '  Fitting multiple ({:.0f}) gaussinas to the extracted arc spectrum'.format(len(idx_peaks))
    #
    median_val = np.median(obs_flx)
    mean_wvl = 0  # np.mean(obs_wvl)
    fit_model = models.Const1D(amplitude=np.median(obs_flx)) + polynomial.Polynomial1D(4)
    for i_p, idx_p in enumerate(idx_peaks):
        peak_val = obs_flx[idx_p] - median_val
        # dela boljs brez nastavljenih boundov
        fit_model += models.Gaussian1D(amplitude=peak_val, mean=obs_wvl[idx_p]-mean_wvl, stddev=0.1)#,
                                       # bounds={'mean': (obs_wvl[idx_p]-0.5, obs_wvl[idx_p]+0.5),
                                       #         'amplitude': (peak_val*0.8, peak_val*1.2)})
    fit_t = fitting.LevMarLSQFitter()
    fitted_model = fit_t(fit_model, obs_wvl-mean_wvl, obs_flx)
    return fitted_model
Exemplo n.º 4
0
def poly_fit(xp, yp, grado_pol):

    t_init = polynomial.Polynomial1D(degree=int(grado_pol))
    fit_t = fitting.LevMarLSQFitter()
    t = fit_t(t_init, xp, yp)
    return t
Exemplo n.º 5
0
#Calculando o potencial pra uma galáxia gaussiana fake
p = Potential()

coords = np.random.normal(0, 3, 1000)
mass = np.random.choice([1., 2.], 1000)

p.coords = coords
p.mass = mass
c, pot, n = p.evaluate_potential(n_bins=10)

#Ajustando uma gaussiana nessa galáxia com o astropy
g_init = models.Gaussian1D(amplitude=-1., mean=0, stddev=1.)
fit_g = fitting.LevMarLSQFitter()
g = fit_g(g_init, c, pot)

#Ajustando um polinômio
p_init = polynomial.Polynomial1D(degree=6)
p = fit_g(p_init, c, pot)

#Minimizando
m = minimize(g, 0)

#plotando
plt.figure(figsize=(8, 5))
plt.plot(c, pot, 'ko')
plt.plot(c, g(c), label='best gaussian fit')
plt.plot(c, p(c), label='best sixth degree polynomial fit')
plt.plot(m.x, m.fun, 'X', label='minimum')
plt.legend()
plt.show()