Coverage for tests/test_tex.py: 27%
47 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 19:03 -0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 19:03 -0800
1# LSST Data Management System
2# Copyright 2012-2017 LSST Corporation.
3#
4# This product includes software developed by the
5# LSST Project (http://www.lsst.org/).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the LSST License Statement and
18# the GNU General Public License along with this program. If not,
19# see <http://www.lsstcorp.org/LegalNotices/>.
20#
23import operator
24import unittest
26import numpy as np
27import numpy.random as random
29import astropy.units as u
31import lsst.utils
32from lsst.validate.drp.calcsrd.tex import select_bin_from_corr, correlation_function_ellipticity
35class TexCalculations(lsst.utils.tests.TestCase):
36 """Test calculation of TEx ellipticity residuals calculations."""
38 def testSelectBinFromCorr(self):
39 """Does select_bin_from_corr correctly return only and all the bins that satisfy condition.
41 This test is meant for maintaining this consistency of performance.
42 These reference *_xip, *_xip_errs were computed based on the above arrays
43 and running through Treecorr wrapper code here manually.
44 It's not testing original correctness of Treecorr.
45 """
46 r = np.array([0.1, 0.2, 0.5, 0.8, 1.0, 1.2, 1.5, 2.0, 2.5])
47 xip = 1e-5 * np.array([0.9, 1.1, 1.5, 0.02, 1.2, 10.0, 5.0, 2.5, 3.0])
48 xip_err = 1e-6 * np.array([1, 1, 1, 1, 1, 2, 2, 2, 2])
50 exp_all_avg_xip, exp_all_avg_xip_err = np.average(xip), np.average(xip_err)
51 obs_all_avg_xip, obs_all_avg_xip_err = \
52 select_bin_from_corr(r, xip, xip_err, radius=0, operator=operator.gt)
53 self.assertFloatsAlmostEqual(exp_all_avg_xip, obs_all_avg_xip, rtol=1e-7)
54 self.assertFloatsAlmostEqual(exp_all_avg_xip_err, obs_all_avg_xip_err, rtol=1e-7)
56 exp_rlt2_avg_xip, exp_rlt2_avg_xip_err = 2.8171429e-05, 1.2857143e-06
57 obs_rlt2_avg_xip, obs_rlt2_avg_xip_err = \
58 select_bin_from_corr(r, xip, xip_err, radius=2, operator=operator.lt)
59 self.assertFloatsAlmostEqual(exp_rlt2_avg_xip, obs_rlt2_avg_xip, rtol=1e-7)
60 self.assertFloatsAlmostEqual(exp_rlt2_avg_xip_err, obs_rlt2_avg_xip_err, rtol=1e-7)
62 exp_rge1_avg_xip, exp_rge1_avg_xip_err = 4.3400000e-05, 1.8000000e-06
63 obs_rge1_avg_xip, obs_rge1_avg_xip_err = \
64 select_bin_from_corr(r, xip, xip_err, radius=1, operator=operator.ge)
65 self.assertFloatsAlmostEqual(exp_rge1_avg_xip, obs_rge1_avg_xip, rtol=1e-7)
66 self.assertFloatsAlmostEqual(exp_rge1_avg_xip_err, obs_rge1_avg_xip_err, rtol=1e-7)
68 def testEllipticityResidualCorr(self):
69 """Does the correlation function correctly compute for a random field?
71 Our goal is mostly to check that we're calling this function correctly.
72 Tests of the deeper performance are implicitly outsourced to TreeCorr.
74 Use same approach as TreeCorr galaxy-galaxy test from
75 https://github.com/rmjarvis/TreeCorr/blob/releases/3.3/tests/test_gg.py
77 Which in turn references
78 http://adsabs.harvard.edu/abs/2002A%26A...389..729S
79 """
80 # Seed was arbitrarily chosen.
81 rand_seed = 1238625876
82 random.seed(rand_seed)
83 # Yes, a million. n this high and l this large
84 # gets xip within 1e-7 absolute of the analytic limit.
85 # Takes 25 seconds to run on an early-2015 MacBook Air 2.2 GHz Intel Core i7
86 n = 1000000
87 ll = 500 * u.arcmin
88 ra = ((random.random_sample(n)-0.5) * ll).to(u.rad)
89 dec = ((random.random_sample(n)-0.5) * ll).to(u.rad)
91 r0 = 10 * u.arcmin
92 gamma0 = 0.05
94 # Ignoring spherical geometry cos(dec) term
95 x, y = ra.to(u.arcmin), dec.to(u.arcmin)
96 r2 = (x**2 + y**2)/r0**2
98 g1 = -gamma0 * np.exp(-r2/2) * (x**2-y**2)/r0**2
99 g2 = -gamma0 * np.exp(-r2/2) * (2*x*y)/r0**2
101 obs_r, obs_xip, obs_xip_err = \
102 correlation_function_ellipticity(ra, dec, g1, g2)
104 r = obs_r
105 prefactor = np.pi/16 * gamma0**2 * (r0/ll)**2 * np.exp(-0.25*(r/r0)**2)
106 exp_xip = prefactor * (r**4 - 16*r**2 * r0**2 + 32*r0**4)/r0**4
108 self.assertFloatsAlmostEqual(exp_xip, obs_xip, atol=1e-7, rtol=1e-1)
110 # 2017-08-05 MWV:
111 # I don't know how to calculate the expected xip_err
112 # so there's presently no test for that.
115def setup_module(module):
116 lsst.utils.tests.init()
119if __name__ == "__main__": 119 ↛ 120line 119 didn't jump to line 120, because the condition on line 119 was never true
120 lsst.utils.tests.init()
121 unittest.main()