Coverage for python/lsst/afw/image/exposure/_exposureContinued.py: 69%

62 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-07-09 06:10 -0700

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# (https://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 <https://www.gnu.org/licenses/>. 

21 

22__all__ = ["Exposure"] 

23 

24import numpy as np 

25 

26from lsst.utils import TemplateMeta 

27from lsst.utils.deprecated import deprecate_pybind11 

28 

29from ..image._slicing import supportSlicing 

30from ..image._disableArithmetic import disableImageArithmetic 

31from ..image._fitsIoWithOptions import imageReadFitsWithOptions, exposureWriteFitsWithOptions 

32from ._exposure import ExposureI, ExposureF, ExposureD, ExposureU, ExposureL 

33from .exposureUtils import bbox_to_convex_polygon, bbox_contains_sky_coords 

34 

35 

36class Exposure(metaclass=TemplateMeta): 

37 

38 def _set(self, index, value, origin): 

39 """Set the pixel at the given index to a triple (value, mask, variance). 

40 

41 Parameters 

42 ---------- 

43 index : `geom.Point2I` 

44 Position of the pixel to assign to. 

45 value : `tuple` 

46 A tuple of (value, mask, variance) scalars. 

47 origin : `ImageOrigin` 

48 Coordinate system of ``index`` (`PARENT` or `LOCAL`). 

49 """ 

50 self.maskedImage._set(index, value=value, origin=origin) 

51 

52 def _get(self, index, origin): 

53 """Return a triple (value, mask, variance) at the given index. 

54 

55 Parameters 

56 ---------- 

57 index : `geom.Point2I` 

58 Position of the pixel to assign to. 

59 origin : `ImageOrigin` 

60 Coordinate system of ``index`` (`PARENT` or `LOCAL`). 

61 """ 

62 return self.maskedImage._get(index, origin=origin) 

63 

64 def __reduce__(self): 

65 from lsst.afw.fits import reduceToFits 

66 return reduceToFits(self) 

67 

68 def convertF(self): 

69 return ExposureF(self, deep=True) 

70 

71 def convertD(self): 

72 return ExposureD(self, deep=True) 

73 

74 def getImage(self): 

75 return self.maskedImage.image 

76 

77 def setImage(self, image): 

78 self.maskedImage.image = image 

79 

80 image = property(getImage, setImage) 

81 

82 def getMask(self): 

83 return self.maskedImage.mask 

84 

85 def setMask(self, mask): 

86 self.maskedImage.mask = mask 

87 

88 mask = property(getMask, setMask) 

89 

90 def getVariance(self): 

91 return self.maskedImage.variance 

92 

93 def setVariance(self, variance): 

94 self.maskedImage.variance = variance 

95 

96 variance = property(getVariance, setVariance) 

97 

98 def getConvexPolygon(self, padding=10): 

99 """Get the convex polygon associated with the bounding box corners. 

100 

101 The returned polygon has additional padding to ensure that the 

102 bounding box is entirely contained within it. To ensure a set 

103 of coordinates are entirely contained within an exposure, run 

104 ``exposure.containsSkyCoords()``. The default padding 

105 size was chosen to be sufficient for the most warped detectors at 

106 the edges of the HyperSuprimeCam focal plane. 

107 

108 Parameters 

109 ---------- 

110 padding : `int` 

111 Pixel padding to ensure that bounding box is entirely contained 

112 within the resulting polygon. 

113 

114 Returns 

115 ------- 

116 convexPolygon : `lsst.sphgeom.ConvexPolygon` 

117 Returns `None` if exposure does not have a valid WCS. 

118 """ 

119 if self.wcs is None: 

120 return None 

121 

122 return bbox_to_convex_polygon(self.getBBox(), self.wcs, padding=padding) 

123 

124 convex_polygon = property(getConvexPolygon) 

125 

126 def containsSkyCoords(self, ra, dec, padding=10): 

127 """Check if a set of sky positions is in the pixel bounding box. 

128 

129 The default padding size was chosen to be sufficient for the 

130 most warped detectors at the edges of the HyperSuprimeCam focal plane. 

131 

132 Parameters 

133 ---------- 

134 ra : `astropy.Quantity`, (N,) 

135 Array of Right Ascension, angular units. 

136 dec : `astropy.Quantity`, (N,) 

137 Array of Declination, angular units. 

138 padding : `int`, optional 

139 Pixel padding to ensure that bounding box is entirely contained 

140 within the sky polygon (see ``getConvexPolygon()``). 

141 

142 Returns 

143 ------- 

144 contained : `np.ndarray`, (N,) 

145 Boolean array indicating which points are contained in the 

146 bounding box. 

147 

148 Raises 

149 ------ 

150 ValueError if exposure does not have a valid wcs. 

151 """ 

152 if self.wcs is None: 

153 raise ValueError("Exposure does not have a valid WCS.") 

154 

155 return bbox_contains_sky_coords( 

156 self.getBBox(), 

157 self.wcs, 

158 ra, 

159 dec, 

160 padding=padding) 

161 

162 readFitsWithOptions = classmethod(imageReadFitsWithOptions) 

163 

164 writeFitsWithOptions = exposureWriteFitsWithOptions 

165 

166 

167Exposure.register(np.int32, ExposureI) 

168Exposure.register(np.float32, ExposureF) 

169Exposure.register(np.float64, ExposureD) 

170Exposure.register(np.uint16, ExposureU) 

171Exposure.register(np.uint64, ExposureL) 

172Exposure.alias("I", ExposureI) 

173Exposure.alias("F", ExposureF) 

174Exposure.alias("D", ExposureD) 

175Exposure.alias("U", ExposureU) 

176Exposure.alias("L", ExposureL) 

177 

178for cls in set(Exposure.values()): 

179 supportSlicing(cls) 

180 disableImageArithmetic(cls) 

181 cls.getFilterLabel = deprecate_pybind11( 

182 cls.getFilterLabel, 

183 reason="Replaced by getFilter. Will be removed after v24.", 

184 version="v24.0") 

185 cls.setFilterLabel = deprecate_pybind11( 

186 cls.setFilterLabel, 

187 reason="Replaced by setFilter. Will be removed after v24.", 

188 version="v24.0") 

189 # Can't attach deprecation warning to filterLabel property.