Coverage for tests/test_phot_util.py : 31%

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 astropy.units as u
30from lsst.utils import getPackageDir
31import lsst.pipe.base as pipeBase
32from lsst.afw.table import SimpleCatalog, GroupView
33from lsst.faro.utils.phot_repeat import (calcPhotRepeat,
34 calcPhotRepeatSample,
35 computeWidths,
36 getRandomDiffRmsInMmags,
37 getRandomDiff)
40DATADIR = os.path.join(getPackageDir('faro'), 'tests', 'data')
43class PhotUtilTest(unittest.TestCase):
44 """Test photometric utility functions."""
46 def load_data(self):
47 '''Helper to load data to process.'''
48 cat_file = 'matchedCatalogTract_0_i.fits.gz'
49 catalog = SimpleCatalog.readFits(os.path.join(DATADIR, cat_file))
50 matches = GroupView.build(catalog)
52 magKey = matches.schema.find('slot_PsfFlux_mag').key
53 nMatchesRequired = 2
55 def nMatchFilter(cat):
56 if len(cat) < nMatchesRequired:
57 return False
58 return np.isfinite(cat.get(magKey)).all()
60 return matches.where(nMatchFilter), magKey
62 def test_calcPhotRepeat(self):
63 """Test photometric repeatability for multiple realizations
64 of random pairs of visits."""
65 # Ensure measurements are deterministic
66 seed = 8675309
68 expected = 72.79576602821936 * u.mmag
69 matches, magKey = self.load_data()
70 result = calcPhotRepeat(matches, magKey, randomSeed=seed)
71 self.assertEqual(result['repeatability'], expected)
73 def test_calcPhotRepeatSample(self):
74 """Test photometric repeatability for one realization
75 of random pairs of visits."""
76 # Ensure measurements are deterministic
77 seed = 8675309
78 rng = np.random.default_rng(seed)
80 expected = pipeBase.Struct(rms=168.09034424742475,
81 iqr=72.11774706688344)
82 matches, magKey = self.load_data()
83 result = calcPhotRepeatSample(matches, magKey, rng=rng)
84 self.assertEqual(result.rms, expected.rms)
85 self.assertEqual(result.iqr, expected.iqr)
87 def test_computeWidths(self):
88 """Test RMS and the scaled inter-quartile range calculation."""
89 expected = (22.54717277176897, 1.8532527731320025)
90 mag = np.linspace(20, 25, 101)
91 result = computeWidths(mag)
92 self.assertEqual(result[0], expected[0])
93 self.assertEqual(result[1], expected[1])
95 def test_getRandomDiffRmsInMmags(self):
96 """Test random sampling of magnitude diffs."""
97 # Ensure measurements are deterministic
98 seed = 8675309
99 rng = np.random.default_rng(seed)
101 expected = -1237.436867076458
102 mag = np.linspace(20, 25, 101)
103 result = getRandomDiffRmsInMmags(mag, rng=rng)
104 self.assertEqual(result, expected)
106 def test_getRandomDiff(self):
107 """Test one random diff"""
108 # Ensure measurements are deterministic
109 seed = 8675309
110 rng = np.random.default_rng(seed)
112 expected = -1.75
113 mag = np.linspace(20, 25, 101)
114 result = getRandomDiff(mag, rng=rng)
115 self.assertEqual(result, expected)
118if __name__ == "__main__": 118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never true
119 unittest.main()