Coverage for tests/test_metrics.py: 17%

68 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-24 04:10 -0700

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 TestDiffMatched(TestCase): 

37 def setUp(self) -> None: 

38 super().setUp() 

39 self.band_default = "analysisTools" 

40 

41 def _testMatchedRefCoaddMetricDerived( 

42 self, 

43 type_metric: type[MatchedRefCoaddDiffTool], 

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

45 **kwargs, 

46 ): 

47 if suffixes_configure is None: 

48 suffixes_configure = [""] 

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

50 plotInfo["bands"] = [] 

51 for compute_chi in (False, True): 

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

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

54 tester.finalize() 

55 

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

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

58 for suffix in suffixes_configure: 

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

60 data = {} 

61 n_data = 10 

62 for key in keys: 

63 if key.endswith("xtendedness"): 

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

65 else: 

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

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

68 

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

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

71 

72 def testMatchedRefCoaddMetric(self): 

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

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

75 # Pass one at a time to test 

76 for kwarg in kwargs: 

77 kwargs_init = {kwarg: ""} 

78 tester = MatchedRefCoaddDiffTool(**kwargs_init) 

79 tester.validate() 

80 with self.assertRaises(KeyError): 

81 tester.configureMetrics() 

82 tester.finalize() 

83 tester.configureMetrics() 

84 # Failing to find any of the required keys 

85 with self.assertRaises(KeyError): 

86 tester({}) 

87 tester = MatchedRefCoaddDiffTool(**kwargs) 

88 tester.validate() 

89 with self.assertRaises(KeyError): 

90 tester.configureMetrics() 

91 tester.finalize() 

92 inputs = list(tester.getInputSchema()) 

93 n_input = len(inputs) 

94 self.assertGreater(n_input, 0) 

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

96 

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

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

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

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

101 

102 def testMatchedRefCoaddDiffMag(self): 

103 self._testMatchedRefCoaddMetricDerived( 

104 MatchedRefCoaddDiffMagTool, 

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

106 mag_y="cmodel", 

107 name_prefix="", 

108 unit="", 

109 ) 

110 

111 def testMatchedRefCoaddDiffColor(self): 

112 self._testMatchedRefCoaddMetricDerived( 

113 MatchedRefCoaddDiffColorTool, 

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

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

116 mag_y1="cmodel_err", 

117 mag_y2="cmodel_err", 

118 bands={ 

119 "g": "i", 

120 "u": "z", 

121 }, 

122 name_prefix="", 

123 unit="", 

124 ) 

125 

126 def testMatchedRefCoaddDiffPositionTool(self): 

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

128 self._testMatchedRefCoaddMetricDerived( 

129 MatchedRefCoaddDiffPositionTool, 

130 coord_meas=variable, 

131 coord_ref=variable, 

132 ) 

133 

134 

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

136 pass 

137 

138 

139def setup_module(module): 

140 lsst.utils.tests.init() 

141 

142 

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

144 lsst.utils.tests.init() 

145 main()