Coverage for pesummary/core/file/formats/default.py: 91.2%

80 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3import numpy as np 

4import os 

5from pesummary.core.file.formats.base_read import ( 

6 Read, SingleAnalysisRead, MultiAnalysisRead 

7) 

8 

9__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

10 

11 

12class SingleAnalysisDefault(SingleAnalysisRead): 

13 """Class to handle result files which only contain a single analysis 

14 

15 Parameters 

16 ---------- 

17 path_to_results_file: str 

18 path to the results file you wish to load 

19 remove_nan_likelihood_samples: Bool, optional 

20 if True, remove samples which have log_likelihood='nan'. Default True 

21 

22 Attributes 

23 ---------- 

24 parameters: list 

25 list of parameters stored in the result file 

26 samples: 2d list 

27 list of samples stored in the result file 

28 samples_dict: dict 

29 dictionary of samples stored in the result file keyed by parameters 

30 input_version: str 

31 version of the result file passed. 

32 extra_kwargs: dict 

33 dictionary of kwargs that were extracted from the result file 

34 injection_parameters: dict 

35 dictionary of injection parameters extracted from the result file 

36 pe_algorithm: str 

37 name of the algorithm used to generate the posterior samples 

38 

39 Methods 

40 ------- 

41 to_dat: 

42 save the posterior samples to a .dat file 

43 to_latex_table: 

44 convert the posterior samples to a latex table 

45 generate_latex_macros: 

46 generate a set of latex macros for the stored posterior samples 

47 """ 

48 def __init__(self, *args, _data=None, **kwargs): 

49 super(SingleAnalysisDefault, self).__init__(*args, **kwargs) 

50 if _data is not None: 

51 self.load(None, _data=_data, **kwargs) 

52 

53 

54class MultiAnalysisDefault(MultiAnalysisRead): 

55 """Class to handle result files which contain multiple analyses 

56 

57 Parameters 

58 ---------- 

59 path_to_results_file: str 

60 path to the results file you wish to load 

61 remove_nan_likelihood_samples: Bool, optional 

62 if True, remove samples which have log_likelihood='nan'. Default True 

63 

64 Attributes 

65 ---------- 

66 parameters: 2d list 

67 list of parameters stored in the result file for each analyses 

68 samples: 2d list 

69 list of samples stored in the result file for each analyses 

70 samples_dict: dict 

71 dictionary of samples stored in the result file keyed by analysis label 

72 input_version: str 

73 version of the result file passed. 

74 extra_kwargs: dict 

75 dictionary of kwargs that were extracted from the result file 

76 injection_parameters: dict 

77 dictionary of injection parameters extracted from the result file 

78 

79 Methods 

80 ------- 

81 samples_dict_for_label: dict 

82 dictionary of samples for a specific analysis 

83 reduced_samples_dict: dict 

84 dictionary of samples for one or more analyses 

85 to_dat: 

86 save the posterior samples to a .dat file 

87 to_latex_table: 

88 convert the posterior samples to a latex table 

89 generate_latex_macros: 

90 generate a set of latex macros for the stored posterior samples 

91 """ 

92 def __init__(self, *args, _data=None, **kwargs): 

93 super(MultiAnalysisDefault, self).__init__(*args, **kwargs) 

94 if _data is not None: 

95 self.load(None, _data=_data, **kwargs) 

96 

97 

98class Default(object): 

99 """Class to handle the default loading options. 

100 

101 Attributes 

102 ---------- 

103 path_to_results_file: str 

104 path to the results file you wish to load 

105 remove_nan_likelihood_samples: Bool, optional 

106 if True, remove samples which have log_likelihood='nan'. Default True 

107 

108 Attributes 

109 ---------- 

110 parameters: list 

111 list of parameters stored in the result file 

112 samples: 2d list 

113 list of samples stored in the result file 

114 samples_dict: dict 

115 dictionary of samples stored in the result file keyed by parameters 

116 input_version: str 

117 version of the result file passed. 

118 extra_kwargs: dict 

119 dictionary of kwargs that were extracted from the result file 

120 injection_parameters: dict 

121 dictionary of injection parameters extracted from the result file 

122 

123 Methods 

124 ------- 

125 to_dat: 

126 save the posterior samples to a .dat file 

127 to_latex_table: 

128 convert the posterior samples to a latex table 

129 generate_latex_macros: 

130 generate a set of latex macros for the stored posterior samples 

131 """ 

132 def load_map(self): 

133 return { 

134 "json": self._grab_data_from_json_file, 

135 "dat": self._grab_data_from_dat_file, 

136 "txt": self._grab_data_from_dat_file, 

137 "csv": self._grab_data_from_csv_file, 

138 "hdf5": self._grab_data_from_hdf5_file, 

139 "h5": self._grab_data_from_hdf5_file, 

140 "hdf": self._grab_data_from_hdf5_file, 

141 "db": self._grab_data_from_sql_database, 

142 "sql": self._grab_data_from_sql_database, 

143 "prior": self._grab_data_from_prior_file, 

144 "npy": self._grab_data_from_numpy_file 

145 } 

146 

147 def __new__( 

148 self, path_to_results_file, _single_default=SingleAnalysisDefault, 

149 _multi_default=MultiAnalysisDefault, **kwargs 

150 ): 

151 self.module = "core" 

152 self.extension = Read.extension_from_path(path_to_results_file) 

153 self.load_function = self.load_map(self)[self.extension] 

154 try: 

155 self._load_data = self.load_function(path_to_results_file, **kwargs) 

156 except Exception as e: 

157 raise Exception( 

158 "Failed to read data for file %s because: %s" % ( 

159 path_to_results_file, e 

160 ) 

161 ) 

162 if np.array(self._load_data["parameters"]).ndim > 1: 

163 return _multi_default( 

164 path_to_results_file, _data=self._load_data, **kwargs 

165 ) 

166 else: 

167 return _single_default( 

168 path_to_results_file, _data=self._load_data, **kwargs 

169 ) 

170 

171 @classmethod 

172 def load_file(cls, path, **kwargs): 

173 if not os.path.isfile(path): 

174 raise FileNotFoundError("%s does not exist" % (path)) 

175 return cls(path, **kwargs) 

176 

177 @staticmethod 

178 def _default_injection(parameters): 

179 """Return a dictionary of nan's for each parameter 

180 

181 Parameters 

182 ---------- 

183 parameters: tuple/list 

184 list of parameters you wish to have null injections for 

185 """ 

186 return {i: float("nan") for i in parameters} 

187 

188 @staticmethod 

189 def _grab_data_from_dat_file(path, **kwargs): 

190 """Grab the data stored in a .dat file 

191 """ 

192 from pesummary.core.file.formats.dat import read_dat 

193 

194 parameters, samples = read_dat(path) 

195 return { 

196 "parameters": parameters, "samples": samples, 

197 "injection": Default._default_injection(parameters) 

198 } 

199 

200 @staticmethod 

201 def _grab_data_from_numpy_file(path, **kwargs): 

202 """Grab the data stored in a .npy file 

203 """ 

204 from pesummary.core.file.formats.numpy import read_numpy 

205 

206 parameters, samples = read_numpy(path) 

207 return { 

208 "parameters": parameters, "samples": samples, 

209 "injection": Default._default_injection(parameters) 

210 } 

211 

212 @staticmethod 

213 def _grab_data_from_csv_file(path, **kwargs): 

214 """Grab the data stored in a .csv file 

215 """ 

216 from pesummary.core.file.formats.csv import read_csv 

217 

218 parameters, samples = read_csv(path) 

219 return { 

220 "parameters": parameters, "samples": samples, 

221 "injection": Default._default_injection(parameters) 

222 } 

223 

224 @staticmethod 

225 def _grab_data_from_prior_file(path, module="core", **kwargs): 

226 """Grab the data stored in a .prior file 

227 """ 

228 import importlib 

229 

230 module = importlib.import_module( 

231 "pesummary.{}.file.formats.bilby".format(module) 

232 ) 

233 func = getattr(module, "prior_samples_from_file") 

234 

235 samples = func(path, **kwargs) 

236 parameters = samples.parameters 

237 analytic = samples.analytic 

238 return { 

239 "parameters": parameters, "samples": samples.samples.T.tolist(), 

240 "injection": Default._default_injection(parameters), 

241 "analytic": analytic 

242 } 

243 

244 @staticmethod 

245 def _grab_data_from_sql_database(path, **kwargs): 

246 """Grab the data stored in a sql database 

247 """ 

248 from pesummary.core.file.formats.sql import read_sql 

249 

250 parameters, samples, labels = read_sql(path, **kwargs) 

251 if len(labels) > 1: 

252 injection = { 

253 label: Default._default_injection(parameters[num]) for num, label 

254 in enumerate(labels) 

255 } 

256 else: 

257 injection = Default._default_injection(parameters) 

258 return { 

259 "parameters": parameters, "samples": samples, "injection": injection, 

260 "labels": labels 

261 } 

262 

263 @staticmethod 

264 def _grab_data_from_json_file(path, path_to_samples=None, **kwargs): 

265 """Grab the data stored in a .json file 

266 """ 

267 from pesummary.core.file.formats.json import read_json 

268 

269 parameters, samples = read_json(path, path_to_samples=path_to_samples) 

270 return { 

271 "parameters": parameters, "samples": samples, 

272 "injection": Default._default_injection(parameters) 

273 } 

274 

275 @staticmethod 

276 def _grab_data_from_hdf5_file( 

277 path, remove_params=[], path_to_samples=None, **kwargs 

278 ): 

279 """Grab the data stored in an hdf5 file 

280 """ 

281 from pesummary.core.file.formats.hdf5 import read_hdf5 

282 

283 parameters, samples = read_hdf5( 

284 path, remove_params=remove_params, path_to_samples=path_to_samples 

285 ) 

286 return { 

287 "parameters": parameters, "samples": samples, 

288 "injection": Default._default_injection(parameters) 

289 } 

290 

291 def add_marginalized_parameters_from_config_file(self, config_file): 

292 """Search the configuration file and add the marginalized parameters 

293 to the list of parameters and samples 

294 

295 Parameters 

296 ---------- 

297 config_file: str 

298 path to the configuration file 

299 """ 

300 pass