def __init__(self, psf_file_path): """Constructor. """ logger.info("Reading PSF data from %s..." % psf_file_path) self.hdu_list = fits.open(psf_file_path) self.hdu_list.info() _data = self.hdu_list["PSF"].data W = _data["W"] sigma = _data["SIGMA"] N = _data["N"] r_c = _data["R_C"] eta = _data["ETA"] self.__params = (W, sigma, N, r_c, eta) # Tabulate the actual PSF values. _r = numpy.linspace(0, self.MAX_RADIUS, 250) _y = gauss_king(_r, *self.__params) fmt = dict(xname="r", xunits="arcsec", yname="PSF", yunits="sr$^{-1}$") xInterpolatedUnivariateSpline.__init__(self, _r, _y, k=2, **fmt) # Include the solid angle for the actual underlying random generator. _y *= 2 * numpy.pi * _r fmt = dict(rvname="r", rvunits="arcsec", pdfname="$2 \\pi r \\times$ PSF", pdfunits="") self.generator = xUnivariateGenerator(_r, _y, k=1, **fmt) # Finally, calculate the self.eef, self.hew = self.build_eef() logger.info(self)
def build_energy_integral(self, emin=None, emax=None): """Build the energy-integrated count spectrum, i.e. .. math:: \\int_{E_{\\rm min}}^{E_{\\rm max}} \\mathcal{C}(E, t) dE \\quad [\\text{Hz}]. The output is stored in the form of a xUnivariateGenerator, featuring all the spline facilities, along with the capability of extracting random numbers. """ if emin is None: emin = self.ymin() if emax is None: emax = self.ymax() _x = self.x _y = numpy.array([self.vslice(_t).integral(emin, emax) for _t in _x]) fmt = dict(rvname=self.xname, rvunits=self.xunits, pdfname='Energy-integrated (%.2f--%.2f keV) spectrum' %\ (emin, emax), pdfunits='Hz') return xUnivariateGenerator(_x, _y, **fmt)
def build_time_integral(self, tmin=None, tmax=None): """Build the time-integrated count spectrum, i.e. .. math:: \\int_{t_{\\rm min}}^{t_{\\rm max}} \\mathcal{C}(E, t) dt \\quad [\\text{keV}^{-1}]. The output is stored in the form of a xUnivariateGenerator, featuring all the spline facilities, along with the capability of extracting random numbers. """ if tmin is None: tmin = self.xmin() if tmax is None: tmax = self.xmax() _x = self.y _y = numpy.array([self.hslice(_E).integral(tmin, tmax) for _E in _x]) fmt = dict(rvname=self.yname, rvunits=self.yunits, pdfname='Time-integrated (%d--%d s) spectrum' %\ (tmin, tmax), pdfunits='keV$^{-1}$') return xUnivariateGenerator(_x, _y, **fmt)
def test_power_law(self, size=1000000, phase=0., index=2.): """ """ _x = numpy.linspace(self.emin, self.emax, 100) _y = _x**(-index) generator = xUnivariateGenerator(_x, _y) energy = generator.rvs(size) visibility = self.modf(energy)*self.polarization_degree(energy) phi = self.generator.rvs_phi(visibility, phase) binning = numpy.linspace(0, 2*numpy.pi, 100) hist = plt.hist(phi, bins=binning) fit_results = self.generator.fit_histogram(hist) mean_energy = numpy.mean(energy) mu_effective = self.modf.weighted_average(energy) fit_results.set_polarization(mu_effective) fit_degree = fit_results.polarization_degree exp_degree = (self.polarization_degree(energy)*self.modf(energy))\ .sum()/self.modf(energy).sum() delta = abs(fit_degree - exp_degree)/exp_degree msg = 'delta = %.3f' % delta self.assertTrue(delta < 0.05, msg)
import os import numpy from ximpol import XIMPOL_DOC_FIGURES from ximpol.irf import load_irfs from ximpol.utils.matplotlib_ import save_current_figure from ximpol.utils.matplotlib_ import pyplot as plt from ximpol.core.rand import xUnivariateGenerator from ximpol.core.spline import xInterpolatedUnivariateSplineLinear from ximpol.core.spline import xInterpolatedBivariateSplineLinear from ximpol.srcmodel.spectrum import power_law, xCountSpectrum OUTPUT_FOLDER = XIMPOL_DOC_FIGURES _x = numpy.linspace(-5., 5., 100) _y = (1./numpy.sqrt(2*numpy.pi))*numpy.exp(-_x**2/2.) pdf = xUnivariateGenerator(_x, _y) print pdf.norm() plt.figure('pdf') pdf.plot(show=False) plt.figure('ppf') pdf.ppf.plot(show=False) print numpy.random.sample(10) plt.show()
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os import numpy from ximpol import XIMPOL_DOC_FIGURES from ximpol.irf import load_irfs from ximpol.utils.matplotlib_ import save_current_figure from ximpol.utils.matplotlib_ import pyplot as plt from ximpol.core.rand import xUnivariateGenerator from ximpol.core.spline import xInterpolatedUnivariateSplineLinear from ximpol.core.spline import xInterpolatedBivariateSplineLinear from ximpol.srcmodel.spectrum import power_law, xCountSpectrum OUTPUT_FOLDER = XIMPOL_DOC_FIGURES _x = numpy.linspace(-5., 5., 100) _y = (1. / numpy.sqrt(2 * numpy.pi)) * numpy.exp(-_x**2 / 2.) pdf = xUnivariateGenerator(_x, _y) print pdf.norm() plt.figure('pdf') pdf.plot(show=False) plt.figure('ppf') pdf.ppf.plot(show=False) print numpy.random.sample(10) plt.show()