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
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
def test_loading_bad_keywords_file(): with within_directory(__example_dir): pha_fn = "example_integral_spi.pha" rsp_fn = "example_integral_spi.rsp" pha_spectrum = PHASpectrum(pha_fn, rsp_file=rsp_fn) assert type(pha_spectrum.is_poisson) == bool ogip = OGIPLike("test_ogip", observation=pha_fn, response=rsp_fn) ogip.__repr__()
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
def test_reading_of_written_pha(): with within_directory(datasets_directory): # check the number of items written with fits.open('test_from_nai3.rsp') as f: # 2 ext + 5 rsp ext assert len(f) == 7 # make sure we can read spectrum number _ = OGIPLike('test', observation='test_from_nai3.pha', spectrum_number=1) _ = OGIPLike('test', observation='test_from_nai3.pha', spectrum_number=2) os.remove('test_from_nai3.pha')
def write_pha_from_binner(self, file_name, start=None, stop=None, overwrite=False, force_rsp_write=False, extract_measured_background=False): """ Write PHA fits files from the selected bins. If writing from an event list, the bins are from create_time_bins. If using a pre-time binned time series, the bins are those native to the data. Start and stop times can be used to control which bins are written to files :param file_name: the file name of the output files :param start: optional start time of the bins :param stop: optional stop time of the bins :param overwrite: if the fits files should be overwritten :param force_rsp_write: force the writing of RSPs :param extract_measured_background: Use the selected background rather than a polynomial fit to the background :return: None """ # we simply create a bunch of dispersion plugins and convert them to OGIP ogip_list = [OGIPLike.from_general_dispersion_spectrum(sl) for sl in self.to_spectrumlike(from_bins=True, start=start, stop=stop, extract_measured_background=extract_measured_background)] # write out the PHAII file pha_writer = PHAWrite(*ogip_list) pha_writer.write(file_name, overwrite=overwrite, force_rsp_write=force_rsp_write)
def write_pha_from_binner(self, file_name, start=None, stop=None, overwrite=False, force_rsp_write=False): """ Write PHA fits files from the selected bins. If writing from an event list, the bins are from create_time_bins. If using a pre-time binned time series, the bins are those native to the data. Start and stop times can be used to control which bins are written to files :param file_name: the file name of the output files :param start: optional start time of the bins :param stop: optional stop time of the bins :param overwrite: if the fits files should be overwritten :param force_rsp_write: force the writing of RSPs :return: None """ # we simply create a bunch of dispersion plugins and convert them to OGIP ogip_list = [ OGIPLike.from_general_dispersion_spectrum(sl) for sl in self.to_spectrumlike(from_bins=True, start=start, stop=stop) ] # write out the PHAII file pha_writer = PHAWrite(*ogip_list) pha_writer.write(file_name, overwrite=overwrite, force_rsp_write=force_rsp_write)
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
def test_pha_write_no_bkg(): with within_directory(__example_dir): # custom remove background f = fits.open("test.pha") f["SPECTRUM"].data["BACKFILE"] = "NONE" f.writeto("test_pha_nobkg.pha", overwrite=True) ogip = OGIPLike("test_ogip", observation="test_pha_nobkg.pha{1}") ogip.write_pha("test_write_nobkg", overwrite=True) written_ogip = OGIPLike("write_ogip", observation="test_write_nobkg.pha{1}") pha_info = written_ogip.get_pha_files() for key in ["pha"]: assert isinstance(pha_info[key], PHASpectrum) f = fits.open("test_write_nobkg.pha") assert f["SPECTRUM"].data["BACKFILE"][0] == "NONE" assert pha_info["pha"].background_file is None 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 == len(pha_info["pha"].rates)
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)
def test_loading_a_generic_pha_file(): with within_directory(__example_dir): ogip = OGIPLike("test_ogip", observation="test.pha{1}") pha_info = ogip.get_pha_files() assert ogip.name == "test_ogip" assert ogip.n_data_points == sum(ogip._mask) assert sum(ogip._mask) == ogip.n_data_points assert ogip.tstart == 0.0 assert ogip.tstop == 9.95012 assert "cons_test_ogip" in ogip.nuisance_parameters assert ogip.nuisance_parameters["cons_test_ogip"].fix == True assert ogip.nuisance_parameters["cons_test_ogip"].free == False assert "pha" in pha_info assert "bak" in pha_info assert "rsp" in pha_info ogip.__repr__()
def test_loading_a_loose_ogip_pha_file(): with within_directory(__example_dir): ogip = OGIPLike("test_ogip", observation="example_integral.pha") pha_info = ogip.get_pha_files() assert ogip.name == "test_ogip" assert ogip.n_data_points == sum(ogip._mask) assert sum(ogip._mask) == ogip.n_data_points # assert ogip.tstart is None # assert ogip.tstop is None assert "cons_test_ogip" in ogip.nuisance_parameters assert ogip.nuisance_parameters["cons_test_ogip"].fix == True assert ogip.nuisance_parameters["cons_test_ogip"].free == False assert "pha" in pha_info # assert 'bak' in pha_info assert "rsp" in pha_info ogip.__repr__()
def test_loading_a_generic_pha_file(): with within_directory(__example_dir): ogip = OGIPLike('test_ogip', observation='test.pha{1}') pha_info = ogip.get_pha_files() assert ogip.name == 'test_ogip' assert ogip.n_data_points == sum(ogip._mask) assert sum(ogip._mask) == ogip.n_data_points assert ogip.tstart == 0. assert ogip.tstop == 9.95012 assert 'cons_test_ogip' in ogip.nuisance_parameters assert ogip.nuisance_parameters['cons_test_ogip'].fix == True assert ogip.nuisance_parameters['cons_test_ogip'].free == False assert 'pha' in pha_info assert 'bak' in pha_info assert 'rsp' in pha_info ogip.__repr__()
def test_various_effective_area(): with within_directory(__example_dir): ogip = OGIPLike("test_ogip", observation="test.pha{1}") ogip.use_effective_area_correction() ogip.fix_effective_area_correction()
def write_pha(self, filename, overwrite=False, force_rsp_write=False): """ Writes the observation, background and (optional) rsp to PHAII fits files :param filename: base file name to write out :param overwrite: if you would like to force overwriting of the files :param force_rsp_write: force the writing of an rsp even if not required """ # we need to pass up the variables to an OGIPLike # so that we have the proper variable name # a local import here because OGIPLike is dependent on this from threeML.plugins.OGIPLike import OGIPLike ogiplike = OGIPLike.from_general_dispersion_spectrum(self) ogiplike.write_pha(file_name=filename, overwrite=overwrite, force_rsp_write=force_rsp_write)
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
def write_pha(self, filename: str, overwrite: bool = False, force_rsp_write: bool = False) -> None: """ Writes the observation, background and (optional) rsp to PHAII fits files :param filename: base file name to write out :param overwrite: if you would like to force overwriting of the files :param force_rsp_write: force the writing of an rsp even if not required """ # we need to pass up the variables to an OGIPLike # so that we have the proper variable name # a local import here because OGIPLike is dependent on this from threeML.plugins.OGIPLike import OGIPLike ogiplike = OGIPLike.from_general_dispersion_spectrum(self) ogiplike.write_pha( file_name=filename, overwrite=overwrite, force_rsp_write=force_rsp_write )
def test_pha_write(): with within_directory(__example_dir): ogip = OGIPLike('test_ogip', observation='test.pha{1}') ogip.write_pha('test_write', overwrite=True) written_ogip = OGIPLike('write_ogip', observation='test_write.pha{1}') pha_info = written_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 == len(pha_info['pha'].rates)
def test_pha_write(): with within_directory(__example_dir): ogip = OGIPLike("test_ogip", observation="test.pha{1}") ogip.write_pha("test_write", overwrite=True) written_ogip = OGIPLike("write_ogip", observation="test_write.pha{1}") pha_info = written_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 == len(pha_info["pha"].rates)
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)
def test_loading_ogip(): with within_directory(datasets_dir): _ = OGIPLike("test_ogip", observation="test.pha{1}")
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)
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_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)
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)
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)
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()