Coverage for tests/test_utils.py: 33%
73 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-09 14:08 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-09 14:08 +0000
1# This file is part of source_injection.
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/>.
22import logging
23import os
24import unittest
26import lsst.utils.tests
27from lsst.daf.butler.tests import makeTestCollection, makeTestRepo
28from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir
29from lsst.obs.base.instrument_tests import DummyCam
30from lsst.pipe.base import Pipeline
31from lsst.source.injection import ExposureInjectTask, ingest_injection_catalog, make_injection_pipeline
32from lsst.source.injection.utils.test_utils import (
33 make_test_exposure,
34 make_test_injection_catalog,
35 make_test_reference_pipeline,
36)
37from lsst.utils.tests import MemoryTestCase, TestCase
39TEST_DIR = os.path.abspath(os.path.dirname(__file__))
42class SourceInjectionUtilsTestCase(TestCase):
43 """Test the utility functions in the source_injection package."""
45 @classmethod
46 def setUpClass(cls):
47 cls.root = makeTestTempDir(TEST_DIR)
48 cls.creator_butler = makeTestRepo(cls.root)
49 cls.writeable_butler = makeTestCollection(cls.creator_butler)
50 # Register an instrument so we can get some bands.
51 DummyCam().register(cls.writeable_butler.registry)
53 @classmethod
54 def tearDownClass(cls):
55 del cls.writeable_butler
56 del cls.creator_butler
57 removeTestTempDir(cls.root)
59 def setUp(self):
60 self.exposure = make_test_exposure()
61 self.injection_catalog = make_test_injection_catalog(
62 self.exposure.getWcs(),
63 self.exposure.getBBox(),
64 )
65 self.reference_pipeline = make_test_reference_pipeline()
67 def tearDown(self):
68 del self.exposure
69 del self.injection_catalog
70 del self.reference_pipeline
72 def test_generate_injection_catalog(self):
73 self.assertEqual(len(self.injection_catalog), 30)
74 expected_columns = {"injection_id", "ra", "dec", "source_type", "mag"}
75 self.assertEqual(set(self.injection_catalog.columns), expected_columns)
77 def test_make_injection_pipeline(self):
78 injection_pipeline = Pipeline("reference_pipeline")
79 injection_pipeline.addTask(ExposureInjectTask, "inject_exposure")
80 merged_pipeline = make_injection_pipeline(
81 dataset_type_name="postISRCCD",
82 reference_pipeline=self.reference_pipeline,
83 injection_pipeline=injection_pipeline,
84 exclude_subsets=False,
85 prefix="injected_",
86 instrument="lsst.obs.subaru.HyperSuprimeCam",
87 log_level=logging.DEBUG,
88 )
89 expanded_pipeline = merged_pipeline.toExpandedPipeline()
90 expected_subset_tasks = ["isr", "inject_exposure", "characterizeImage"]
91 merged_task_subsets = [merged_pipeline.findSubsetsWithLabel(x) for x in expected_subset_tasks]
92 self.assertEqual(len(merged_task_subsets), len(expected_subset_tasks))
93 for taskDef in expanded_pipeline:
94 conns = taskDef.connections
95 if taskDef.label == "isr":
96 self.assertEqual(conns.outputExposure.name, "postISRCCD")
97 elif taskDef.label == "inject_exposure":
98 self.assertEqual(conns.input_exposure.name, "postISRCCD")
99 self.assertEqual(conns.output_exposure.name, "injected_postISRCCD")
100 self.assertEqual(conns.output_catalog.name, "injected_postISRCCD_catalog")
101 elif taskDef.label == "characterizeImage":
102 self.assertEqual(conns.exposure.name, "injected_postISRCCD")
103 self.assertEqual(conns.characterized.name, "injected_icExp")
104 self.assertEqual(conns.backgroundModel.name, "injected_icExpBackground")
105 self.assertEqual(conns.sourceCat.name, "injected_icSrc")
107 def test_ingest_injection_catalog(self):
108 input_dataset_refs = ingest_injection_catalog(
109 writeable_butler=self.writeable_butler,
110 table=self.injection_catalog,
111 band="g",
112 output_collection="test_collection",
113 dataset_type_name="injection_catalog",
114 log_level=logging.DEBUG,
115 )
116 output_dataset_refs = self.writeable_butler.registry.queryDatasets(
117 "injection_catalog",
118 collections="test_collection",
119 )
120 self.assertEqual(len(input_dataset_refs), output_dataset_refs.count())
121 input_ids = {x.id for x in input_dataset_refs}
122 output_ids = {x.id for x in output_dataset_refs}
123 self.assertEqual(input_ids, output_ids)
124 injected_catalog = self.writeable_butler.get(input_dataset_refs[0])
125 self.assertTrue(all(self.injection_catalog == injected_catalog))
128class MemoryTestCase(MemoryTestCase):
129 """Test memory usage of functions in this script."""
131 pass
134def setup_module(module):
135 """Configure pytest."""
136 lsst.utils.tests.init()
139if __name__ == "__main__": 139 ↛ 140line 139 didn't jump to line 140, because the condition on line 139 was never true
140 lsst.utils.tests.init()
141 unittest.main()