LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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