Hide keyboard shortcuts

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# 

22 

23__all__ = ["MaskedImage", "VariancePixel"] 

24 

25import numpy as np 

26 

27from lsst.utils import TemplateMeta 

28from lsst.utils.deprecated import deprecate_pybind11 

29 

30from ..image.fitsIoWithOptions import imageReadFitsWithOptions, exposureWriteFitsWithOptions 

31from ..slicing import supportSlicing 

32from ..disableArithmetic import disableImageArithmetic 

33from .maskedImage import MaskedImageI, MaskedImageF, MaskedImageD, MaskedImageU, MaskedImageL 

34 

35 

36VariancePixel = np.float32 

37 

38 

39class 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 

119MaskedImage.register(np.int32, MaskedImageI) 

120MaskedImage.register(np.float32, MaskedImageF) 

121MaskedImage.register(np.float64, MaskedImageD) 

122MaskedImage.register(np.uint16, MaskedImageU) 

123MaskedImage.register(np.uint64, MaskedImageL) 

124MaskedImage.alias("I", MaskedImageI) 

125MaskedImage.alias("F", MaskedImageF) 

126MaskedImage.alias("D", MaskedImageD) 

127MaskedImage.alias("U", MaskedImageU) 

128MaskedImage.alias("L", MaskedImageL) 

129 

130for cls in set(MaskedImage.values()): 

131 supportSlicing(cls) 

132 disableImageArithmetic(cls) 

133 cls.__ilshift__ = deprecate_pybind11(cls.__ilshift__, 

134 reason="Use `assign` instead. To be removed after 20.0.0.")