Esempio n. 1
0
def test_spectrum_like_with_background_model():
    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1E-1
    sim_kT = 20.

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=5, index=-1.5, piv=100.)

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)


    background_plugin = SpectrumLike.from_background('background',spectrum_generator)


    bb = Blackbody()


    pl = Powerlaw()
    pl.piv = 100

    bkg_ps = PointSource('bkg',0,0,spectral_shape=pl)

    bkg_model = Model(bkg_ps)

    jl_bkg = JointLikelihood(bkg_model,DataList(background_plugin))

    _ = jl_bkg.fit()




    plugin_bkg_model = SpectrumLike('full',spectrum_generator.observed_spectrum,background=background_plugin)

    pts = PointSource('mysource', 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(plugin_bkg_model))

    result = jl.fit()

    K_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.K')

    kT_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.kT')

    assert np.all(np.isclose([K_variates.mean(), kT_variates.mean()], [sim_K, sim_kT], rtol=0.5))
def test_spectrumlike_fit():

    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1e-1
    sim_kT = 20.0

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.0)

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    bb = Blackbody()

    pts = PointSource("mysource", 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(spectrum_generator))

    result = jl.fit()

    K_variates = jl.results.get_variates("mysource.spectrum.main.Blackbody.K")

    kT_variates = jl.results.get_variates(
        "mysource.spectrum.main.Blackbody.kT")

    assert np.all(
        np.isclose([K_variates.average, kT_variates.average], [sim_K, sim_kT],
                   atol=1))
def test_dispersionspectrumlike_fit():

    response = OGIPResponse(
        get_path_of_data_file("datasets/ogip_powerlaw.rsp"))

    sim_K = 1e-1
    sim_kT = 20.0

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.0)

    spectrum_generator = DispersionSpectrumLike.from_function(
        "test",
        source_function=source_function,
        response=response,
        background_function=background_function,
    )

    bb = Blackbody()

    pts = PointSource("mysource", 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(spectrum_generator))

    result = jl.fit()

    K_variates = jl.results.get_variates("mysource.spectrum.main.Blackbody.K")

    kT_variates = jl.results.get_variates(
        "mysource.spectrum.main.Blackbody.kT")

    assert np.all(
        np.isclose([K_variates.average, kT_variates.average], [sim_K, sim_kT],
                   atol=1))
Esempio n. 4
0
def test_spectrum_like_with_background_model():
    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1E-1
    sim_kT = 20.

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=5, index=-1.5, piv=100.)

    spectrum_generator = SpectrumLike.from_function(
        'fake',
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge)

    background_plugin = SpectrumLike.from_background('background',
                                                     spectrum_generator)

    bb = Blackbody()

    pl = Powerlaw()
    pl.piv = 100

    bkg_ps = PointSource('bkg', 0, 0, spectral_shape=pl)

    bkg_model = Model(bkg_ps)

    jl_bkg = JointLikelihood(bkg_model, DataList(background_plugin))

    _ = jl_bkg.fit()

    plugin_bkg_model = SpectrumLike('full',
                                    spectrum_generator.observed_spectrum,
                                    background=background_plugin)

    pts = PointSource('mysource', 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(plugin_bkg_model))

    result = jl.fit()

    K_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.K')

    kT_variates = jl.results.get_variates(
        'mysource.spectrum.main.Blackbody.kT')

    assert np.all(
        np.isclose([K_variates.average, kT_variates.average], [sim_K, sim_kT],
                   rtol=0.5))
Esempio n. 5
0
def test_spectrumlike_fit():

    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1E-1
    sim_kT = 20.

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.)

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)

    bb = Blackbody()

    pts = PointSource('mysource', 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(spectrum_generator))

    result = jl.fit()

    K_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.K')

    kT_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.kT')

    assert np.all(np.isclose([K_variates.mean(), kT_variates.mean()], [sim_K, sim_kT], atol=1 ))
Esempio n. 6
0
def test_dispersionspectrumlike_fit():



    response = OGIPResponse(get_path_of_data_file('datasets/ogip_powerlaw.rsp'))

    sim_K = 1E-1
    sim_kT = 20.

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.)

    spectrum_generator = DispersionSpectrumLike.from_function('test', source_function=source_function,
                                                                    response=response,
                                                                         background_function=background_function)


    bb = Blackbody()

    pts = PointSource('mysource', 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(spectrum_generator))

    result = jl.fit()

    K_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.K')

    kT_variates = jl.results.get_variates('mysource.spectrum.main.Blackbody.kT')

    assert np.all(np.isclose([K_variates.mean(), kT_variates.mean()], [sim_K, sim_kT], atol=1))
def test_spectrum_like_with_background_model():
    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1e-1
    sim_kT = 20.0

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=5, index=-1.5, piv=100.0)

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    background_plugin = SpectrumLike.from_background("background",
                                                     spectrum_generator)

    bb = Blackbody()

    pl = Powerlaw()
    pl.piv = 100

    bkg_ps = PointSource("bkg", 0, 0, spectral_shape=pl)

    bkg_model = Model(bkg_ps)

    jl_bkg = JointLikelihood(bkg_model, DataList(background_plugin))

    _ = jl_bkg.fit()

    plugin_bkg_model = SpectrumLike("full",
                                    spectrum_generator.observed_spectrum,
                                    background=background_plugin)

    pts = PointSource("mysource", 0, 0, spectral_shape=bb)

    model = Model(pts)

    # MLE fitting

    jl = JointLikelihood(model, DataList(plugin_bkg_model))

    result = jl.fit()

    K_variates = jl.results.get_variates("mysource.spectrum.main.Blackbody.K")

    kT_variates = jl.results.get_variates(
        "mysource.spectrum.main.Blackbody.kT")

    assert np.all(
        np.isclose([K_variates.average, kT_variates.average], [sim_K, sim_kT],
                   rtol=0.5))

    ## test with ogiplike
    with within_directory(__example_dir):
        ogip = OGIPLike("test_ogip",
                        observation="test.pha{1}",
                        background=background_plugin)
def test_assigning_source_name():

    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1e-1
    sim_kT = 20.0

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.0)

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    # good name setting

    bb = Blackbody()

    pts = PointSource("good_name", 0, 0, spectral_shape=bb)

    model = Model(pts)

    # before setting model

    spectrum_generator.assign_to_source("good_name")

    jl = JointLikelihood(model, DataList(spectrum_generator))

    _ = jl.fit()

    # after setting model

    pts = PointSource("good_name", 0, 0, spectral_shape=bb)

    model = Model(pts)

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    jl = JointLikelihood(model, DataList(spectrum_generator))

    spectrum_generator.assign_to_source("good_name")

    # after with bad name

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    jl = JointLikelihood(model, DataList(spectrum_generator))

    with pytest.raises(RuntimeError):

        spectrum_generator.assign_to_source("bad_name")

        # before with bad name

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    spectrum_generator.assign_to_source("bad_name")

    with pytest.raises(RuntimeError):

        jl = JointLikelihood(model, DataList(spectrum_generator))

    # multisource model

    spectrum_generator = SpectrumLike.from_function(
        "fake",
        source_function=source_function,
        background_function=background_function,
        energy_min=low_edge,
        energy_max=high_edge,
    )

    ps1 = PointSource("ps1", 0, 0, spectral_shape=Blackbody())
    ps2 = PointSource("ps2", 0, 0, spectral_shape=Powerlaw())

    model = Model(ps1, ps2)

    model.ps2.spectrum.main.Powerlaw.K.fix = True
    model.ps2.spectrum.main.Powerlaw.index.fix = True

    spectrum_generator.assign_to_source("ps1")

    dl = DataList(spectrum_generator)

    jl = JointLikelihood(model, dl)

    _ = jl.fit()
Esempio n. 9
0
def fit_point_source(roi,
                     maptree,
                     response,
                     point_source_model,
                     bin_list,
                     confidence_intervals=False,
                     liff=False,
                     pixel_size=0.17,
                     verbose=False):

    data_radius = roi.data_radius.to("deg").value

    if not liff:

        # This is a 3ML plugin
        hawc = HAL("HAWC",
                   maptree,
                   response,
                   roi,
                   flat_sky_pixels_size=pixel_size)

        hawc.set_active_measurements(bin_list=bin_list)

    else:

        from threeML import HAWCLike

        hawc = HAWCLike("HAWC", maptree, response, fullsky=True)

        hawc.set_bin_list(bin_list)

        ra_roi, dec_roi = roi.ra_dec_center

        hawc.set_ROI(ra_roi, dec_roi, data_radius)

    if not liff:

        hawc.display()

    data = DataList(hawc)

    jl = JointLikelihood(point_source_model, data, verbose=verbose)

    point_source_model.display(complete=True)

    try:

        jl.set_minimizer("minuit")

    except:

        jl.set_minimizer("minuit")

    param_df, like_df = jl.fit()

    if confidence_intervals:

        ci = jl.get_errors()

    else:

        ci = None

    return param_df, like_df, ci, jl.results
Esempio n. 10
0
def test_assigning_source_name():

    energies = np.logspace(1, 3, 51)

    low_edge = energies[:-1]
    high_edge = energies[1:]

    sim_K = 1E-1
    sim_kT = 20.

    # get a blackbody source function
    source_function = Blackbody(K=sim_K, kT=sim_kT)

    # power law background function
    background_function = Powerlaw(K=1, index=-1.5, piv=100.)

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)

    # good name setting

    bb = Blackbody()

    pts = PointSource('good_name', 0, 0, spectral_shape=bb)

    model = Model(pts)

    # before setting model

    spectrum_generator.assign_to_source('good_name')



    jl = JointLikelihood(model, DataList(spectrum_generator))


    _ = jl.fit()


    # after setting model

    pts = PointSource('good_name', 0, 0, spectral_shape=bb)

    model = Model(pts)


    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)




    jl = JointLikelihood(model, DataList(spectrum_generator))

    spectrum_generator.assign_to_source('good_name')


    # after with bad name

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)

    jl = JointLikelihood(model, DataList(spectrum_generator))

    with pytest.raises(AssertionError):

        spectrum_generator.assign_to_source('bad_name')

        # before with bad name

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)

    spectrum_generator.assign_to_source('bad_name')

    with pytest.raises(AssertionError):

        jl = JointLikelihood(model, DataList(spectrum_generator))



    #multisource model

    spectrum_generator = SpectrumLike.from_function('fake',
                                                    source_function=source_function,
                                                    background_function=background_function,
                                                    energy_min=low_edge,
                                                    energy_max=high_edge)

    ps1 = PointSource('ps1', 0, 0, spectral_shape=Blackbody())
    ps2 = PointSource('ps2', 0, 0, spectral_shape=Powerlaw())

    model = Model(ps1, ps2)

    model.ps2.spectrum.main.Powerlaw.K.fix = True
    model.ps2.spectrum.main.Powerlaw.index.fix = True

    spectrum_generator.assign_to_source('ps1')

    dl = DataList(spectrum_generator)

    jl = JointLikelihood(model, dl)

    _ = jl.fit()
Esempio n. 11
0
def test_model_residual_maps(geminga_maptree, geminga_response, geminga_roi):

    #data_radius = 5.0
    #model_radius = 7.0
    output = dirname(geminga_maptree)

    ra_src, dec_src = 101.7, 16.0
    maptree, response, roi = geminga_maptree, geminga_response, geminga_roi

    hawc = HAL("HAWC", maptree, response, roi)

    # Use from bin 1 to bin 9
    hawc.set_active_measurements(1, 9)

    # Display information about the data loaded and the ROI
    hawc.display()
    '''
    Define model: Two sources, 1 point, 1 extended

    Same declination, but offset in RA

    Different spectral index, but both power laws

    '''
    pt_shift = 3.0
    ext_shift = 2.0

    # First source
    spectrum1 = Powerlaw()
    source1 = PointSource("point",
                          ra=ra_src + pt_shift,
                          dec=dec_src,
                          spectral_shape=spectrum1)

    spectrum1.K = 1e-12 / (u.TeV * u.cm**2 * u.s)
    spectrum1.piv = 1 * u.TeV
    spectrum1.index = -2.3

    spectrum1.piv.fix = True
    spectrum1.K.fix = True
    spectrum1.index.fix = True

    # Second source
    shape = Gaussian_on_sphere(lon0=ra_src - ext_shift,
                               lat0=dec_src,
                               sigma=0.3)
    spectrum2 = Powerlaw()
    source2 = ExtendedSource("extended",
                             spatial_shape=shape,
                             spectral_shape=spectrum2)

    spectrum2.K = 1e-12 / (u.TeV * u.cm**2 * u.s)
    spectrum2.piv = 1 * u.TeV
    spectrum2.index = -2.0

    shape.lon0.fix = True
    shape.lat0.fix = True
    shape.sigma.fix = True

    spectrum2.piv.fix = True
    spectrum2.K.fix = True
    spectrum2.index.fix = True

    # Define model with both sources
    model = Model(source1, source2)

    # Define the data we are using
    data = DataList(hawc)

    # Define the JointLikelihood object (glue the data to the model)
    jl = JointLikelihood(model, data, verbose=False)

    # This has the effect of loading the model cache
    fig = hawc.display_spectrum()

    # the test file names
    model_file_name = "{0}/test_model.hdf5".format(output)
    residual_file_name = "{0}/test_residual.hdf5".format(output)

    # Write the map trees for testing
    model_map_tree = hawc.write_model_map(model_file_name,
                                          poisson_fluctuate=True,
                                          test_return_map=True)
    residual_map_tree = hawc.write_residual_map(residual_file_name,
                                                test_return_map=True)

    # Read the maps back in
    hawc_model = map_tree_factory(model_file_name, roi)
    hawc_residual = map_tree_factory(residual_file_name, roi)

    check_map_trees(hawc_model, model_map_tree)
    check_map_trees(hawc_residual, residual_map_tree)