lsst.afw  14.0-40-g0271be365
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 from __future__ import absolute_import, division, print_function
24 
25 __all__ = ["MaskedImage"]
26 
27 from future.utils import with_metaclass
28 import numpy as np
29 
30 from lsst.utils import TemplateMeta
31 
32 from ..slicing import supportSlicing
33 from .maskedImage import MaskedImageI, MaskedImageF, MaskedImageD, MaskedImageU, MaskedImageL
34 
35 
36 class MaskedImage(with_metaclass(TemplateMeta, object)):
37 
38  def set(self, x, y=None, values=None):
39  """Set the point (x, y) to a triple (value, mask, variance)"""
40 
41  if values is None:
42  assert (y is None)
43  values = x
44  try:
45  self.getImage().set(values[0])
46  self.getMask().set(values[1])
47  self.getVariance().set(values[2])
48  except TypeError:
49  self.getImage().set(values)
50  self.getMask().set(0)
51  self.getVariance().set(0)
52  else:
53  try:
54  self.getImage().set(x, y, values[0])
55  if len(values) > 1:
56  self.getMask().set(x, y, values[1])
57  if len(values) > 2:
58  self.getVariance().set(x, y, values[2])
59  except TypeError:
60  self.getImage().set(x)
61  self.getMask().set(y)
62  self.getVariance().set(values)
63 
64  def get(self, x, y):
65  """Return a triple (value, mask, variance) at the point (x, y)"""
66  return (self.getImage().get(x, y),
67  self.getMask().get(x, y),
68  self.getVariance().get(x, y))
69 
70  def getArrays(self):
71  """Return a tuple (value, mask, variance) numpy arrays."""
72  return (self.getImage().getArray() if self.getImage() else None,
73  self.getMask().getArray() if self.getMask() else None,
74  self.getVariance().getArray() if self.getVariance() else None)
75 
76  def convertF(self):
77  return MaskedImageF(self, True)
78 
79  def convertD(self):
80  return MaskedImageD(self, True)
81 
82  def __reduce__(self):
83  from lsst.afw.fits import reduceToFits
84  return reduceToFits(self)
85 
86 
87 MaskedImage.register(np.int32, MaskedImageI)
88 MaskedImage.register(np.float32, MaskedImageF)
89 MaskedImage.register(np.float64, MaskedImageD)
90 MaskedImage.register(np.uint16, MaskedImageU)
91 MaskedImage.register(np.int64, MaskedImageL)
92 MaskedImage.alias("I", MaskedImageI)
93 MaskedImage.alias("F", MaskedImageF)
94 MaskedImage.alias("D", MaskedImageD)
95 MaskedImage.alias("U", MaskedImageU)
96 MaskedImage.alias("L", MaskedImageL)
97 
98 for cls in set(MaskedImage.values()):
99  supportSlicing(cls)
def supportSlicing(cls)
Definition: slicing.py:93