From 34e36c5a6c73ea4ab582d428314a3c4aece8b804 Mon Sep 17 00:00:00 2001 From: Eric Charles Date: Mon, 4 Jan 2021 10:28:14 -0800 Subject: [PATCH] Added unit tests --- comsky/utils.py | 9 +++--- tests/.coveragerc | 10 ++++++ tests/do_cover.sh | 1 + tests/test_utils.py | 79 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 tests/.coveragerc create mode 100755 tests/do_cover.sh create mode 100644 tests/test_utils.py diff --git a/comsky/utils.py b/comsky/utils.py index d957527..a391b9b 100644 --- a/comsky/utils.py +++ b/comsky/utils.py @@ -24,7 +24,7 @@ def SafeRVS(dist, nevt): ----- if dist is a float, this will just return an array of size `nevt` all set to `dist` """ - if np.isscalar(dist): + if np.isscalar(dist): #pragma: no cover return np.ones(nevt)*dist return dist.rvs(size=nevt) @@ -33,8 +33,10 @@ def SafeRVS(dist, nevt): def AddRingToMap(m, pix, nside, radius=30, width=5): """Given a healpix map and a sky direction (given here as the healpix pixel index), add the back-projected compton cone around that pixel to the map. - That is done by finding the pixels in a ring (with given radius and width) around the given direction, - and adding 1 to the map value of the pixels in that ring. (Might want to normalize it by the ring area eventually.) + That is done by finding the pixels in a ring (with given radius and width) + around the given direction, + and adding 1 to the map value of the pixels in that ring. + (Might want to normalize it by the ring area eventually.) Parameters ---------- @@ -294,7 +296,6 @@ def AddSampledEventsToMap(m, l, b, **kwargs): theta_cent, phi_cent = hp.pixelfunc.vec2ang(center_vecs.T) vecs = GetLociPoints(phi_cent, theta_cent, alpha_obs, phi_obs) ipix = hp.pixelfunc.vec2pix(nside, vecs[0], vecs[1], vecs[2]) - print(ipix.size) for ipix_ in ipix: m[ipix_] += 1 diff --git a/tests/.coveragerc b/tests/.coveragerc new file mode 100644 index 0000000..005ccab --- /dev/null +++ b/tests/.coveragerc @@ -0,0 +1,10 @@ +# .coveragerc to control coverage.py +[run] +omit= + *comsky/_version.py + *comsky/version.py + + +[html] +directory = cover + diff --git a/tests/do_cover.sh b/tests/do_cover.sh new file mode 100755 index 0000000..420068b --- /dev/null +++ b/tests/do_cover.sh @@ -0,0 +1 @@ +py.test --cov=comsky --cov-report=html diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..b1701d0 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,79 @@ +""" +Unit tests for PDF class +""" +import sys +import os +import numpy as np +import scipy.stats as sps +import healpy as hp +import unittest +import comsky + +NSIDE = 256 + +class UtilsTestCase(unittest.TestCase): + + def setUp(self): + """ + Make any objects that are used in multiple tests. + """ + self.files = [] + + def tearDown(self): + "Clean up any mock data files created by the tests." + for ff in self.files: + os.unlink(ff) + + def test_point_source(self): + nSrc = 1000 + l = 184.55746 + b = -5.78436 + mSrc = comsky.utils.MakePointSource(nSrc, NSIDE, l, b) + sampSrc = comsky.utils.MakePointSource(nSrc, NSIDE, l, b, sample=True) + + def test_galactic(self): + nIso = 1000 + mIso = comsky.utils.MakeIsotropicBackground(nIso, NSIDE) + sampIso = comsky.utils.MakeIsotropicBackground(nIso, NSIDE, sample=True) + + def test_isotropic(self): + nGal = 10000 + mGal = comsky.utils.MakeGalacticBackground(nGal, NSIDE) + sampGal = comsky.utils.MakeGalacticBackground(nGal, NSIDE, sample=True) + + def testPSFConvolve(self): + nSrc = 10000 + l = 0 + b = 90 + mPSF = comsky.utils.MakePointSource(nSrc, NSIDE, l, b) + almPSF = hp.sphtfunc.map2alm(mPSF) + vecNorth = np.array([0., 0., 1.]) + mGalTrue = np.zeros(hp.pixelfunc.nside2npix(NSIDE)) + sel = hp.query_disc(NSIDE, vecNorth, np.radians(92)) + mGalTrue[sel] += 1. + sel = hp.query_disc(NSIDE, vecNorth, np.radians(88)) + mGalTrue[sel] -= 1. + mGalConv = comsky.utils.ConvolveUsingAlm(mGalTrue, almPSF) + + def testSamplePoints(self): + npts = 100 + l = 0 + b = 0 + rad = 30 + phi_dist = sps.uniform(scale=2*np.pi) + theta, phi = hp.pixelfunc.lonlat2thetaphi(l, b) + pts = comsky.utils.SamplePointsFromRings(phi, theta, np.radians(rad), phi_dist) + + + def testAddRing(self): + nside = 256 + m = np.zeros((hp.pixelfunc.nside2npix(nside))) + l = 0 + b = 0 + theta, phi = hp.pixelfunc.lonlat2thetaphi(l, b) + ipix = hp.pixelfunc.ang2pix(nside, theta, phi) + comsky.utils.AddRingToMap(m, ipix, nside, radius=30, width=5) + + +if __name__ == '__main__': + unittest.main()