def test_arithmetic(): spec1 = Spectrum.from_powerlaw(1.0, 0.05, 1.0e-4, 0.1, 10.0, 10000) spec2 = Spectrum.from_powerlaw(2.0, 0.01, 1.0e-3, 0.1, 10.0, 10000) spec3 = spec1+spec2 flux3 = spec1.flux+spec2.flux assert_allclose(spec3.flux, flux3) spec4 = spec3*3.0 spec5 = 3.0*spec3 flux4 = spec3.flux*3.0 assert_allclose(spec4.flux, spec5.flux) assert_allclose(spec4.flux, flux4) spec6 = spec3/2.5 flux6 = spec3.flux/2.5 assert_allclose(spec6.flux, flux6) spec7 = Spectrum.from_constant(1.0e-4, 0.1, 10.0, 10000) spec8 = spec1+spec7 assert_allclose(spec8.flux.value, spec1.flux.value+1.0e-4)
def test_arithmetic(): spec1 = Spectrum.from_powerlaw(1.0, 0.05, 1.0e-4) spec2 = Spectrum.from_powerlaw(2.0, 0.01, 1.0e-3) spec3 = spec1+spec2 flux3 = spec1.flux+spec2.flux assert_allclose(spec3.flux, flux3) spec4 = spec3*3.0 spec5 = 3.0*spec3 flux4 = spec3.flux*3.0 assert_allclose(spec4.flux, spec5.flux) assert_allclose(spec4.flux, flux4) spec6 = spec3/2.5 flux6 = spec3.flux/2.5 assert_allclose(spec6.flux, flux6) spec7 = Spectrum.from_constant(1.0e-4) spec8 = spec1+spec7 assert_allclose(spec8.flux.value, spec1.flux.value+1.0e-4)
def test_absorption_line(answer_store, answer_dir): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) const_flux = 1.0e-3 line_pos = 1.0 line_width = 0.02 line_amp = 1.0e-5 exp_time = (100.0, "ks") inst_name = "lynx_xgs" spec = Spectrum.from_constant(const_flux, 0.1, 3.0, 100000) spec.add_absorption_line(line_pos, line_width, line_amp) spectrum_answer_testing(spec, "absorption_line_test.h5", answer_store, answer_dir) simulate_spectrum(spec, inst_name, exp_time, "absorption_line_evt.pha", overwrite=True, prng=prng) file_answer_testing("SPECTRUM", "absorption_line_evt.pha", answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_read_write(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) spec1 = Spectrum.from_powerlaw(1.0, 0.05, 1.0e-4, 0.1, 10.0, 10000) spec1.write_file("test_spec.dat", overwrite=True) spec2 = Spectrum.from_file("test_spec.dat") assert_allclose(spec1.flux, spec2.flux) assert_allclose(spec1.emid, spec2.emid) assert_allclose(spec1.ebins, spec2.ebins) assert_allclose(spec1.cumspec, spec2.cumspec) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_read_write(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) spec1 = Spectrum.from_powerlaw(1.0, 0.05, 1.0e-4) spec1.write_file("test_spec.dat", overwrite=True) spec2 = Spectrum.from_file("test_spec.dat") assert_allclose(spec1.flux, spec2.flux) assert_allclose(spec1.emid, spec2.emid) assert_allclose(spec1.ebins, spec2.ebins) assert_allclose(spec1.cumspec, spec2.cumspec) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 alpha_sim = 0.95 redshift = 0.02 exp_time = (100., "ks") area = (3000., "cm**2") spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim, emin=0.1, emax=11.5, nbins=2000) spec.apply_foreground_absorption(nH_sim, model="tbabs") positions = [(30.01, 45.0)] events = make_point_sources(area, exp_time, positions, (30.0, 45.0), spec, prng=prng) events.write_simput_file("ptsrc", overwrite=True) instrument_simulator("ptsrc_simput.fits", "ptsrc_evt.fits", exp_time, "sq_aciss_cy20", [30.0, 45.0], overwrite=True, foreground=False, ptsrc_bkgnd=False, instr_bkgnd=False, prng=prng) write_spectrum("ptsrc_evt.fits", "point_source_evt.pi", overwrite=True) os.system("cp %s %s ." % (arf.filename, rmf.filename)) load_user_model(mymodel, "tplaw") add_user_pars("tplaw", ["nH", "norm", "redshift", "alpha"], [0.02, norm_sim*0.8, redshift, 0.9], parmins=[0.0, 0.0, 0.0, 0.1], parmaxs=[10.0, 1.0e9, 10.0, 10.0], parfrozen=[True, False, True, False]) load_pha("point_source_evt.pi") set_stat("cstat") set_method("simplex") ignore(":0.4, 9.0:") set_model("tplaw") fit() res = get_fit_results() assert np.abs(res.parvals[0]-norm_sim)/norm_sim < 0.05 assert np.abs(res.parvals[1]-alpha_sim)/alpha_sim < 0.05 os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 alpha_sim = 0.95 redshift = 0.02 exp_time = (100., "ks") area = (3000., "cm**2") spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim, emin=0.1, emax=11.5, nbins=2000) spec.apply_foreground_absorption(nH_sim, model="tbabs") positions = [(30.01, 45.0)] events = make_point_sources(area, exp_time, positions, (30.0, 45.0), spec, prng=prng) events.write_simput_file("ptsrc", overwrite=True) instrument_simulator("ptsrc_simput.fits", "ptsrc_evt.fits", exp_time, "sq_aciss_cy19", [30.0, 45.0], overwrite=True, foreground=False, ptsrc_bkgnd=False, instr_bkgnd=False, prng=prng) write_spectrum("ptsrc_evt.fits", "point_source_evt.pi", overwrite=True) os.system("cp %s %s ." % (arf.filename, rmf.filename)) load_user_model(mymodel, "tplaw") add_user_pars("tplaw", ["nH", "norm", "redshift", "alpha"], [0.02, norm_sim*0.8, redshift, 0.9], parmins=[0.0, 0.0, 0.0, 0.1], parmaxs=[10.0, 1.0e9, 10.0, 10.0], parfrozen=[True, False, True, False]) load_pha("point_source_evt.pi") set_stat("cstat") set_method("simplex") ignore(":0.4, 9.0:") set_model("tplaw") fit() res = get_fit_results() assert np.abs(res.parvals[0]-norm_sim)/norm_sim < 0.05 assert np.abs(res.parvals[1]-alpha_sim)/alpha_sim < 0.05 os.chdir(curdir) shutil.rmtree(tmpdir)
def test_convolved_spectra(): arf = AuxiliaryResponseFile("xrs_hdxi_3x10.arf") spec1 = Spectrum.from_powerlaw(2.0, 0.01, 1.0, 0.1, 10.0, 1000) cspec1 = ConvolvedSpectrum(spec1, arf) cspec2 = spec1*arf spec2 = cspec1.deconvolve() assert_array_equal(cspec1.ebins.value, cspec2.ebins.value) assert_array_equal(spec1.ebins.value, spec2.ebins.value) assert_array_equal(cspec1.flux.value, cspec2.flux.value) assert_allclose(spec1.flux.value, spec2.flux.value)
def test_rescale_flux(): spec = Spectrum.from_powerlaw(2.0, 0.01, 1.0, 0.1, 10.0, 10000) spec.rescale_flux(1.0e-4, emin=0.5, emax=7.0, flux_type="photons") f = spec.get_flux_in_band(0.5, 7.0)[0] assert_allclose(1.0e-4, f.value) spec.rescale_flux(1.0e-12, emin=0.4, emax=1.0, flux_type="energy") f = spec.get_flux_in_band(0.4, 1.0)[1] assert_allclose(1.0e-12, f.value)
def test_rescale_flux(): spec = Spectrum.from_powerlaw(2.0, 0.01, 1.0) spec.rescale_flux(1.0e-4, emin=0.5, emax=7.0, flux_type="photons") f = spec.get_flux_in_band(0.5, 7.0)[0] assert_allclose(1.0e-4, f.value) spec.rescale_flux(1.0e-12, emin=0.4, emax=1.0, flux_type="energy") f = spec.get_flux_in_band(0.4, 1.0)[1] assert_allclose(1.0e-12, f.value)
def to_scaled_spectrum(self, fov, focal_length=None): from soxs.instrument import FlatResponse fov = parse_value(fov, "arcmin") if focal_length is None: focal_length = self.default_focal_length else: focal_length = parse_value(focal_length, "m") flux = self.flux.value * fov * fov flux *= (focal_length / self.default_focal_length)**2 arf = FlatResponse(self.ebins.value[0], self.ebins.value[-1], 1.0, self.ebins.size - 1) return ConvolvedSpectrum(Spectrum(self.ebins.value, flux), arf)
def plaw_fit(alpha_sim, answer_store, answer_dir): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 redshift = 0.01 exp_time = (50.0, "ks") area = 40000.0 inst_name = "new_hdxi" spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim, 0.1, 10.0, 20000) spec.apply_foreground_absorption(nH_sim, model="tbabs") spectrum_answer_testing(spec, f"power_law_{alpha_sim}.h5", answer_store, answer_dir) pt_src_pos = PointSourceModel(30.0, 45.0) pt_src = SimputPhotonList.from_models("plaw_model", spec, pt_src_pos, exp_time, area, prng=prng) cat = SimputCatalog.from_source("plaw_model_simput.fits", pt_src, overwrite=True) instrument_simulator("plaw_model_simput.fits", f"plaw_model_{alpha_sim}_evt.fits", exp_time, inst_name, [30.0, 45.0], instr_bkgnd=False, ptsrc_bkgnd=False, foreground=False, prng=prng) write_spectrum(f"plaw_model_{alpha_sim}_evt.fits", f"plaw_model_{alpha_sim}_evt.pha", overwrite=True) file_answer_testing("EVENTS", f"plaw_model_{alpha_sim}_evt.fits", answer_store, answer_dir) file_answer_testing("SPECTRUM", f"plaw_model_{alpha_sim}_evt.pha", answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_emission_line(answer_store, answer_dir): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) const_flux = 1.0e-4 line_pos = 5.0 line_width = 0.02 line_amp = 1.0e-5 exp_time = (100.0, "ks") area = 30000.0 inst_name = "lynx_lxm" spec = Spectrum.from_constant(const_flux, 1.0, 10.0, 20000) spec.add_emission_line(line_pos, line_width, line_amp) spectrum_answer_testing(spec, "emission_line_test.h5", answer_store, answer_dir) pt_src_pos = PointSourceModel(30.0, 45.0) sim_cat = SimputCatalog.from_models("emission_line", "emission_line", spec, pt_src_pos, exp_time, area, prng=prng) sim_cat.write_catalog(overwrite=True) instrument_simulator("emission_line_simput.fits", "emission_line_evt.fits", exp_time, inst_name, [30.0, 45.0], instr_bkgnd=False, ptsrc_bkgnd=False, foreground=False, prng=prng) write_spectrum("emission_line_evt.fits", "emission_line_evt.pha", overwrite=True) file_answer_testing("EVENTS", "emission_line_evt.fits", answer_store, answer_dir) file_answer_testing("SPECTRUM", "emission_line_evt.pha", answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_append(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) exp_time = (50.0, "ks") area = (4.0, "m**2") ra0 = 30.0 dec0 = 45.0 spec = Spectrum.from_powerlaw(1.1, 0.05, 1.0e-4) e1 = spec.generate_energies(exp_time, area, prng=prng) pt_src1 = PointSourceModel(ra0 + 0.05, dec0 + 0.05, e1.size) e2 = spec.generate_energies(exp_time, area, prng=prng) pt_src2 = PointSourceModel(ra0 - 0.05, dec0 - 0.05, e1.size) write_photon_list("pt_src", "pt_src1", e1.flux, pt_src1.ra, pt_src1.dec, e1, overwrite=True) write_photon_list("pt_src", "pt_src2", e2.flux, pt_src2.ra, pt_src2.dec, e2, append=True) assert os.path.exists("pt_src_simput.fits") assert os.path.exists("pt_src1_phlist.fits") assert os.path.exists("pt_src2_phlist.fits") f = pyfits.open("pt_src_simput.fits") cat = f["SRC_CAT"].data["SPECTRUM"] assert cat[0] == "pt_src1_phlist.fits[PHLIST,1]" assert cat[1] == "pt_src2_phlist.fits[PHLIST,1]" f.close() os.chdir(curdir) shutil.rmtree(tmpdir)
def plaw_fit(alpha_sim, answer_store, answer_dir): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 redshift = 0.01 exp_time = (50.0, "ks") area = 40000.0 inst_name = "new_hdxi" spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim, 0.1, 10.0, 20000) spec.apply_foreground_absorption(nH_sim, model="tbabs") spectrum_answer_testing(spec, "power_law_%s.h5" % alpha_sim, answer_store, answer_dir) pt_src_pos = PointSourceModel(30.0, 45.0) sim_cat = SimputCatalog.from_models("plaw_model", "plaw_model", spec, pt_src_pos, exp_time, area, prng=prng) sim_cat.write_catalog(overwrite=True) instrument_simulator("plaw_model_simput.fits", "plaw_model_%s_evt.fits" % alpha_sim, exp_time, inst_name, [30.0, 45.0], instr_bkgnd=False, ptsrc_bkgnd=False, foreground=False, prng=prng) write_spectrum("plaw_model_%s_evt.fits" % alpha_sim, "plaw_model_%s_evt.pha" % alpha_sim, overwrite=True) file_answer_testing("EVENTS", "plaw_model_%s_evt.fits" % alpha_sim, answer_store, answer_dir) file_answer_testing("SPECTRUM", "plaw_model_%s_evt.pha" % alpha_sim, answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_append(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) exp_time = (50.0, "ks") area = (4.0, "m**2") ra0 = 30.0 dec0 = 45.0 spec = Spectrum.from_powerlaw(1.1, 0.05, 1.0e-4, 0.1, 10.0, 10000) pos1 = PointSourceModel(ra0+0.05, dec0+0.05) pos2 = PointSourceModel(ra0-0.05, dec0-0.05) pl1 = SimputPhotonList.from_models("pt_src1", spec, pos1, exp_time, area) pl2 = SimputPhotonList.from_models("pt_src2", spec, pos2, exp_time, area) sc = SimputCatalog.from_source("pt_src_simput.fits", pl1, src_filename="pt_src1_phlist.fits", overwrite=True) sc.append(pl2, src_filename="pt_src2_phlist.fits", overwrite=True) assert os.path.exists("pt_src1_phlist.fits") assert os.path.exists("pt_src2_phlist.fits") assert os.path.exists("pt_src_simput.fits") f = pyfits.open("pt_src_simput.fits") cat = f["SRC_CAT"].data["SPECTRUM"] assert cat[0] == "pt_src1_phlist.fits[PHLIST,1]" assert cat[1] == "pt_src2_phlist.fits[PHLIST,1]" f.close() os.chdir(curdir) shutil.rmtree(tmpdir)
def test_append(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) exp_time = (50.0, "ks") area = (4.0, "m**2") ra0 = 30.0 dec0 = 45.0 spec = Spectrum.from_powerlaw(1.1, 0.05, 1.0e-4, 0.1, 10.0, 10000) e1 = spec.generate_energies(exp_time, area, prng=prng) ra1, dec1 = PointSourceModel(ra0+0.05, dec0+0.05).generate_coords(e1.size, prng=prng) e2 = spec.generate_energies(exp_time, area, prng=prng) ra2, dec2 = PointSourceModel(ra0-0.05, dec0-0.05).generate_coords(e1.size, prng=prng) write_photon_list("pt_src", "pt_src1", e1.flux, ra1, dec1, e1, overwrite=True) write_photon_list("pt_src", "pt_src2", e2.flux, ra2, dec2, e2, append=True) assert os.path.exists("pt_src_simput.fits") assert os.path.exists("pt_src1_phlist.fits") assert os.path.exists("pt_src2_phlist.fits") f = pyfits.open("pt_src_simput.fits") cat = f["SRC_CAT"].data["SPECTRUM"] assert cat[0] == "pt_src1_phlist.fits[PHLIST,1]" assert cat[1] == "pt_src2_phlist.fits[PHLIST,1]" f.close() os.chdir(curdir) shutil.rmtree(tmpdir)
def plaw_fit(alpha_sim): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) nH_sim = 0.02 norm_sim = 1.0e-4 redshift = 0.01 exp_time = 5.0e4 area = 40000.0 inst_name = "hdxi" spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim) spec.apply_foreground_absorption(nH_sim) e = spec.generate_energies(exp_time, area) pt_src = PointSourceModel(30.0, 45.0, e.size) write_photon_list("plaw_model", "plaw_model", e.flux, pt_src.ra, pt_src.dec, e, clobber=True) instrument_simulator("plaw_model_simput.fits", "plaw_model_evt.fits", exp_time, inst_name, [30.0, 45.0], astro_bkgnd=None, instr_bkgnd_scale=0.0) inst = get_instrument_from_registry(inst_name) arf = AuxiliaryResponseFile(inst["arf"]) rmf = RedistributionMatrixFile(inst["rmf"]) os.system("cp %s ." % arf.filename) os.system("cp %s ." % rmf.filename) write_spectrum("plaw_model_evt.fits", "plaw_model_evt.pha", clobber=True) load_user_model(mymodel, "wplaw") add_user_pars("wplaw", ["nH", "norm", "redshift", "alpha"], [0.01, norm_sim * 0.8, redshift, 0.9], parmins=[0.0, 0.0, 0.0, 0.1], parmaxs=[10.0, 1.0e9, 10.0, 10.0], parfrozen=[False, False, True, False]) load_pha("plaw_model_evt.pha") set_stat("cstat") set_method("simplex") ignore(":0.5, 9.0:") set_model("wplaw") fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - nH_sim) < res.parmaxes[0] assert np.abs(res.parvals[1] - norm_sim) < res.parmaxes[1] assert np.abs(res.parvals[2] - alpha_sim) < res.parmaxes[2] os.chdir(curdir) shutil.rmtree(tmpdir)
instrument_simulator, make_background_file, simulate_spectrum from soxs.background.foreground import hm_astro_bkgnd from soxs.background.spectra import ConvolvedBackgroundSpectrum from soxs.spectra import Spectrum from soxs.response import AuxiliaryResponseFile, RedistributionMatrixFile from soxs.utils import soxs_files_path from numpy.random import RandomState from numpy.testing import assert_allclose import astropy.io.fits as pyfits import tempfile import os import shutil import numpy as np import astropy.units as u acisi_particle_bkgnd = Spectrum.from_file( os.path.join(soxs_files_path, "acisi_particle_bkgnd.h5")) def test_uniform_bkgnd_scale(): prng = RandomState(25) hdxi_arf = AuxiliaryResponseFile("xrs_hdxi_3x10.arf") events, event_params = make_background((50, "ks"), "lynx_hdxi", [30., 45.], foreground=True, instr_bkgnd=True, ptsrc_bkgnd=False, prng=prng) ncts = np.logical_and(events["energy"] >= 0.7, events["energy"] <= 2.0).sum() t_exp = event_params["exposure_time"] fov = (event_params["fov"]*60.0)**2 S = ncts/t_exp/fov dS = np.sqrt(ncts)/t_exp/fov foreground = ConvolvedBackgroundSpectrum.convolve(hm_astro_bkgnd, hdxi_arf) f_sum = foreground.get_flux_in_band(0.7, 2.0)[0]
def to_spectrum(self, fov): fov = parse_value(fov, "arcmin") flux = self.flux.value*fov*fov return Spectrum(self.ebins.value, flux)