Coverage for tests/test_loadDiaCatalogs.py: 31%

65 statements  

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

1# This file is part of ap_association. 

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 

22import os 

23import numpy as np 

24import tempfile 

25import unittest 

26import yaml 

27 

28from lsst.ap.association import LoadDiaCatalogsTask, LoadDiaCatalogsConfig 

29from lsst.dax.apdb import Apdb, ApdbSql, ApdbTables 

30from lsst.utils import getPackageDir 

31import lsst.utils.tests 

32from utils_tests import makeExposure, makeDiaObjects, makeDiaSources, makeDiaForcedSources 

33 

34 

35def _data_file_name(basename, module_name): 

36 """Return path name of a data file. 

37 

38 Parameters 

39 ---------- 

40 basename : `str` 

41 Name of the file to add to the path string. 

42 module_name : `str` 

43 Name of lsst stack package environment variable. 

44 

45 Returns 

46 ------- 

47 data_file_path : `str` 

48 Full path of the file to load from the "data" directory in a given 

49 repository. 

50 """ 

51 return os.path.join(getPackageDir(module_name), "data", basename) 

52 

53 

54class TestLoadDiaCatalogs(unittest.TestCase): 

55 

56 def setUp(self): 

57 # Create an instance of random generator with fixed seed. 

58 rng = np.random.default_rng(1234) 

59 

60 self.db_file_fd, self.db_file = tempfile.mkstemp( 

61 dir=os.path.dirname(__file__)) 

62 self.addCleanup(os.remove, self.db_file) 

63 self.addCleanup(os.close, self.db_file_fd) 

64 

65 self.apdbConfig = ApdbSql.init_database(db_url="sqlite:///" + self.db_file) 

66 self.apdb = Apdb.from_config(self.apdbConfig) 

67 

68 self.exposure = makeExposure(False, False) 

69 

70 self.diaObjects = makeDiaObjects(20, self.exposure, rng) 

71 self.diaSources = makeDiaSources( 

72 100, self.diaObjects["diaObjectId"].to_numpy(), self.exposure, rng) 

73 self.diaForcedSources = makeDiaForcedSources( 

74 200, self.diaObjects["diaObjectId"].to_numpy(), self.exposure, rng) 

75 

76 self.dateTime = self.exposure.visitInfo.date 

77 self.apdb.store(self.dateTime.toAstropy(), 

78 self.diaObjects, 

79 self.diaSources, 

80 self.diaForcedSources) 

81 

82 # These columns are not in the DPDD, yet do appear in DiaSource.yaml. 

83 # We don't need to check them against the default APDB schema. 

84 self.ignoreColumns = ["band", "bboxSize", "isDipole", "flags"] 

85 

86 def testRun(self): 

87 """Test the full run method for the loader. 

88 """ 

89 diaLoader = LoadDiaCatalogsTask() 

90 result = diaLoader.run(self.exposure, self.apdb) 

91 

92 self.assertEqual(len(result.diaObjects), len(self.diaObjects)) 

93 self.assertEqual(len(result.diaSources), len(self.diaSources)) 

94 self.assertEqual(len(result.diaForcedSources), 

95 len(self.diaForcedSources)) 

96 

97 def testLoadDiaObjects(self): 

98 """Test that the correct number of diaObjects are loaded. 

99 """ 

100 diaLoader = LoadDiaCatalogsTask() 

101 region = diaLoader._getRegion(self.exposure) 

102 diaObjects = diaLoader.loadDiaObjects(region, 

103 self.apdb) 

104 self.assertEqual(len(diaObjects), len(self.diaObjects)) 

105 

106 def testLoadDiaForcedSources(self): 

107 """Test that the correct number of diaForcedSources are loaded. 

108 """ 

109 diaLoader = LoadDiaCatalogsTask() 

110 region = diaLoader._getRegion(self.exposure) 

111 diaForcedSources = diaLoader.loadDiaForcedSources( 

112 self.diaObjects, 

113 region, 

114 self.dateTime, 

115 self.apdb) 

116 self.assertEqual(len(diaForcedSources), len(self.diaForcedSources)) 

117 

118 def testLoadDiaSources(self): 

119 """Test that the correct number of diaSources are loaded. 

120 

121 Also check that they can be properly loaded both by location and 

122 ``diaObjectId``. 

123 """ 

124 diaConfig = LoadDiaCatalogsConfig() 

125 diaLoader = LoadDiaCatalogsTask(config=diaConfig) 

126 

127 region = diaLoader._getRegion(self.exposure) 

128 diaSources = diaLoader.loadDiaSources(self.diaObjects, 

129 region, 

130 self.dateTime, 

131 self.apdb) 

132 self.assertEqual(len(diaSources), len(self.diaSources)) 

133 

134 def test_apdbSchema(self): 

135 """Test that the default DiaSource schema from dax_apdb agrees with the 

136 column names defined here in ap_association/data/DiaSource.yaml. 

137 """ 

138 tableDef = self.apdb.tableDef(ApdbTables.DiaSource) 

139 apdbSchemaColumns = [column.name for column in tableDef.columns] 

140 

141 functorFile = _data_file_name("DiaSource.yaml", "ap_association") 

142 with open(functorFile) as yaml_stream: 

143 diaSourceFunctor = yaml.safe_load_all(yaml_stream) 

144 for functor in diaSourceFunctor: 

145 diaSourceColumns = [column for column in list(functor['funcs'].keys()) 

146 if column not in self.ignoreColumns] 

147 self.assertLess(set(diaSourceColumns), set(apdbSchemaColumns)) 

148 

149 

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

151 pass 

152 

153 

154def setup_module(module): 

155 lsst.utils.tests.init() 

156 

157 

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

159 lsst.utils.tests.init() 

160 unittest.main()