Coverage for pesummary/gw/webpage/tgr.py: 88.5%

130 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 glob 

4from getpass import getuser 

5from pesummary.core.webpage.main import _WebpageGeneration 

6 

7__author__ = [ 

8 "Charlie Hoy <charlie.hoy@ligo.org>", 

9 "Aditya Vijaykumar <aditya.vijaykumar@ligo.org>", 

10] 

11TESTS = ["imrct"] 

12 

13 

14class TGRWebpageGeneration(_WebpageGeneration): 

15 """Class to handle webpage generation displaying the outcome of various 

16 TGR tests 

17 

18 Parameters 

19 ---------- 

20 webdir: str 

21 the web directory of the run 

22 path_to_results_file: str 

23 the path to the lalinference h5 file 

24 *args: tuple 

25 all args passed to the _WebpageGeneration class 

26 **kwargs: dict 

27 all kwargs passed to the _WebpageGeneration class 

28 """ 

29 

30 def __init__( 

31 self, 

32 webdir, 

33 path_to_results_file, 

34 *args, 

35 test="all", 

36 test_key_data={}, 

37 open_files=None, 

38 links_to_pe_pages=[], 

39 input_file_summary={}, 

40 **kwargs 

41 ): 

42 self.test = test 

43 if self.test.lower() == "all": 

44 labels = TESTS 

45 else: 

46 labels = [self.test] 

47 

48 _labels = labels 

49 self.links_to_pe_pages = links_to_pe_pages 

50 if open_files is not None: 

51 _labels = list(open_files.keys()) 

52 super(TGRWebpageGeneration, self).__init__( 

53 *args, 

54 webdir=webdir, 

55 labels=_labels, 

56 user=getuser(), 

57 samples=open_files, 

58 **kwargs 

59 ) 

60 self.labels = labels 

61 self.file_versions = { 

62 label: "No version information found" for label in self.labels 

63 } 

64 self.test_key_data = test_key_data 

65 self.open_files = open_files 

66 self.input_file_summary = input_file_summary 

67 if not len(self.test_key_data): 

68 self.test_key_data = {_test: {} for _test in self.labels} 

69 if not len(self.input_file_summary) and self.open_files is None: 

70 self.input_file_summary = {_test: {} for _test in self.labels} 

71 elif not len(self.input_file_summary): 

72 self.input_file_summary = self.key_data 

73 self.copy_css_and_js_scripts() 

74 

75 @property 

76 def _metafile(self): 

77 return "tgr_samples.h5" 

78 

79 def generate_webpages(self, make_diagnostic_plots=False): 

80 """Generate all webpages for all tests""" 

81 self.make_home_pages() 

82 if self.test == "imrct" or self.test == "all": 

83 self.make_imrct_pages( 

84 make_diagnostic_plots=make_diagnostic_plots 

85 ) 

86 self.make_version_page() 

87 self.make_logging_page() 

88 self.make_about_page() 

89 self.make_downloads_page() 

90 self.generate_specific_javascript() 

91 

92 def make_navbar_for_result_page(self): 

93 links = self.make_navbar_for_homepage() 

94 if self.test == "imrct" and len(self.links_to_pe_pages): 

95 link_format = "external:{}" 

96 if len(self.links_to_pe_pages) > 2: 

97 analysis_label = [ 

98 label.split(":inspiral")[0] for label in self.samples.keys() 

99 if "inspiral" in label and "postinspiral" not in label 

100 ] 

101 _links = ["PE Pages"] 

102 for label in analysis_label: 

103 inspiral_ind = self.labels.index( 

104 "{}:inspiral".format(label) 

105 ) 

106 postinspiral_ind = self.labels.index( 

107 "{}:postinspiral".format(label) 

108 ) 

109 _links.append( 

110 [ 

111 label, [ 

112 { 

113 "inspiral": link_format.format( 

114 self.links_to_pe_pages[inspiral_ind] 

115 ) 

116 }, 

117 { 

118 "postinspiral": link_format.format( 

119 self.links_to_pe_pages[postinspiral_ind] 

120 ) 

121 } 

122 ] 

123 ] 

124 ) 

125 links.insert(2, _links) 

126 else: 

127 links.insert( 

128 2, 

129 [ 

130 "PE Pages", 

131 [ 

132 {self.labels[0]: link_format.format(self.links_to_pe_pages[0])}, 

133 {self.labels[1]: link_format.format(self.links_to_pe_pages[1])}, 

134 ], 

135 ], 

136 ) 

137 return links 

138 

139 def make_navbar_for_comparison_page(self): 

140 return 

141 

142 def _test_links(self): 

143 """Return the navbar structure for the Test tab""" 

144 if self.test == "all": 

145 return TESTS 

146 return [self.test] 

147 

148 def make_navbar_for_homepage(self): 

149 """Make a navbar for the homepage""" 

150 links = ["home", ["Tests", self._test_links()], "Logging", "Version"] 

151 return links 

152 

153 def _make_home_pages(self, pages): 

154 """Make the home pages 

155 

156 Parameters 

157 ---------- 

158 pages: list 

159 list of pages that you wish to create 

160 """ 

161 html_file = self.setup_page("home", self.navbar["home"]) 

162 html_file.make_banner( 

163 "Tests of General Relativity", custom=" ", key="custom" 

164 ) 

165 imrct_plots = glob.glob( 

166 "{}/plots/combined_imrct_deviations_triangle_plot.png".format( 

167 self.webdir 

168 ) 

169 ) 

170 if len(imrct_plots): 

171 image_contents = [ 

172 [_plot.replace(self.webdir, ".") for _plot in imrct_plots] 

173 ] 

174 else: 

175 image_contents = [["plots/primary_imrct_deviations_triangle_plot.png"]] 

176 html_file = self.make_modal_carousel( 

177 html_file, image_contents=image_contents, unique_id=True 

178 ) 

179 _banner_desc = ( 

180 "Below we show summary statistics associated with each test of GR" 

181 ) 

182 html_file.make_banner( 

183 approximant="Summary Statistics", 

184 key="custom", 

185 custom=_banner_desc, 

186 _style="font-size: 26px;", 

187 ) 

188 _style = "margin-top:3em; margin-bottom:5em; max-width:1400px" 

189 _class = "row justify-content-center" 

190 _include = { 

191 "imrct": ["GR Quantile (%)"] 

192 } 

193 for label in self.labels: 

194 html_file.make_container(style=_style) 

195 html_file.make_div(4, _class=_class, _style=None) 

196 analyses = list(self.test_key_data[label].keys()) 

197 table_contents = [ 

198 [i] 

199 + [ 

200 self.test_key_data[label][i][key] 

201 if key in self.test_key_data[label][i].keys() 

202 else "-" 

203 for key in _include[label] 

204 ] for i in analyses 

205 ] 

206 _headings = [label] + _include[label] 

207 html_file.make_table( 

208 headings=_headings, 

209 format="table-hover", 

210 heading_span=1, 

211 contents=table_contents, 

212 accordian=False, 

213 ) 

214 html_file.end_div(4) 

215 html_file.end_container() 

216 html_file.export("{}.csv".format("summary_of_{}.csv".format(label))) 

217 html_file.make_footer(user=self.user, rundir=self.webdir) 

218 html_file.close() 

219 

220 def make_imrct_pages(self, make_diagnostic_plots=False): 

221 """Make the IMR consistency test pages""" 

222 analysis_label = [ 

223 label.split(":inspiral")[0] for label in self.samples.keys() if 

224 "inspiral" in label and "postinspiral" not in label 

225 ] 

226 if analysis_label == ["inspiral"]: 

227 analysis_label = ["primary"] 

228 pages = ["imrct"] 

229 self.create_blank_html_pages(pages) 

230 html_file = self.setup_page( 

231 "imrct", self.navbar["result_page"], title="IMR Consistency Test" 

232 ) 

233 html_file.make_banner( 

234 approximant="IMR Consistency Test", key="custom", custom=" " 

235 ) 

236 desc = "Below we show the executive plots for the IMR consistency test" 

237 html_file.make_banner( 

238 approximant="Executive plots", 

239 key="custom", 

240 custom=desc, 

241 _style="font-size: 26px;", 

242 ) 

243 path = self.image_path["other"] 

244 base_string = path + "{}_imrct_{}.png" 

245 image_contents = [ 

246 path + base_string.format(analysis_label[num], "deviations_triangle_plot") 

247 for num in range(len(analysis_label)) 

248 ] 

249 image_contents = [ 

250 image_contents[i:2 + i] for i in range(0, len(image_contents), 2) 

251 ] 

252 captions = [ 

253 ( 

254 "This triangle plot shows the 2D and marginalized 1D " 

255 "posterior distributions for the fractional parameters " 

256 "fractional_final_mass and fractional_final_spin for the " 

257 "{} analysis. The prediction from General Relativity is " 

258 "shown as a plus." 

259 ).format(analysis_label[num]) for num in range(len(analysis_label)) 

260 ] 

261 captions = [captions[i:2 + i] for i in range(0, len(captions), 2)] 

262 html_file = self.make_modal_carousel( 

263 html_file, 

264 image_contents, 

265 captions=captions, 

266 cli=None, 

267 unique_id=True, 

268 extra_div=True, 

269 autoscale=False, 

270 ) 

271 _style = "margin-top:3em; margin-bottom:5em; max-width:1400px" 

272 _class = "row justify-content-center" 

273 html_file.make_container(style=_style) 

274 html_file.make_div(4, _class=_class, _style=None) 

275 _data = self.test_key_data["imrct"] 

276 headings = [" "] + list(_data[analysis_label[0]].keys()) 

277 table_contents = [ 

278 [_label] + list(_data[_label].values()) for _label in analysis_label 

279 ] 

280 html_file.make_table( 

281 headings=[" "] + list(_data[analysis_label[0]].keys()), 

282 format="table-hover", 

283 heading_span=1, 

284 contents=table_contents, 

285 accordian=False, 

286 ) 

287 html_file.end_div(4) 

288 html_file.end_container() 

289 html_file.export("{}.csv".format("summary_of_IMRCT_test.csv")) 

290 if make_diagnostic_plots: 

291 desc = ( 

292 "Below we show additional plots generated for the IMR consistency " 

293 "test" 

294 ) 

295 html_file.make_banner( 

296 approximant="Additional plots", 

297 key="custom", 

298 custom=desc, 

299 _style="font-size: 26px;", 

300 ) 

301 image_contents = [ 

302 [ 

303 base_string.format(_label, "final_mass_non_evolved_final_spin_non_evolved"), 

304 base_string.format(_label, "mass_1_mass_2"), 

305 base_string.format(_label, "a_1_a_2"), 

306 ] for _label in analysis_label 

307 ] 

308 _base = ( 

309 "2D posterior distribution for {} estimated from the inspiral " 

310 "and post-inspiral parts of the signal for analysis: {}" 

311 ) 

312 captions = [ 

313 [ 

314 _base.format("final_mass and final_spin", _label), 

315 _base.format("mass_1 and mass_2", _label), 

316 _base.format("a_1 and a_2", _label), 

317 ] for _label in analysis_label 

318 ] 

319 cli = None 

320 html_file = self.make_modal_carousel( 

321 html_file, 

322 image_contents, 

323 captions=captions, 

324 cli=cli, 

325 extra_div=True, 

326 unique_id=True, 

327 autoscale=True, 

328 ) 

329 html_file.make_footer(user=self.user, rundir=self.webdir) 

330 html_file.close() 

331 

332 def _make_downloads_page(self, pages): 

333 """Make a page with links to files which can be downloaded 

334 

335 Parameters 

336 ---------- 

337 pages: list 

338 list of pages you wish to create 

339 """ 

340 return super(TGRWebpageGeneration, self)._make_downloads_page( 

341 pages, fix_bottom=True 

342 ) 

343 

344 def _make_entry_in_downloads_table(self, *args, **kwargs): 

345 """Make a label specific entry into the downloads table. Given that 

346 we do not want to have label specific entries in the downloads table 

347 this function simply returns None to overwrite the inherited 

348 pesummary.core.webpage.main._WebpageGeneration function 

349 """ 

350 return