Coverage for metadetect / lsst / masking.py: 5%

69 statements  

« prev     ^ index     » next       coverage.py v7.13.2, created at 2026-02-21 01:40 -0800

1import numpy as np 

2 

3EXPAND_RAD = 16 

4AP_RAD = 1.5 

5 

6 

7def apply_apodized_edge_masks_mbexp( 

8 mbexp, noise_mbexp=None, mfrac_mbexp=None, ormasks=None, 

9): 

10 """ 

11 Apply apodized edge masks. The .image, .variance and .bmask for each 

12 exposure is modified in place 

13 

14 Parameters 

15 ---------- 

16 mbexp: lsst.afw.image.MultibandExposure 

17 The data to mask. The image and mask are modified, with the mask 

18 value set to BRIGHT or BRIGHT_EXPANDED for the expanded mask. 

19 noise_mbexp: lsst.afw.image.MultibandExposure 

20 Optional noise data to mask. The image and mask are modified. 

21 mfrac_mbexp: lsst.afw.image.MultibandExposure 

22 Optional mfrac data to mask. The image and mask are modified, with 

23 mfrac set to 1 everywhere that apodization is applied 

24 ormasks: list of arrays, optional 

25 A list of masks to logically or with the bright mask 

26 """ 

27 

28 import lsst.afw.image as afw_image 

29 from ..masking import _build_square_apodization_mask 

30 

31 afw_image.Mask.addMaskPlane('APODIZED_EDGE') 

32 edge = afw_image.Mask.getPlaneBitMask('APODIZED_EDGE') 

33 

34 bands = mbexp.bands 

35 band0 = bands[0] 

36 ap_mask = np.ones_like(mbexp[band0].image.array) 

37 _build_square_apodization_mask(AP_RAD, ap_mask) 

38 

39 msk = np.where(ap_mask < 1) 

40 if msk[0].size > 0: 

41 for band in bands: 

42 exps = [mbexp[band]] 

43 if noise_mbexp is not None: 

44 exps.append(noise_mbexp[band]) 

45 

46 for exp in exps: 

47 exp.image.array[msk] *= ap_mask[msk] 

48 exp.variance.array[msk] = np.inf 

49 exp.mask.array[msk] |= edge 

50 

51 if ormasks is not None: 

52 for ormask in ormasks: 

53 ormask[msk] |= edge 

54 

55 if mfrac_mbexp is not None: 

56 mfrac_mbexp[band].image.array[msk] = 1.0 

57 

58 

59def apply_apodized_bright_masks_mbexp( 

60 mbexp, bright_info, noise_mbexp=None, mfrac_mbexp=None, ormasks=None, 

61): 

62 """ 

63 Apply bright object masks with apodization and expansion. The .image, 

64 .variance and .bmask for each exposure is modified in place 

65 

66 Parameters 

67 ---------- 

68 mbexp: lsst.afw.image.MultibandExposure 

69 The data to mask. The image and mask are modified, with the mask 

70 value set to BRIGHT or BRIGHT_EXPANDED for the expanded mask. 

71 bright_info: structured array 

72 Array with fields ra, dec, radius_pixels 

73 noise_mbexp: lsst.afw.image.MultibandExposure 

74 Optional noise data to mask. The image and mask are modified. 

75 mfrac_mbexp: lsst.afw.image.MultibandExposure 

76 Optional mfrac data to mask. The image and mask are modified, with 

77 mfrac set to 1 everywhere that apodization is applied 

78 ormasks: list of arrays, optional 

79 A list of masks to logically or with the bright mask 

80 """ 

81 import lsst.afw.image as afw_image 

82 from ..masking import ( 

83 make_foreground_apodization_mask, make_foreground_bmask, 

84 ) 

85 bands = mbexp.bands 

86 

87 afw_image.Mask.addMaskPlane('BRIGHT') 

88 afw_image.Mask.addMaskPlane('BRIGHT_EXPANDED') 

89 bright = afw_image.Mask.getPlaneBitMask('BRIGHT') 

90 bright_expanded = afw_image.Mask.getPlaneBitMask('BRIGHT_EXPANDED') 

91 

92 wcs = mbexp[bands[0]].getWcs() 

93 xy0 = mbexp[bands[0]].getXY0() 

94 

95 # these will be in a possibly larger coordinate system 

96 xm, ym = wcs.skyToPixelArray( 

97 ra=bright_info['ra'], dec=bright_info['dec'], degrees=True, 

98 ) 

99 

100 # put in local coordinate system 

101 xm -= xy0.x 

102 ym -= xy0.y 

103 rm = bright_info['radius_pixels'] 

104 

105 dims = mbexp[bands[0]].image.array.shape 

106 

107 ap_mask = make_foreground_apodization_mask( 

108 xm=xm, 

109 ym=ym, 

110 rm=rm, 

111 dims=dims, 

112 symmetrize=False, 

113 ap_rad=AP_RAD, 

114 ) 

115 

116 msk = np.where(ap_mask < 1) 

117 if msk[0].size > 0: 

118 for band in bands: 

119 

120 exps = [mbexp[band]] 

121 if noise_mbexp is not None: 

122 exps.append(noise_mbexp[band]) 

123 

124 for exp in exps: 

125 exp.image.array[msk] *= ap_mask[msk] 

126 exp.variance.array[msk] = np.inf 

127 exp.mask.array[msk] |= bright 

128 

129 if ormasks is not None: 

130 for ormask in ormasks: 

131 ormask[msk] |= bright 

132 

133 if mfrac_mbexp is not None: 

134 mfrac_mbexp[band].image.array[msk] = 1.0 

135 

136 expanded_bmask = make_foreground_bmask( 

137 xm=xm, 

138 ym=ym, 

139 rm=rm + EXPAND_RAD, 

140 dims=dims, 

141 symmetrize=False, 

142 mask_bit_val=bright_expanded, 

143 ) 

144 if np.any(expanded_bmask): 

145 for band in bands: 

146 exps = [mbexp[band]] 

147 if noise_mbexp is not None: 

148 exps.append(noise_mbexp[band]) 

149 

150 for exp in exps: 

151 exp.mask.array[:, :] |= expanded_bmask 

152 

153 if ormasks is not None: 

154 for ormask in ormasks: 

155 ormask |= expanded_bmask