From b967ea68127da33f8401386a3f6bce4b864107b5 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Mon, 28 Mar 2016 18:42:41 +0100 Subject: [PATCH] Added some tests. --- .coveragerc | 9 +++ README.rst | 1 + grbeams/__init__.py | 0 grbeams/__init__.pyc | Bin 0 -> 140 bytes grbeams/distributions.py | 89 ++++++++++++++++++++++++++++++ grbeams/distributions.pyc | Bin 0 -> 3559 bytes grbeams/distributions.py~ | 103 +++++++++++++++++++++++++++++++++++ grbeams/scenarios.py | 93 +++++++++++++++++++++++++++++++ grbeams/scenarios.pyc | Bin 0 -> 1761 bytes grbeams/scenarios.py~ | 63 +++++++++++++++++++++ poster/grbeams_poster.aux | 71 ++++++++++++++---------- requirements.txt | 3 + setup.py | 55 +++++++++++++++++++ tests/__init__.py | 0 tests/__init__.pyc | Bin 0 -> 138 bytes tests/test_distributions.py | 93 +++++++++++++++++++++++++++++++ tests/test_distributions.pyc | Bin 0 -> 4754 bytes tests/test_distributions.py~ | 28 ++++++++++ tests/test_scenarios.py | 43 +++++++++++++++ tests/test_scenarios.pyc | Bin 0 -> 2032 bytes tests/test_scenarios.py~ | 93 +++++++++++++++++++++++++++++++ 21 files changed, 714 insertions(+), 30 deletions(-) create mode 100644 .coveragerc create mode 100644 README.rst create mode 100644 grbeams/__init__.py create mode 100644 grbeams/__init__.pyc create mode 100644 grbeams/distributions.py create mode 100644 grbeams/distributions.pyc create mode 100644 grbeams/distributions.py~ create mode 100644 grbeams/scenarios.py create mode 100644 grbeams/scenarios.pyc create mode 100644 grbeams/scenarios.py~ create mode 100644 requirements.txt create mode 100755 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/__init__.pyc create mode 100755 tests/test_distributions.py create mode 100644 tests/test_distributions.pyc create mode 100755 tests/test_distributions.py~ create mode 100644 tests/test_scenarios.py create mode 100644 tests/test_scenarios.pyc create mode 100644 tests/test_scenarios.py~ diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..341cdd2 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,9 @@ +[run] +source = grbeams +omit = + */python?.?/* + */lib-python/?.?/*.py + */lib_pypy/_*.py + */site-packages/ordereddict.py + */site-packages/nose/* + */unittest2/* \ No newline at end of file diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..d65c390 --- /dev/null +++ b/README.rst @@ -0,0 +1 @@ +Hi Siong! diff --git a/grbeams/__init__.py b/grbeams/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/grbeams/__init__.pyc b/grbeams/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ce7275e87719d6182037a15dda659ac86d38c724 GIT binary patch literal 140 zcmZSn%*&N$^fN4(0SXv_v;z z`bDV)`Nf$f`9+zj#ro+*NvVms2s%DKGcU6wK3=b&vV;Su(=min(self.range)) and (emQTqT3Kr))3i>$eQA{5U+&zvD*~cg3wJdz}i+NT@cyg zRJv42TOw;urOTCck*pndR$CK*T-kSc{o3f~OP3xZ1vU}+C zbidtLB!{fSJ%xN(;z{(#YNreDq>`i+>P)I~nbq`e@lx(O1c+Zk*XtE(sCzxX%#&f6 zjdPkV@-!>cUay0pn04+MrrXEmP;Y0dFgoA1dQ>{&OKY^-KC*qShO^)L4&2u0BB4_? z(HwQHW-Ia!Fxb<%SO1Q}6D+Hv09fI&EEeRX2I1X>U)Xis7GcB?KgL(rG0Mo?9CX+% z{L;`q(-_^fy};OQqPvX5C6KEI2o3Tut#iMkV{_z|U0ARxl{Zfm`=xX-)vbhdLot5wC34nlT{ z&Ibwk{?7b8i850d-|La5T??IPx1)A+9o`S{D!_Xp#Az(-I%5kbAHv0l9p2;bSX3SV zh04=$uDs?$k8IgjeUlr10RRd_<%LupNpWP(bRo}FKCYorDS12W^^MrGUNZF4u}P14 zCkK09OLtt3^Gx=&)U*Pd%x11QA{c~tvoje85h_D~pMoiAqhnv`yK@I+{wNzf!q3DL z_oHUqA)hB~lolj}v!{-#Ll|Ucm{5^Os9w$X!=u~Q1j^JEG)D$njaEc|?KQuS>34cC zu=?U%)P^mn=NPzBKPLu1cJt~3;dByqWC*U5Qfb<_LJ(@RdhiQoRhaF{iS9X?DLbo$ zZx@fZt_9eeU?$;6oB8y%?j$dMhUN$%(dIO#u%gBoeTabvkX$(d)~i_j9E;0XoS?^a zT2A*Tx$(yiGggAnLRj{-S0NBdCvQKY0Ad;&SyS2`5zpNniGuzMG-fRkJ&fjMOtMCs zA+~;JnQ|Y>i3qP|$_i)7%fbvwJDi&--8Q-#Srm7b1@RiZ3L@p(P9V}H`jD(% zPnL18IVYg`3Nd-jsZ!Os$>7b-_4-fr8UiDP0r6~HOQ%nfYE_o(%A2kKhjd~pM2Yv~ zQLZZ^P(& zX(5uzT6J+nU*H@!QiWsY3F~J>vmb7I6amGENuPBjgw)N@=min(self.range)) and (efficiency=min(self.range)) and (efficiency max(self.rates): return 0 + return np.interp(rate.value, self.rates.value, self.pdf_data) + +# class Scenario(): +# """ +# Represents an observing scenario. +# """ + +# def __init__(self, rate_posterior_file, upper_limit=1e-4): +# """ +# A generic scenario class to contain the information +# about the observing scenario. + +# Parameters +# ---------- +# rate_posterior_file : str +# The filepath to the file containing the rates posterior. + +# upper_limit : float +# The upper limit on the posterior from the loudest event. +# """ +# posterior_data = np.loadtxt(rate_posterior_file) + +# self.bns_rate = self.posterior_data[:,1] *u.megaparsec**(-3) +# self.bns_rate_pdf = self.posterior_data[:,0] + +# # Should probably compute this directly +# self.upper_limit = upper_limit + +# def plot_posterior(self): +# self.compute_posteriors() +# fig, ax = plt.subplots(1) +# ax.plot(self.bns_rate, self.bns_rate_pdf, label="Posterior") + +# def comp_bns_rate_pdf(self, bns_rate): +# pdf_value = +# return pdf_value + +# from scipy.misc import factorial +# def rate_fit(x,C,T,b,n): +# return C*T*( (x+b)*T )**n * np.exp(-(x+b)*T) / factorial(n) + +# def fit_rate(x,y): +# popt,_ = optimize.curve_fit(rate_fit, x, y) +# return popt + +# def comp_grb_rate(self, efficiency, theta, bns_rate): +# """ +# Computes the GRB rate: +# Rgrb = epsilon*(1-cos(theta))*Rbns +# """ +# return efficiency*(1-np.cos(theta/180.0 * np.pi))*bns_rate + +# def cbc_rate_from_theta(self, grb_rate,theta,efficiency): +# """ +# Returns Rcbc = Rgrb / (1-cos(theta)) +# """ +# return grb_rate / ( efficiency*(1.-np.cos(theta * np.pi / 180)) ) diff --git a/grbeams/scenarios.pyc b/grbeams/scenarios.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f39f51608311f6792d06fc14ec0c609b0aa70b5e GIT binary patch literal 1761 zcmb_cOK%e~5T4DeZTbSWTsRqtLy%~yUN|6xP^ma@sHzP@kju*1bsDGplI<--DyQ;~ zIdMSnjgxMIP&q)Y^{nl&$KT9+og5Nh33b9kyONs9U{ca5ze&lj6C^6K|x}0ph!f z61>F<>BOgrjwir*cJf9{q!lBjauLfkt~`W(eb5^%lkx2~SqY0u!eCrvWyLP=F>xu= zN-a(4A4udpo4pRA=Qtg%K&Xi-*M-c~xh$ozDs~*+nUcn}G4Qb6I!3t6yV&Cl`b}1) zW4E!`x*(IWHKD_n=B zYu?`aLOq=iHt3pb$FuO@cNV(J{WOUNws#N;dQ zhJB0z#NWry^6N-DkAf{~1rN|Y0~Y+u@km6GucRo-@Vum$hMPDL-iKI%`QLg7xnwy& zz79IU#?IXKI+olv#OhOLLoCdfC0_NSob@XdlJ(K^BOi4?&8w`Og>1oJad(J)gkH>J Q@{-Y8PUF2`H`r?Y0>5{nv;Y7A literal 0 HcmV?d00001 diff --git a/grbeams/scenarios.py~ b/grbeams/scenarios.py~ new file mode 100644 index 0000000..17741fb --- /dev/null +++ b/grbeams/scenarios.py~ @@ -0,0 +1,63 @@ +from .distributions import Distribution + +class Scenario(): + """ + Represents an observing scenario. + """ + + def __init__(self, rate_posterior_file, upper_limit=1e-4): + """ + A generic scenario class to contain the information + about the observing scenario. + + Parameters + ---------- + rate_posterior_file : str + The filepath to the file containing the rates posterior. + + upper_limit : float + The upper limit on the posterior from the loudest event. + """ + posterior_data = np.loadtxt(rate_posterior_file) + + self.bns_rate = self.posterior_data[:,1] *u.megaparsec**(-3) + self.bns_rate_pdf = self.posterior_data[:,0] + + # Should probably compute this directly + self.upper_limit = upper_limit + + def compute_posteriors(self): + """ + + """ + + + def plot_posterior(self): + self.compute_posteriors() + fig, ax = plt.subplots(1) + ax.plot(self.bns_rate, self.bns_rate_pdf, label="Posterior") + + def comp_bns_rate_pdf(self, bns_rate): + pdf_value = np.interp(bns_rate, self.bns_rate, self.bns_rate_pdf) + return pdf_value + + from scipy.misc import factorial + def rate_fit(x,C,T,b,n): + return C*T*( (x+b)*T )**n * np.exp(-(x+b)*T) / factorial(n) + + def fit_rate(x,y): + popt,_ = optimize.curve_fit(rate_fit, x, y) + return popt + + def comp_grb_rate(self, efficiency, theta, bns_rate): + """ + Computes the GRB rate: + Rgrb = epsilon*(1-cos(theta))*Rbns + """ + return efficiency*(1-np.cos(theta/180.0 * np.pi))*bns_rate + + def cbc_rate_from_theta(self, grb_rate,theta,efficiency): + """ + Returns Rcbc = Rgrb / (1-cos(theta)) + """ + return grb_rate / ( efficiency*(1.-np.cos(theta * np.pi / 180)) ) diff --git a/poster/grbeams_poster.aux b/poster/grbeams_poster.aux index 1deed8f..d8a0182 100644 --- a/poster/grbeams_poster.aux +++ b/poster/grbeams_poster.aux @@ -1,36 +1,47 @@ \relax -\citation{rates_paper} -\citation{rates_paper} -\citation{scenarios} -\citation{scenarios} -\citation{scenarios} -\citation{scenarios} -\citation{*} -\bibstyle{plain} -\bibdata{sample} +\citation{2013PhRvL.111r1101C} +\citation{2014arXiv1409.8149C} +\citation{2008CQGra..25j5002B} +\citation{s6lowmass} +\citation{2013arXiv1304.0670L} +\citation{Gregory05} +\citation{s6lowmass} +\citation{2008CQGra..25j5002B} +\citation{s6lowmass} +\citation{2008CQGra..25j5002B} +\citation{2013arXiv1304.0670L} +\citation{2013arXiv1304.0670L} +\citation{Gregory05} +\citation{2013arXiv1304.0670L} +\citation{2013arXiv1304.0670L} +\citation{Gregory05} +\citation{2013arXiv1304.0670L} +\citation{2013arXiv1304.0670L} +\bibstyle{unsrt} +\bibdata{grbeams_poster} +\bibcite{2013PhRvL.111r1101C}{1} +\bibcite{2014arXiv1409.8149C}{2} +\bibcite{2008CQGra..25j5002B}{3} +\bibcite{s6lowmass}{4} +\bibcite{2013arXiv1304.0670L}{5} +\bibcite{Gregory05}{6} \newlabel{eq:rate2angle}{{1}{1}} -\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Figure caption\relax }}{1}} -\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Figure caption\relax }}{1}} -\newlabel{eq:loudestEventPosterior}{{5}{1}} -\newlabel{fig:}{{3a}{1}} -\newlabel{sub@fig:}{{(a)}{a}} -\newlabel{fig:}{{3b}{1}} -\newlabel{sub@fig:}{{(b)}{b}} -\@writefile{lof}{\contentsline {figure}{\numberline {3}{\ignorespaces S6 results\relax }}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(a)}{\ignorespaces {Rate Posterior}}}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(b)}{\ignorespaces {Jet Posterior}}}{1}} -\@writefile{lof}{\contentsline {figure}{\numberline {4}{\ignorespaces Figure caption\relax }}{1}} +\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Expected relative numbers of observed GRBs and binary coalescences for different distributions on the GRB beaming angle. Lines in the figure correspond to jet angle population means, while the $x$-axis shows the width of the distribution. All distributions are Gaussian, truncated at $(0, 90]$ degrees.\relax }}{1}} +\newlabel{fig:thetapopulation}{{1}{1}} +\newlabel{eq:transformationmarginalisation}{{3}{1}} +\newlabel{fig:S6ratePosterior}{{2a}{1}} +\newlabel{sub@fig:S6ratePosterior}{{(a)}{a}} +\newlabel{fig:S6JetPosterior}{{2b}{1}} +\newlabel{sub@fig:S6JetPosterior}{{(b)}{b}} +\@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces \emph {Left}: The binary neutron star merger rate posterior constructed from the 90\% rate upper limit (dashed line) reported in\nobreakspace {}\cite {s6lowmass} and the formalism described in\nobreakspace {}\cite {2008CQGra..25j5002B}. \emph {Right}: the jet angle posteriors obtained from equation\nobreakspace {}3\hbox {}. Different lines for the jet posterior correspond to the different priors on the GRB efficiency.\relax }}{1}} +\@writefile{lof}{\contentsline {figure}{\numberline {3}{\ignorespaces Rate posterior PDFs expected following the 2016 and 2022 observing scenarios described in\nobreakspace {}\cite {2013arXiv1304.0670L}, assuming the `realistic' rate of BNS coalescence from\nobreakspace {}\cite {2013arXiv1304.0670L} (solid vertical line) and using the Bayesian Poisson rate determination described in\nobreakspace {}\cite {Gregory05}.\relax }}{1}} +\newlabel{fig:jet_injection_2016}{{4a}{1}} +\newlabel{sub@fig:jet_injection_2016}{{(a)}{a}} +\newlabel{fig:jet_injection_2022}{{4b}{1}} +\newlabel{sub@fig:jet_injection_2022}{{(b)}{b}} +\@writefile{lof}{\contentsline {figure}{\numberline {4}{\ignorespaces Simulated jet angle posterior PDFs generated by setting $\theta =30^{\circ }$ and computing the corresponding ${{\mathcal R}_{\mathrm {grb}}}$ using $\epsilon =1.0$ and ${{\mathcal R}}\sim 10^{-6}$\tmspace +\thinmuskip {.1667em}Mpc$^{-3}$\tmspace +\thinmuskip {.1667em}yr$^{-1}$. \relax }}{1}} \newlabel{fig:}{{5a}{1}} \newlabel{sub@fig:}{{(a)}{a}} \newlabel{fig:}{{5b}{1}} \newlabel{sub@fig:}{{(b)}{b}} -\@writefile{lof}{\contentsline {figure}{\numberline {5}{\ignorespaces Example results for $\theta _{\rm jet}=30^{\circ }$ and binary coalescence rates in\nobreakspace {}\cite {scenarios} to derive a `simulated' GRB rate.\relax }}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(a)}{\ignorespaces {Rate Posterior}}}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(b)}{\ignorespaces {Jet Posterior}}}{1}} -\newlabel{fig:}{{6a}{1}} -\newlabel{sub@fig:}{{(a)}{a}} -\newlabel{fig:}{{6b}{1}} -\newlabel{sub@fig:}{{(b)}{b}} -\@writefile{lof}{\contentsline {figure}{\numberline {6}{\ignorespaces Results in ADE assuming ${{\mathcal R}_{\mathrm {grb}}}= 3\times 10^{-9}$\tmspace +\thinmuskip {.1667em}Mpc$^{-3}$yr$^{-1}$ and binary coalescence rates in\nobreakspace {}\cite {scenarios}.\relax }}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(a)}{\ignorespaces {Rate Posterior}}}{1}} -\@writefile{lof}{\contentsline {subfigure}{\numberline{(b)}{\ignorespaces {Jet Posterior}}}{1}} +\@writefile{lof}{\contentsline {figure}{\numberline {5}{\ignorespaces Results Expected in ADE, given\nobreakspace {}\cite {2013arXiv1304.0670L}.\relax }}{1}} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b5f41 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +astropy>=1.1.2 +numpy>=1.10 +scipy>=0.16 diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..7bcfbc8 --- /dev/null +++ b/setup.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +try: + from setuptools import setup +except ImportError: + from distutils.core import setup + + +with open('README.rst') as readme_file: + readme = readme_file.read() + +requirements = [ + # TODO: put package requirements here +] + +test_requirements = [ + # TODO: put package test requirements here +] + +setup( + name='grbeams', + version='0.1.0', + description="Short Gamma Ray Burst Beaming.", + long_description=readme, + author="James Clark", + author_email='james.clark@ligo.org', + url='https://github.com/astroclark/grbeams', + packages=[ + 'grbeams', + ], + package_dir={'grbeams': + 'grbeams'}, + include_package_data=True, + install_requires=requirements, + license="GNU GPL", + zip_safe=False, + keywords='grbeams', + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: ISC License (ISCL)', + 'Natural Language :: English', + "Programming Language :: Python :: 2", + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + ], + test_suite='tests', + #tests_require=test_requirements +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/__init__.pyc b/tests/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..07a4f5952ddd5b38baa9c6b65179a990e05e3db1 GIT binary patch literal 138 zcmZSn%*!Q|^)oD)0SXv_v;z z`bDV)`Nf$f`9+zj#ro+*NvVms#rh?w#U;i1@$s2?nI-Y@dIgmw96)_Gx%nxjIjMFa J(~E(a0RYQJ9t!{f literal 0 HcmV?d00001 diff --git a/tests/test_distributions.py b/tests/test_distributions.py new file mode 100755 index 0000000..2cbd977 --- /dev/null +++ b/tests/test_distributions.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_distributions +---------------------------------- + +Tests for `distributions` module. +""" + +import unittest +import numpy as np +from grbeams import distributions + + +class TestDistributions(unittest.TestCase): + + def setUp(self): + # Create a delta distribution with its spike at x=0.5 + self.deltadist = distributions.DeltaDistribution(x=0.5) + # Create a Uniform distribution over (0.5, 0.7) + self.uniformdist = distributions.UniformDistribution(range=(0.5, 0.7)) + # Create a Jeffrey's distribution + self.jeffrey = distributions.JeffreyDistribution() + + def test_delta_on_value(self): + # Test the delta distribution at the point of the spike. + self.assertEqual(self.deltadist.pdf(0.5), 1.0) + + def test_delta_off_value(self): + # Test he delta distribution off the point of the spike. + self.assertEqual(self.deltadist.pdf(0.1), 0) + + def test_delta_integral(self): + # Test that the whole distribution integrates to give 1 + x = np.linspace(-1, 1, 1001) + probs = [self.deltadist.pdf(place) for place in x] + self.assertEqual(sum(probs), 1) + + def test_uniform_in_range(self): + # Test that the probability is non-zero inside the distribution's range + self.assertGreater(self.uniformdist.pdf(0.6), 0) + + def test_uniform_out_range(self): + # Test that the probability is zero outside the range + self.assertEqual(self.uniformdist.pdf(0.1, 0),0) + + def test_uniform_integral(self): + # Check that the distribution integrates to 1 + + x = np.linspace(-1, 1, 1001) + probs = [self.deltadist.pdf(place) for place in x] + self.assertEqual(sum(probs), 1) + + def test_uniform_in_range(self): + # Test that the probability is non-zero inside the distribution's range + self.assertGreater(self.uniformdist.pdf(0.6), 0) + + def test_uniform_out_range(self): + # Test that the probability is zero outside the range + self.assertEqual(self.uniformdist.pdf(0.1),0) + + def test_uniform_integral(self): + # Check that the distribution integrates to 1 + x = np.linspace(-1, 1, 1001) + probs = np.array([self.uniformdist.pdf(place) for place in x]) + self.assertAlmostEqual(np.trapz(probs, x=x), 1) + + def test_uniform_edge(self): + # Check that the lower edge is included, but the upper edge is not + self.assertGreater(self.uniformdist.pdf(0.5), 0) + self.assertEqual(self.uniformdist.pdf(0.7), 0) + + def test_jeffrey_in_range(self): + # Check that the Jeffrey distribution has a value > 0 in its range + self.assertGreater(self.jeffrey.pdf(0.5),0) + + def test_jeffrey_out_range(self): + # Check that the Jeffrey distribution has a value > 0 in its range + self.assertEqual(self.jeffrey.pdf(-0.5),0) + self.assertEqual(self.jeffrey.pdf(1.5),0) + + def test_jeffrey_integral(self): + # Check that the distribution integrates to inf (it's non-normalisable) + x = np.linspace(0, 1, 1001) + probs = np.array([self.jeffrey.pdf(place) for place in x]) + self.assertAlmostEqual(np.trapz(probs, x=x), np.inf) + + def tearDown(self): + pass + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_distributions.pyc b/tests/test_distributions.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5335a9b68434a0b473c75d90ccb74d078b5dc43e GIT binary patch literal 4754 zcmc&&&2Jk;6o0$^NbESJd<0qu5Q>m|fCPyH2MPh23aLUqOwx*4z-E&jn+@w-cV`R) zC8u!b4F3ry{*_8dNZgRPzzN>(jpMJlP^sRs&dk1jGdnZy>-XNK^v7!blTZJ;b4bIh z#P|2PnTR`XZSFmad`diOGvR2T+9jb&l$5Dgp>~D5s7%_UqbhZHQiZ-FIwQK*Dc_eFH4Jzuuk6!qa|yfgHdMFT*dU6H&EtcCjkDC$-}uj~Uf<)_%tj~6H}rv zFGQK5k`NV&%0g6!nqV!k3NV>Mt=z)W5+5>+;C;91-FuXA(JQ zKF0|(jM=TR>iCJT-?xQtA$e$w&h5>6gD_Fhyu};+s3Rh(IzKE_JQ%^nAM6rjY4BB; z4D|ENHn1{1nxH@afa|C27)yzes=<^TVt; z*Ld&qDDYnM9w`dmlIxcV=Ma>35%5v2L#uNom{OpptU;MbKtsq0;xrJngEl8!WeRFv z<-v=w&gp;y-Sv~<{-7u*TDE6rWy=QE3HSPpaQ|~0sLOmFg@iog{R*f%IW?uVAF2y<9!Vo4gG<3dHn+y;)u1^S?7A1N;_QQt-RO#9aIa z)4zz~B>?sq#jgRjhd^GDg{r{vi061b=O|!<2mVa~(Z!l>qSO)DQ1ljA>Wpw}SPPF5 z*e%}pf${z@mUW^(jAefop@(+<(^a#s&?{Q{QHnd4ew5-`5P!98YxY^fX8+2qzsJH&SL=wJIAM7F5Km#%tH(mM}oo7 zu>=J;EtjA{F8<2}inBV*ce1b3yG%P*_nMc$$6yeoVNVBv#Ya;RxPJ-+fx=go!YqZ) zXa!j%KT3iTTvRWDhi?V-GP+mLq4IU|FtMo5O9kDa_%Yb6>erTQW&TyAUmDUCzE^71 zp-{b$E7YI~p)68v5JcG_O9_#=hP8k=Nhax_*FP55l6k|AUH5&C$$LEUMrgF0L7}3x hupZW5xSVpj>&^u4!MzPW1FL?+Yj~GSS3hoe{{S?&=3W2* literal 0 HcmV?d00001 diff --git a/tests/test_distributions.py~ b/tests/test_distributions.py~ new file mode 100755 index 0000000..2726c1d --- /dev/null +++ b/tests/test_distributions.py~ @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_acreroad_1420 +---------------------------------- + +Tests for `acreroad_1420` module. +""" + +import unittest + +from acreroad_1420 import acreroad_1420 + + +class TestAcreroad_1420(unittest.TestCase): + + def setUp(self): + pass + + def test_something(self): + pass + + def tearDown(self): + pass + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_scenarios.py b/tests/test_scenarios.py new file mode 100644 index 0000000..33a4be3 --- /dev/null +++ b/tests/test_scenarios.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_distributions +---------------------------------- + +Tests for `distributions` module. +""" + +import unittest +import numpy as np +import astropy.units as u +from grbeams import scenarios + + +class TestScenarios(unittest.TestCase): + + def setUp(self): + self.rateunits = u.megaparsec**-3*u.year**-1 + # Create a BNS distribution which is uniform + rates = np.linspace(0, 100, 100)*self.rateunits + pdf = np.ones(len(rates)) * (1.0/100) + + self.bnsuniform = scenarios.BNSDistribution(rates, pdf) + + def test_bns_negative_rate(self): + # A negative rate has no physical meaning, and should return a probability of zero. + self.assertEqual(self.bnsuniform.pdf(-1*self.rateunits), 0) + + def test_bns_excessive_rate(self): + # Return zero if the rate is higher than the pdf data provided. + self.assertEqual(self.bnsuniform.pdf(1000*self.rateunits), 0) + + def test_bns_in_range(self): + # Return a non-zero probability from the distro in range + self.assertGreater(self.bnsuniform.pdf(50*self.rateunits), 0) + + def tearDown(self): + pass + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_scenarios.pyc b/tests/test_scenarios.pyc new file mode 100644 index 0000000000000000000000000000000000000000..81838bc38edd2ddcc30fc890ceb4ec307fdbae4b GIT binary patch literal 2032 zcmcIl&rcIk5T4y_OIfO*6b(Tmi3c|^(!|7riBSU>Z#`_$#9kgt_q8r`f4J{eC4m$E zu^vr4c`*9TY$=$Sc(5$p*?GTa=KE&e;C;X5KRo*Jc0kj|!}ld#V<2LpKBPmjOJj%n zP%7P}zIU$I=&(-xh73I#FHqW~ev_QIM%tmnMH=EvoxTvA5xwjCq?age(2+}JE<0Mh z;AC**QZHCw=kQ9t)yAqgF}6$&CN{}4<3IZ6>|4x=qxIe+}LXv@%i5Q%#0V_yv=)`Kxps+P{g46E+c0JjJTrIDE36DQ(P0J zL18bb z>qDk`6ctfv^Z?-y9qXtR;>yVCNtRe+T})>Md=ib5%oNc;%ho&tn~Zm?vk)J$%Mjb$ z-tMKmijRZLz!cJ!2E1QR8$BLcri~p{c8YiyV1h)Lr_eg@^Hg`@C`otlHrzYTU02e?_-AQ)fB`dKNz}EZeVPl9 zkCcIBM+oNlyBFLBY{W99kukcouRctoameHFItvCU#Td3&!#fvrw_t~nD=@1vWY8ub zwPGeWuwpo4G#8*>fme!1fQBoud-dpU!^;ev{yfmeoWr|^r8&ZT409Loq(xMDd|hgg zSzbNX8<(*r8FHA7^!>k}U(^5MGN^piJ@uNsk$9o+k>T@ o$Y=>DlxxZP;Q;0eSVDhRk^Ef#uW@^6xGksUEITXS`g+Ux4L2{jpa1{> literal 0 HcmV?d00001 diff --git a/tests/test_scenarios.py~ b/tests/test_scenarios.py~ new file mode 100644 index 0000000..6b0f83e --- /dev/null +++ b/tests/test_scenarios.py~ @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_distributions +---------------------------------- + +Tests for `distributions` module. +""" + +import unittest +import numpy as np +from grbeams import scenarios + + +class TestDistributions(unittest.TestCase): + + def setUp(self): + # Create a delta distribution with its spike at x=0.5 + self.deltadist = distributions.DeltaDistribution(x=0.5) + # Create a Uniform distribution over (0.5, 0.7) + self.uniformdist = distributions.UniformDistribution(range=(0.5, 0.7)) + # Create a Jeffrey's distribution + self.jeffrey = distributions.JeffreyDistribution() + + def test_delta_on_value(self): + # Test the delta distribution at the point of the spike. + self.assertEqual(self.deltadist.pdf(0.5), 1.0) + + def test_delta_off_value(self): + # Test he delta distribution off the point of the spike. + self.assertEqual(self.deltadist.pdf(0.1), 0) + + def test_delta_integral(self): + # Test that the whole distribution integrates to give 1 + x = np.linspace(-1, 1, 1001) + probs = [self.deltadist.pdf(place) for place in x] + self.assertEqual(sum(probs), 1) + + def test_uniform_in_range(self): + # Test that the probability is non-zero inside the distribution's range + self.assertGreater(self.uniformdist.pdf(0.6), 0) + + def test_uniform_out_range(self): + # Test that the probability is zero outside the range + self.assertEqual(self.uniformdist.pdf(0.1, 0),0) + + def test_uniform_integral(self): + # Check that the distribution integrates to 1 + + x = np.linspace(-1, 1, 1001) + probs = [self.deltadist.pdf(place) for place in x] + self.assertEqual(sum(probs), 1) + + def test_uniform_in_range(self): + # Test that the probability is non-zero inside the distribution's range + self.assertGreater(self.uniformdist.pdf(0.6), 0) + + def test_uniform_out_range(self): + # Test that the probability is zero outside the range + self.assertEqual(self.uniformdist.pdf(0.1),0) + + def test_uniform_integral(self): + # Check that the distribution integrates to 1 + x = np.linspace(-1, 1, 1001) + probs = np.array([self.uniformdist.pdf(place) for place in x]) + self.assertAlmostEqual(np.trapz(probs, x=x), 1) + + def test_uniform_edge(self): + # Check that the lower edge is included, but the upper edge is not + self.assertGreater(self.uniformdist.pdf(0.5), 0) + self.assertEqual(self.uniformdist.pdf(0.7), 0) + + def test_jeffrey_in_range(self): + # Check that the Jeffrey distribution has a value > 0 in its range + self.assertGreater(self.jeffrey.pdf(0.5),0) + + def test_jeffrey_out_range(self): + # Check that the Jeffrey distribution has a value > 0 in its range + self.assertEqual(self.jeffrey.pdf(-0.5),0) + self.assertEqual(self.jeffrey.pdf(1.5),0) + + def test_jeffrey_integral(self): + # Check that the distribution integrates to inf (it's non-normalisable) + x = np.linspace(0, 1, 1001) + probs = np.array([self.jeffrey.pdf(place) for place in x]) + self.assertAlmostEqual(np.trapz(probs, x=x), np.inf) + + def tearDown(self): + pass + +if __name__ == '__main__': + unittest.main()