Coverage for tests/test_pipelines.py: 14%

123 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-13 08:09 +0000

1# This file is part of ap_pipe. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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/>. 

21 

22import itertools 

23import tempfile 

24import unittest 

25 

26import lsst.daf.butler.tests as butlerTests 

27import lsst.pipe.base 

28from lsst.pipe.base.tests.pipelineStepTester import PipelineStepTester # Can't use fully-qualified name 

29import lsst.utils 

30import lsst.utils.tests 

31 

32from lsst.resources import ResourcePath 

33 

34 

35class PipelineDefintionsTestSuite(lsst.utils.tests.TestCase): 

36 """Tests of the self-consistency of our pipeline definitions. 

37 """ 

38 def setUp(self): 

39 self.path = ResourcePath("eups://ap_pipe/pipelines/", forceDirectory=True) 

40 # Each pipeline file should have a subset that represents it in 

41 # higher-level pipelines. 

42 self.synonyms = {"ApPipe.yaml": "apPipe", 

43 "ApPipeWithIsrTaskLSST.yaml": "apPipe", 

44 "ApPipeWithPreconvolution.yaml": "apPipe", 

45 "ApPipeWithFakes.yaml": "apPipe", 

46 "SingleFrame.yaml": "singleFrame", 

47 "SingleFrameWithIsrTaskLSST.yaml": "singleFrame", 

48 "RunIsrWithoutInterChipCrosstalk.yaml": "runIsr", 

49 "RunIsrForCrosstalkSources.yaml": "runOverscan", 

50 "CreateInjectionCatalogs.yaml": "fakeCreation", 

51 } 

52 

53 def test_graph_build(self): 

54 """Test that each pipeline definition file can be 

55 used to build a graph. 

56 """ 

57 files = ResourcePath.findFileResources([self.path], file_filter=r".*\.yaml$") 

58 for file in files: 

59 if "QuickTemplate" in file.path: 

60 # Our QuickTemplate definition cannot be tested here because it 

61 # depends on drp_tasks, which we cannot make a dependency here. 

62 continue 

63 if "PromptTemplate" in file.path: 

64 # Our PromptTemplate definition cannot be tested here because it 

65 # depends on drp_tasks, which we cannot make a dependency here. 

66 continue 

67 with self.subTest(file=str(file)): 

68 pipeline = lsst.pipe.base.Pipeline.from_uri(file) 

69 pipeline.addConfigOverride("parameters", "apdb_config", "some/file/path.yaml") 

70 # If this fails, it will produce a useful error message. 

71 pipeline.to_graph() 

72 

73 def test_datasets(self): 

74 files = ResourcePath.findFileResources( 

75 [self.path.join("_ingredients", forceDirectory=True)], file_filter=r".*\.yaml$" 

76 ) 

77 for file in files: 

78 if "QuickTemplate" in file.path: 

79 # Our QuickTemplate definition cannot be tested here because it 

80 # depends on drp_tasks, which we cannot make a dependency here. 

81 continue 

82 if "injection/" in file.path: 

83 # The source-injection post-processing ingredient is a partial 

84 # pipeline merged into full AP pipelines at build time; 

85 # it is validated separately by test_injection_ingredient. 

86 continue 

87 with self.subTest(file=str(file)): 

88 expected_inputs = { 

89 # ISR 

90 "raw", "camera", "crosstalk", "crosstalkSources", "bias", "dark", "flat", "ptc", 

91 "fringe", "straylightData", "bfKernel", "newBFKernel", "defects", "linearizer", 

92 "opticsTransmission", "filterTransmission", "atmosphereTransmission", 

93 "illumMaskedImage", "deferredChargeCalib", 

94 # ISR-LSST 

95 "bfk", "cti", "dnlLUT", "gain_correction", 

96 # Everything else 

97 "skyMap", "gaia_dr3_20230707", "gaia_dr2_20200414", "ps1_pv3_3pi_20170110", 

98 "template_coadd", "pretrainedModelPackage", "dia_source_apdb" 

99 } 

100 # Detect source-injection pipelines by task label rather than 

101 # relying on filename conventions. 

102 temp_pipeline = lsst.pipe.base.Pipeline.from_uri(file) 

103 temp_pipeline.addConfigOverride("parameters", "apdb_config", "some/file/path.yaml") 

104 if "injectVisit" in temp_pipeline.task_labels: 

105 expected_inputs.add("injection_catalog") 

106 expected_inputs.add("VisitDetectorFakeSourceCat") 

107 tester = PipelineStepTester( 

108 filename=file, 

109 step_suffixes=[""], # Test full pipeline 

110 initial_dataset_types=[("ps1_pv3_3pi_20170110", {"htm7"}, "SimpleCatalog", False), 

111 ("gaia_dr2_20200414", {"htm7"}, "SimpleCatalog", False), 

112 ("gaia_dr3_20230707", {"htm7"}, "SimpleCatalog", False), 

113 ], 

114 expected_inputs=expected_inputs, 

115 # Pipeline outputs highly in flux, don't test 

116 expected_outputs=set(), 

117 pipeline_patches={"parameters:apdb_config": "some/file/path.yaml", 

118 }, 

119 ) 

120 # Tester modifies Butler registry, so need a fresh repo every time 

121 with tempfile.TemporaryDirectory() as tempRepo: 

122 butler = butlerTests.makeTestRepo(tempRepo) 

123 tester.run(butler, self) 

124 

125 def test_whole_subset(self): 

126 """Test that each pipeline's synonymous subset includes all tasks, 

127 including those imported from other files. 

128 """ 

129 files = ResourcePath.findFileResources([self.path], file_filter=r".*\.yaml$") 

130 for file in files: 

131 if "QuickTemplate" in file.path: 

132 # Our QuickTemplate definition cannot be tested here because it 

133 # depends on drp_tasks, which we cannot make a dependency here. 

134 continue 

135 elif "injection/" in file.path: 

136 # PostInjectedTasksApPipe is not actually an AP pipeline 

137 continue 

138 elif "ApdbDeduplication" in file.path: 

139 # The task to export catalogs from the APDB and re-run 

140 # association is not intended to be part of Prompt Processing 

141 # or batch AP pipeline runs. 

142 continue 

143 elif "PromptTemplate" in file.path: 

144 # Our PromptTemplate definition cannot be tested here because it 

145 # depends on drp_tasks, which we cannot make a dependency here. 

146 continue 

147 with self.subTest(file=str(file)): 

148 pipeline = lsst.pipe.base.Pipeline.from_uri(file) 

149 subset = self.synonyms.get(file.basename(), "<unknown_synonym>") 

150 self.assertEqual(pipeline.subsets.get(subset, "<missing>"), set(pipeline.task_labels), 

151 msg=f"These tasks are missing from subset '{subset}'") 

152 

153 def test_ap_pipe_subsets(self): 

154 """Test the unique subsets of ApPipe. 

155 """ 

156 files = ResourcePath.findFileResources([self.path], file_filter=r"^ApPipe.*\.yaml$") 

157 required_subsets = {"preload", "prompt", "afterburner"} 

158 # getRegionTimeFromVisit is part of no subset besides apPipe. This is a 

159 # very deliberate exception; see RFC-997. 

160 no_subset_wanted = {"getRegionTimeFromVisit"} 

161 

162 for file in files: 

163 if "injection/" in file.path: 

164 # PostInjectedTasksApPipe is not actually an AP pipeline 

165 continue 

166 with self.subTest(file=str(file)): 

167 pipeline = lsst.pipe.base.Pipeline.from_uri(file) 

168 # Do all steps exist? 

169 self.assertGreaterEqual(pipeline.subsets.keys(), required_subsets, 

170 msg="An AP pipeline is missing subsets " 

171 f"{required_subsets - pipeline.subsets.keys()}.") 

172 # Is each task part of exactly one step? 

173 for set1, set2 in itertools.product(required_subsets, required_subsets): 

174 if set1 == set2: 

175 continue 

176 tasks1 = pipeline.subsets[set1] 

177 tasks2 = pipeline.subsets[set2] 

178 self.assertTrue(tasks1.isdisjoint(tasks2), 

179 msg=f"Subsets '{set1}' and '{set2}' share tasks " 

180 f"{tasks1.intersection(tasks2)}.") 

181 subsetted = set().union(*[pipeline.subsets[s] for s in required_subsets]) 

182 self.assertEqual(subsetted, set(pipeline.task_labels) - no_subset_wanted, 

183 msg=f"These tasks are not in any of the subsets {required_subsets}.") 

184 

185 def test_injection_ingredient(self): 

186 """Test the source-injection post-processing ingredient pipeline. 

187 

188 PostInjectedTasksApPipe is a partial pipeline merged into full AP 

189 pipelines at build time by make_injection_pipeline. This test 

190 validates that it can build a graph and contains the expected tasks. 

191 """ 

192 ingredient = self.path.join("_ingredients").join("injection").join("PostInjectedTasksApPipe.yaml") 

193 with self.subTest(file=str(ingredient)): 

194 pipeline = lsst.pipe.base.Pipeline.from_uri(ingredient) 

195 expected_tasks = { 

196 "injectedMatchDiaSrc", 

197 "injectedMatchAssocDiaSrc", 

198 "consolidateMatchDiaSrc", 

199 "consolidateMatchAssocDiaSrc", 

200 } 

201 self.assertGreaterEqual( 

202 set(pipeline.task_labels), 

203 expected_tasks, 

204 msg="Source-injection post-processing ingredient is missing expected tasks.", 

205 ) 

206 

207 def test_generated_pipeline_readiness(self): 

208 """Test that the generated ApPipeWithFakes ingredient exists and is valid. 

209 

210 pipelines/_ingredients/ApPipeWithFakes.yaml is generated at build time 

211 by make_injection_pipeline (invoked via scons). This test verifies that 

212 generation occurred before pipeline tests run, and that the generated 

213 pipeline includes the source-injection task. If this test is skipped, 

214 run 'scons' in the ap_pipe root directory first. 

215 """ 

216 generated = self.path.join("_ingredients/ApPipeWithFakes.yaml") 

217 if not generated.exists(): 

218 # fail the test with a message that explains how to fix the problem, 

219 # rather than silently skipping it 

220 self.fail( 

221 f"{generated} has not been generated yet. " 

222 "Run 'scons' in the ap_pipe root directory to generate it." 

223 ) 

224 with self.subTest(file=str(generated)): 

225 pipeline = lsst.pipe.base.Pipeline.from_uri(generated) 

226 pipeline.addConfigOverride("parameters", "apdb_config", "some/file/path.yaml") 

227 self.assertIn( 

228 "injectVisit", 

229 pipeline.task_labels, 

230 msg="Generated ApPipeWithFakes.yaml is missing the 'injectVisit' task.", 

231 ) 

232 

233 def test_preconvolution_isr_matches_ap_pipe(self): 

234 """Test that, for each instrument, ApPipeWithPreconvolution defines 

235 the same isr task (class and config) as the corresponding ApPipe. 

236 

237 Preconvolution changes only image subtraction and DIA-source 

238 detection; instrument signature removal must be unaffected. 

239 """ 

240 files = [ 

241 f for f in ResourcePath.findFileResources( 

242 [self.path], file_filter=r"^ApPipeWithPreconvolution\.yaml$" 

243 ) 

244 if "_ingredients" not in f.path 

245 ] 

246 # Sanity-check that this test actually has cameras to compare. 

247 self.assertGreater(len(files), 0, 

248 msg="No camera-specific ApPipeWithPreconvolution.yaml files found.") 

249 

250 for precon_file in files: 

251 with self.subTest(file=str(precon_file)): 

252 base_file = precon_file.dirname().join("ApPipe.yaml") 

253 self.assertTrue(base_file.exists(), 

254 msg=f"Expected sibling ApPipe.yaml next to {precon_file}: " 

255 f"{base_file} does not exist.") 

256 

257 precon = lsst.pipe.base.Pipeline.from_uri(precon_file) 

258 base = lsst.pipe.base.Pipeline.from_uri(base_file) 

259 # apdb_config has no default and must be set before to_graph(). 

260 precon.addConfigOverride("parameters", "apdb_config", "some/file/path.yaml") 

261 base.addConfigOverride("parameters", "apdb_config", "some/file/path.yaml") 

262 

263 precon_isr = precon.to_graph().tasks["isr"] 

264 base_isr = base.to_graph().tasks["isr"] 

265 

266 self.assertEqual(precon_isr.task_class_name, base_isr.task_class_name, 

267 msg=f"isr task class differs between ApPipe.yaml and " 

268 f"ApPipeWithPreconvolution.yaml in {precon_file.dirname()}.") 

269 # Can't just do `assertEqual(precon_isr, base_isr)` since 

270 # Task nodes are intentionally not equality comparable. 

271 self.assertTrue( 

272 base_isr.config.compare(precon_isr.config, shortcut=False), 

273 msg=f"isr task config differs between ApPipe.yaml and " 

274 f"ApPipeWithPreconvolution.yaml in {precon_file.dirname()}." 

275 ) 

276 

277 def test_inherited_subsets(self): 

278 """Test that instrument-specific pipelines have all the subsets of their 

279 generic counterparts. 

280 

281 Note that this does not check inheritance *within* `_ingredients`! 

282 """ 

283 files = [ 

284 f for f in ResourcePath.findFileResources([self.path], file_filter=r".*\.yaml$") 

285 if "_ingredients" not in f.path 

286 ] 

287 for file in files: 

288 if "QuickTemplate" in file.path: 

289 # Our QuickTemplate definition cannot be tested here because it 

290 # depends on drp_tasks, which we cannot make a dependency here. 

291 continue 

292 with self.subTest(file=str(file)): 

293 generic = self.path.join("_ingredients/", forceDirectory=True).join(file.basename()) 

294 if not generic.exists(): 

295 continue 

296 special_subsets = lsst.pipe.base.Pipeline.from_uri(file).subsets.keys() 

297 generic_subsets = lsst.pipe.base.Pipeline.from_uri(generic).subsets.keys() 

298 self.assertGreaterEqual(special_subsets, generic_subsets, 

299 msg="The instrument-specific pipeline is missing subsets " 

300 f"{generic_subsets - special_subsets}.") 

301 

302 

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

304 pass 

305 

306 

307def setup_module(module): 

308 lsst.utils.tests.init() 

309 

310 

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

312 lsst.utils.tests.init() 

313 unittest.main()