lsst.afw  16.0-19-g2da375352+1
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"]
24 
25 import numpy as np
26 
27 from lsst.utils import TemplateMeta
28 
29 from ..slicing import supportSlicing
30 from .maskedImage import MaskedImageI, MaskedImageF, MaskedImageD, MaskedImageU, MaskedImageL
31 
32 
33 class MaskedImage(metaclass=TemplateMeta):
34 
35  def set(self, value, mask=None, variance=None):
36  """Assign a tuple of scalars to the entirety of all three planes.
37  """
38  if mask is None and variance is None:
39  try:
40  value, mask, variance = value
41  except TypeError:
42  pass
43  if mask is None:
44  mask = 0
45  if variance is None:
46  variance = 0.0
47  self.image.set(value)
48  self.mask.set(mask)
49  self.variance.set(variance)
50 
51  def _set(self, index, value, origin):
52  """Set the pixel at the given index to a triple (value, mask, variance).
53 
54  Parameters
55  ----------
56  index : `geom.Point2I`
57  Position of the pixel to assign to.
58  value : `tuple`
59  A tuple of (value, mask, variance) scalars.
60  origin : `ImageOrigin`
61  Coordinate system of ``index`` (`PARENT` or `LOCAL`).
62  """
63  self.image[index, origin] = value[0]
64  self.mask[index, origin] = value[1]
65  self.variance[index, origin] = value[2]
66 
67  def _get(self, index, origin):
68  """Return a triple (value, mask, variance) at the given index.
69 
70  Parameters
71  ----------
72  index : `geom.Point2I`
73  Position of the pixel to assign to.
74  origin : `ImageOrigin`
75  Coordinate system of ``index`` (`PARENT` or `LOCAL`).
76  """
77  return (self.image[index, origin],
78  self.mask[index, origin],
79  self.variance[index, origin])
80 
81  def getArrays(self):
82  """Return a tuple (value, mask, variance) numpy arrays."""
83  return (self.image.array if self.image else None,
84  self.mask.array if self.mask else None,
85  self.variance.array if self.variance else None)
86 
87  def convertF(self):
88  return MaskedImageF(self, True)
89 
90  def convertD(self):
91  return MaskedImageD(self, True)
92 
93  def __reduce__(self):
94  from lsst.afw.fits import reduceToFits
95  return reduceToFits(self)
96 
97 
98 MaskedImage.register(np.int32, MaskedImageI)
99 MaskedImage.register(np.float32, MaskedImageF)
100 MaskedImage.register(np.float64, MaskedImageD)
101 MaskedImage.register(np.uint16, MaskedImageU)
102 MaskedImage.register(np.int64, MaskedImageL)
103 MaskedImage.alias("I", MaskedImageI)
104 MaskedImage.alias("F", MaskedImageF)
105 MaskedImage.alias("D", MaskedImageD)
106 MaskedImage.alias("U", MaskedImageU)
107 MaskedImage.alias("L", MaskedImageL)
108 
109 for cls in set(MaskedImage.values()):
110  supportSlicing(cls)