Coverage for tests/test_metrics.py: 21%

55 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-21 19:24 +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 MatchedRefCoaddDiffMagMetric, 

30 MatchedRefCoaddDiffMetric, 

31 MatchedRefCoaddDiffPositionMetric, 

32) 

33 

34 

35class TestDiffMatched(TestCase): 

36 def setUp(self) -> None: 

37 super().setUp() 

38 self.band_default = "analysisTools" 

39 

40 def _testMatchedRefCoaddMetricDerived(self, type_metric: type[MatchedRefCoaddDiffMetric], **kwargs): 

41 for compute_chi in (False, True): 

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

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

44 tester.finalize() 

45 

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

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

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

49 data = {key.format(band=self.band_default): np.arange(5) for key in keys} 

50 output = tester(data) 

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

52 

53 def testMatchedRefCoaddMetric(self): 

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

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

56 # Pass one at a time to test 

57 for kwarg in kwargs: 

58 kwargs_init = {kwarg: ""} 

59 tester = MatchedRefCoaddDiffMetric(**kwargs_init) 

60 tester.validate() 

61 with self.assertRaises(KeyError): 

62 tester.configureMetrics() 

63 tester.finalize() 

64 tester.configureMetrics() 

65 # Failing to find any of the required keys 

66 with self.assertRaises(KeyError): 

67 tester({}) 

68 tester = MatchedRefCoaddDiffMetric(**kwargs) 

69 tester.validate() 

70 with self.assertRaises(KeyError): 

71 tester.configureMetrics() 

72 tester.finalize() 

73 inputs = list(tester.getInputSchema()) 

74 n_input = len(inputs) 

75 self.assertGreater(n_input, 0) 

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

77 

78 self.assertEquals(len(inputs), n_input) 

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

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

81 self.assertEquals(len(tester(data)), 0) 

82 

83 def testMatchedRefCoaddDiffMagMetric(self): 

84 self._testMatchedRefCoaddMetricDerived( 

85 MatchedRefCoaddDiffMagMetric, 

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

87 mag_y="cmodel", 

88 name_prefix="", 

89 unit="", 

90 ) 

91 

92 def testMatchedRefCoaddDiffPositionMetric(self): 

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

94 self._testMatchedRefCoaddMetricDerived(MatchedRefCoaddDiffPositionMetric, variable=variable) 

95 

96 

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

98 pass 

99 

100 

101def setup_module(module): 

102 lsst.utils.tests.init() 

103 

104 

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

106 lsst.utils.tests.init() 

107 main()