Coverage for python / lsst / meas / algorithms / treecorrUtils.py: 52%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-18 08:51 +0000

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 

23from lsst.pex.config import ChoiceField, Config, Field, FieldValidationError 

24 

25 

26class TreecorrConfig(Config): 

27 """A Config class that holds some of the parameters supported by treecorr. 

28 

29 The fields in this class correspond to the parameters that can be passed to 

30 any calls to `treecorr` methods, including catalog creation and two-point 

31 correlation function calculations. The default values set for the fields 

32 are identical to the default values set in v4.3 of `treecorr`. 

33 

34 A separate config class is used instead 

35 of constructing a `~lsst.pex.config.DictField` so that mixed types can be 

36 supported and the config can be validated at the beginning of the 

37 execution. 

38 

39 Notes 

40 ----- 

41 This is intended to be used with correlations of PSF residuals. It only supports 

42 some of the fields that are relevant for rho-statistics calculations and the likes 

43 of it. 

44 """ 

45 

46 nbins = Field[int]( 

47 doc=( 

48 "How many bins to use. " 

49 "(Exactly three of nbins, bin_size, min_sep, max_sep " 

50 "are required. If nbins is not given, it will be " 

51 "calculated from the values of the other three, " 

52 "rounding up to the next highest integer. " 

53 "In this case, bin_size will be readjusted to account " 

54 "for this rounding up." 

55 ), 

56 optional=True, 

57 check=lambda x: x > 0, 

58 ) 

59 

60 bin_size = Field[float]( 

61 doc=( 

62 "The width of the bins in log(separation). " 

63 "Exactly three of nbins, bin_size, min_sep, max_sep are required. " 

64 "If bin_size is not given, it will be calculated from the values " 

65 "of the other three." 

66 ), 

67 optional=True, 

68 ) 

69 

70 min_sep = Field[float]( 

71 doc=( 

72 "The minimum separation in units of sep_units, if relevant. " 

73 "Exactly three of nbins, bin_size, min_sep, max_sep are required. " 

74 "If min_sep is not given, it will be calculated from the values " 

75 "of the other three." 

76 ), 

77 optional=True, 

78 ) 

79 

80 max_sep = Field[float]( 

81 doc=( 

82 "The maximum separation in units of sep_units, if relevant. " 

83 "Exactly three of nbins, bin_size, min_sep, max_sep are required. " 

84 "If max_sep is not given, it will be calculated from the values " 

85 "of the other three." 

86 ), 

87 optional=True, 

88 ) 

89 

90 sep_units = ChoiceField[str]( 

91 doc=( 

92 "The units to use for the separation values, given as a string. " 

93 "This includes both min_sep and max_sep above, as well as the " 

94 "units of the output distance values." 

95 ), 

96 default=None, 

97 optional=True, 

98 allowed={ 

99 units: units for units in ["arcsec", "arcmin", "degree", "hour", "radian"] 

100 }, 

101 ) 

102 

103 bin_slop = Field[float]( 

104 doc=( 

105 "How much slop to allow in the placement of pairs in the bins. " 

106 "If bin_slop = 1, then the bin into which a particular pair is " 

107 "placed may be incorrect by at most 1.0 bin widths. " 

108 r"If None, use a bin_slop that gives a maximum error of 10% on " 

109 "any bin, which has been found to yield good results for most " 

110 "applications." 

111 ), 

112 default=None, 

113 optional=True, 

114 ) 

115 

116 precision = Field[int]( 

117 doc=( 

118 "The precision to use for the output values. This specifies how many digits to write." 

119 ), 

120 default=4, 

121 optional=True, 

122 check=lambda x: x > 0, 

123 ) 

124 

125 metric = ChoiceField[str]( 

126 doc=( 

127 "Which metric to use for distance measurements. For details, see " 

128 "https://rmjarvis.github.io/TreeCorr/_build/html/metric.html" 

129 ), 

130 default="Euclidean", 

131 optional=True, 

132 allowed={ 

133 "Euclidean": "straight-line Euclidean distance between two points", 

134 "FisherRperp": ( 

135 "the perpendicular component of the distance, " 

136 "following the definitions in " 

137 "Fisher et al, 1994 (MNRAS, 267, 927)" 

138 ), 

139 "OldRperp": ( 

140 "the perpendicular component of the distance using the " 

141 "definition of Rperp from TreeCorr v3.x." 

142 ), 

143 "Rlens": ( 

144 "Distance from the first object (taken to be a lens) to " 

145 "the line connecting Earth and the second object " 

146 "(taken to be a lensed source)." 

147 ), 

148 "Arc": "the true great circle distance for spherical coordinates.", 

149 "Periodic": "Like ``Euclidean``, but with periodic boundaries.", 

150 }, 

151 ) 

152 

153 bin_type = ChoiceField[str]( 

154 doc="What type of binning should be used?", 

155 default="Log", 

156 optional=True, 

157 allowed={ 

158 "Log": ( 

159 "Logarithmic binning in the distance. The bin steps will " 

160 "be uniform in log(r) from log(min_sep) .. log(max_sep)." 

161 ), 

162 "Linear": ( 

163 "Linear binning in the distance. The bin steps will be " 

164 "uniform in r from min_sep .. max_sep." 

165 ), 

166 "TwoD": ( 

167 "2-dimensional binning from x = (-max_sep .. max_sep) " 

168 "and y = (-max_sep .. max_sep). The bin steps will be " 

169 "uniform in both x and y. (i.e. linear in x,y)" 

170 ), 

171 }, 

172 ) 

173 

174 var_method = ChoiceField[str]( 

175 doc="Which method to use for estimating the variance", 

176 default="shot", 

177 optional=True, 

178 allowed={ 

179 method: method 

180 for method in [ 

181 "shot", 

182 "jackknife", 

183 "sample", 

184 "bootstrap", 

185 "marked_bootstrap", 

186 ] 

187 }, 

188 ) 

189 

190 npatch = Field[int]( 

191 doc="How many patches to split the catalog into for the purpose of " 

192 "jackknife variance or other options that involve running via " 

193 "patches (boostrap, marked_boostrap etc.)", 

194 default=1, 

195 optional=True, 

196 ) 

197 

198 num_bootstrap = Field[int]( 

199 doc=( 

200 "How many bootstrap samples to use for the 'bootstrap' and 'marked_bootstrap' var methods." 

201 ), 

202 default=500, 

203 optional=True, 

204 ) 

205 

206 rng_seed = Field[int]( 

207 doc="Value to seed the treecorr random number generator with. Used to generate patches.", 

208 default=13579, 

209 ) 

210 

211 def validate(self): 

212 # Docs inherited from base class 

213 super().validate() 

214 req_params = (self.nbins, self.bin_size, self.min_sep, self.max_sep) 

215 num_req_params = sum(param is not None for param in req_params) 

216 if num_req_params != 3: 

217 msg = ( 

218 "You must specify exactly three of ``nbins``, ``bin_size``, ``min_sep`` and ``max_sep``" 

219 f" in treecorr_config. {num_req_params} parameters were set instead." 

220 ) 

221 raise FieldValidationError(self.__class__.bin_size, self, msg) 

222 

223 if self.min_sep is not None and self.max_sep is not None: 

224 if self.min_sep > self.max_sep: 

225 raise FieldValidationError( 

226 self.__class__.min_sep, self, "min_sep must be <= max_sep" 

227 )