Coverage for tests/test_metrics.py: 19%

71 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-15 10:01 +0000

1# This file is part of analysis_tools. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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/>. 

21from __future__ import annotations 

22 

23from unittest import TestCase, main 

24 

25import lsst.utils.tests 

26import numpy as np 

27from lsst.analysis.tools.atools import ( 

28 MagnitudeTool, 

29 MatchedRefCoaddDiffColorTool, 

30 MatchedRefCoaddDiffMagTool, 

31 MatchedRefCoaddDiffPositionTool, 

32 MatchedRefCoaddDiffTool, 

33) 

34 

35 

36class MatchedRefCoaddDiffToolMinimal(MatchedRefCoaddDiffTool): 

37 """A bare-minimum implementation of a diff tool.""" 

38 

39 def get_key_flux_y(self) -> str: 

40 return self.mag_x 

41 

42 

43class TestDiffMatched(TestCase): 

44 def setUp(self) -> None: 

45 super().setUp() 

46 self.band_default = "analysisTools" 

47 

48 def _testMatchedRefCoaddMetricDerived( 

49 self, 

50 type_metric: type[MatchedRefCoaddDiffTool], 

51 suffixes_configure: list[str] | None = None, 

52 **kwargs, 

53 ): 

54 if suffixes_configure is None: 

55 suffixes_configure = [""] 

56 plotInfo = {key: "" for key in ("plotName", "run", "tableName")} 

57 plotInfo["bands"] = [] 

58 for compute_chi in (False, True): 

59 tester = type_metric(**kwargs, compute_chi=compute_chi) 

60 # tester.getInputSchema won't work properly before finalizing 

61 tester.finalize() 

62 

63 keys = set(k[0] for k in tester.getInputSchema()) 

64 self.assertGreater(len(keys), 0) 

65 for suffix in suffixes_configure: 

66 self.assertGreater(len(list(tester.configureMetrics(attr_suffix=suffix))), 0) 

67 data = {} 

68 n_data = 10 

69 for key in keys: 

70 if key.endswith("xtendedness"): 

71 values = np.linspace(0.0, 1.0, n_data) 

72 else: 

73 values = np.linspace(0.1, 15.0, n_data) 

74 data[key.format(band=self.band_default)] = values 

75 

76 output = tester(data, skymap=None, plotInfo=plotInfo) 

77 self.assertGreater(len(output), 0) 

78 

79 def testMatchedRefCoaddMetric(self): 

80 kwargs = {key: "" for key in ("unit", "name_prefix")} 

81 # The metric can now be set up with default kwargs 

82 # Pass one at a time to test 

83 for kwarg in kwargs: 

84 kwargs_init = {kwarg: ""} 

85 tester = MatchedRefCoaddDiffToolMinimal(**kwargs_init) 

86 tester.validate() 

87 with self.assertRaises(KeyError): 

88 tester.configureMetrics() 

89 tester.finalize() 

90 tester.configureMetrics() 

91 # Failing to find any of the required keys 

92 with self.assertRaises(KeyError): 

93 tester({}) 

94 tester = MatchedRefCoaddDiffToolMinimal(**kwargs) 

95 tester.validate() 

96 with self.assertRaises(KeyError): 

97 tester.configureMetrics() 

98 tester.finalize() 

99 inputs = list(tester.getInputSchema()) 

100 n_input = len(inputs) 

101 self.assertGreater(n_input, 0) 

102 self.assertGreater(len(list(tester.configureMetrics())), 0) 

103 

104 self.assertEqual(len(inputs), n_input) 

105 data = {key.format(band="analysisTools"): np.array([0.0]) for key, *_ in inputs} 

106 # There's no metric or plot so it just returns an empty dict 

107 self.assertEqual(len(tester(data)), 0) 

108 

109 def testMatchedRefCoaddDiffMag(self): 

110 self._testMatchedRefCoaddMetricDerived( 

111 MatchedRefCoaddDiffMagTool, 

112 fluxes={"cmodel": MagnitudeTool.fluxes_default.cmodel_err}, 

113 mag_y="cmodel", 

114 name_prefix="", 

115 unit="", 

116 ) 

117 

118 def testMatchedRefCoaddDiffColor(self): 

119 self._testMatchedRefCoaddMetricDerived( 

120 MatchedRefCoaddDiffColorTool, 

121 suffixes_configure=["_0", "_1"], 

122 fluxes={"cmodel": MagnitudeTool.fluxes_default.cmodel_err}, 

123 mag_y1="cmodel_err", 

124 mag_y2="cmodel_err", 

125 bands={ 

126 "g": "i", 

127 "u": "z", 

128 }, 

129 name_prefix="", 

130 unit="", 

131 ) 

132 

133 def testMatchedRefCoaddDiffPositionTool(self): 

134 for variable in ("x", "y"): 

135 self._testMatchedRefCoaddMetricDerived( 

136 MatchedRefCoaddDiffPositionTool, 

137 coord_meas=variable, 

138 coord_ref=variable, 

139 ) 

140 

141 

142class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

143 pass 

144 

145 

146def setup_module(module): 

147 lsst.utils.tests.init() 

148 

149 

150if __name__ == "__main__": 150 ↛ 151line 150 didn't jump to line 151, because the condition on line 150 was never true

151 lsst.utils.tests.init() 

152 main()