Exemple #1
0
def test_UnresolvedExtendedXYLike_dataframe():

    yerr = np.array(gauss_sigma)
    y = np.array(gauss_signal)

    # chi2 version

    xy = UnresolvedExtendedXYLike("test", x, y, yerr)

    df = xy.to_dataframe()

    # read back in dataframe

    new_xy = UnresolvedExtendedXYLike.from_dataframe('df', df)

    assert not xy.is_poisson

    # poisson version

    xy = UnresolvedExtendedXYLike("test", x, y, poisson_data=True)

    df = xy.to_dataframe()

    # read back in dataframe

    new_xy = UnresolvedExtendedXYLike.from_dataframe('df', df, poisson=True)

    assert xy.is_poisson
Exemple #2
0
def test_UnresolvedExtendedXYLike_chi2():

    # Get fake data with Gaussian noise

    yerr = np.array(gauss_sigma)
    y = np.array(gauss_signal)

    # Fit

    xy = UnresolvedExtendedXYLike("test", x, y, yerr)

    fitfun = Line() + Gaussian()
    fitfun.F_2 = 60.0
    fitfun.mu_2 = 4.5

    res = xy.fit(fitfun)

    # Verify that the fit converged where it should have
    assert np.allclose(
        res[0]['value'].values,
        [0.82896119, 40.20269202, 62.80359114, 5.04080011, 0.27286713],
        rtol=0.05)

    # test not setting yerr

    xy = UnresolvedExtendedXYLike("test", x, y)

    assert np.all(xy.yerr == np.ones_like(y))

    fitfun = Line() + Gaussian()
    fitfun.F_2 = 60.0
    fitfun.mu_2 = 4.5

    res = xy.fit(fitfun)
Exemple #3
0
def test_UnresolvedExtendedXYLike_poisson():

    # Now Poisson case
    y = np.array(poiss_sig)

    xy = UnresolvedExtendedXYLike("test", x, y, poisson_data=True)

    fitfun = Line() + Gaussian()

    fitfun.F_2 = 60.0
    fitfun.F_2.bounds = (0, 200.0)
    fitfun.mu_2 = 5.0
    fitfun.a_1.bounds = (0.1, 5.0)
    fitfun.b_1.bounds = (0.1, 100.0)

    res = xy.fit(fitfun)

    # Verify that the fit converged where it should have

    #print res[0]['value']
    assert np.allclose(res[0]['value'],
                       [0.783748, 40.344599, 71.560055, 4.989727, 0.330570],
                       rtol=0.05)
Exemple #4
0
def test_UnresolvedExtendedXYLike_txt():

    yerr = np.array(gauss_sigma)
    y = np.array(gauss_signal)

    # chi2 version

    xy = UnresolvedExtendedXYLike("test", x, y, yerr)

    fname = 'test_txt.txt'

    xy.to_txt(fname)

    # read back in txt file

    new_xy = UnresolvedExtendedXYLike.from_text_file('txt', fname)

    assert not xy.is_poisson

    # poisson version

    xy = UnresolvedExtendedXYLike("test", x, y, poisson_data=True)

    fname = 'test_txt_poisson.txt'

    xy.to_txt(fname)

    # read back in txt file

    new_xy = UnresolvedExtendedXYLike.from_text_file('txt', fname)

    assert new_xy.is_poisson

    # Remove files
    os.remove('test_txt.txt')
    os.remove('test_txt_poisson.txt')
Exemple #5
0
def test_UnresolvedExtendedxy_plot():
    # Get fake data with Gaussian noise

    yerr = np.array(gauss_sigma)
    y = np.array(gauss_signal)

    # Fit

    xy = UnresolvedExtendedXYLike("test", x, y, yerr)

    xy.plot()

    fitfun = Line() + Gaussian()
    fitfun.F_2 = 60.0
    fitfun.mu_2 = 4.5

    res = xy.fit(fitfun)

    xy.plot()
Exemple #6
0
def test_UnresolvedExtendedXYLike_assign_to_source():

    # Get fake data with Gaussian noise

    yerr = np.array(gauss_sigma)
    y = np.array(gauss_signal)

    # Fit

    xy = UnresolvedExtendedXYLike("test", x, y, yerr)

    xy.assign_to_source("exs1")

    fitfun = Line() + Gaussian()
    fitfun.F_2 = 60.0
    fitfun.mu_2 = 4.5

    fitfun2 = Line() + Gaussian()
    fitfun2.F_2 = 60.0
    fitfun2.mu_2 = 4.5

    shape = Gaussian_on_sphere()
    exs1 = ExtendedSource("exs1", spatial_shape=shape, spectral_shape=fitfun)
    pts2 = PointSource("pts2", ra=2.5, dec=3.2, spectral_shape=fitfun2)

    for parameter in list(fitfun2.parameters.values()):
        parameter.fix = True

    for parameter in list(shape.parameters.values()):
        parameter.fix = True

    model = Model(exs1, pts2)
    data = DataList(xy)

    jl = JointLikelihood(model, data)

    _ = jl.fit()

    predicted_parameters = np.array(
        [0.82896119, 40.20269202, 62.80359114, 5.04080011, 0.27286713])

    assert np.allclose([
        fitfun.a_1.value, fitfun.b_1.value, fitfun.F_2.value,
        fitfun.mu_2.value, fitfun.sigma_2.value
    ],
                       predicted_parameters,
                       rtol=0.05)

    # Test that the likelihood does not change by changing the parameters of the other source
    log_like_before = jl.minus_log_like_profile(*predicted_parameters)

    fitfun2.F_2 = 120.0

    log_like_after = jl.minus_log_like_profile(*predicted_parameters)

    assert log_like_before == log_like_after

    # Now test that if we do not assign a source, then the log likelihood value will change
    xy.assign_to_source(None)

    # Test that the likelihood this time changes by changing the parameters of the other source
    log_like_before = jl.minus_log_like_profile(*predicted_parameters)

    fitfun2.F_2 = 60.0

    log_like_after = jl.minus_log_like_profile(*predicted_parameters)

    assert log_like_before != log_like_after