Coverage for pesummary/gw/file/read.py: 96.0%

50 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 

3from pesummary.gw.file.formats.lalinference import LALInference 

4from pesummary.gw.file.formats.bilby import Bilby 

5from pesummary.gw.file.formats.default import Default 

6from pesummary.gw.file.formats.pesummary import ( 

7 TGRPESummary, PESummary, PESummaryDeprecated 

8) 

9from pesummary.gw.file.formats.pycbc import PyCBC 

10from pesummary.gw.file.formats.GWTC1 import GWTC1 

11from pesummary.core.file.read import ( 

12 is_bilby_hdf5_file, is_bilby_json_file, is_pesummary_hdf5_file, 

13 is_pesummary_json_file, is_pesummary_hdf5_file_deprecated, 

14 is_pesummary_json_file_deprecated, _is_pesummary_hdf5_file, 

15 _is_pesummary_json_file 

16) 

17from pesummary.core.file.read import read as CoreRead 

18 

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

20 

21 

22def is_GWTC1_file(path): 

23 """Determine if the results file is one released as part of the GWTC1 

24 catalog 

25 

26 Parameters 

27 ---------- 

28 path: str 

29 path to the results file 

30 """ 

31 import h5py 

32 

33 f = h5py.File(path, 'r') 

34 keys = list(f.keys()) 

35 f.close() 

36 if "Overall_posterior" in keys or "overall_posterior" in keys: 

37 return True 

38 return False 

39 

40 

41def is_lalinference_file(path): 

42 """Determine if the results file is a LALInference results file 

43 

44 Parameters 

45 ---------- 

46 path: str 

47 path to the results file 

48 """ 

49 import h5py 

50 f = h5py.File(path, 'r') 

51 keys = list(f.keys()) 

52 f.close() 

53 if "lalinference" in keys: 

54 return True 

55 return False 

56 

57 

58def is_tgr_pesummary_hdf5_file(path): 

59 """Determine if the results file is a pesummary TGR hdf5 file 

60 

61 Parameters 

62 ---------- 

63 path: str 

64 path to results file 

65 """ 

66 return _is_pesummary_hdf5_file(path, _check_tgr_pesummary_file) 

67 

68 

69def is_tgr_pesummary_json_file(path): 

70 """Determine if the results file is a pesummary TGR json file 

71 

72 Parameters 

73 ---------- 

74 path: str 

75 path to results file 

76 """ 

77 return _is_pesummary_json_file(path, _check_tgr_pesummary_file) 

78 

79 

80def _check_tgr_pesummary_file(f): 

81 """Check the contents of a dictionary to see if it is a pesummary TGR 

82 dictionary 

83 

84 Parameters 

85 ---------- 

86 f: dict 

87 dictionary of the contents of the file 

88 """ 

89 labels = f.keys() 

90 if "version" not in labels: 

91 return False 

92 try: 

93 if all( 

94 "imrct" in f[label].keys() for label in labels if label != "version" 

95 and label != "history" 

96 ): 

97 return True 

98 else: 

99 return False 

100 except Exception: 

101 return False 

102 

103 

104def is_pycbc_file(path): 

105 """Determine if the results file is a pycbc hdf5 file 

106 

107 Parameters 

108 ---------- 

109 path: str 

110 path to results file 

111 """ 

112 import h5py 

113 f = h5py.File(path, 'r') 

114 if all(_ in f.attrs for _ in ["sampling_params", "variable_params", "model"]): 

115 return True 

116 return False 

117 

118 

119GW_HDF5_LOAD = { 

120 is_lalinference_file: LALInference.load_file, 

121 is_bilby_hdf5_file: Bilby.load_file, 

122 is_tgr_pesummary_hdf5_file: TGRPESummary.load_file, 

123 is_pesummary_hdf5_file: PESummary.load_file, 

124 is_pesummary_hdf5_file_deprecated: PESummaryDeprecated.load_file, 

125 is_GWTC1_file: GWTC1.load_file, 

126 is_pycbc_file: PyCBC.load_file, 

127} 

128 

129GW_JSON_LOAD = { 

130 is_bilby_json_file: Bilby.load_file, 

131 is_tgr_pesummary_json_file: TGRPESummary.load_file, 

132 is_pesummary_json_file: PESummary.load_file, 

133 is_pesummary_json_file_deprecated: PESummaryDeprecated.load_file 

134} 

135 

136GW_DEFAULT = {"default": Default.load_file} 

137 

138 

139def read( 

140 path, HDF5_LOAD=GW_HDF5_LOAD, JSON_LOAD=GW_JSON_LOAD, file_format=None, 

141 **kwargs 

142): 

143 """Read in a results file. 

144 

145 Parameters 

146 ---------- 

147 path: str 

148 path to results file 

149 HDF5_LOAD: dict 

150 dictionary containing possible methods for loading a HDF5 file. Key 

151 is a function which returns True or False depending on whether the input 

152 file belongs to that class of objects, value is the load function 

153 JSON_LOAD: dict 

154 dictionary containing possible methods for loading a JSON file. Key 

155 is a function which returns True or False depending on whether the input 

156 file belongs to that class of objects, value is the load function 

157 **kwargs: dict, optional 

158 all additional kwargs are passed directly to the load_file class method 

159 """ 

160 return CoreRead( 

161 path, HDF5_LOAD=GW_HDF5_LOAD, JSON_LOAD=GW_JSON_LOAD, DEFAULT=GW_DEFAULT, 

162 file_format=file_format, **kwargs 

163 )