Coverage for python/lsst/analysis/drp/_treecorrConfig.py: 54%

41 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-12-08 02:45 -0800

1# This file is part of analysis_drp. 

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 

23__all__ = ("BinnedCorr2Config",) 

24 

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

26 

27 

28class BinnedCorr2Config(Config): 

29 """A Config class that holds the various parameters supported by treecorr. 

30 

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

32 BinnedCorr2 in `treecorr`, which is the base class for all two-point 

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

34 are identical to the default values set in v4.2 of `treecorr`. The 

35 parameters that are excluded in this class are 

36 'verbose', 'log_file', 'output_dots', 'rng' and 'pairwise' (deprecated). 

37 For details about these options, see the documentation for `treecorr`: 

38 https://rmjarvis.github.io/TreeCorr/_build/html/correlation2.html 

39 

40 A separate config class is used instead 

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

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

43 execution. The ``toDict`` provides a minimal dictionary that override only 

44 the default values and excludes the key-values pairs when the item is None. 

45 

46 Notes 

47 ----- 

48 This is intended to be used in CalcRhoStatistics class. 

49 """ 

50 

51 nbins = Field( 51 ↛ exitline 51 didn't jump to the function exit

52 doc=( 

53 "How many bins to use. " 

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

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

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

57 "rounding up to the next highest integer. " 

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

59 "for this rounding up." 

60 ), 

61 dtype=int, 

62 optional=True, 

63 check=lambda x: x > 0, 

64 ) 

65 

66 bin_size = Field( 

67 doc=( 

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

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

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

71 "of the other three." 

72 ), 

73 dtype=float, 

74 optional=True, 

75 ) 

76 

77 min_sep = Field( 

78 doc=( 

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

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

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

82 "of the other three." 

83 ), 

84 dtype=float, 

85 optional=True, 

86 ) 

87 

88 max_sep = Field( 

89 doc=( 

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

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

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

93 "of the other three." 

94 ), 

95 dtype=float, 

96 optional=True, 

97 ) 

98 

99 sep_units = ChoiceField( 

100 doc=( 

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

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

103 "units of the output distance values." 

104 ), 

105 default="radian", 

106 dtype=str, 

107 optional=True, 

108 allowed={ 

109 units: units 

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

111 }, 

112 ) 

113 

114 bin_slop = Field( 

115 doc=( 

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

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

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

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

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

121 "applications." 

122 ), 

123 default=None, 

124 dtype=float, 

125 optional=True, 

126 ) 

127 

128 brute = Field( 

129 doc=( 

130 "Whether to use the 'brute force' algorithm? " 

131 "Unlike treecorr, setting this to 1 or 2 is not supported." 

132 ), 

133 default=False, 

134 dtype=bool, 

135 optional=True, 

136 ) 

137 

138 split_method = ChoiceField( 

139 doc=("How to split the cells in the tree when building the tree " "structure."), 

140 default="mean", 

141 dtype=str, 

142 optional=True, 

143 allowed={ 

144 "mean": "Use the arithmetic mean of the coordinate being split.", 

145 "median": "Use the median of the coordinate being split.", 

146 "middle": ( 

147 "Use the middle of the range; i.e. the average of the " 

148 "minimum and maximum value." 

149 ), 

150 "random": ( 

151 "Use a random point somewhere in the middle two " 

152 "quartiles of the range." 

153 ), 

154 }, 

155 ) 

156 

157 min_top = Field( 

158 doc=( 

159 "The minimum number of top layers to use when setting up the " 

160 "field. The top-level cells are where each calculation job " 

161 "starts." 

162 ), 

163 default=3, 

164 dtype=int, 

165 optional=True, 

166 ) 

167 

168 max_top = Field( 

169 doc=( 

170 "The maximum number of top layers to use when setting up the " 

171 "field. The top-level cells are where each calculation job " 

172 "starts. There will typically be of order 2**max_top cells." 

173 ), 

174 default=10, 

175 dtype=int, 

176 optional=True, 

177 ) 

178 

179 precision = Field( 179 ↛ exitline 179 didn't jump to the function exit

180 doc=( 

181 "The precision to use for the output values. This specifies how " 

182 "many digits to write." 

183 ), 

184 default=4, 

185 dtype=int, 

186 optional=True, 

187 check=lambda x: x > 0, 

188 ) 

189 

190 m2_uform = ChoiceField( 

191 doc="The default functional form to use for aperture mass calculations.", 

192 default="Crittenden", 

193 dtype=str, 

194 optional=True, 

195 allowed={ 

196 "Crittenden": "Crittenden et al. (2002); ApJ, 568, 20", 

197 "Schneider": "Schneider, et al (2002); A&A, 389, 729", 

198 }, 

199 ) 

200 

201 metric = ChoiceField( 

202 doc=( 

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

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

205 ), 

206 default="Euclidean", 

207 dtype=str, 

208 optional=True, 

209 allowed={ 

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

211 "FisherRperp": ( 

212 "the perpendicular component of the distance, " 

213 "following the definitions in " 

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

215 ), 

216 "OldRperp": ( 

217 "the perpendicular component of the distance using the " 

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

219 ), 

220 "Rlens": ( 

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

222 "the line connecting Earth and the second object " 

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

224 ), 

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

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

227 }, 

228 ) 

229 

230 bin_type = ChoiceField( 

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

232 default="Log", 

233 dtype=str, 

234 optional=True, 

235 allowed={ 

236 "Log": ( 

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

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

239 ), 

240 "Linear": ( 

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

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

243 ), 

244 "TwoD": ( 

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

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

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

248 ), 

249 }, 

250 ) 

251 

252 min_rpar = Field( 

253 doc=( 

254 "The minimum difference in Rparallel to allow for pairs being " 

255 "included in the correlation function. " 

256 ), 

257 default=None, 

258 dtype=float, 

259 optional=True, 

260 ) 

261 

262 max_rpar = Field( 

263 doc=( 

264 "The maximum difference in Rparallel to allow for pairs being " 

265 "included in the correlation function. " 

266 ), 

267 default=None, 

268 dtype=float, 

269 optional=True, 

270 ) 

271 

272 period = Field( 

273 doc="For the 'Periodic' metric, the period to use in all directions.", 

274 default=None, 

275 dtype=float, 

276 optional=True, 

277 ) 

278 

279 xperiod = Field( 

280 doc="For the 'Periodic' metric, the period to use in the x direction.", 

281 default=None, 

282 dtype=float, 

283 optional=True, 

284 ) 

285 

286 yperiod = Field( 

287 doc="For the 'Periodic' metric, the period to use in the y direction.", 

288 default=None, 

289 dtype=float, 

290 optional=True, 

291 ) 

292 

293 zperiod = Field( 

294 doc="For the 'Periodic' metric, the period to use in the z direction.", 

295 default=None, 

296 dtype=float, 

297 optional=True, 

298 ) 

299 

300 var_method = ChoiceField( 

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

302 default="shot", 

303 dtype=str, 

304 optional=True, 

305 allowed={ 

306 method: method 

307 for method in [ 

308 "shot", 

309 "jackknife", 

310 "sample", 

311 "bootstrap", 

312 "marked_bootstrap", 

313 ] 

314 }, 

315 ) 

316 

317 num_bootstrap = Field( 

318 doc=( 

319 "How many bootstrap samples to use for the 'bootstrap' and " 

320 "'marked_bootstrap' var methods." 

321 ), 

322 default=500, 

323 dtype=int, 

324 optional=True, 

325 ) 

326 

327 num_threads = Field( 

328 doc=( 

329 "How many OpenMP threads to use during the calculation. " 

330 "If set to None, use all the available CPU cores." 

331 ), 

332 default=None, 

333 dtype=int, 

334 optional=True, 

335 ) 

336 

337 def validate(self): 

338 # Docs inherited from base class 

339 super().validate() 

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

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

342 if num_req_params != 3: 

343 msg = ( 

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

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

346 ) 

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

348 

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

350 if self.min_sep > self.max_sep: 

351 raise FieldValidationError( 

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

353 ) 

354 

355 if self.min_rpar is not None and self.max_rpar is not None: 

356 if self.min_rpar > self.max_rpar: 

357 raise FieldValidationError( 

358 self.__class__.min_rpar, self, "min_rpar must be <= max_rpar" 

359 ) 

360 

361 def toDict(self): 

362 # Docs inherited from base class 

363 # We are excluding items that are None due to treecorr limitations. 

364 # TODO: DM-38480. This override can be removed after treecorr v4.3 is 

365 # released and makes its way onto rubin-env. 

366 return {k: v for k, v in super().toDict().items() if v is not None}