Coverage for python/lsst/meas/algorithms/cloughTocher2DInterpolator.py: 58%

20 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-16 03:26 -0700

1# This file is part of meas_algorithms. 

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__ = ( 

23 "CloughTocher2DInterpolateConfig", 

24 "CloughTocher2DInterpolateTask", 

25) 

26 

27 

28from lsst.pex.config import Config, Field, ListField 

29from lsst.pipe.base import Task 

30from scipy.interpolate import CloughTocher2DInterpolator 

31 

32from . import CloughTocher2DInterpolatorUtils as ctUtils 

33 

34 

35class CloughTocher2DInterpolateConfig(Config): 

36 """Config for CloughTocher2DInterpolateTask.""" 

37 

38 badMaskPlanes = ListField[str]( 

39 doc="List of mask planes to interpolate over.", 

40 default=["BAD", "SAT", "CR"], 

41 ) 

42 fillValue = Field[float]( 

43 doc="Constant value to fill outside of the convex hull of the good " 

44 "pixels. A long (longer than twice the ``interpLength``) streak of " 

45 "bad pixels at an edge will be set to this value.", 

46 default=0.0, 

47 ) 

48 interpLength = Field[int]( 48 ↛ exitline 48 didn't jump to the function exit

49 doc="Maximum number of pixels away from a bad pixel to include in " 

50 "building the interpolant. Must be greater than or equal to 1.", 

51 default=4, 

52 check=lambda x: x >= 1, 

53 ) 

54 

55 

56class CloughTocher2DInterpolateTask(Task): 

57 """Interpolated over bad pixels using CloughTocher2DInterpolator. 

58 

59 Pixels with mask bits set to any of those listed ``badMaskPlanes`` config 

60 are considered bad and are interpolated over. All good (non-bad) pixels 

61 within ``interpLength`` pixels of a bad pixel in either direction are used 

62 to construct the interpolant. An extended streak of bad pixels at an edge, 

63 longer than ``interpLength``, is set to `fillValue`` specified in config. 

64 """ 

65 

66 ConfigClass = CloughTocher2DInterpolateConfig 

67 _DefaultName = "cloughTocher2DInterpolate" 

68 

69 def run( 

70 self, 

71 maskedImage, 

72 badpix=None, 

73 goodpix=None, 

74 ): 

75 """Interpolate over bad pixels in a masked image. 

76 

77 This modifies the ``image`` attribute of the ``maskedImage`` in place. 

78 This method returns, and accepts, the coordinates of the bad pixels 

79 that were interpolated over, and the coordinates and values of the 

80 good pixels that were used to construct the interpolant. This avoids 

81 having to search for the bad and the good pixels repeatedly when the 

82 mask plane is shared among many images, as would be the case with 

83 noise realizations. 

84 

85 Parameters 

86 ---------- 

87 maskedImage : `~lsst.afw.image.MaskedImageF` 

88 Image on which to perform interpolation (and modify in-place). 

89 badpix: `numpy.ndarray`, optional 

90 N x 3 numpy array, where N is the number of bad pixels. 

91 The coordinates of the bad pixels to interpolate over in the first 

92 two columns, and the pixel values (unused) in the third column. 

93 If None, then the coordinates of the bad pixels are determined by 

94 an exhaustive search over the image. If ``goodpix`` is not 

95 provided, then this parameter is ignored. 

96 goodpix: `numpy.ndarray`, optional 

97 M x 3 numpy array, where M is the number of good pixels. 

98 The first two columns are the coordinates of the good pixels around 

99 ``badpix`` that must be included when constructing the interpolant. 

100 the interpolant. The values are populated from the image plane of 

101 the ``maskedImage`` and returned (provided values will be ignored). 

102 If ``badpix`` is not provided, then this parameter is ignored. 

103 

104 Returns 

105 ------- 

106 badpix: `numpy.ndarray` 

107 N x 3 numpy array, where N is the number of bad pixels. 

108 The coordinates of the bad pixels that were interpolated over are 

109 in the first two columns, and the corresponding pixel values in the 

110 third. If ``badpix`` was provided, this is the same as the input. 

111 goodpix: `numpy.ndarray` 

112 M x 3 numpy array, where M is the number of bad pixels. 

113 The coordinates of the good pixels that were used to construct the 

114 interpolant arein the first two columns, and the corresponding 

115 pixel values in the third. If ``goodpix`` was provided, the first 

116 two columns are same as the input, with the third column updated 

117 with the pixel values from the image plane of the ``maskedImage``. 

118 """ 

119 

120 if badpix is None or goodpix is None: 

121 badpix, goodpix = ctUtils.findGoodPixelsAroundBadPixels( 

122 maskedImage, 

123 self.config.badMaskPlanes, 

124 buffer=self.config.interpLength, 

125 ) 

126 else: 

127 # Even if badpix and goodpix is provided, make sure to update 

128 # the values of goodpix. 

129 ctUtils.updateArrayFromImage(goodpix, maskedImage.image) 

130 

131 # Construct the interpolant with goodpix. 

132 interpolator = CloughTocher2DInterpolator( 

133 list(zip(goodpix[:, 0], goodpix[:, 1])), 

134 goodpix[:, 2], 

135 fill_value=self.config.fillValue, 

136 ) 

137 

138 # Compute the interpolated values at bad pixel locations. 

139 badpix[:, 2] = interpolator(badpix[:, :2]) 

140 

141 # Fill in the bad pixels. 

142 ctUtils.updateImageFromArray(maskedImage.image, badpix) 

143 

144 return badpix, goodpix