示例#1
0
def test_one_free_parameter_input_output():

    fluxUnit = 1. / (u.TeV * u.cm**2 * u.s)

    temp_file = "__test_mle.fits"

    spectrum = Powerlaw()
    source = PointSource("tst", ra=100, dec=20, spectral_shape=spectrum)
    model = Model(source)

    spectrum.piv = 7 * u.TeV
    spectrum.index = -2.3
    spectrum.K = 1e-15 * fluxUnit

    spectrum.piv.fix = True

    #two free parameters (one with units)
    spectrum.index.fix = False
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001] * 2)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)

    #one free parameter with units
    spectrum.index.fix = True
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001] * 1)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)

    #one free parameter without units
    spectrum.index.fix = False
    spectrum.K.fix = True
    cov_matrix = np.diag([0.001] * 1)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)
示例#2
0
def test_healpixRoi(geminga_maptree, geminga_response):

    #test to make sure writing a model with HealpixMapROI works fine
    ra, dec = 101.7, 16.
    data_radius = 9.
    model_radius = 24.

    m = np.zeros(hp.nside2npix(NSIDE))
    vec = Sky2Vec(ra, dec)
    m[hp.query_disc(NSIDE,
                    vec, (data_radius * u.degree).to(u.radian).value,
                    inclusive=False)] = 1

    #hp.fitsfunc.write_map("roitemp.fits" , m, nest=False, coord="C", partial=False, overwrite=True )

    map_roi = HealpixMapROI(data_radius=data_radius,
                            ra=ra,
                            dec=dec,
                            model_radius=model_radius,
                            roimap=m)
    #fits_roi = HealpixMapROI(data_radius=data_radius, ra=ra, dec=dec, model_radius=model_radius, roifile="roitemp.fits")
    hawc = HAL("HAWC", geminga_maptree, geminga_response, map_roi)
    hawc.set_active_measurements(1, 9)
    '''
  Define model: Two sources, 1 point, 1 extended

  Same declination, but offset in RA

  Different spectral idnex, but both power laws
  '''
    pt_shift = 3.0
    ext_shift = 2.0

    # First soource
    spectrum1 = Powerlaw()
    source1 = PointSource("point",
                          ra=ra + pt_shift,
                          dec=dec,
                          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 - ext_shift, lat0=dec, 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

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

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

    model = Model(source1, source2)

    hawc.set_model(model)

    # Write the model map
    model_map_tree = hawc.write_model_map("test.hd5", test_return_map=True)

    # Read the model back
    hawc_model = map_tree_factory('test.hd5', map_roi)

    # Check written model and read model are the same
    check_map_trees(hawc_model, model_map_tree)

    os.remove("test.hd5")
示例#3
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)