LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
binExposureTask.py
Go to the documentation of this file.
1# This file is part of ip_isr.
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/>.
21
22__all__ = ["BinExposureTask",
23 "BinExposureConfig",
24 "binExposure"]
25
26import lsst.pipe.base as pipeBase
27import lsst.pipe.base.connectionTypes as cT
28import lsst.pex.config as pexConfig
29import lsst.afw.math as afwMath
30from lsst.utils.timer import timeMethod
31import lsst.afw.image as afwImage
32
33
35 pipeBase.PipelineTaskConnections,
36 dimensions=("instrument", "exposure", "detector"),
37 defaultTemplates={"inputName": "postISRCCD", "outputName": "postISRCCDBin"}
38):
39
40 inputExposure = cT.Input(
41 name="{inputName}",
42 doc="Input exposure to bin.",
43 storageClass="Exposure",
44 dimensions=["instrument", "exposure", "detector"],
45 )
46 binnedExposure = cT.Output(
47 name="{outputName}",
48 doc="Binned exsposure.",
49 storageClass="Exposure",
50 dimensions=["instrument", "exposure", "detector"],
51 )
52
53 def __init__(self, *, config=None):
54 """Customize the connections and storageClass for a specific
55 instance. This enables both to be dynamically set at runtime,
56 allowing BinExposureTask to work with different types of
57 Exposures such as postISRCCD, calexp, deepCoadd_calexp, etc.
58
59 Parameters
60 ----------
61 config : `BinExposureConfig`
62 A config for `BinExposureTask`.
63 """
64 super().__init__(config=config)
65 if config and config.exposureDimensions != self.inputExposure.dimensions:
66 self.dimensions.clear()
67 self.dimensions.update(config.exposureDimensions)
68 self.inputExposure = cT.Input(
69 name=self.inputExposure.name,
70 doc=self.inputExposure.doc,
71 storageClass=self.inputExposure.storageClass,
72 dimensions=frozenset(config.exposureDimensions),
73 )
74 self.binnedExposure = cT.Output(
75 name=self.binnedExposure.name,
76 doc=self.binnedExposure.doc,
77 storageClass=self.binnedExposure.storageClass,
78 dimensions=frozenset(config.exposureDimensions),
79 )
80 if config and config.exposureStorageClass != self.inputExposure.storageClass:
81 self.inputExposure = cT.Input(
82 name=self.inputExposure.name,
83 doc=self.inputExposure.doc,
84 storageClass=config.exposureStorageClass,
85 dimensions=self.inputExposure.dimensions,
86 )
87 self.binnedExposure = cT.Output(
88 name=self.binnedExposure.name,
89 doc=self.binnedExposure.doc,
90 storageClass=config.exposureStorageClass,
91 dimensions=self.binnedExposure.dimensions,
92 )
93
94
95class BinExposureConfig(
96 pipeBase.PipelineTaskConfig,
97 pipelineConnections=BinExposureConnections
98):
99 """Config for BinExposureTask"""
100 exposureDimensions = pexConfig.ListField(
101 # Sort to ensure default order is consistent between runs
102 default=sorted(BinExposureConnections.dimensions),
103 dtype=str,
104 doc="Override for the dimensions of the input and binned exposures.",
105 )
106 exposureStorageClass = pexConfig.Field(
107 default='ExposureF',
108 dtype=str,
109 doc=(
110 "Override the storageClass of the input and binned exposures. "
111 "Must be of type lsst.afw.Image.Exposure, or one of its subtypes."
112 )
113 )
114 binFactor = pexConfig.Field(
115 dtype=int,
116 doc="Binning factor applied to both spatial dimensions.",
117 default=8,
118 check=lambda x: x > 1,
119 )
120
121
122class BinExposureTask(pipeBase.PipelineTask):
123 """Perform an nxn binning of an Exposure dataset type.
124
125 The binning factor is the same in both spatial dimensions (i.e.,
126 an nxn binning is performed). Each of the input Exposure's image
127 arrays are binned by the same factor.
128 """
129 # TODO: DM-46501: Add tasks to nxn bin Image and MaskedImage classes
130 ConfigClass = BinExposureConfig
131 _DefaultName = "binExposure"
132
133 @timeMethod
134 def run(self, inputExposure, binFactor=None):
135 """Perform an nxn binning of an Exposure.
136
137 Parameters:
138 -----------
139 inputExposure : `lsst.afw.image.Exposure` or one of its
140 sub-types.
141 Exposure to spatially bin
142 binFactor : `int`, optional.
143 nxn binning factor. If not provided then self.config.binFactor
144 is used.
145
146 Returns:
147 --------
148 result : `lsst.pipe.base.Struct`
149 Results as a struct with attributes:
150
151 ``binnedExposure``
152 Binned exposure (`lsst.afw.image.Exposure` or one of its
153 sub-types. The type matches that of the inputExposure).
154 """
155 if not binFactor:
156 binFactor = self.config.binFactor
157 return pipeBase.Struct(
158 binnedExposure=binExposure(inputExposure, binFactor)
159 )
160
161
162def binExposure(inputExposure, binFactor=8):
163 """Bin an exposure to reduce its spatial dimensions.
164
165 Performs an nxn binning of the input exposure, reducing both spatial
166 dimensions of each of the input exposure's image data by the provided
167 factor.
168
169 Parameters:
170 -----------
171 inputExposure: `lsst.afw.image.Exposure` or one of its sub-types.
172 Input exposure data to bin.
173 binFactor: `int`
174 Binning factor to apply to each input exposure's image data.
175 Default 8.
176
177 Returns:
178 --------
179 binnedExposure: `lsst.afw.image.Exposure` or one of its sub-types.
180 Binned version of input image.
181
182 Raises
183 ------
184 TypeError
185 Raised if either the binning factor is not of type `int`, or if the
186 input data to be binned is not of type `lsst.afw.image.Exposure`
187 or one of its sub-types.
188 """
189
190 if not isinstance(binFactor, int):
191 raise TypeError("binFactor must be of type int")
192 if not isinstance(inputExposure, afwImage.Exposure):
193 raise TypeError("inputExp must be of type lsst.afw.image.Exposure or one of its sub-tyoes.")
194
195 binned = inputExposure.getMaskedImage()
196 binned = afwMath.binImage(binned, binFactor)
197 binnedExposure = afwImage.makeExposure(binned)
198
199 binnedExposure.setInfo(inputExposure.getInfo())
200
201 return binnedExposure
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Definition Exposure.h:72
std::shared_ptr< Exposure< ImagePixelT, MaskPixelT, VariancePixelT > > makeExposure(MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > &mimage, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
A function to return an Exposure of the correct type (cf.
Definition Exposure.h:484
std::shared_ptr< ImageT > binImage(ImageT const &inImage, int const binX, int const binY, lsst::afw::math::Property const flags=lsst::afw::math::MEAN)
Definition binImage.cc:44