Coverage for tests/test_pipelines.py: 96%

123 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-15 10:25 +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 "ApPipe-legacy.yaml": "apPipe", 

44 "ApPipeDaytime.yaml": "apPipe", 

45 "ApPipeWithIsrTaskLSST.yaml": "apPipe", 

46 "ApPipeWithPreconvolution.yaml": "apPipe", 

47 "ApPipeWithPreconvolution-legacy.yaml": "apPipe", 

48 "ApPipeWithFakes.yaml": "apPipe", 

49 "SingleFrame.yaml": "singleFrame", 

50 "SingleFrameWithIsrTaskLSST.yaml": "singleFrame", 

51 "RunIsrWithoutInterChipCrosstalk.yaml": "runIsr", 

52 "RunIsrForCrosstalkSources.yaml": "runOverscan", 

53 "CreateInjectionCatalogs.yaml": "fakeCreation", 

54 } 

55 

56 def test_graph_build(self): 

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

58 used to build a graph. 

59 """ 

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

61 for file in files: 

62 if "QuickTemplate" in file.path: 

63 # Our QuickTemplate definition cannot be tested here because it 

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

65 continue 

66 if "PromptTemplate" in file.path: 

67 # Our PromptTemplate definition cannot be tested here because it 

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

69 continue 

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

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

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

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

74 pipeline.to_graph() 

75 

76 def test_datasets(self): 

77 files = ResourcePath.findFileResources( 

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

79 ) 

80 for file in files: 

81 if "QuickTemplate" in file.path: 

82 # Our QuickTemplate definition cannot be tested here because it 

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

84 continue 

85 if "injection/" in file.path: 

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

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

88 # it is validated separately by test_injection_ingredient. 

89 continue 

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

91 expected_inputs = { 

92 # ISR 

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

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

95 "opticsTransmission", "filterTransmission", "atmosphereTransmission", 

96 "illumMaskedImage", "deferredChargeCalib", 

97 # ISR-LSST 

98 "bfk", "cti", "dnlLUT", "gain_correction", 

99 # Everything else 

100 "skyMap", "gaia_dr3_20230707", "gaia_dr2_20200414", "ps1_pv3_3pi_20170110", 

101 "template_coadd", "pretrainedModelPackage", "dia_source_apdb" 

102 } 

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

104 # relying on filename conventions. 

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

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

107 if "injectVisit" in temp_pipeline.task_labels: 

108 expected_inputs.add("injection_catalog") 

109 expected_inputs.add("VisitDetectorFakeSourceCat") 

110 tester = PipelineStepTester( 

111 filename=file, 

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

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

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

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

116 ], 

117 expected_inputs=expected_inputs, 

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

119 expected_outputs=set(), 

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

121 }, 

122 ) 

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

124 with tempfile.TemporaryDirectory() as tempRepo: 

125 butler = butlerTests.makeTestRepo(tempRepo) 

126 tester.run(butler, self) 

127 

128 def test_whole_subset(self): 

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

130 including those imported from other files. 

131 """ 

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

133 for file in files: 

134 if "QuickTemplate" in file.path: 

135 # Our QuickTemplate definition cannot be tested here because it 

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

137 continue 

138 elif "injection/" in file.path: 

139 # PostInjectedTasksApPipe is not actually an AP pipeline 

140 continue 

141 elif "ApdbDeduplication" in file.path: 

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

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

144 # or batch AP pipeline runs. 

145 continue 

146 elif "PromptTemplate" in file.path: 

147 # Our PromptTemplate definition cannot be tested here because it 

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

149 continue 

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

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

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

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

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

155 

156 def test_ap_pipe_subsets(self): 

157 """Test the unique subsets of ApPipe. 

158 """ 

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

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

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

162 # very deliberate exception; see RFC-997. 

163 no_subset_wanted = {"getRegionTimeFromVisit"} 

164 

165 for file in files: 

166 if "injection/" in file.path: 166 ↛ 168line 166 didn't jump to line 168 because the condition on line 166 was never true

167 # PostInjectedTasksApPipe is not actually an AP pipeline 

168 continue 

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

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

171 # Do all steps exist? 

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

173 msg="An AP pipeline is missing subsets " 

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

175 # Is each task part of exactly one step? 

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

177 if set1 == set2: 

178 continue 

179 tasks1 = pipeline.subsets[set1] 

180 tasks2 = pipeline.subsets[set2] 

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

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

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

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

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

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

187 

188 def test_injection_ingredient(self): 

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

190 

191 PostInjectedTasksApPipe is a partial pipeline merged into full AP 

192 pipelines at build time by make_injection_pipeline. This test 

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

194 """ 

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

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

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

198 expected_tasks = { 

199 "injectedMatchDiaSrc", 

200 "injectedMatchAssocDiaSrc", 

201 "consolidateMatchDiaSrc", 

202 "consolidateMatchAssocDiaSrc", 

203 } 

204 self.assertGreaterEqual( 

205 set(pipeline.task_labels), 

206 expected_tasks, 

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

208 ) 

209 

210 def test_generated_pipeline_readiness(self): 

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

212 

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

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

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

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

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

218 """ 

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

220 if not generated.exists(): 220 ↛ 223line 220 didn't jump to line 223 because the condition on line 220 was never true

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

222 # rather than silently skipping it 

223 self.fail( 

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

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

226 ) 

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

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

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

230 self.assertIn( 

231 "injectVisit", 

232 pipeline.task_labels, 

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

234 ) 

235 

236 def test_preconvolution_isr_matches_ap_pipe(self): 

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

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

239 

240 Preconvolution changes only image subtraction and DIA-source 

241 detection; instrument signature removal must be unaffected. 

242 """ 

243 files = [ 

244 f for f in ResourcePath.findFileResources( 

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

246 ) 

247 if "_ingredients" not in f.path 

248 ] 

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

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

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

252 

253 for precon_file in files: 

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

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

256 self.assertTrue(base_file.exists(), 

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

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

259 

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

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

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

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

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

265 

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

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

268 

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

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

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

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

273 # Task nodes are intentionally not equality comparable. 

274 self.assertTrue( 

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

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

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

278 ) 

279 

280 def test_inherited_subsets(self): 

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

282 generic counterparts. 

283 

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

285 """ 

286 files = [ 

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

288 if "_ingredients" not in f.path 

289 ] 

290 for file in files: 

291 if "QuickTemplate" in file.path: 

292 # Our QuickTemplate definition cannot be tested here because it 

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

294 continue 

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

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

297 if not generic.exists(): 

298 continue 

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

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

301 self.assertGreaterEqual(special_subsets, generic_subsets, 

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

303 f"{generic_subsets - special_subsets}.") 

304 

305 

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

307 pass 

308 

309 

310def setup_module(module): 

311 lsst.utils.tests.init() 

312 

313 

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

315 lsst.utils.tests.init() 

316 unittest.main()