Coverage for parallel_bilby/parser/shared.py: 98%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

54 statements  

1import argparse 

2 

3import bilby 

4import bilby_pipe 

5from numpy import inf 

6 

7from ..utils import get_version_information 

8 

9logger = bilby.core.utils.logger 

10 

11__version__ = get_version_information() 

12 

13 

14class StoreBoolean(argparse.Action): 

15 """argparse class for robust handling of booleans with configargparse 

16 

17 When using configargparse, if the argument is setup with 

18 action="store_true", but the default is set to True, then there is no way, 

19 in the config file to switch the parameter off. To resolve this, this class 

20 handles the boolean properly. 

21 

22 """ 

23 

24 def __call__(self, parser, namespace, value, option_string=None): 

25 value = str(value).lower() 

26 if value in ["true"]: 

27 setattr(namespace, self.dest, True) 

28 else: 

29 setattr(namespace, self.dest, False) 

30 

31 

32def _create_base_parser(sampler="dynesty"): 

33 base_parser = argparse.ArgumentParser("base", add_help=False) 

34 base_parser.add( 

35 "--version", 

36 action="version", 

37 version=f"%(prog)s={__version__}\nbilby={bilby.__version__}", 

38 ) 

39 if sampler in ["all", "dynesty"]: 

40 base_parser = _add_dynesty_settings_to_parser(base_parser) 

41 base_parser = _add_misc_settings_to_parser(base_parser) 

42 return base_parser 

43 

44 

45def _add_dynesty_settings_to_parser(parser): 

46 dynesty_group = parser.add_argument_group(title="Dynesty Settings") 

47 dynesty_group.add_argument( 

48 "-n", "--nlive", default=1000, type=int, help="Number of live points" 

49 ) 

50 dynesty_group.add_argument( 

51 "--dlogz", 

52 default=0.1, 

53 type=float, 

54 help="Stopping criteria: remaining evidence, (default=0.1)", 

55 ) 

56 dynesty_group.add_argument( 

57 "--n-effective", 

58 default=inf, 

59 type=float, 

60 help="Stopping criteria: effective number of samples, (default=inf)", 

61 ) 

62 dynesty_group.add_argument( 

63 "--dynesty-sample", 

64 default="acceptance-walk", 

65 type=str, 

66 help="Dynesty sampling method (default=acceptance-walk). " 

67 "Note, the dynesty rwalk method is overwritten by parallel bilby for an optimised version ", 

68 ) 

69 dynesty_group.add_argument( 

70 "--dynesty-bound", 

71 default="live", 

72 type=str, 

73 help="Dynesty bounding method (default=multi)", 

74 ) 

75 dynesty_group.add_argument( 

76 "--walks", 

77 default=100, 

78 type=int, 

79 help="Minimum number of walks, defaults to 100", 

80 ) 

81 dynesty_group.add_argument( 

82 "--proposals", 

83 type=bilby_pipe.utils.nonestr, 

84 action="append", 

85 default=None, 

86 help="The jump proposals to use, the options are 'diff' and 'volumetric'", 

87 ) 

88 dynesty_group.add_argument( 

89 "--maxmcmc", 

90 default=5000, 

91 type=int, 

92 help="Maximum number of walks, defaults to 5000", 

93 ) 

94 dynesty_group.add_argument( 

95 "--nact", 

96 default=2, 

97 type=int, 

98 help="Number of autocorrelation times to take, defaults to 2", 

99 ) 

100 dynesty_group.add_argument( 

101 "--naccept", 

102 default=60, 

103 type=int, 

104 help="The average number of accepted steps per MCMC chain, defaults to 60", 

105 ) 

106 dynesty_group.add_argument( 

107 "--min-eff", 

108 default=10, 

109 type=float, 

110 help="The minimum efficiency at which to switch from uniform sampling.", 

111 ) 

112 dynesty_group.add_argument( 

113 "--facc", default=0.5, type=float, help="See dynesty.NestedSampler" 

114 ) 

115 dynesty_group.add_argument( 

116 "--enlarge", default=1.5, type=float, help="See dynesty.NestedSampler" 

117 ) 

118 dynesty_group.add_argument( 

119 "--n-check-point", 

120 default=1000, 

121 type=int, 

122 help="Steps to take before attempting checkpoint", 

123 ) 

124 dynesty_group.add_argument( 

125 "--max-its", 

126 default=10**10, 

127 type=int, 

128 help="Maximum number of iterations to sample for (default=1.e10)", 

129 ) 

130 dynesty_group.add_argument( 

131 "--max-run-time", 

132 default=1.0e10, 

133 type=float, 

134 help="Maximum time to run for (default=1.e10 s)", 

135 ) 

136 dynesty_group.add_argument( 

137 "--fast-mpi", 

138 default=False, 

139 type=bool, 

140 help="Fast MPI communication pattern (default=False)", 

141 ) 

142 dynesty_group.add_argument( 

143 "--mpi-timing", 

144 default=False, 

145 type=bool, 

146 help="Print MPI timing when finished (default=False)", 

147 ) 

148 dynesty_group.add_argument( 

149 "--mpi-timing-interval", 

150 default=0, 

151 type=int, 

152 help="Interval to write timing snapshot to disk (default=0 -- disabled)", 

153 ) 

154 dynesty_group.add_argument( 

155 "--nestcheck", 

156 default=False, 

157 action="store_true", 

158 help=( 

159 "Save a 'nestcheck' pickle in the outdir (default=False). " 

160 "This pickle stores a `nestcheck.data_processing.process_dynesty_run` " 

161 "object, which can be used during post processing to compute the " 

162 "implementation and bootstrap errors explained by Higson et al (2018) " 

163 "in “Sampling Errors In Nested Sampling Parameter Estimation”." 

164 ), 

165 ) 

166 dynesty_group.add_argument( 

167 "--rejection-sample-posterior", 

168 default=True, 

169 action=StoreBoolean, 

170 help=( 

171 "Whether to generate the posterior samples by rejection sampling the " 

172 "nested samples or resampling with replacement" 

173 ), 

174 ) 

175 return parser 

176 

177 

178def _add_misc_settings_to_parser(parser): 

179 misc_group = parser.add_argument_group(title="Misc. Settings") 

180 misc_group.add_argument( 

181 "--bilby-zero-likelihood-mode", default=False, action="store_true" 

182 ) 

183 misc_group.add_argument( 

184 "--sampling-seed", 

185 type=bilby_pipe.utils.noneint, 

186 default=None, 

187 help="Random seed for sampling, parallel runs will be incremented", 

188 ) 

189 misc_group.add_argument( 

190 "-c", "--clean", action="store_true", help="Run clean: ignore any resume files" 

191 ) 

192 misc_group.add_argument( 

193 "--no-plot", 

194 action="store_true", 

195 help="If true, don't generate check-point plots", 

196 ) 

197 misc_group.add_argument( 

198 "--do-not-save-bounds-in-resume", 

199 default=True, 

200 action="store_true", 

201 help=( 

202 "If true, do not store bounds in the resume file. This can make " 

203 "resume files large (~GB)" 

204 ), 

205 ) 

206 misc_group.add_argument( 

207 "--check-point-deltaT", 

208 default=3600, 

209 type=float, 

210 help="Write a checkpoint resume file and diagnostic plots every deltaT [s]. Default: 1 hour.", 

211 ) 

212 misc_group.add_argument( 

213 "--rotate-checkpoints", 

214 action="store_true", 

215 help="If true, backup checkpoint before overwriting (ending in '.bk').", 

216 ) 

217 return parser