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