Coverage for tests/test_tex_util.py : 33%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of <REPLACE WHEN RENAMED>.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""Unit tests for the metrics measurement system.
23"""
25import unittest
26import os
27import numpy as np
28import operator
29import astropy.units as u
31from lsst.utils import getPackageDir
32from lsst.afw.table import SimpleCatalog, GroupView
33from lsst.faro.utils.coord_util import averageRaFromCat, averageDecFromCat
34from lsst.faro.utils.tex import (correlation_function_ellipticity,
35 select_bin_from_corr,
36 medianEllipticity1ResidualsFromCat,
37 medianEllipticity2ResidualsFromCat)
39DATADIR = os.path.join(getPackageDir('faro'), 'tests', 'data')
42class TEXUtilTest(unittest.TestCase):
43 """Test TEX utility functions."""
45 def load_data(self):
46 '''Helper to load data to process.'''
47 cat_file = 'matchedCatalogTract_0_i.fits.gz'
48 cat = SimpleCatalog.readFits(os.path.join(DATADIR, cat_file))
50 selection = np.isfinite(cat.get('e1')) & np.isfinite(cat.get('e2'))
52 return cat.subset(selection).copy(deep=True)
54 def test_correlation_function_ellipticity(self):
55 """Test correlation function calculation."""
56 expected_r = 4.394963986052715*u.arcmin
57 expected_xip = 0.00127 # The reduced precision is being looked at: DM-29030
58 expected_xip_err = 0.0004931737255066678
60 cat = self.load_data()
61 matches = GroupView.build(cat)
63 ra = matches.aggregate(averageRaFromCat) * u.radian
64 dec = matches.aggregate(averageDecFromCat) * u.radian
66 e1_res = matches.aggregate(medianEllipticity1ResidualsFromCat)
67 e2_res = matches.aggregate(medianEllipticity2ResidualsFromCat)
69 result = correlation_function_ellipticity(ra, dec, e1_res, e2_res, bin_slop=0)
70 self.assertTrue(u.isclose(np.mean(result[0]), expected_r))
71 self.assertTrue(u.isclose(np.mean(result[1]), expected_xip, atol=0.00001*u.dimensionless_unscaled))
72 self.assertTrue(u.isclose(np.mean(result[2]), expected_xip_err))
74 def test_select_bin_from_corr(self):
75 """Test selection of angular range from correlation function."""
77 # Test less than or equal to logic
78 expected = (4.666666666666667, 1.3820881233139908)
79 radius = np.arange(1, 11) * u.arcmin
80 xip = radius.value**2
81 xip_err = np.sqrt(radius.value)
82 result = select_bin_from_corr(radius, xip, xip_err, radius=3.*u.arcmin, operator=operator.le)
83 self.assertEqual(result, expected)
85 # Test greater than or equal to logic
86 expected = (47.5, 2.506758077978876)
87 radius = np.arange(1, 11) * u.arcmin
88 xip = radius.value**2
89 xip_err = np.sqrt(radius.value)
90 result = select_bin_from_corr(radius, xip, xip_err, radius=3.*u.arcmin, operator=operator.ge)
91 self.assertEqual(result, expected)
93 def test_medianEllipticity1ResidualsFromCat(self):
94 """Test ellipticity residuals e1."""
95 expected = -0.004235647618770565
96 cat = self.load_data()
97 result = medianEllipticity1ResidualsFromCat(cat)
98 self.assertEqual(result, expected)
100 def test_medianEllipticity2ResidualsFromCat(self):
101 """Test ellipticity residuals e2."""
102 expected = 0.0008848644793033184
103 cat = self.load_data()
104 result = medianEllipticity2ResidualsFromCat(cat)
105 self.assertEqual(result, expected)
108if __name__ == "__main__": 108 ↛ 109line 108 didn't jump to line 109, because the condition on line 108 was never true
109 unittest.main()