Coverage for python/lsst/afw/image/maskedImage/maskedImageContinued.py : 54%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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#
23__all__ = ["MaskedImage", "VariancePixel"]
25import numpy as np
27from lsst.utils import TemplateMeta
29from ..image.fitsIoWithOptions import imageReadFitsWithOptions, exposureWriteFitsWithOptions
30from ..slicing import supportSlicing
31from ..disableArithmetic import disableImageArithmetic
32from .maskedImage import MaskedImageI, MaskedImageF, MaskedImageD, MaskedImageU, MaskedImageL
35VariancePixel = np.float32
38class MaskedImage(metaclass=TemplateMeta):
40 def set(self, value, mask=None, variance=None):
41 """Assign a tuple of scalars to the entirety of all three planes.
42 """
43 if mask is None and variance is None:
44 try:
45 value, mask, variance = value
46 except TypeError:
47 pass
48 if mask is None:
49 mask = 0
50 if variance is None:
51 variance = 0.0
52 self.image.set(value)
53 self.mask.set(mask)
54 self.variance.set(variance)
56 def _set(self, index, value, origin):
57 """Set the pixel at the given index to a triple (value, mask, variance).
59 Parameters
60 ----------
61 index : `geom.Point2I`
62 Position of the pixel to assign to.
63 value : `tuple`
64 A tuple of (value, mask, variance) scalars.
65 origin : `ImageOrigin`
66 Coordinate system of ``index`` (`PARENT` or `LOCAL`).
67 """
68 self.image[index, origin] = value[0]
69 self.mask[index, origin] = value[1]
70 self.variance[index, origin] = value[2]
72 def _get(self, index, origin):
73 """Return a triple (value, mask, variance) at the given index.
75 Parameters
76 ----------
77 index : `geom.Point2I`
78 Position of the pixel to assign to.
79 origin : `ImageOrigin`
80 Coordinate system of ``index`` (`PARENT` or `LOCAL`).
81 """
82 return (self.image[index, origin],
83 self.mask[index, origin],
84 self.variance[index, origin])
86 def getArrays(self):
87 """Return a tuple (value, mask, variance) numpy arrays."""
88 return (self.image.array if self.image else None,
89 self.mask.array if self.mask else None,
90 self.variance.array if self.variance else None)
92 def convertF(self):
93 return MaskedImageF(self, True)
95 def convertD(self):
96 return MaskedImageD(self, True)
98 def __reduce__(self):
99 from lsst.afw.fits import reduceToFits
100 return reduceToFits(self)
102 def __str__(self):
103 string = "image={},\nmask={}, maskPlaneDict={}\nvariance={}, bbox={}"
104 return string.format(self.image.array,
105 self.mask.array,
106 self.mask.getMaskPlaneDict(),
107 self.variance.array,
108 self.getBBox())
110 def __repr__(self):
111 return "{}.{}=({})".format(self.__module__, self.__class__.__name__, str(self))
113 readFitsWithOptions = classmethod(imageReadFitsWithOptions)
115 writeFitsWithOptions = exposureWriteFitsWithOptions
118MaskedImage.register(np.int32, MaskedImageI)
119MaskedImage.register(np.float32, MaskedImageF)
120MaskedImage.register(np.float64, MaskedImageD)
121MaskedImage.register(np.uint16, MaskedImageU)
122MaskedImage.register(np.uint64, MaskedImageL)
123MaskedImage.alias("I", MaskedImageI)
124MaskedImage.alias("F", MaskedImageF)
125MaskedImage.alias("D", MaskedImageD)
126MaskedImage.alias("U", MaskedImageU)
127MaskedImage.alias("L", MaskedImageL)
129for cls in set(MaskedImage.values()):
130 supportSlicing(cls)
131 disableImageArithmetic(cls)