Coverage for tests/test_diffractionSpikeMask.py: 28%
67 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-06 02:11 -0800
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-06 02:11 -0800
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/>.
22import os
23import glob
25import numpy as np
27import lsst.afw.image as afwImage
28import lsst.geom as geom
29from lsst.meas.algorithms.testUtils import MockReferenceObjectLoaderFromFiles
30import lsst.meas.base.tests
31from lsst.pipe.tasks.diffractionSpikeMask import DiffractionSpikeMaskTask, DiffractionSpikeMaskConfig
32from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary
33import lsst.utils.tests
35from utils import makeTestVisitInfo
38TESTDIR = os.path.abspath(os.path.dirname(__file__))
39RefCatDir = os.path.join(TESTDIR, "data", "sdssrefcat")
41testColorterms = ColortermLibrary(data={
42 "test*": ColortermDict(data={
43 "test-g": Colorterm(primary="g", secondary="r", c0=0.00, c1=0.00),
44 "test-r": Colorterm(primary="r", secondary="i", c0=0.00, c1=0.00, c2=0.00),
45 "test-i": Colorterm(primary="i", secondary="z", c0=1.00, c1=0.00, c2=0.00),
46 "test-z": Colorterm(primary="z", secondary="i", c0=0.00, c1=0.00, c2=0.00),
47 })
48})
51def setup_module(module):
52 lsst.utils.tests.init()
55class DiffractionSpikeMaskTest(lsst.utils.tests.TestCase):
57 def setUp(self):
59 # Load sample input from disk
60 testDir = os.path.dirname(__file__)
62 # The .xy.fits file has sources in the range ~ [0,2000],[0,4500]
63 # which is bigger than the exposure
64 self.bbox = geom.Box2I(geom.Point2I(0, 0), geom.Extent2I(2048, 4612))
66 smallExposure = afwImage.ExposureF(os.path.join(testDir, "data", "v695833-e0-c000-a00.sci.fits"))
67 self.exposure = afwImage.ExposureF(self.bbox)
68 self.exposure.setWcs(smallExposure.getWcs())
69 self.exposure.setFilter(afwImage.FilterLabel(band="i", physical="test-i"))
70 self.exposure.info.setVisitInfo(makeTestVisitInfo())
72 # Make a reference loader
73 filenames = sorted(glob.glob(os.path.join(RefCatDir, 'ref_cats', 'cal_ref_cat', '??????.fits')))
74 self.refObjLoader = MockReferenceObjectLoaderFromFiles(filenames, htmLevel=8)
76 def tearDown(self):
77 del self.exposure
78 del self.refObjLoader
80 def test_loadAndMaskStars(self):
81 """Run the bright star mask with a selection of reference sources."""
83 config = DiffractionSpikeMaskConfig(magnitudeThreshold=16)
84 task = DiffractionSpikeMaskTask(self.refObjLoader, config=config)
85 exposure = self.exposure.clone()
86 # Set the saturated mask plane in half of the image
87 saturatedMaskBit = exposure.mask.getPlaneBitMask(config.saturatedMaskPlane)
88 bbox = exposure.getBBox()
89 bbox.grow(-lsst.geom.Extent2I(0, bbox.height//4))
90 exposure[bbox].mask.array |= saturatedMaskBit
91 brightCat = task.run(exposure=exposure)
92 self.assertGreater(len(brightCat), 0)
93 # Verify that the new mask plane has been added
94 spikeMaskBit = exposure.mask.getPlaneBitMask(config.spikeMask)
95 # The images should not be modified
96 self.assertImagesEqual(self.exposure.image, exposure.image)
97 self.assertImagesEqual(self.exposure.variance, exposure.variance)
98 # Ensure that the mask has changed
99 self.assertFloatsNotEqual(self.exposure.mask.array, exposure.mask.array)
101 # Check that the mask is set for the bright sources inside the image
102 # Note that the catalog will include bright sources *off* the image that
103 # have long enough diffraction spikes to overlap the edge of the image
104 xvals, yvals = exposure.wcs.skyToPixelArray(brightCat[config.raKey], brightCat[config.decKey])
105 bbox = exposure.getBBox()
106 # Shrink the bounding box so that the bbox.contains check below is
107 # sufficient to avoid errors.
108 bbox.grow(-2)
109 points = [geom.Point2D(xv, yv) for xv, yv in zip(xvals, yvals)]
110 inside = 0
111 outside = 0
112 for pt in points:
113 if bbox.contains(int(pt.getX()), int(pt.getY())):
114 ptBox = geom.Box2I.makeCenteredBox(pt, geom.Extent2I(3, 3))
115 maskSet = exposure[ptBox].mask.array & spikeMaskBit > 0
116 self.assertTrue(np.all(maskSet))
117 inside += 1
118 else:
119 outside += 1
120 self.assertGreater(inside, 0)
121 self.assertGreater(outside, 0)
123 def test_noBrightStars(self):
124 """Run the bright star mask with no bright stars"""
126 # Set a very high magnitude limit so that no stars are selected
127 config = DiffractionSpikeMaskConfig(magnitudeThreshold=0)
128 task = DiffractionSpikeMaskTask(self.refObjLoader, config=config)
129 exposure = self.exposure.clone()
130 brightCat = task.run(exposure=exposure)
131 self.assertEqual(len(brightCat), 0)
132 # Verify that the new mask plane has been added
133 exposure.mask.getPlaneBitMask(config.spikeMask)
134 # The images should not be modified
135 self.assertImagesEqual(self.exposure.image, exposure.image)