Exemplo n.º 1
0
    Test that certain operators do not work with models whose inputs/outputs do
    not match up correctly.
    """

    with pytest.raises(ModelDefinitionError):
        Rotation2D(90) | Gaussian1D(1, 0, 0.1)

    with pytest.raises(ModelDefinitionError):
        Rotation2D(90) + Gaussian1D(1, 0, 0.1)


@pytest.mark.parametrize('poly', [
    Chebyshev2D(1, 2),
    Polynomial2D(2),
    Legendre2D(1, 2),
    Chebyshev1D(5),
    Legendre1D(5),
    Polynomial1D(5)
])
def test_compound_with_polynomials(poly):
    """
    Tests that polynomials are scaled when used in compound models.
    Issue #3699
    """
    poly.parameters = [1, 2, 3, 4, 1, 2]
    shift = Shift(3)
    model = poly | shift
    x, y = np.mgrid[:20, :37]
    result_compound = model(x, y)
    result = shift(poly(x, y))
    assert_allclose(result, result_compound)
    val2 = g(x + 2, y + 2, with_bounding_box=True)
    assert np.isnan(val2).sum() == 100
    # val3 = g(.1, .1, with_bounding_box=True)


@pytest.mark.skipif("not HAS_SCIPY")
def test_bounding_box_with_units():
    points = np.arange(5) * u.pix
    lt = np.arange(5) * u.AA
    t = Tabular1D(points, lt)

    assert t(1 * u.pix, with_bounding_box=True) == 1. * u.AA


@pytest.mark.parametrize(
    'poly', [Chebyshev1D(5), Legendre1D(5),
             Polynomial1D(5)])
def test_compound_with_polynomials_1d(poly):
    """
    Tests that polynomials are offset when used in compound models.
    Issue #3699
    """
    poly.parameters = [1, 2, 3, 4, 1, 2]
    shift = Shift(3)
    model = poly | shift
    x = np.linspace(-5, 5, 10)
    result_compound = model(x)
    result = shift(poly(x))
    assert_allclose(result, result_compound)
    assert model.param_names == ('c0_0', 'c1_0', 'c2_0', 'c3_0', 'c4_0',
                                 'c5_0', 'offset_1')
Exemplo n.º 3
0
    assert_allclose(val[mask], compare[mask])
    val2 = g(x+2, y+2, with_bounding_box=True)
    assert np.isnan(val2).sum() == 100
    # val3 = g(.1, .1, with_bounding_box=True)


@pytest.mark.skipif("not HAS_SCIPY")
def test_bounding_box_with_units():
    points = np.arange(5) * u.pix
    lt = np.arange(5) * u.AA
    t = Tabular1D(points, lt)

    assert t(1 * u.pix, with_bounding_box=True) == 1. * u.AA


@pytest.mark.parametrize('poly', [Chebyshev1D(5), Legendre1D(5), Polynomial1D(5)])
def test_compound_with_polynomials_1d(poly):
    """
    Tests that polynomials are offset when used in compound models.
    Issue #3699
    """
    poly.parameters = [1, 2, 3, 4, 1, 2]
    shift = Shift(3)
    model = poly | shift
    x = np.linspace(-5, 5, 10)
    result_compound = model(x)
    result = shift(poly(x))
    assert_allclose(result, result_compound)
    assert model.param_names == ('c0_0', 'c1_0', 'c2_0', 'c3_0', 'c4_0', 'c5_0', 'offset_1')

Exemplo n.º 4
0
tied_RV = {'x_0': tie_RV }


#need good guess

#gg_init =( Voigt1D(x_0=lineB[0], amplitude_L=lineB[1], fwhm_L=lineB[2], fwhm_G=lineB[2] ) + 
#           Voigt1D(x_0=0, amplitude_L=lineC[1], fwhm_L=lineC[2], fwhm_G=lineC[2], tied=tied_RV) + 
#           Chebyshev1D(3, c0=C0, c1=C1, c2=C2, c3=C3, fixed={'c0': True, 'c1': True, 'c2': True, 'c3': True})   )




#  3 LINES MODEL CAT
gg_init =( Voigt1D(x_0=lineB[0], amplitude_L=lineB[1], fwhm_L=lineB[2], fwhm_G=lineB[2] ,bounds={"amplitude_L": (-0.1, 50)} ) + 
           Voigt1D(x_0=0, amplitude_L=lineC[1], fwhm_L=lineC[2], fwhm_G=lineC[2], tied=tied_RV, bounds={"amplitude_L": (-0.1, 50)}) + 
           Chebyshev1D(3, c0=C0, c1=C1, c2=C2, c3=C3, fixed={'c0': True, 'c1': True, 'c2': True, 'c3': True}) +
           Voigt1D(x_0=lineA[0], amplitude_L=lineA[1], fwhm_L=lineA[2], fwhm_G=lineA[2], tied=tied_RVa, bounds={"amplitude_L": (-0.1, 50)} )   
           )



#Levenberg-Marquardt algorithm
#SLSQPLSQFitter() does not work for strong lines  sequential quadratic programming
#fitter = fitting.SLSQPLSQFitter() 
fitter = fitting.LevMarLSQFitter()
gg_fit = fitter(gg_init, x ,y)


#gg_fit.mean.tied = tie_center

print(gg_fit)
Exemplo n.º 5
0
    # trivial scale factor)
    model2 = shift | model1

    assert_allclose(model2.inverse(1), (poly | shift.inverse)(1))

    # Make sure an inverse is not allowed if the models were combined with the
    # wrong operator, or if one of the models doesn't have an inverse defined
    with pytest.raises(NotImplementedError):
        (shift + model1).inverse

    with pytest.raises(NotImplementedError):
        (model1 & poly).inverse


@pytest.mark.parametrize('poly', [Chebyshev2D(1, 2), Polynomial2D(2), Legendre2D(1, 2),
                                  Chebyshev1D(5), Legendre1D(5), Polynomial1D(5)])
def test_compound_with_polynomials(poly):
    """
    Tests that polynomials are scaled when used in compound models.
    Issue #3699
    """
    poly.parameters = [1, 2, 3, 4, 1, 2]
    shift = Shift(3)
    model = poly | shift
    x, y = np.mgrid[:20, :37]
    result_compound = model(x, y)
    result = shift(poly(x, y))
    assert_allclose(result, result_compound)


# has to be defined at module level since pickling doesn't work right (in
Exemplo n.º 6
0
mask_CATb = (x > regions_ex[2]) & (x < regions_ex[3])
mask_CATc = (x > regions_ex[4]) & (x < regions_ex[5])

mask_SNR = [any(tup) for tup in zip(mask_CATa,mask_CATb, mask_CATc)]
Imask_SNR = np.invert(mask_SNR)


#correct offset excluded regions
#lineBcentre=8546.0 * u.AA
try:
    Corr_rv= rv(LINES[list(LINES.keys())[1]],float(lineBcentre/(1. * u.AA)))
except:
    Corr_rv= rv(LINES[list(LINES.keys())[1]],float(lineBcentre))


Che_model=fit_generic_continuum(spec, model=Chebyshev1D(2), exclude_regions=[SpectralRegion(corr_mask(regions_ex[0],Corr_rv)*u.AA, corr_mask(regions_ex[1],Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(regions_ex[2],Corr_rv)*u.AA, corr_mask(regions_ex[3],Corr_rv)*u.AA), 
                                                          SpectralRegion(corr_mask(regions_ex[4],Corr_rv)*u.AA, corr_mask(regions_ex[5],Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8409,Corr_rv)*u.AA, corr_mask(8415,Corr_rv)*u.AA), 
                                                          SpectralRegion(corr_mask(8415,Corr_rv)*u.AA, corr_mask(8422,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8422,Corr_rv)*u.AA, corr_mask(8428,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8431,Corr_rv)*u.AA, corr_mask(8442,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8465,Corr_rv)*u.AA, corr_mask(8471,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8579,Corr_rv)*u.AA, corr_mask(8585,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8595,Corr_rv)*u.AA, corr_mask(8600,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8610,Corr_rv)*u.AA, corr_mask(8630,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8329,Corr_rv)*u.AA, corr_mask(8354,Corr_rv)*u.AA),
                                                          SpectralRegion(corr_mask(8252,Corr_rv)*u.AA, corr_mask(8256,Corr_rv)*u.AA),
                                                          ])