Coverage for tests/test_snapCombine.py: 30%
62 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 04:34 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-01 04:34 -0700
1# This file is part of pipe_tasks.
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/>.
21import unittest
23import numpy as np
25import lsst.utils.tests
26import lsst.afw.image as afwImage
27from lsst.coadd.utils import setCoaddEdgeBits
28import lsst.meas.base.tests
29from lsst.pipe.tasks.snapCombine import SnapCombineTask
32def simple_add(exp0, exp1, bad_pixel_mask):
33 """Add two exposures, avoiding bad pixels
34 """
35 result = exp0.Factory(exp0, True)
36 weights = afwImage.ImageF(exp0.getDimensions())
38 good0 = np.bitwise_and(exp0.mask.array, bad_pixel_mask) == 0
39 good1 = np.bitwise_and(exp1.mask.array, bad_pixel_mask) == 0
41 result.image.array[:, :] = np.where(good0, exp0.image.array, 0) + np.where(good1, exp1.image.array, 0)
42 result.variance.array[:, :] = np.where(good0, exp0.variance.array, 0) + \
43 np.where(good1, exp1.variance.array, 0)
44 result.mask.array[:, :] = np.bitwise_or(np.where(good0, exp0.mask.array, 0),
45 np.where(good1, exp1.mask.array, 0))
46 weights.array[:, :] = np.where(good0, 1, 0) + np.where(good1, 1, 0)
48 result.maskedImage /= weights
49 result.maskedImage *= 2 # want addition, not mean, where both pixels are valid
51 setCoaddEdgeBits(result.mask, weights)
53 return result
56class SnapCombineTestCase(lsst.utils.tests.TestCase):
57 """A test case for SnapCombineTask."""
58 def setUp(self):
59 # Different x/y dimensions so they're easy to distinguish in a plot,
60 # and non-zero minimum, to help catch xy0 errors.
61 bbox = lsst.geom.Box2I(lsst.geom.Point2I(5, 4), lsst.geom.Point2I(205, 184))
63 center = lsst.geom.Point2D(50, 60)
64 # Two TestDatasets, as that is where VisitInfo is created.
65 dataset = lsst.meas.base.tests.TestDataset(bbox, visitId=12345)
66 dataset.addSource(10000, center)
67 schema = dataset.makeMinimalSchema()
68 self.snap0, _ = dataset.realize(noise=1001, schema=schema)
69 self.snap0.mask.array[10, 10] |= afwImage.Mask.getPlaneBitMask("BAD")
70 dataset = lsst.meas.base.tests.TestDataset(bbox, visitId=123456)
71 dataset.addSource(10000, center)
72 self.snap1, _ = dataset.realize(noise=1002, schema=schema)
74 def testAddition(self):
75 """Test addition with bad pixels
76 """
77 config = SnapCombineTask.ConfigClass()
78 config.bad_mask_planes = ("BAD", "SAT", "NO_DATA", "CR")
79 bad_pixel_mask = afwImage.MaskX.getPlaneBitMask(config.bad_mask_planes)
80 task = SnapCombineTask(config=config)
82 result = task.run(self.snap0, self.snap1).exposure
84 expect = simple_add(self.snap0, self.snap1, bad_pixel_mask)
85 self.assertMaskedImagesAlmostEqual(result.maskedImage, expect.maskedImage)
86 self._check_visitInfo(result.visitInfo)
88 def testAdditionAllGood(self):
89 """Test the case where all pixels are valid
90 """
91 config = SnapCombineTask.ConfigClass()
92 task = SnapCombineTask(config=config)
94 result = task.run(self.snap0, self.snap1).exposure
96 expect = self.snap0.maskedImage.Factory(self.snap0.maskedImage, True)
97 expect += self.snap1.maskedImage
98 self.assertMaskedImagesAlmostEqual(result.maskedImage, expect)
100 def _check_visitInfo(self, visit_info):
101 self.assertEqual(visit_info.exposureTime,
102 self.snap0.visitInfo.exposureTime + self.snap1.visitInfo.exposureTime)
103 self.assertEqual(visit_info.date.get(),
104 (self.snap0.visitInfo.date.get() + self.snap1.visitInfo.date.get())/2)
106 def test_visitInfo(self):
107 task = SnapCombineTask()
108 visit_info = task._merge_visit_info(self.snap0.visitInfo, self.snap1.visitInfo)
109 self._check_visitInfo(visit_info)
112class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
113 pass
116def setup_module(module):
117 lsst.utils.tests.init()
120if __name__ == "__main__": 120 ↛ 121line 120 didn't jump to line 121, because the condition on line 120 was never true
121 lsst.utils.tests.init()
122 unittest.main()