Exemple #1
0
def test_simulating_data_sets():
    with within_directory(__example_dir):

        ogip = OGIPLike('test_ogip', observation='test.pha{1}')

        with pytest.raises(AssertionError):
            _ = ogip.simulated_parameters

        n_data_points = 128
        ogip.set_active_measurements("all")

        assert ogip._n_synthetic_datasets == 0

        ab = AnalysisBuilder(ogip)
        _ = ab.get_jl('normal')

        new_ogip = ogip.get_simulated_dataset('sim')

        assert new_ogip.name == 'sim'
        assert ogip._n_synthetic_datasets == 1
        assert new_ogip.n_data_points == n_data_points

        assert new_ogip.n_data_points == sum(new_ogip._mask)
        assert sum(new_ogip._mask) == new_ogip.n_data_points
        assert new_ogip.tstart == 0.

        assert 'cons_sim' in new_ogip.nuisance_parameters
        assert new_ogip.nuisance_parameters['cons_sim'].fix == True
        assert new_ogip.nuisance_parameters['cons_sim'].free == False

        pha_info = new_ogip.get_pha_files()

        assert 'pha' in pha_info
        assert 'bak' in pha_info
        assert 'rsp' in pha_info

        del ogip
        del new_ogip

        ogip = OGIPLike('test_ogip', observation='test.pha{1}')

        ab = AnalysisBuilder(ogip)
        _ = ab.get_jl('normal')

        # Now check that generationing a lot of data sets works

        sim_data_sets = [
            ogip.get_simulated_dataset('sim%d' % i) for i in range(100)
        ]

        assert len(sim_data_sets) == ogip._n_synthetic_datasets

        for i, ds in enumerate(sim_data_sets):

            assert ds.name == "sim%d" % i
            assert sum(ds._mask) == sum(ogip._mask)
            assert ds._rebinner is None
Exemple #2
0
def _get_dataset():

    datadir = os.path.join(get_test_datasets_directory(), "bn090217206")

    obsSpectrum = os.path.join(datadir, "bn090217206_n6_srcspectra.pha{1}")
    bakSpectrum = os.path.join(datadir, "bn090217206_n6_bkgspectra.bak{1}")
    rspFile = os.path.join(datadir, "bn090217206_n6_weightedrsp.rsp{1}")
    NaI6 = OGIPLike("NaI6", obsSpectrum, bakSpectrum, rspFile)
    NaI6.set_active_measurements("10.0-30.0", "40.0-950.0")

    return NaI6
Exemple #3
0
def get_dataset():

    datadir = os.path.join(get_test_datasets_directory(), "bn090217206")

    obsSpectrum = os.path.join(datadir, "bn090217206_n6_srcspectra.pha{1}")
    bakSpectrum = os.path.join(datadir, "bn090217206_n6_bkgspectra.bak{1}")
    rspFile = os.path.join(datadir, "bn090217206_n6_weightedrsp.rsp{1}")
    NaI6 = OGIPLike("NaI6", obsSpectrum, bakSpectrum, rspFile)
    NaI6.set_active_measurements("10.0-30.0", "40.0-950.0")

    return NaI6
Exemple #4
0
def get_dataset():

    data_dir = Path(get_test_datasets_directory(), "bn090217206")

    obs_spectrum = Path(data_dir, "bn090217206_n6_srcspectra.pha{1}")
    bak_spectrum = Path(data_dir, "bn090217206_n6_bkgspectra.bak{1}")
    rsp_file = Path(data_dir, "bn090217206_n6_weightedrsp.rsp{1}")
    NaI6 = OGIPLike("NaI6", str(obs_spectrum), str(bak_spectrum),
                    str(rsp_file))
    NaI6.set_active_measurements("10.0-30.0", "40.0-950.0")

    return NaI6
Exemple #5
0
def get_dataset_det(det):

    data_dir = Path(get_test_datasets_directory(), "bn090217206")

    obs_spectrum = Path(data_dir, f"bn090217206_{det}_srcspectra.pha{{1}}")
    bak_spectrum = Path(data_dir, f"bn090217206_{det}_bkgspectra.bak{{1}}")
    rsp_file = Path(data_dir, f"bn090217206_{det}_weightedrsp.rsp{{1}}")
    p = OGIPLike(det, str(obs_spectrum), str(bak_spectrum), str(rsp_file))
    if det[0] == "b":
        p.set_active_measurements("250-25000")
    else:
        p.set_active_measurements("10.0-30.0", "40.0-950.0")

    return p
Exemple #6
0
def test_likelihood_ratio_test():
    with within_directory(__example_dir):
        ogip = OGIPLike('test_ogip', observation='test.pha{1}')

        ogip.set_active_measurements("all")

        ab = AnalysisBuilder(ogip)

        jl1 = ab.get_jl('normal')

        res1, _ = jl1.fit(compute_covariance=True)

        jl2 = ab.get_jl('cpl')
        res2, _ = jl2.fit(compute_covariance=True)

    lrt = LikelihoodRatioTest(jl1, jl2)

    null_hyp_prob, TS, data_frame, like_data_frame = lrt.by_mc(
        n_iterations=50, continue_on_failure=True)
Exemple #7
0
def test_ogip_rebinner():
    with within_directory(__example_dir):
        ogip = OGIPLike("test_ogip", observation="test.pha{1}")

        n_data_points = 128
        ogip.set_active_measurements("all")

        assert ogip.n_data_points == n_data_points

        ogip.rebin_on_background(min_number_of_counts=100)

        assert ogip.n_data_points < 128

        with pytest.raises(AssertionError):
            ogip.set_active_measurements("all")

        ogip.remove_rebinning()

        assert ogip._rebinner is None

        assert ogip.n_data_points == n_data_points

        ogip.view_count_spectrum()
Exemple #8
0
def test_swift_gbm():
    with within_directory(__example_dir):
        gbm_dir = "gbm"
        bat_dir = "bat"

        bat = OGIPLike(
            "BAT",
            observation=os.path.join(bat_dir, "gbm_bat_joint_BAT.pha"),
            response=os.path.join(bat_dir, "gbm_bat_joint_BAT.rsp"),
        )

        bat.set_active_measurements("15-150")
        bat.view_count_spectrum()

        nai6 = OGIPLike(
            "n6",
            os.path.join(gbm_dir, "gbm_bat_joint_NAI_06.pha"),
            os.path.join(gbm_dir, "gbm_bat_joint_NAI_06.bak"),
            os.path.join(gbm_dir, "gbm_bat_joint_NAI_06.rsp"),
            spectrum_number=1,
        )

        nai6.set_active_measurements("8-900")
        nai6.view_count_spectrum()

        bgo0 = OGIPLike(
            "b0",
            os.path.join(gbm_dir, "gbm_bat_joint_BGO_00.pha"),
            os.path.join(gbm_dir, "gbm_bat_joint_BGO_00.bak"),
            os.path.join(gbm_dir, "gbm_bat_joint_BGO_00.rsp"),
            spectrum_number=1,
        )

        bgo0.set_active_measurements("250-10000")
        bgo0.view_count_spectrum()

        bat.use_effective_area_correction(0.2, 1.5)
        bat.fix_effective_area_correction(0.6)
        bat.use_effective_area_correction(0.2, 1.5)

        band = Band()
        model = Model(PointSource("joint_fit", 0, 0, spectral_shape=band))

        band.K = 0.04
        band.xp = 300.0

        data_list = DataList(bat, nai6, bgo0)

        jl = JointLikelihood(model, data_list)

        _ = jl.fit()

        _ = display_spectrum_model_counts(jl, step=False)
Exemple #9
0
def test_swift_gbm():
    with within_directory(__example_dir):
        gbm_dir = "gbm"
        bat_dir = "bat"

        bat = OGIPLike('BAT',
                       observation=os.path.join(bat_dir,
                                                'gbm_bat_joint_BAT.pha'),
                       response=os.path.join(bat_dir, 'gbm_bat_joint_BAT.rsp'))

        bat.set_active_measurements('15-150')
        bat.view_count_spectrum()

        nai6 = OGIPLike('n6',
                        os.path.join(gbm_dir, 'gbm_bat_joint_NAI_06.pha'),
                        os.path.join(gbm_dir, 'gbm_bat_joint_NAI_06.bak'),
                        os.path.join(gbm_dir, 'gbm_bat_joint_NAI_06.rsp'),
                        spectrum_number=1)

        nai6.set_active_measurements('8-900')
        nai6.view_count_spectrum()

        bgo0 = OGIPLike('b0',
                        os.path.join(gbm_dir, 'gbm_bat_joint_BGO_00.pha'),
                        os.path.join(gbm_dir, 'gbm_bat_joint_BGO_00.bak'),
                        os.path.join(gbm_dir, 'gbm_bat_joint_BGO_00.rsp'),
                        spectrum_number=1)

        bgo0.set_active_measurements('250-10000')
        bgo0.view_count_spectrum()

        bat.use_effective_area_correction(.2, 1.5)
        bat.fix_effective_area_correction(.6)
        bat.use_effective_area_correction(.2, 1.5)

        band = Band()
        model = Model(PointSource('joint_fit', 0, 0, spectral_shape=band))

        band.K = .04
        band.xp = 300.

        data_list = DataList(bat, nai6, bgo0)

        jl = JointLikelihood(model, data_list)

        _ = jl.fit()

        _ = display_spectrum_model_counts(jl, step=False)
Exemple #10
0
def test_ogip_energy_selection():
    with within_directory(__example_dir):
        ogip = OGIPLike("test_ogip", observation="test.pha{1}")

        assert sum(ogip._mask) == sum(ogip.quality.good)

        # Test that  selecting a subset reduces the number of data points
        ogip.set_active_measurements("10-30")

        assert sum(ogip._mask) == ogip.n_data_points
        assert sum(ogip._mask) < 128

        # Test selecting all channels
        ogip.set_active_measurements("all")

        assert sum(ogip._mask) == ogip.n_data_points
        assert sum(ogip._mask) == 128

        # Test channel setting
        ogip.set_active_measurements(exclude=["c0-c1"])

        assert sum(ogip._mask) == ogip.n_data_points
        assert sum(ogip._mask) == 126

        # Test mixed ene/chan setting
        ogip.set_active_measurements(exclude=["0-c1"], verbose=True)

        assert sum(ogip._mask) == ogip.n_data_points
        assert sum(ogip._mask) == 126

        # Test that energies cannot be input backwards
        with pytest.raises(AssertionError):
            ogip.set_active_measurements("50-30")

        with pytest.raises(AssertionError):
            ogip.set_active_measurements("c20-c10")

        with pytest.raises(AssertionError):
            ogip.set_active_measurements("c100-0")

        with pytest.raises(AssertionError):
            ogip.set_active_measurements("c1-c200")

        with pytest.raises(AssertionError):
            ogip.set_active_measurements("10-c200")

        ogip.set_active_measurements("reset")

        assert sum(ogip._mask) == sum(ogip.quality.good)
Exemple #11
0
def test_pha_files_in_generic_ogip_constructor_spec_number_in_arguments():
    with within_directory(__example_dir):
        ogip = OGIPLike("test_ogip", observation="test.pha", spectrum_number=1)
        ogip.set_active_measurements("all")

        pha_info = ogip.get_pha_files()

        for key in ["pha", "bak"]:

            assert isinstance(pha_info[key], PHASpectrum)

        assert pha_info["pha"].background_file == "test_bak.pha{1}"
        assert pha_info["pha"].ancillary_file is None
        assert pha_info["pha"].instrument == "GBM_NAI_03"
        assert pha_info["pha"].mission == "GLAST"
        assert pha_info["pha"].is_poisson == True
        assert pha_info["pha"].n_channels == ogip.n_data_points
        assert pha_info["pha"].n_channels == len(pha_info["pha"].rates)

        # Test that Poisson rates cannot call rate error
        assert pha_info["pha"].rate_errors is None

        assert (
            sum(pha_info["pha"].sys_errors == np.zeros_like(pha_info["pha"].rates))
            == pha_info["bak"].n_channels
        )
        assert (
            pha_info["pha"].response_file.split("/")[-1]
            == "glg_cspec_n3_bn080916009_v07.rsp"
        )
        assert pha_info["pha"].scale_factor == 1.0

        assert pha_info["bak"].background_file is None

        # Test that we cannot get a bak file
        #
        # with pytest.raises(KeyError):
        #
        #     _ = pha_info['bak'].background_file
        #
        # Test that we cannot get a anc file
        # with pytest.raises(KeyError):
        #
        #     _ = pha_info['bak'].ancillary_file

        assert pha_info["bak"].response_file is None

        assert pha_info["bak"].ancillary_file is None

        # # Test that we cannot get a RSP file
        # with pytest.raises(AttributeError):
        #      _ = pha_info['bak'].response_file

        assert pha_info["bak"].instrument == "GBM_NAI_03"
        assert pha_info["bak"].mission == "GLAST"

        assert pha_info["bak"].is_poisson == False

        assert pha_info["bak"].n_channels == ogip.n_data_points
        assert pha_info["bak"].n_channels == len(pha_info["pha"].rates)

        assert len(pha_info["bak"].rate_errors) == pha_info["bak"].n_channels

        assert (
            sum(pha_info["bak"].sys_errors == np.zeros_like(pha_info["pha"].rates))
            == pha_info["bak"].n_channels
        )

        assert pha_info["bak"].scale_factor == 1.0

        assert isinstance(pha_info["rsp"], OGIPResponse)
Exemple #12
0
def test_pha_files_in_generic_ogip_constructor_spec_number_in_file_name():
    with within_directory(__example_dir):

        ogip = OGIPLike('test_ogip', observation='test.pha{1}')
        ogip.set_active_measurements('all')
        pha_info = ogip.get_pha_files()

        for key in ['pha', 'bak']:

            assert isinstance(pha_info[key], PHASpectrum)

        assert pha_info['pha'].background_file == 'test_bak.pha{1}'
        assert pha_info['pha'].ancillary_file is None
        assert pha_info['pha'].instrument == 'GBM_NAI_03'
        assert pha_info['pha'].mission == 'GLAST'
        assert pha_info['pha'].is_poisson == True
        assert pha_info['pha'].n_channels == ogip.n_data_points
        assert pha_info['pha'].n_channels == len(pha_info['pha'].rates)

        # Test that Poisson rates cannot call rate error
        assert pha_info['pha'].rate_errors is None

        assert sum(pha_info['pha'].sys_errors == np.zeros_like(
            pha_info['pha'].rates)) == pha_info['bak'].n_channels

        assert pha_info['pha'].response_file.split(
            '/')[-1] == 'glg_cspec_n3_bn080916009_v07.rsp'
        assert pha_info['pha'].scale_factor == 1.0

        assert pha_info['bak'].background_file is None

        # Test that we cannot get a bak file
        #
        #
        # with pytest.raises(KeyError):
        #
        #     _ = pha_info['bak'].background_file

        # Test that we cannot get a anc file
        # with pytest.raises(KeyError):
        #
        #     _ = pha_info['bak'].ancillary_file

        # Test that we cannot get a RSP file

        assert pha_info['bak'].response_file is None

        assert pha_info['bak'].ancillary_file is None

        # with pytest.raises(AttributeError):
        #      _ = pha_info['bak'].response_file

        assert pha_info['bak'].instrument == 'GBM_NAI_03'
        assert pha_info['bak'].mission == 'GLAST'

        assert pha_info['bak'].is_poisson == False

        assert pha_info['bak'].n_channels == ogip.n_data_points
        assert pha_info['bak'].n_channels == len(pha_info['pha'].rates)

        assert len(pha_info['bak'].rate_errors) == pha_info['bak'].n_channels

        assert sum(pha_info['bak'].sys_errors == np.zeros_like(
            pha_info['pha'].rates)) == pha_info['bak'].n_channels

        assert pha_info['bak'].scale_factor == 1.0

        assert isinstance(pha_info['rsp'], OGIPResponse)