lsst.afw  16.0-19-g2da375352+1
multiband.py
Go to the documentation of this file.
1 # This file is part of afw.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://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 <http://www.gnu.org/licenses/>.
21 
22 __all__ = ["MultibandExposure"]
23 
24 import numpy as np
25 
26 from lsst.geom import Point2D
27 from . import Exposure, ExposureF
28 from ..image.multiband import MultibandImage, MultibandTripleBase
29 from ..image.multiband import tripleFromSingles, tripleFromArrays, makeTripleFromKwargs
30 from ..maskedImage import MaskedImage
31 
32 
34  """MultibandExposure class
35 
36  This class acts as a container for multiple `afw.Exposure` objects.
37  All exposures must have the same bounding box, and the associated
38  images must all have the same data type.
39 
40  See `MultibandTripleBase` for parameter definitions.
41  """
42  def __init__(self, filters, image, mask, variance, psfs=None):
43  super().__init__(filters, image, mask, variance)
44  if psfs is not None:
45  for psf, exposure in zip(psfs, self.singles):
46  exposure.setPsf(psf)
47 
48  @staticmethod
49  def fromExposures(filters, singles):
50  """Construct a MultibandImage from a collection of single band images
51 
52  see `tripleFromExposures` for a description of parameters
53  """
54  return tripleFromSingles(MultibandExposure, filters, singles)
55 
56  @staticmethod
57  def fromArrays(filters, image, mask, variance, bbox=None):
58  """Construct a MultibandExposure from a collection of arrays
59 
60  see `tripleFromArrays` for a description of parameters
61  """
62  return tripleFromArrays(MultibandExposure, filters, image, mask, variance, bbox)
63 
64  @staticmethod
65  def fromKwargs(filters, filterKwargs, singleType=ExposureF, **kwargs):
66  """Build a MultibandImage from a set of keyword arguments
67 
68  see `makeTripleFromKwargs` for a description of parameters
69  """
70  return makeTripleFromKwargs(MultibandExposure, filters, filterKwargs, singleType, **kwargs)
71 
72  def _buildSingles(self, image=None, mask=None, variance=None):
73  """Make a new list of single band objects
74 
75  Parameters
76  ----------
77  image: `list`
78  List of `Image` objects that represent the image in each band.
79  mask: `list`
80  List of `Mask` objects that represent the mask in each band.
81  variance: `list`
82  List of `Image` objects that represent the variance in each band.
83 
84  Returns
85  -------
86  singles: tuple
87  Tuple of `MaskedImage` objects for each band,
88  where the `image`, `mask`, and `variance` of each `single`
89  point to the multiband objects.
90  """
91  singles = []
92  if image is None:
93  image = self.image
94  if mask is None:
95  mask = self.mask
96  if variance is None:
97  variance = self.variance
98 
99  dtype = image.array.dtype
100  for f in self.filters:
101  maskedImage = MaskedImage(image=image[f], mask=mask[f], variance=variance[f], dtype=dtype)
102  single = Exposure(maskedImage, dtype=dtype)
103  singles.append(single)
104  return tuple(singles)
105 
106  @staticmethod
107  def fromButler(butler, filters, filterKwargs, *args, **kwargs):
108  """Load a multiband exposure from a butler
109 
110  Because each band is stored in a separate exposure file,
111  this method can be used to load all of the exposures for
112  a given set of bands
113 
114  Parameters
115  ----------
116  butler: `Butler`
117  Butler connection to use to load the single band
118  calibrated images
119  filters: `list` or `str`
120  List of filter names for each band
121  filterKwargs: `dict`
122  Keyword arguments to pass to the Butler
123  that are different for each filter.
124  The keys are the names of the arguments and the values
125  should also be dictionaries, with filter names as keys
126  and the value of the argument for the given filter as values.
127  args: `list`
128  Arguments to the Butler.
129  kwargs: `dict`
130  Keyword arguments to pass to the Butler
131  that are the same in all bands.
132 
133  Returns
134  -------
135  result: `MultibandExposure`
136  The new `MultibandExposure` created by combining all of the
137  single band exposures.
138  """
139  # Load the Exposure in each band
140  exposures = []
141  for f in filters:
142  if filterKwargs is not None:
143  for key, value in filterKwargs:
144  kwargs[key] = value[f]
145  exposures.append(butler.get(*args, filter=f, **kwargs))
146  return MultibandExposure.fromExposures(filters, exposures)
147 
148  def computePsfKernelImage(self, position=None):
149  """Get a multiband PSF image
150 
151  The PSF Kernel Image is computed for each band
152  and combined into a (filter, y, x) array and stored
153  as `self._psfImage`.
154  The result is not cached, so if the same PSF is expected
155  to be used multiple times it is a good idea to store the
156  result in another variable.
157 
158  Parameters
159  ----------
160  position: `Point2D` or `tuple`
161  Coordinates to evaluate the PSF. If `position` is `None`
162  then `Psf.getAveragePosition()` is used.
163 
164  Returns
165  -------
166  self._psfImage: array
167  The multiband PSF image.
168  """
169  psfs = []
170  # Make the coordinates into a Point2D (if necessary)
171  if not isinstance(position, Point2D) and position is not None:
172  position = Point2D(position[0], position[1])
173  for single in self.singles:
174  if position is None:
175  psfs.append(single.getPsf().computeKernelImage().array)
176  else:
177  psfs.append(single.getPsf().computeKernelImage(position).array)
178  psfs = np.array(psfs)
179  psfImage = MultibandImage(self.filters, array=psfs)
180  return psfImage
181 
182  def computePsfImage(self, position=None):
183  """Get a multiband PSF image
184 
185  The PSF Kernel Image is computed for each band
186  and combined into a (filter, y, x) array and stored
187  as `self._psfImage`.
188  The result is not cached, so if the same PSF is expected
189  to be used multiple times it is a good idea to store the
190  result in another variable.
191 
192  Parameters
193  ----------
194  position: `Point2D` or `tuple`
195  Coordinates to evaluate the PSF. If `position` is `None`
196  then `Psf.getAveragePosition()` is used.
197 
198  Returns
199  -------
200  self._psfImage: array
201  The multiband PSF image.
202  """
203  psfs = []
204  # Make the coordinates into a Point2D (if necessary)
205  if not isinstance(position, Point2D) and position is not None:
206  position = Point2D(position[0], position[1])
207  for single in self.singles:
208  if position is None:
209  psfs.append(single.getPsf().computeImage().array)
210  else:
211  psfs.append(single.getPsf().computeImage(position).array)
212  psfs = np.array(psfs)
213  psfImage = MultibandImage(self.filters, array=psfs)
214  return psfImage
def fromButler(butler, filters, filterKwargs, args, kwargs)
Definition: multiband.py:107
def makeTripleFromKwargs(cls, filters, filterKwargs, singleType, kwargs)
Definition: multiband.py:749
def fromKwargs(filters, filterKwargs, singleType=ExposureF, kwargs)
Definition: multiband.py:65
def __init__(self, filters, image, mask, variance, psfs=None)
Definition: multiband.py:42
def fromArrays(filters, image, mask, variance, bbox=None)
Definition: multiband.py:57
def tripleFromSingles(cls, filters, singles)
Definition: multiband.py:699
def tripleFromArrays(cls, filters, image, mask, variance, bbox=None)
Definition: multiband.py:718