Coverage for tests/test_jointcal_cfht_minimal.py: 33%

61 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-10-12 03:47 -0700

1# This file is part of jointcal. 

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 <https://www.gnu.org/licenses/>. 

21 

22"""Test with a minimal catalog extracted from cfht.""" 

23import unittest 

24import os 

25 

26import lsst.geom 

27import lsst.utils 

28import lsst.pex.exceptions 

29 

30import jointcalTestBase 

31import lsst.jointcal.testUtils 

32 

33 

34# for MemoryTestCase 

35def setup_module(module): 

36 lsst.utils.tests.init() 

37 

38 

39@unittest.skipUnless(lsst.jointcal.testUtils.canRunTests(), "obs_cfht not available to use cfht_minimal.") 

40class JointcalTestCFHTMinimal(jointcalTestBase.JointcalTestBase, lsst.utils.tests.TestCase): 

41 """ 

42 Test with a stripped down CFHT dataset containing 3 stars, so by-hand 

43 calculation of metrics is possible. 

44 

45 See `notebooks/cfht_minimal_direct_calculation.ipynb` for numpy-based 

46 computations of chi2, etc. using this dataset. 

47 """ 

48 @classmethod 

49 def setUpClass(cls): 

50 cls.data_dir = os.path.join(lsst.utils.getPackageDir('jointcal'), 'tests/data') 

51 

52 def setUp(self): 

53 input_dir = os.path.join(self.data_dir, 'cfht_minimal') 

54 all_visits = [849375, 850587] 

55 

56 where = "instrument='MegaPrime' and tract=0 and skymap='discrete'" # and detector=12" 

57 inputCollections = ["singleFrame", "skymaps"] 

58 refcats = {"sdss_dr9_fink_v5b": os.path.join(input_dir, "sdss-dr9-fink-v5b.ecsv")} 

59 outputDataId = {'instrument': 'MegaPrime', 'tract': 0, 'skymap': 'discrete'} 

60 

61 self.setUp_base("lsst.obs.cfht.MegaPrime", "MegaPrime", 

62 input_dir=input_dir, 

63 all_visits=all_visits, 

64 inputCollections=inputCollections, 

65 refcats=refcats, 

66 where=where, 

67 refcatPath=input_dir, 

68 outputDataId=outputDataId, 

69 log_level="debug") 

70 test_config = os.path.join(os.path.dirname(__file__), 'config/cfht_minimal-config.py') 

71 self.configfiles.append(test_config) 

72 

73 def test_jointcalTask_2_visits_photometry(self): 

74 configOptions = {"doAstrometry": False, "photometryModel": "simpleFlux", 

75 "writeInitMatrix": True} 

76 

77 # NOTE: ndof==1 from 4 fit parameters (2 model, 2 fittedStar), and 

78 # 5 degrees-of-freedom (3 star measurements, with 2 reference stars). 

79 # This chi2 is exact, from calculations in cfht_minimal_direct_calculation.ipynb 

80 metrics = {'photometry_collected_refStars': 346, 

81 'photometry_prepared_refStars': 2, 

82 'photometry_matched_fittedStars': 2, 

83 'photometry_prepared_fittedStars': 2, 

84 'photometry_prepared_ccdImages': 2, 

85 'photometry_final_chi2': 2.336915, 

86 'photometry_final_ndof': 1 

87 } 

88 

89 outputVisits = {849375: (12,), 850587: (12,)} 

90 repo = self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

91 photometryOutputs=outputVisits) 

92 

93 # Check that the Hessian/gradient files were written. 

94 self.assertTrue(os.path.exists("photometry_preinit-0_r.MP9601-mat.txt")) 

95 os.remove("photometry_preinit-0_r.MP9601-mat.txt") 

96 self.assertTrue(os.path.exists("photometry_preinit-0_r.MP9601-grad.txt")) 

97 os.remove("photometry_preinit-0_r.MP9601-grad.txt") 

98 self.assertTrue(os.path.exists("photometry_postinit-0_r.MP9601-mat.txt")) 

99 os.remove("photometry_postinit-0_r.MP9601-mat.txt") 

100 self.assertTrue(os.path.exists("photometry_postinit-0_r.MP9601-grad.txt")) 

101 os.remove("photometry_postinit-0_r.MP9601-grad.txt") 

102 

103 # Check that the config was persisted, and that it matches the settings above 

104 config = lsst.jointcal.jointcal.JointcalConfig() 

105 for key, value in configOptions.items(): 

106 setattr(config, key, value) 

107 butler = lsst.daf.butler.Butler(repo, collections=['MegaPrime/tests/all']) 

108 output_config = butler.get('jointcal_config') 

109 self.assertEqual(output_config.photometryModel, config.photometryModel) 

110 self.assertEqual(output_config.doAstrometry, config.doAstrometry) 

111 self.assertEqual(output_config.writeInitMatrix, config.writeInitMatrix) 

112 

113 def test_jointcalTask_2_visits_photometry_magnitude(self): 

114 self.config = lsst.jointcal.jointcal.JointcalConfig() 

115 configOptions = {"doAstrometry": False, "photometryModel": "simpleMagnitude"} 

116 

117 # NOTE: ndof==1 from 4 fit parameters (2 model, 2 fittedStar), and 

118 # 5 degrees-of-freedom (3 star measurements, with 2 reference stars). 

119 metrics = {'photometry_collected_refStars': 346, 

120 'photometry_prepared_refStars': 2, 

121 'photometry_matched_fittedStars': 2, 

122 'photometry_prepared_fittedStars': 2, 

123 'photometry_prepared_ccdImages': 2, 

124 'photometry_final_chi2': 2.23008, 

125 'photometry_final_ndof': 1 

126 } 

127 

128 outputVisits = {849375: (12,), 850587: (12,)} 

129 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

130 photometryOutputs=outputVisits) 

131 

132 def test_jointcalTask_fails_raise(self): 

133 """Raise an exception if there is no data to process.""" 

134 self.configfiles.append(os.path.join(self.path, 'config/minSnr.py')) 

135 with self.assertRaisesRegex(RuntimeError, "No data to process"): 

136 self._runJointcalTest("lsst.obs.cfht.MegaPrime", "MegaPrime") 

137 

138 

139class MemoryTester(lsst.utils.tests.MemoryTestCase): 

140 pass 

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 unittest.main()