Coverage for tests/test_loadDiaCatalogs.py: 30%

70 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-22 03:19 -0700

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, ApdbSqlConfig, 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 np.random.seed(1234) 

58 

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

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

61 

62 self.apdbConfig = ApdbSqlConfig() 

63 self.apdbConfig.db_url = "sqlite:///" + self.db_file 

64 self.apdbConfig.dia_object_index = "baseline" 

65 self.apdbConfig.dia_object_columns = [] 

66 

67 Apdb.makeSchema(self.apdbConfig) 

68 self.apdb = ApdbSql(config=self.apdbConfig) 

69 

70 self.exposure = makeExposure(False, False) 

71 

72 self.diaObjects = makeDiaObjects(20, self.exposure) 

73 self.diaSources = makeDiaSources( 

74 100, 

75 self.diaObjects["diaObjectId"].to_numpy(), 

76 self.exposure) 

77 self.diaForcedSources = makeDiaForcedSources( 

78 200, 

79 self.diaObjects["diaObjectId"].to_numpy(), 

80 self.exposure) 

81 

82 self.dateTime = self.exposure.visitInfo.date 

83 self.apdb.store(self.dateTime, 

84 self.diaObjects, 

85 self.diaSources, 

86 self.diaForcedSources) 

87 

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

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

90 self.ignoreColumns = ["band", "bboxSize", "isDipole"] 

91 

92 def tearDown(self): 

93 os.close(self.db_file_fd) 

94 os.remove(self.db_file) 

95 

96 def testRun(self): 

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

98 """ 

99 diaLoader = LoadDiaCatalogsTask() 

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

101 

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

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

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

105 len(self.diaForcedSources)) 

106 

107 def testLoadDiaObjects(self): 

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

109 """ 

110 diaLoader = LoadDiaCatalogsTask() 

111 region = diaLoader._getRegion(self.exposure) 

112 diaObjects = diaLoader.loadDiaObjects(region, 

113 self.apdb) 

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

115 

116 def testLoadDiaForcedSources(self): 

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

118 """ 

119 diaLoader = LoadDiaCatalogsTask() 

120 region = diaLoader._getRegion(self.exposure) 

121 diaForcedSources = diaLoader.loadDiaForcedSources( 

122 self.diaObjects, 

123 region, 

124 self.dateTime, 

125 self.apdb) 

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

127 

128 def testLoadDiaSources(self): 

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

130 

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

132 ``diaObjectId``. 

133 """ 

134 diaConfig = LoadDiaCatalogsConfig() 

135 diaLoader = LoadDiaCatalogsTask(config=diaConfig) 

136 

137 region = diaLoader._getRegion(self.exposure) 

138 diaSources = diaLoader.loadDiaSources(self.diaObjects, 

139 region, 

140 self.dateTime, 

141 self.apdb) 

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

143 

144 def test_apdbSchema(self): 

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

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

147 """ 

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

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

150 

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

152 with open(functorFile) as yaml_stream: 

153 diaSourceFunctor = yaml.safe_load_all(yaml_stream) 

154 for functor in diaSourceFunctor: 

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

156 if column not in self.ignoreColumns] 

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

158 

159 

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

161 pass 

162 

163 

164def setup_module(module): 

165 lsst.utils.tests.init() 

166 

167 

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

169 lsst.utils.tests.init() 

170 unittest.main()