def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) pt_src = SimputSpectrum.from_spectrum("pt_src", spec, ra0, dec0) cat = SimputCatalog.from_source("pt_src_simput.fits", pt_src, overwrite=True) inst = get_instrument_from_registry("lynx_hdxi") inst["name"] = "hdxi_big_psf" inst["psf"] = ["gaussian", 5.0] add_instrument_to_registry(inst) instrument_simulator("pt_src_simput.fits", "pt_src_evt.fits", exp_time, "hdxi_big_psf", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) psf_scale = inst["psf"][1] dtheta = inst["fov"]*60.0/inst["num_pixels"] f = pyfits.open("pt_src_evt.fits") x = f["EVENTS"].data["X"] y = f["EVENTS"].data["Y"] f.close() scalex = np.std(x)*sigma_to_fwhm*dtheta scaley = np.std(y)*sigma_to_fwhm*dtheta assert (scalex - psf_scale)/psf_scale < 0.03 assert (scaley - psf_scale)/psf_scale < 0.03 os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) e = spec.generate_energies(exp_time, area, prng=prng) pt_src = PointSourceModel(ra0, dec0, e.size) write_photon_list("pt_src", "pt_src", e.flux, pt_src.ra, pt_src.dec, e, overwrite=True) inst = get_instrument_from_registry("hdxi") inst["name"] = "hdxi_big_psf" inst["psf"] = ["gaussian", 5.0] add_instrument_to_registry(inst) instrument_simulator("pt_src_simput.fits", "pt_src_evt.fits", exp_time, "hdxi_big_psf", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) psf_scale = inst["psf"][1] dtheta = inst["fov"] * 60.0 / inst["num_pixels"] f = pyfits.open("pt_src_evt.fits") x = f["EVENTS"].data["X"] y = f["EVENTS"].data["Y"] f.close() scalex = np.std(x) * sigma_to_fwhm * dtheta scaley = np.std(y) * sigma_to_fwhm * dtheta assert (scalex - psf_scale) / psf_scale < 0.01 assert (scaley - psf_scale) / psf_scale < 0.01 os.chdir(curdir) shutil.rmtree(tmpdir)
def test_thermal_from_spectrum(answer_store, answer_dir): prng = RandomState(89) tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) inst = get_instrument_from_registry(inst_name) simulate_spectrum(spec, inst["name"], exp_time, "thermal_model_spec_evt.pha", prng=prng) file_answer_testing("SPECTRUM", "thermal_model_spec_evt.pha", answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_nolines_thermal_from_spectrum(answer_store, answer_dir): prng = RandomState(101) tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) inst = get_instrument_from_registry(inst_name) simulate_spectrum(spec_nolines, inst["name"], exp_time, "nolines_thermal_model_evt.pha", prng=prng) file_answer_testing("SPECTRUM", "nolines_thermal_model_evt.pha", answer_store, answer_dir) os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_source(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) pt_src_pos = PointSourceModel(ra0, dec0) sim_cat = SimputCatalog.from_models("pt_src", "pt_src", spec, pt_src_pos, exp_time, area, prng=prng) sim_cat.write_catalog(overwrite=True) inst = get_instrument_from_registry("hdxi") inst["name"] = "hdxi_big_psf" inst["psf"] = ["gaussian", 5.0] add_instrument_to_registry(inst) instrument_simulator("pt_src_simput.fits", "pt_src_evt.fits", exp_time, "hdxi_big_psf", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) psf_scale = inst["psf"][1] dtheta = inst["fov"]*60.0/inst["num_pixels"] f = pyfits.open("pt_src_evt.fits") x = f["EVENTS"].data["X"] y = f["EVENTS"].data["Y"] f.close() scalex = np.std(x)*sigma_to_fwhm*dtheta scaley = np.std(y)*sigma_to_fwhm*dtheta assert (scalex - psf_scale)/psf_scale < 0.03 assert (scaley - psf_scale)/psf_scale < 0.03 os.chdir(curdir) shutil.rmtree(tmpdir)
from soxs.events import write_spectrum from soxs.instrument_registry import get_instrument_from_registry, \ make_simple_instrument def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" try: make_simple_instrument("acisi_cy19", "sq_acisi_cy19", 20.0, 2400) except KeyError: pass acis_spec = get_instrument_from_registry("sq_acisi_cy19") rmf = RedistributionMatrixFile(acis_spec["rmf"]) arf = AuxiliaryResponseFile(acis_spec['arf']) def mymodel(pars, x, xhi=None): dx = x[1] - x[0] xmid = x + 0.5 * dx wm = WabsModel(pars[0]) wabs = wm.get_absorb(xmid) plaw = pars[1] * dx * (xmid * (1.0 + pars[2]))**(-pars[3]) return wabs * plaw @requires_module("sherpa")
load_pha, ignore, fit, set_model, set_stat, set_method, \ get_fit_results from six import string_types from soxs.instrument import RedistributionMatrixFile, \ AuxiliaryResponseFile, instrument_simulator from soxs.events import write_spectrum from soxs.instrument_registry import get_instrument_from_registry ckms = clight.in_units("km/s").v def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" try: mucal_spec = get_instrument_from_registry("mucal") except KeyError: pass rmf = RedistributionMatrixFile(mucal_spec["rmf"]) arf = AuxiliaryResponseFile(mucal_spec['arf']) fit_model = TableApecModel(rmf.elo[0], rmf.ehi[-1], rmf.n_e) agen_var = TableApecModel(rmf.elo[0], rmf.ehi[-1], rmf.n_e, var_elem=["O", "Ca"], thermal_broad=True) def mymodel(pars, x, xhi=None): dx = x[1]-x[0] tm = TBabsModel(pars[0]) tbabs = tm.get_absorb(x+0.5*dx) bapec = fit_model.return_spectrum(pars[1], pars[2], pars[3], pars[4], velocity=pars[5])
get_fit_results from soxs.spectra import Spectrum from soxs.events import write_spectrum from soxs.instrument_registry import get_instrument_from_registry, \ make_simple_instrument from soxs.instrument import RedistributionMatrixFile, \ AuxiliaryResponseFile, instrument_simulator prng = 49 try: make_simple_instrument("aciss_cy19", "sq_aciss_cy19", 20.0, 2400) except KeyError: pass acis_spec = get_instrument_from_registry("sq_aciss_cy19") rmf = RedistributionMatrixFile(acis_spec["rmf"]) arf = AuxiliaryResponseFile(acis_spec['arf']) def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" def mymodel(pars, x, xhi=None): tm = TBabsModel(pars[0]) tbabs = tm.get_absorb(x) dx = x[1]-x[0] plaw = pars[1]*dx*(x*(1.0+pars[2]))**(-pars[3]) return tbabs*plaw
def test_annulus(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) r_in = 10.0 r_out = 30.0 e = spec.generate_energies(exp_time, area, prng=prng) ann_src = AnnulusModel(ra0, dec0, r_in, r_out, e.size, prng=prng) write_photon_list("ann", "ann", e.flux, ann_src.ra, ann_src.dec, e, overwrite=True) instrument_simulator("ann_simput.fits", "ann_evt.fits", exp_time, "hdxi", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) inst = get_instrument_from_registry("hdxi") arf = AuxiliaryResponseFile(inst["arf"]) cspec = ConvolvedSpectrum(spec, arf) ph_flux = cspec.get_flux_in_band(0.5, 7.0)[0].value S0 = ph_flux / (np.pi * (r_out**2 - r_in**2)) write_radial_profile("ann_evt.fits", "ann_evt_profile.fits", [ra0, dec0], 1.1 * r_in, 0.9 * r_out, 100, ctr_type="celestial", emin=0.5, emax=7.0, overwrite=True) load_data(1, "ann_evt_profile.fits", 3, ["RMID", "SUR_BRI", "SUR_BRI_ERR"]) set_stat("chi2") set_method("levmar") set_source("const1d.src") src.c0 = 0.8 * S0 fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - S0) < res.parmaxes[0] os.chdir(curdir) shutil.rmtree(tmpdir)
def test_beta_model(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) r_c = 20.0 beta = 1.0 exp_time = Quantity(500.0, "ks") e = spec.generate_energies(exp_time, area, prng=prng) beta_src = BetaModel(ra0, dec0, r_c, beta, e.size, prng=prng) write_photon_list("beta", "beta", e.flux, beta_src.ra, beta_src.dec, e, overwrite=True) instrument_simulator("beta_simput.fits", "beta_evt.fits", exp_time, "hdxi", [ra0, dec0], ptsrc_bkgnd=False, instr_bkgnd=False, foreground=False, prng=prng) inst = get_instrument_from_registry("hdxi") arf = AuxiliaryResponseFile(inst["arf"]) cspec = ConvolvedSpectrum(spec, arf) ph_flux = cspec.get_flux_in_band(0.5, 7.0)[0].value S0 = 3.0 * ph_flux / (2.0 * np.pi * r_c * r_c) write_radial_profile("beta_evt.fits", "beta_evt_profile.fits", [ra0, dec0], 0.0, 100.0, 200, ctr_type="celestial", emin=0.5, emax=7.0, overwrite=True) load_data(1, "beta_evt_profile.fits", 3, ["RMID", "SUR_BRI", "SUR_BRI_ERR"]) set_stat("chi2") set_method("levmar") set_source("beta1d.src") src.beta = 1.0 src.r0 = 10.0 src.ampl = 0.8 * S0 freeze(src.xpos) fit() set_covar_opt("sigma", 1.645) covar() res = get_covar_results() assert np.abs(res.parvals[0] - r_c) < res.parmaxes[0] assert np.abs(res.parvals[1] - beta) < res.parmaxes[1] assert np.abs(res.parvals[2] - S0) < res.parmaxes[2] 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 = (50.0, "ks") area = 40000.0 inst_name = "hdxi" spec = Spectrum.from_powerlaw(alpha_sim, redshift, norm_sim) spec.apply_foreground_absorption(nH_sim, model="tbabs") e = spec.generate_energies(exp_time, area, prng=prng) 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, overwrite=True) instrument_simulator("plaw_model_simput.fits", "plaw_model_evt.fits", exp_time, inst_name, [30.0, 45.0], instr_bkgnd=False, ptsrc_bkgnd=False, foreground=False, prng=prng) inst = get_instrument_from_registry(inst_name) arf = AuxiliaryResponseFile(inst["arf"]) rmf = RedistributionMatrixFile(inst["rmf"]) os.system("cp %s ." % arf.filename) convert_rmf(rmf.filename) write_spectrum("plaw_model_evt.fits", "plaw_model_evt.pha", overwrite=True) load_user_model(mymodel, "tplaw") add_user_pars("tplaw", ["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.6, 8.0:") set_model("tplaw") 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)
get_fit_results from soxs.spectra import Spectrum from soxs.events import write_spectrum from soxs.instrument_registry import get_instrument_from_registry, \ make_simple_instrument from soxs.instrument import RedistributionMatrixFile, \ AuxiliaryResponseFile, instrument_simulator prng = 49 try: make_simple_instrument("chandra_aciss_cy20", "sq_aciss_cy20", 20.0, 2400) except KeyError: pass acis_spec = get_instrument_from_registry("sq_aciss_cy20") rmf = RedistributionMatrixFile(acis_spec["rmf"]) arf = AuxiliaryResponseFile(acis_spec['arf']) def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" def mymodel(pars, x, xhi=None): tm = TBabsModel(pars[0]) tbabs = tm.get_absorb(x) dx = x[1]-x[0] plaw = pars[1]*dx*(x*(1.0+pars[2]))**(-pars[3]) return tbabs*plaw
from six import string_types from soxs.instrument import RedistributionMatrixFile, \ AuxiliaryResponseFile, instrument_simulator from soxs.events import write_spectrum from soxs.instrument_registry import get_instrument_from_registry ckms = clight.in_units("km/s").v def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" try: mucal_spec = get_instrument_from_registry("lynx_lxm") except KeyError: pass rmf = RedistributionMatrixFile(mucal_spec["rmf"]) arf = AuxiliaryResponseFile(mucal_spec['arf']) fit_model = TableApecModel(rmf.elo[0], rmf.ehi[-1], rmf.n_e) agen_var = TableApecModel(rmf.elo[0], rmf.ehi[-1], rmf.n_e, var_elem=["O", "Ca"], thermal_broad=True) def mymodel(pars, x, xhi=None): dx = x[1] - x[0]