LSSTApplications  20.0.0
LSSTDataManagementBasePackage
maskedImageContinued.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2017 LSST/AURA.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ["MaskedImage", "VariancePixel"]
24 
25 import numpy as np
26 
27 from lsst.utils import TemplateMeta
28 from lsst.utils.deprecated import deprecate_pybind11
29 
30 from ..image.fitsIoWithOptions import imageReadFitsWithOptions, exposureWriteFitsWithOptions
31 from ..slicing import supportSlicing
32 from ..disableArithmetic import disableImageArithmetic
33 from .maskedImage import MaskedImageI, MaskedImageF, MaskedImageD, MaskedImageU, MaskedImageL
34 
35 
36 VariancePixel = np.float32
37 
38 
39 class MaskedImage(metaclass=TemplateMeta):
40 
41  def set(self, value, mask=None, variance=None):
42  """Assign a tuple of scalars to the entirety of all three planes.
43  """
44  if mask is None and variance is None:
45  try:
46  value, mask, variance = value
47  except TypeError:
48  pass
49  if mask is None:
50  mask = 0
51  if variance is None:
52  variance = 0.0
53  self.image.set(value)
54  self.mask.set(mask)
55  self.variance.set(variance)
56 
57  def _set(self, index, value, origin):
58  """Set the pixel at the given index to a triple (value, mask, variance).
59 
60  Parameters
61  ----------
62  index : `geom.Point2I`
63  Position of the pixel to assign to.
64  value : `tuple`
65  A tuple of (value, mask, variance) scalars.
66  origin : `ImageOrigin`
67  Coordinate system of ``index`` (`PARENT` or `LOCAL`).
68  """
69  self.image[index, origin] = value[0]
70  self.mask[index, origin] = value[1]
71  self.variance[index, origin] = value[2]
72 
73  def _get(self, index, origin):
74  """Return a triple (value, mask, variance) at the given index.
75 
76  Parameters
77  ----------
78  index : `geom.Point2I`
79  Position of the pixel to assign to.
80  origin : `ImageOrigin`
81  Coordinate system of ``index`` (`PARENT` or `LOCAL`).
82  """
83  return (self.image[index, origin],
84  self.mask[index, origin],
85  self.variance[index, origin])
86 
87  def getArrays(self):
88  """Return a tuple (value, mask, variance) numpy arrays."""
89  return (self.image.array if self.image else None,
90  self.mask.array if self.mask else None,
91  self.variance.array if self.variance else None)
92 
93  def convertF(self):
94  return MaskedImageF(self, True)
95 
96  def convertD(self):
97  return MaskedImageD(self, True)
98 
99  def __reduce__(self):
100  from lsst.afw.fits import reduceToFits
101  return reduceToFits(self)
102 
103  def __str__(self):
104  string = "image={},\nmask={}, maskPlaneDict={}\nvariance={}, bbox={}"
105  return string.format(self.image.array,
106  self.mask.array,
107  self.mask.getMaskPlaneDict(),
108  self.variance.array,
109  self.getBBox())
110 
111  def __repr__(self):
112  return "{}.{}=({})".format(self.__module__, self.__class__.__name__, str(self))
113 
114  readFitsWithOptions = classmethod(imageReadFitsWithOptions)
115 
116  writeFitsWithOptions = exposureWriteFitsWithOptions
117 
118 
119 MaskedImage.register(np.int32, MaskedImageI)
120 MaskedImage.register(np.float32, MaskedImageF)
121 MaskedImage.register(np.float64, MaskedImageD)
122 MaskedImage.register(np.uint16, MaskedImageU)
123 MaskedImage.register(np.uint64, MaskedImageL)
124 MaskedImage.alias("I", MaskedImageI)
125 MaskedImage.alias("F", MaskedImageF)
126 MaskedImage.alias("D", MaskedImageD)
127 MaskedImage.alias("U", MaskedImageU)
128 MaskedImage.alias("L", MaskedImageL)
129 
130 for cls in set(MaskedImage.values()):
131  supportSlicing(cls)
133  cls.__ilshift__ = deprecate_pybind11(cls.__ilshift__,
134  reason="Use `assign` instead. To be removed after 20.0.0.")
pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.__repr__
def __repr__(self)
Definition: maskedImageContinued.py:111
lsst::afw::image.slicing.supportSlicing
def supportSlicing(cls)
Definition: slicing.py:244
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.convertD
def convertD(self)
Definition: maskedImageContinued.py:96
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.getArrays
def getArrays(self)
Definition: maskedImageContinued.py:87
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.convertF
def convertF(self)
Definition: maskedImageContinued.py:93
lsst::utils.deprecated
Definition: deprecated.py:1
lsst::utils
Definition: Backtrace.h:29
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.__reduce__
def __reduce__(self)
Definition: maskedImageContinued.py:99
lsst::afw::fits.pickleFits.reduceToFits
def reduceToFits(obj)
Definition: pickleFits.py:7
lsst::afw::fits
Definition: fits.h:31
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.set
def set(self, value, mask=None, variance=None)
Definition: maskedImageContinued.py:41
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage.__str__
def __str__(self)
Definition: maskedImageContinued.py:103
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage
Definition: maskedImageContinued.py:39
lsst::afw::image.disableArithmetic.disableImageArithmetic
def disableImageArithmetic(cls)
Definition: disableArithmetic.py:45
lsst::utils.deprecated.deprecate_pybind11
def deprecate_pybind11(obj, reason, category=FutureWarning)
Definition: deprecated.py:32