Coverage for python/lsst/source/injection/utils/test_utils.py: 27%
46 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-19 13:56 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-19 13:56 +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/>.
23import numpy as np
24from lsst.afw.geom import makeCdMatrix, makeSkyWcs
25from lsst.afw.image import makePhotoCalibFromCalibZeroPoint
26from lsst.geom import Box2I, Extent2I, Point2D, Point2I, SpherePoint, degrees
27from lsst.ip.isr.isrTask import IsrTask
28from lsst.meas.algorithms.testUtils import plantSources
29from lsst.pipe.base import Pipeline
30from lsst.pipe.base.pipelineIR import LabeledSubset
31from lsst.pipe.tasks.characterizeImage import CharacterizeImageTask
32from lsst.source.injection import generate_injection_catalog
35def make_test_exposure():
36 """Make a test exposure with a PSF attached and stars placed randomly.
38 This function generates a noisy synthetic image with Gaussian PSFs injected
39 into the frame.
40 The exposure is returned with a WCS, PhotoCalib and PSF attached.
42 Returns
43 -------
44 exposure : `lsst.afw.image.Exposure`
45 Exposure with calibs attached and stars placed randomly.
46 """
47 # Inspired by meas_algorithms test_dynamicDetection.py.
48 xy0 = Point2I(12345, 67890) # xy0 for image
49 dims = Extent2I(2345, 2345) # Dimensions of image
50 bbox = Box2I(xy0, dims) # Bounding box of image
51 sigma = 3.21 # PSF sigma
52 buffer = 4.0 # Buffer for star centers around edge
53 n_sigma = 5.0 # Number of PSF sigmas for kernel
54 sky = 12345.6 # Initial sky level
55 num_stars = 100 # Number of stars
56 noise = np.sqrt(sky) * np.pi * sigma**2 # Poisson noise per PSF
57 faint = 1.0 * noise # Faintest level for star fluxes
58 bright = 100.0 * noise # Brightest level for star fluxes
59 star_bbox = Box2I(bbox) # Area on image in which we can put star centers
60 star_bbox.grow(-int(buffer * sigma)) # Shrink star_bbox
61 pixel_scale = 1.0e-5 * degrees # Pixel scale (1E-5 deg = 0.036 arcsec)
63 # Make an exposure with a PSF attached; place stars randomly.
64 rng = np.random.default_rng(12345)
65 stars = [
66 (xx, yy, ff, sigma)
67 for xx, yy, ff in zip(
68 rng.uniform(star_bbox.getMinX(), star_bbox.getMaxX(), num_stars),
69 rng.uniform(star_bbox.getMinY(), star_bbox.getMaxY(), num_stars),
70 np.linspace(faint, bright, num_stars),
71 )
72 ]
73 exposure = plantSources(bbox, 2 * int(n_sigma * sigma) + 1, sky, stars, True)
75 # Set WCS and PhotoCalib.
76 exposure.setWcs(
77 makeSkyWcs(
78 crpix=Point2D(0, 0),
79 crval=SpherePoint(0, 0, degrees),
80 cdMatrix=makeCdMatrix(scale=pixel_scale),
81 )
82 )
83 exposure.setPhotoCalib(makePhotoCalibFromCalibZeroPoint(1e10, 1e8))
84 return exposure
87def make_test_injection_catalog(wcs, bbox):
88 """Make a test source injection catalog.
90 This function generates a test source injection catalog consisting of 30
91 star-like sources of varying magnitude.
93 Parameters
94 ----------
95 wcs : `lsst.afw.geom.SkyWcs`
96 WCS associated with the exposure.
97 bbox : `lsst.geom.Box2I`
98 Bounding box of the exposure.
100 Returns
101 -------
102 injection_catalog : `astropy.table.Table`
103 Source injection catalog.
104 """
105 radec0 = wcs.pixelToSky(bbox.getBeginX(), bbox.getBeginY())
106 radec1 = wcs.pixelToSky(bbox.getEndX(), bbox.getEndY())
107 ra_lim = sorted([radec0.getRa().asDegrees(), radec1.getRa().asDegrees()])
108 dec_lim = sorted([radec0.getDec().asDegrees(), radec1.getDec().asDegrees()])
109 injection_catalog = generate_injection_catalog(
110 ra_lim=ra_lim,
111 dec_lim=dec_lim,
112 wcs=wcs,
113 number=10,
114 source_type="DeltaFunction",
115 mag=[10.0, 15.0, 20.0],
116 )
117 return injection_catalog
120def make_test_reference_pipeline():
121 """Make a test reference pipeline containing the ISR task."""
122 reference_pipeline = Pipeline("reference_pipeline")
123 reference_pipeline.addTask(IsrTask, "isr")
124 reference_pipeline.addTask(CharacterizeImageTask, "characterizeImage")
125 reference_pipeline._pipelineIR.labeled_subsets["test_subset"] = LabeledSubset("test_subset", set(), None)
126 reference_pipeline.addLabelToSubset("test_subset", "isr")
127 reference_pipeline.addLabelToSubset("test_subset", "characterizeImage")
128 return reference_pipeline