Coverage for pesummary/gw/file/formats/pycbc.py: 28.3%

46 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-12-09 22:34 +0000

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

2 

3error_msg = ( 

4 "Unable to install '{}'. You will not be able to use some of the inbuilt " 

5 "functions." 

6) 

7import numpy as np 

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

9from pesummary import conf 

10from pesummary.core.file.formats.ini import read_ini 

11from pesummary.utils.utils import logger 

12try: 

13 from pycbc.inference.io import loadfile 

14except ImportError: 

15 logger.warning(error_msg.format("pycbc")) 

16 

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

18 

19def read_pycbc(path, **kwargs): 

20 """Grab the parameters and samples in a pycbc file 

21 

22 Parameters 

23 ---------- 

24 path: str 

25 path to the result file you wish to read in 

26 """ 

27 with loadfile(path, "r") as f: 

28 params = list(f["samples"].keys()) 

29 _samples = f.read_samples(params) 

30 samples = {key: _samples[key] for key in _samples.dtype.names} 

31 try: 

32 from pycbc.conversions import snr_from_loglr 

33 samples["network_matched_filter_snr"] = snr_from_loglr( 

34 samples["loglikelihood"] - _samples.lognl 

35 ) 

36 except AttributeError: 

37 pass 

38 try: 

39 config = read_ini(f.read_config_file(return_cp=False).read()) 

40 except (KeyError, IndexError): 

41 # no config file stored 

42 config = None 

43 extra_kwargs = { 

44 "sampler": {}, "meta_data": {}, "other": dict(f.attrs) 

45 } 

46 try: 

47 extra_kwargs["sampler"][conf.log_evidence] = np.round( 

48 extra_kwargs["other"].pop("log_evidence"), 2 

49 ) 

50 extra_kwargs["sampler"][conf.log_evidence_error] = np.round( 

51 extra_kwargs["other"].pop("dlog_evidence"), 2 

52 ) 

53 except KeyError: 

54 pass 

55 

56 low_freqs = [ 

57 item for key, item in extra_kwargs["other"].items() if 

58 "likelihood_low_freq" in key 

59 ] 

60 if len(low_freqs): 

61 extra_kwargs["meta_data"]["f_low"] = np.min(low_freqs) 

62 try: 

63 extra_kwargs["meta_data"]["f_ref"] = extra_kwargs["other"].pop("f_ref") 

64 except KeyError: 

65 pass 

66 

67 data = { 

68 "parameters": list(samples.keys()), 

69 "samples": np.array([_ for _ in samples.values()]).T.tolist(), 

70 "injection": None, 

71 "version": None, 

72 "kwargs": extra_kwargs, 

73 "config": config 

74 } 

75 return data 

76 

77 

78class PyCBC(GWSingleAnalysisRead): 

79 """PESummary wrapper of `pycbc` (https://git.ligo.org/lscsoft/bilby). The 

80 path_to_results_file argument will be passed directly to 

81 `pycbc.inference.io.loadfile`. All functions therefore use `pycbc` 

82 methods and requires `pycbc` to be installed. 

83 """ 

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

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

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

87 

88 def _grab_data_from_pycbc_file(self, *args, **kwargs): 

89 """Load the results file using the `pycbc` library 

90 """ 

91 return read_pycbc(*args, **kwargs)