Coverage for pesummary/gw/file/formats/dingo.py: 24.1%

54 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2026-01-15 17:49 +0000

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

2 

3from pesummary.core.file.formats.dingo import Dingo as CoreDingo 

4from pesummary.gw.file.formats.base_read import GWSingleAnalysisRead 

5from pesummary.utils.utils import logger 

6 

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

8 

9 

10def read_dingo(path, disable_prior=False, **kwargs): 

11 """Grab the parameters and samples in a dingo file 

12 

13 Parameters 

14 ---------- 

15 path: str 

16 path to the result file you wish to read in 

17 disable_prior: Bool, optional 

18 if True, do not collect prior samples from the `dingo` result file. 

19 Default False 

20 """ 

21 from pesummary.core.file.formats.dingo import read_dingo as _read_dingo 

22 

23 return _read_dingo(path, disable_prior=disable_prior, _dingo_class=Dingo, **kwargs) 

24 

25 

26class Dingo(GWSingleAnalysisRead): 

27 """PESummary wrapper of `dingo` (https://github.com/dingo-gw/dingo). The 

28 path_to_results_file argument will be passed directly to 

29 `bilby.gw.result.Result`. All functions therefore use `dingo` 

30 methods and requires `dingo` to be installed. 

31 

32 Parameters 

33 ---------- 

34 path_to_results_file: str 

35 path to the results file that you wish to read in with `dingo`. 

36 disable_prior: Bool, optional 

37 if True, do not collect prior samples from the `dingo` result file. 

38 Default False 

39 disable_prior_conversion: Bool, optional 

40 if True, disable the conversion module from deriving alternative prior 

41 distributions. Default False 

42 pe_algorithm: str 

43 name of the algorithm used to generate the posterior samples 

44 remove_nan_likelihood_samples: Bool, optional 

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

46 

47 Attributes 

48 ---------- 

49 parameters: list 

50 list of parameters stored in the result file 

51 converted_parameters: list 

52 list of parameters that have been derived from the sampled distributions 

53 samples: 2d list 

54 list of samples stored in the result file 

55 samples_dict: dict 

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

57 input_version: str 

58 version of the result file passed. 

59 extra_kwargs: dict 

60 dictionary of kwargs that were extracted from the result file 

61 prior: dict 

62 dictionary of prior samples extracted from the bilby result file. The 

63 analytic priors are evaluated for 5000 samples 

64 injection_parameters: dict 

65 dictionary of injection parameters stored in the result file 

66 converted_parameters: list 

67 list of parameters that have been added 

68 

69 Methods 

70 ------- 

71 to_dat: 

72 save the posterior samples to a .dat file 

73 to_latex_table: 

74 convert the posterior samples to a latex table 

75 generate_latex_macros: 

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

77 to_lalinference: 

78 convert the posterior samples to a lalinference result file 

79 generate_all_posterior_samples: 

80 generate all posterior distributions that may be derived from 

81 sampled distributions 

82 """ 

83 

84 def __init__(self, path_to_results_file, **kwargs): 

85 super().__init__(path_to_results_file, **kwargs) 

86 self.load(self._grab_data_from_dingo_file, **kwargs) 

87 

88 @staticmethod 

89 def grab_priors(dingo_object, nsamples=5000): 

90 """Draw samples from the prior functions stored in the dingo object""" 

91 from pesummary.utils.array import Array 

92 

93 f = dingo_object 

94 try: 

95 samples = f.pesummary_prior.sample(size=nsamples) 

96 priors = {key: Array(samples[key]) for key in samples} 

97 except Exception as e: 

98 logger.info("Failed to draw prior samples because {}".format(e)) 

99 priors = {} 

100 return priors 

101 

102 @staticmethod 

103 def grab_extra_kwargs(dingo_object): 

104 """Grab any additional information stored in the dingo file""" 

105 f = dingo_object 

106 kwargs = CoreDingo.grab_extra_kwargs(dingo_object) 

107 try: 

108 kwargs["meta_data"]["f_ref"] = f.f_ref 

109 except Exception: 

110 pass 

111 try: 

112 kwargs["meta_data"]["approximant"] = f.approximant 

113 except Exception: 

114 pass 

115 try: 

116 kwargs["meta_data"]["IFOs"] = " ".join(f.interferometers) 

117 except Exception: 

118 pass 

119 try: 

120 kwargs["meta_data"]["f_low"] = f.domain.f_min 

121 except Exception: 

122 pass 

123 try: 

124 kwargs["meta_data"]["delta_f"] = f.domain.delta_f 

125 except Exception: 

126 pass 

127 try: 

128 kwargs["meta_data"]["f_final"] = f.domain.f_max 

129 except Exception: 

130 pass 

131 return kwargs 

132 

133 @staticmethod 

134 def _grab_data_from_dingo_file(path, disable_prior=False, **kwargs): 

135 """ 

136 Load the results file using the `dingo` library 

137 

138 Complex matched filter SNRs are stored in the result file. 

139 The amplitude and angle are extracted here. 

140 """ 

141 return read_dingo(path, disable_prior=disable_prior, **kwargs) 

142 

143 # TODO: Load strain data and PSD? Calibration spline posterior? Complex params? 

144 # Latex labels?