Example #1
0
def hawc_point_source_fitted_joint_like():

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'),
                                  abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(
        response), "Data files do not exist at %s" % data_path

    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,3.15e-11,2.37,42.3
    # at this position:
    # 100,22

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()
    source = PointSource("TestSource",
                         ra=100.0,
                         dec=22.0,
                         spectral_shape=spectrum)

    spectrum.K = 3.15e-11 / (u.TeV * u.cm**2 * u.s)
    spectrum.K.bounds = (1e-22, 1e-18)  # without units energies are in keV

    spectrum.piv = 1 * u.TeV
    spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)

    q = source(1 * u.keV)

    assert np.isclose(q.value, 67.3458058177)

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)
    llh = HAWCLike("HAWC", maptree, response)
    llh.set_active_measurements(1, 9)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)
    jl = JointLikelihood(lm, datalist, verbose=True)

    jl.set_minimizer("ROOT")

    parameter_frame, like = jl.fit(compute_covariance=False)

    return jl, parameter_frame, like
Example #2
0
def test_CommonNorm_fit():
    assert is_plugin_available("HAWCLike"), "HAWCLike is not available!"

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'), abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), "Data files do not exist at %s" % data_path
    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,3.15e-11,2.37,42.3
    # at this position:
    # 100,22

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()
    source = PointSource("TestSource", ra=100.0, dec=22.0, spectral_shape=spectrum)

    spectrum.K = 3.15e-11 / (u.TeV * u.cm ** 2 * u.s)
    spectrum.K.bounds = (1e-22, 1e-18)  # without units energies are in keV
    spectrum.K.fix = True

    spectrum.piv = 1 * u.TeV
    spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)
    spectrum.index.free = False

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)
    spectrum.xc.free = False

    q = source(1 * u.keV)

    assert np.isclose(q.value, 67.3458058177)

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)
    llh = HAWCLike("HAWC", maptree, response)
    llh.set_active_measurements(1, 9)

    llh.activate_CommonNorm()

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)
    jl = JointLikelihood(lm, datalist, verbose=True)

    jl.set_minimizer("ROOT")

    parameter_frame, like = jl.fit(compute_covariance=False)

    assert np.isclose(lm.HAWC_ComNorm.value, 1.0756519971562115, rtol=1e-2)
Example #3
0
def test_set_active_measurements():

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'), abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), "Data files do not exist at %s" % data_path

    llh = HAWCLike("HAWC", maptree, response)
    # Test one way
    llh.set_active_measurements(1, 9)
    # Test the other way
    llh.set_active_measurements(bin_list=['4','5','6','7','8','9'])
Example #4
0
def test_set_active_measurements():

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'), abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), "Data files do not exist at %s" % data_path

    llh = HAWCLike("HAWC", maptree, response)
    # Test one way
    llh.set_active_measurements(1, 9)
    # Test the other way
    llh.set_active_measurements(bin_list=['4','5','6','7','8','9'])
Example #5
0
def test_set_active_measurements():

    data_path = sanitize_filename(os.environ.get("HAWC_3ML_TEST_DATA_DIR"),
                                  abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), (
        "Data files do not exist at %s" % data_path)

    llh = HAWCLike("HAWC", maptree, response)
    # Test one way
    llh.set_active_measurements(1, 9)
    # Test the other way
    llh.set_active_measurements(bin_list=["4", "5", "6", "7", "8", "9"])
Example #6
0
def test_hawc_extended_source_fit():
    # Ensure test environment is valid

    assert is_plugin_available("HAWCLike"), "HAWCLike is not available!"

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'), abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), "Data files do not exist at %s" % data_path

    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,1.32e-07,2.37,42.3
    # at this position:
    # 100,22
    # with a disk shape with an extension of 1.5 deg

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()

    shape = Disk_on_sphere()

    source = ExtendedSource("ExtSource",
                            spatial_shape=shape,
                            spectral_shape=spectrum)

    shape.lon0 = 100.0
    shape.lon0.fix = True

    shape.lat0 = 22.0
    shape.lat0.fix = True

    shape.radius = 1.5 * u.degree
    shape.radius.bounds = (0.5 * u.degree, 1.55 * u.degree)
    # shape.radius.fix = True

    spectrum.K = 4.39964273e-20
    spectrum.K.bounds = (1e-24, 1e-17)

    spectrum.piv = 1 * u.TeV
    # spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)
    # spectrum.index.fix = True

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)
    spectrum.xc.fix = True

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)
    llh = HAWCLike("HAWC", maptree, response)
    llh.set_active_measurements(1, 9)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)
    jl = JointLikelihood(lm, datalist, verbose=True)

    jl.set_minimizer("ROOT")

    parameter_frame, like = jl.fit(compute_covariance=False)

    # Check that we have converged to the right solution
    # (the true value of course are not exactly the value simulated,
    # they are just the point where the fit should converge)
    assert is_within_tolerance(4.7805737823025172e-20, parameter_frame['value']['ExtSource.spectrum.main.Cutoff_powerlaw.K'])
    assert is_within_tolerance(-2.44931279819,
                               parameter_frame['value']['ExtSource.spectrum.main.Cutoff_powerlaw.index'])
    assert is_within_tolerance(1.4273457159139373, parameter_frame['value']['ExtSource.Disk_on_sphere.radius'])

    assert is_within_tolerance(186389.581117, like['-log(likelihood)']['HAWC'])

    # Print up the TS, significance, and fit parameters, and then plot stuff
    print("\nTest statistic:")
    TS = llh.calc_TS()
    sigma = np.sqrt(TS)

    assert is_within_tolerance(3510.26, TS)
    assert is_within_tolerance(59.2475, sigma)

    print("Test statistic: %g" % TS)
    print("Significance:   %g\n" % sigma)

    # Get the differential flux at 1 TeV
    diff_flux = spectrum(1 * u.TeV)
    # Convert it to 1 / (TeV cm2 s)
    diff_flux_TeV = diff_flux.to(1 / (u.TeV * u.cm ** 2 * u.s))

    print("Norm @ 1 TeV:  %s \n" % diff_flux_TeV)

    assert is_within_tolerance(4.66888328668e-11, diff_flux_TeV.value)

    spectrum.display()
    shape.display()
Example #7
0
def test_hawc_fullsky_options():

    assert is_plugin_available("HAWCLike"), "HAWCLike is not available!"

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'), abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(response), "Data files do not exist at %s" % data_path

    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,3.15e-11,2.37,42.3
    # at this position:
    # 100,22

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()
    source = PointSource("TestSource", ra=100.0, dec=22.0, spectral_shape=spectrum)

    spectrum.K = 3.15e-11 / (u.TeV * u.cm ** 2 * u.s)
    spectrum.K.bounds = (1e-22, 1e-18)  # without units energies are in keV

    spectrum.piv = 1 * u.TeV
    spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)

    q = source(1 * u.keV)

    assert np.isclose(q.value, 67.3458058177)

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)

    # Test with fullsky=True, and try to perform a fit to verify that we throw an exception

    llh = HAWCLike("HAWC", maptree, response, fullsky=True)
    llh.set_active_measurements(1, 9)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)

    with pytest.raises(RuntimeError):

        jl = JointLikelihood(lm, datalist, verbose=False)

    # Now we use set_ROI and this should work
    llh.set_ROI(100.0, 22.0, 2.0)

    jl = JointLikelihood(lm, datalist, verbose=False)

    # Now test that we can use set_ROI even though fullsky=False
    llh = HAWCLike("HAWC", maptree, response, fullsky=False)
    llh.set_active_measurements(1, 9)
    llh.set_ROI(100.0, 22.0, 1.0)
    
    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)

    jl = JointLikelihood(lm, datalist, verbose=False)
Example #8
0
def test_hawc_extended_source_fit():
    # Ensure test environment is valid

    assert is_plugin_available("HAWCLike"), "HAWCLike is not available!"

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'),
                                  abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(
        response), "Data files do not exist at %s" % data_path

    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,1.32e-07,2.37,42.3
    # at this position:
    # 100,22
    # with a disk shape with an extension of 1.5 deg

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()

    shape = Disk_on_sphere()

    source = ExtendedSource("ExtSource",
                            spatial_shape=shape,
                            spectral_shape=spectrum)

    shape.lon0 = 100.0
    shape.lon0.fix = True

    shape.lat0 = 22.0
    shape.lat0.fix = True

    shape.radius = 1.5 * u.degree
    shape.radius.bounds = (0.5 * u.degree, 1.55 * u.degree)
    # shape.radius.fix = True

    spectrum.K = 4.39964273e-20
    spectrum.K.bounds = (1e-24, 1e-17)

    spectrum.piv = 1 * u.TeV
    # spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)
    # spectrum.index.fix = True

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)
    spectrum.xc.fix = True

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)
    llh = HAWCLike("HAWC", maptree, response)
    llh.set_active_measurements(1, 9)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)
    jl = JointLikelihood(lm, datalist, verbose=True)

    jl.set_minimizer("ROOT")

    parameter_frame, like = jl.fit(compute_covariance=False)

    # Check that we have converged to the right solution
    # (the true value of course are not exactly the value simulated,
    # they are just the point where the fit should converge)
    assert is_within_tolerance(
        4.7805737823025172e-20,
        parameter_frame['value']['ExtSource.spectrum.main.Cutoff_powerlaw.K'])
    assert is_within_tolerance(
        -2.44931279819, parameter_frame['value']
        ['ExtSource.spectrum.main.Cutoff_powerlaw.index'])
    assert is_within_tolerance(
        1.4273457159139373,
        parameter_frame['value']['ExtSource.Disk_on_sphere.radius'])

    assert is_within_tolerance(186389.581117, like['-log(likelihood)']['HAWC'])

    # Print up the TS, significance, and fit parameters, and then plot stuff
    print("\nTest statistic:")
    TS = llh.calc_TS()
    sigma = np.sqrt(TS)

    assert is_within_tolerance(3510.26, TS)
    assert is_within_tolerance(59.2475, sigma)

    print("Test statistic: %g" % TS)
    print("Significance:   %g\n" % sigma)

    # Get the differential flux at 1 TeV
    diff_flux = spectrum(1 * u.TeV)
    # Convert it to 1 / (TeV cm2 s)
    diff_flux_TeV = diff_flux.to(1 / (u.TeV * u.cm**2 * u.s))

    print("Norm @ 1 TeV:  %s \n" % diff_flux_TeV)

    assert is_within_tolerance(4.66888328668e-11, diff_flux_TeV.value)

    spectrum.display()
    shape.display()
Example #9
0
def test_hawc_fullsky_options():

    assert is_plugin_available("HAWCLike"), "HAWCLike is not available!"

    data_path = sanitize_filename(os.environ.get('HAWC_3ML_TEST_DATA_DIR'),
                                  abspath=True)

    maptree = os.path.join(data_path, _maptree_name)
    response = os.path.join(data_path, _response_name)

    assert os.path.exists(maptree) and os.path.exists(
        response), "Data files do not exist at %s" % data_path

    # The simulated source has this spectrum (credits for simulation: Colas Riviere):
    # CutOffPowerLaw,3.15e-11,2.37,42.3
    # at this position:
    # 100,22

    # Define the spectral and spatial models for the source
    spectrum = Cutoff_powerlaw()
    source = PointSource("TestSource",
                         ra=100.0,
                         dec=22.0,
                         spectral_shape=spectrum)

    spectrum.K = 3.15e-11 / (u.TeV * u.cm**2 * u.s)
    spectrum.K.bounds = (1e-22, 1e-18)  # without units energies are in keV

    spectrum.piv = 1 * u.TeV
    spectrum.piv.fix = True

    spectrum.index = -2.37
    spectrum.index.bounds = (-4, -1)

    spectrum.xc = 42.3 * u.TeV
    spectrum.xc.bounds = (1 * u.TeV, 100 * u.TeV)

    q = source(1 * u.keV)

    assert np.isclose(q.value, 67.3458058177)

    # Set up a likelihood model using the source.
    # Then create a HAWCLike object using the model, the maptree, and detector
    # response.
    lm = Model(source)

    # Test with fullsky=True, and try to perform a fit to verify that we throw an exception

    llh = HAWCLike("HAWC", maptree, response, fullsky=True)
    llh.set_active_measurements(1, 9)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood and run the fit
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)

    with pytest.raises(RuntimeError):

        jl = JointLikelihood(lm, datalist, verbose=False)

    # Now we use set_ROI and this should work
    llh.set_ROI(100.0, 22.0, 2.0)

    jl = JointLikelihood(lm, datalist, verbose=False)

    # Now test that we can use set_ROI even though fullsky=False
    llh = HAWCLike("HAWC", maptree, response, fullsky=False)
    llh.set_active_measurements(1, 9)
    llh.set_ROI(100.0, 22.0, 1.0)

    # Double check the free parameters
    print("Likelihood model:\n")
    print(lm)

    # Set up the likelihood
    print("Performing likelihood fit...\n")
    datalist = DataList(llh)

    jl = JointLikelihood(lm, datalist, verbose=False)