LSST Applications g1653933729+a8ce1bb630,g1a997c3884+a8ce1bb630,g1b393d1bc7+82476ad7c1,g28da252d5a+3d3e1c4204,g2bbee38e9b+97aa061eef,g2bc492864f+97aa061eef,g2cdde0e794+3ad5f2bb52,g2f1216ac18+8615c5b65f,g3156d2b45e+07302053f8,g347aa1857d+97aa061eef,g35bb328faa+a8ce1bb630,g3a166c0a6a+97aa061eef,g3e281a1b8c+693a468c5f,g4005a62e65+17cd334064,g414038480c+56e3b84a79,g41af890bb2+e5200c8fd9,g65afce507f+0106b0cffc,g80478fca09+e9b577042c,g82479be7b0+a273c6d073,g858d7b2824+b43ab392d2,g9125e01d80+a8ce1bb630,ga5288a1d22+3199fccd69,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gbb4f38f987+b43ab392d2,gc28159a63d+97aa061eef,gcd3f1c0c93+2e89b03209,gcf0d15dbbd+a0207f3e71,gd35896b8e2+3e8344a67c,gda3e153d99+b43ab392d2,gda6a2b7d83+a0207f3e71,gdaeeff99f8+1711a396fd,ge2409df99d+e6e587e663,ge33fd446bb+b43ab392d2,ge79ae78c31+97aa061eef,gf0baf85859+5daf287408,gf5289d68f6+c4f2338d90,gfda6b12a05+3bcad770a9,w.2024.42
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