Coverage for pesummary/tests/input_test.py: 99.0%

411 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 os 

4import shutil 

5import glob 

6import copy 

7 

8import argparse 

9from pesummary.gw.cli.inputs import WebpagePlusPlottingPlusMetaFileInput 

10from pesummary.gw.cli.parser import ( 

11 add_dynamic_PSD_to_namespace, add_dynamic_calibration_to_namespace, 

12 ArgumentParser 

13) 

14from .base import make_result_file, gw_parameters, data_dir, testing_dir 

15 

16import numpy as np 

17import h5py 

18 

19import pytest 

20import tempfile 

21 

22tmpdir = tempfile.TemporaryDirectory(prefix=".", dir=".").name 

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

24 

25 

26class TestCommandLine(object): 

27 

28 def setup_method(self): 

29 self.parser = ArgumentParser() 

30 self.parser.add_all_known_options_to_parser() 

31 if not os.path.isdir(tmpdir): 

32 os.mkdir(tmpdir) 

33 make_result_file(gw=True, lalinference=True, outdir=tmpdir) 

34 os.rename( 

35 "{}/test.hdf5".format(tmpdir), 

36 "{}/lalinference_example.h5".format(tmpdir) 

37 ) 

38 make_result_file(gw=True, bilby=True, outdir=tmpdir, extension="hdf5") 

39 os.rename( 

40 "{}/test.h5".format(tmpdir), "{}/bilby_example.h5".format(tmpdir) 

41 ) 

42 

43 def teardown_method(self): 

44 """Remove the files and directories created from this class 

45 """ 

46 if os.path.isdir(tmpdir): 

47 shutil.rmtree(tmpdir) 

48 

49 def test_webdir(self): 

50 assert self.parser.get_default("webdir") == None 

51 opts = self.parser.parse_args(["--webdir", "test"]) 

52 assert opts.webdir == "test" 

53 

54 def test_baseurl(self): 

55 assert self.parser.get_default("baseurl") == None 

56 opts = self.parser.parse_args(["--baseurl", "test"]) 

57 assert opts.baseurl == "test" 

58 

59 def test_add_to_existing(self): 

60 assert self.parser.get_default("add_to_existing") == False 

61 opts = self.parser.parse_args(["--add_to_existing"]) 

62 assert opts.add_to_existing == True 

63 

64 def test_approximant(self): 

65 assert self.parser.get_default("approximant") == None 

66 opts = self.parser.parse_args(["--approximant", "test"]) 

67 assert opts.approximant == ["test"] 

68 

69 def test_config(self): 

70 assert self.parser.get_default("config") == None 

71 opts = self.parser.parse_args(["--config", data_dir + "/example.ini"]) 

72 assert opts.config == [data_dir + "/example.ini"] 

73 

74 def test_dump(self): 

75 assert self.parser.get_default("dump") == False 

76 opts = self.parser.parse_args(["--dump"]) 

77 assert opts.dump == True 

78 

79 def test_email(self): 

80 assert self.parser.get_default("email") == None 

81 opts = self.parser.parse_args(["--email", "test"]) 

82 assert opts.email == "test" 

83 

84 def test_existing(self): 

85 assert self.parser.get_default("existing") == None 

86 opts = self.parser.parse_args(["--existing_webdir", "test"]) 

87 assert opts.existing == "test" 

88 

89 def test_gracedb(self): 

90 assert self.parser.get_default("gracedb") == None 

91 opts = self.parser.parse_args(["--gracedb", "Gtest"]) 

92 assert opts.gracedb == "Gtest" 

93 

94 def test_inj_file(self): 

95 assert self.parser.get_default("inj_file") == None 

96 opts = self.parser.parse_args( 

97 ["--inj_file", testing_dir + "/main_injection.xml"] 

98 ) 

99 assert opts.inj_file == [testing_dir + "/main_injection.xml"] 

100 

101 def test_samples(self): 

102 assert self.parser.get_default("samples") == None 

103 opts = self.parser.parse_args( 

104 ["--samples", "{}/lalinference_example.h5".format(tmpdir)] 

105 ) 

106 assert opts.samples == ["{}/lalinference_example.h5".format(tmpdir)] 

107 

108 def test_sensitivity(self): 

109 assert self.parser.get_default("sensitivity") == False 

110 opts = self.parser.parse_args(["--sensitivity"]) 

111 assert opts.sensitivity == True 

112 

113 def test_user(self): 

114 assert self.parser.get_default("user") == "albert.einstein" 

115 opts = self.parser.parse_args(["--user", "test"]) 

116 assert opts.user == "test" 

117 

118 def test_verbose(self): 

119 opts = self.parser.parse_args(["-v"]) 

120 assert opts.verbose == True 

121 

122 def test_gwdata(self): 

123 opts = self.parser.parse_args(["--gwdata", "H1:H1-CALIB-STRAIN:hello.lcf"]) 

124 assert opts.gwdata == {"H1:H1-CALIB-STRAIN": "hello.lcf"} 

125 

126 

127class TestInputExceptions(object): 

128 

129 def setup_method(self): 

130 if os.path.isdir(tmpdir): 

131 shutil.rmtree(tmpdir) 

132 os.mkdir(tmpdir) 

133 self.parser = ArgumentParser() 

134 self.parser.add_all_known_options_to_parser() 

135 make_result_file(gw=True, lalinference=True, outdir=tmpdir) 

136 os.rename( 

137 "{}/test.hdf5".format(tmpdir), 

138 "{}/lalinference_example.h5".format(tmpdir) 

139 ) 

140 make_result_file(gw=True, bilby=True, outdir=tmpdir, extension="hdf5") 

141 os.rename( 

142 "{}/test.h5".format(tmpdir), "{}/bilby_example.h5".format(tmpdir) 

143 ) 

144 

145 def test_no_webdir(self): 

146 with pytest.raises(Exception) as info: 

147 opts = self.parser.parse_args([]) 

148 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

149 assert "Please provide a web directory" in str(info.value) 

150 

151 def test_make_webdir_if_it_does_not_exist(self): 

152 assert os.path.isdir("{}/path".format(tmpdir)) == False 

153 opts = self.parser.parse_args(['--webdir', '{}/path'.format(tmpdir), 

154 '--approximant', 'IMRPhenomPv2', 

155 '--samples', "{}/bilby_example.h5".format(tmpdir), 

156 '--disable_prior_sampling', "--no_conversion"]) 

157 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

158 assert os.path.isdir("{}/path".format(tmpdir)) == True 

159 

160 def test_invalid_existing_directory(self): 

161 if os.path.isdir("./.existing"): 

162 shutil.rmtree("./.existing") 

163 with pytest.raises(Exception) as info: 

164 opts = self.parser.parse_args(['--existing_webdir', './.existing']) 

165 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

166 dir_name = os.path.abspath('./.existing') 

167 assert "Please provide a valid existing directory" in str(info.value) 

168 

169 def test_not_base_of_existing_directory(self): 

170 if os.path.isdir("./.existing2"): 

171 shutil.rmtree("./.existing2") 

172 if os.path.isdir("./.existing2/samples"): 

173 shutil.rmtree("./.existing2/samples") 

174 os.mkdir("./.existing2") 

175 os.mkdir("./.existing2/samples") 

176 opts = self.parser.parse_args(['--existing_webdir', './.existing2/samples']) 

177 with pytest.raises(Exception) as info: 

178 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

179 assert "Please provide a valid existing directory" in str(info.value) 

180 

181 def test_add_to_existing_and_no_existing_flag(self): 

182 opts = self.parser.parse_args(["--add_to_existing"]) 

183 with pytest.raises(Exception) as info: 

184 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

185 assert "Please provide a web directory to store the webpages" in str(info.value) 

186 

187 def test_no_samples(self): 

188 opts = self.parser.parse_args(["--webdir", tmpdir]) 

189 with pytest.raises(Exception) as info: 

190 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

191 assert "Please provide a results file" in str(info.value) 

192 

193 def test_non_existance_samples(self): 

194 with pytest.raises(Exception) as info: 

195 opts = self.parser.parse_args(["--webdir", tmpdir, 

196 "--samples", "{}/no_existance".format(tmpdir)]) 

197 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

198 assert "{}/no_existance".format(tmpdir) in str(info.value) 

199 

200 def test_napproximant_not_equal_to_nsamples(self): 

201 opts = self.parser.parse_args(["--webdir", tmpdir, 

202 "--samples", "{}/bilby_example.h5".format(tmpdir), 

203 "{}/bilby_example.h5".format(tmpdir), 

204 "--disable_prior_sampling", "--no_conversion", 

205 "--approximant", "IMRPhenomPv2"]) 

206 with pytest.raises(Exception) as info: 

207 x = WebpagePlusPlottingPlusMetaFileInput(opts) 

208 assert "Please pass an approximant for each" in str(info.value) 

209 

210 

211class TestInput(object): 

212 

213 def setup_method(self): 

214 if not os.path.isdir(tmpdir): 

215 os.mkdir(tmpdir) 

216 self.parser = ArgumentParser() 

217 self.parser.add_all_known_options_to_parser() 

218 make_result_file(gw=True, lalinference=True, outdir=tmpdir) 

219 os.rename( 

220 "{}/test.hdf5".format(tmpdir), 

221 "{}/lalinference_example.h5".format(tmpdir) 

222 ) 

223 data = make_result_file(gw=True, bilby=True, outdir=tmpdir, extension="hdf5") 

224 self.parameters, self.samples = data 

225 os.rename( 

226 "{}/test.h5".format(tmpdir), "{}/bilby_example.h5".format(tmpdir) 

227 ) 

228 self.default_arguments = [ 

229 "--approximant", "IMRPhenomPv2", 

230 "--webdir", tmpdir, 

231 "--samples", "{}/bilby_example.h5".format(tmpdir), 

232 "--email", "albert.einstein@ligo.org", 

233 "--gracedb", "Grace", "--no_conversion", 

234 "--labels", "example", "--disable_prior_sampling"] 

235 self.original_arguments = copy.deepcopy(self.default_arguments) 

236 self.make_input_object() 

237 

238 def teardown_method(self): 

239 """Remove the files and directories created from this class 

240 """ 

241 if os.path.isdir(tmpdir): 

242 shutil.rmtree(tmpdir) 

243 

244 @staticmethod 

245 def make_existing_file(path): 

246 parameters = np.array(["mass_1", "mass_2", "luminosity_distance"], 

247 dtype="S") 

248 samples = np.array([[10, 5, 400], [40, 20, 800], [50, 10, 200]]) 

249 injected_samples = np.array([float("nan"), float("nan"), float("nan")]) 

250 

251 f = h5py.File(path + "/posterior_samples.h5", "w") 

252 posterior_samples = f.create_group("posterior_samples") 

253 label = posterior_samples.create_group("example") 

254 label.create_dataset("parameter_names", data=parameters) 

255 label.create_dataset("samples", data=samples) 

256 injection_data = f.create_group("injection_data") 

257 label = injection_data.create_group("example") 

258 label.create_dataset("injection_values", data=injected_samples) 

259 f.close() 

260 return path + "/posterior_samples.h5" 

261 

262 def add_argument(self, argument, reset=False): 

263 if reset: 

264 self.default_arguments = self.original_arguments 

265 if isinstance(argument, list): 

266 for i in argument: 

267 self.default_arguments.append(i) 

268 else: 

269 self.default_arguments.append(argument) 

270 self.opts, unknown = self.parser.parse_known_args(self.default_arguments) 

271 add_dynamic_PSD_to_namespace(self.opts) 

272 add_dynamic_calibration_to_namespace(self.opts) 

273 self.inputs = WebpagePlusPlottingPlusMetaFileInput(self.opts) 

274 

275 def replace_existing_argument(self, argument, new_value): 

276 if argument in self.default_arguments: 

277 index = self.default_arguments.index(argument) 

278 arguments = self.default_arguments 

279 arguments[index+1] = new_value 

280 self.default_arguments = arguments 

281 self.make_input_object() 

282 

283 def make_input_object(self): 

284 self.opts, unknown = self.parser.parse_known_args(self.default_arguments) 

285 add_dynamic_PSD_to_namespace(self.opts) 

286 add_dynamic_calibration_to_namespace(self.opts) 

287 self.inputs = WebpagePlusPlottingPlusMetaFileInput(self.opts) 

288 

289 def test_webdir(self): 

290 assert self.inputs.webdir == os.path.abspath(tmpdir) 

291 

292 def test_samples(self): 

293 assert self.inputs.result_files == ["{}/bilby_example.h5".format(tmpdir)] 

294 

295 def test_approximant(self): 

296 assert self.inputs.approximant == {"example": "IMRPhenomPv2"} 

297 

298 def test_existing(self): 

299 assert self.inputs.existing == None 

300 

301 def test_baseurl(self): 

302 assert self.inputs.baseurl == "https://" + os.path.abspath(tmpdir) 

303 

304 def test_inj_file(self): 

305 assert self.inputs.injection_file == [None] 

306 

307 def test_config(self): 

308 assert self.inputs.config == [None] 

309 

310 def test_email(self): 

311 assert self.inputs.email == "albert.einstein@ligo.org" 

312 

313 def test_add_to_existing(self): 

314 assert self.inputs.add_to_existing == False 

315 

316 def test_sensitivity(self): 

317 assert self.inputs.sensitivity == False 

318 

319 def test_dump(self): 

320 assert self.inputs.dump == False 

321 self.add_argument(["--dump"]) 

322 assert self.inputs.dump == True 

323 

324 def test_gracedb(self): 

325 assert self.inputs.gracedb == "Grace" 

326 parser = ArgumentParser() 

327 parser.add_all_known_options_to_parser() 

328 default_arguments = [ 

329 "--approximant", "IMRPhenomPv2", 

330 "--webdir", tmpdir, 

331 "--samples", "{}/bilby_example.h5".format(tmpdir), 

332 "--email", "albert.einstein@ligo.org", 

333 "--gracedb", "Mock", "--disable_prior_sampling", 

334 "--labels", "example", "--no_conversion"] 

335 opts = parser.parse_args(default_arguments) 

336 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

337 assert inputs.gracedb is None 

338 

339 def test_detectors(self): 

340 assert self.inputs.detectors == {"example": None} 

341 

342 def test_labels(self): 

343 assert self.inputs.labels == ["example"] 

344 self.add_argument(["--label", "new_example"]) 

345 assert self.inputs.labels == ["new_example"] 

346 

347 def test_existing_labels(self): 

348 assert self.inputs.existing_labels == None 

349 path = self.make_existing_file("{}/samples".format(tmpdir)) 

350 with open("{}/home.html".format(tmpdir), "w") as f: 

351 f.writelines("test") 

352 parser = ArgumentParser() 

353 parser.add_all_known_options_to_parser() 

354 default_arguments = [ 

355 "--approximant", "IMRPhenomPv2", 

356 "--existing_webdir", tmpdir, 

357 "--samples", "{}/bilby_example.h5".format(tmpdir), "--no_conversion", 

358 "--gracedb", "Grace", "--disable_prior_sampling"] 

359 opts = parser.parse_args(default_arguments) 

360 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

361 assert inputs.existing_labels == ["example"] 

362 

363 def test_existing_samples(self): 

364 assert self.inputs.existing_samples == None 

365 path = self.make_existing_file("{}/samples".format(tmpdir)) 

366 with open("{}/home.html".format(tmpdir), "w") as f: 

367 f.writelines("test") 

368 parser = ArgumentParser() 

369 parser.add_all_known_options_to_parser() 

370 default_arguments = [ 

371 "--approximant", "IMRPhenomPv2", 

372 "--existing_webdir", tmpdir, 

373 "--samples", "{}/bilby_example.h5".format(tmpdir), 

374 "--email", "albert.einstein@ligo.org", 

375 "--gracedb", "Grace"] 

376 opts = parser.parse_args(default_arguments) 

377 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

378 assert all( 

379 i == j for i, j in zip(inputs.existing_samples["example"]["mass_1"], [10, 40, 50])) 

380 assert all( 

381 i == j for i,j in zip(inputs.existing_samples["example"]["mass_2"], [5, 20, 10])) 

382 assert all( 

383 i == j for i,j in zip(inputs.existing_samples["example"]["luminosity_distance"], [400, 800, 200])) 

384 

385 def test_psd(self): 

386 with open("{}/psd.dat".format(tmpdir), "w") as f: 

387 f.writelines(["1.00 3.44\n", "2.00 5.66\n", "3.00 4.56\n", "4.00 9.83\n"]) 

388 assert self.inputs.psd == {"example": {}} 

389 self.add_argument( 

390 ["--psd", "{}/psd.dat".format(tmpdir), "--f_low", "1.0", "--f_final", "3.0"] 

391 ) 

392 assert list(self.inputs.psd["example"].keys()) == ["psd.dat"] 

393 self.add_argument( 

394 ["--psd", "H1:{}/psd.dat".format(tmpdir), "--f_low", "1.0", "--f_final", "3.0"] 

395 ) 

396 assert list(self.inputs.psd["example"].keys()) == ["H1"] 

397 np.testing.assert_almost_equal( 

398 self.inputs.psd["example"]["H1"], 

399 [[1.00, 3.44], [2.00, 5.66], [3.00, 4.56], [4.00, 9.83]] 

400 ) 

401 

402 def test_preliminary_pages_for_single_analysis(self): 

403 """Test that preliminary watermarks are added when an analysis 

404 is not reproducible 

405 """ 

406 # when neither a psd or config is passed, add preliminary watermark 

407 assert self.inputs.preliminary_pages == {"example": True} 

408 # when a psd is added but not a config, add preliminary watermark 

409 with open("{}/psd.dat".format(tmpdir), "w") as f: 

410 f.writelines(["1.00 3.44\n", "2.00 5.66\n", "3.00 4.56\n", "4.00 9.83\n"]) 

411 self.add_argument(["--psd", "{}/psd.dat".format(tmpdir)]) 

412 assert self.inputs.preliminary_pages == {"example": True} 

413 # when a config is added but no psd, add preliminary watermark 

414 self.add_argument( 

415 ["--config", data_dir + "/config_lalinference.ini"], reset=True 

416 ) 

417 assert self.inputs.preliminary_pages == {"example": True} 

418 # when both config and psd is added, do not add preliminary watermark 

419 self.add_argument( 

420 ["--psd", "{}/psd.dat".format(tmpdir)], reset=True 

421 ) 

422 self.add_argument(["--config", data_dir + "/config_lalinference.ini"]) 

423 assert self.inputs.preliminary_pages == {"example": False} 

424 

425 def test_add_existing_plot(self): 

426 """Test that existing plots are assigned correctly 

427 """ 

428 # when no plot is passed, add_existing_plot = None 

429 assert self.inputs.existing_plot is None 

430 with open("{}/test.png".format(tmpdir), "w") as f: 

431 f.writelines("") 

432 with open("{}/test2.png".format(tmpdir), "w") as f: 

433 f.writelines("") 

434 # when the standard dict format is provided, assign to correct label 

435 real_path = os.path.abspath(tmpdir) 

436 self.add_argument(["--add_existing_plot", "example:{}/test.png".format(tmpdir)]) 

437 # check file now points to webdir/plots 

438 assert self.inputs.existing_plot == {"example": real_path + "/plots/test.png"} 

439 # check that it copied to plots dir 

440 assert any("test.png" in i for i in glob.glob("{}/plots/*.png".format(tmpdir))) 

441 os.remove("{}/plots/test.png".format(tmpdir)) 

442 # when a file is passed without label, assign plot to each label 

443 self.add_argument(["--add_existing_plot", "{}/test.png".format(tmpdir)]) 

444 assert self.inputs.existing_plot == {"example": real_path + "/plots/test.png"} 

445 

446 os.remove("{}/plots/test.png".format(tmpdir)) 

447 # when multiple files are passed for one label, check that it is correctly 

448 # assigned 

449 self.add_argument( 

450 [ 

451 "--add_existing_plot", "example:{}/test.png".format(tmpdir), 

452 "example:{}/test2.png".format(tmpdir) 

453 ] 

454 ) 

455 assert self.inputs.existing_plot == { 

456 "example": [real_path + "/plots/test.png", real_path + "/plots/test2.png"] 

457 } 

458 os.remove("{}/plots/test.png".format(tmpdir)) 

459 os.remove("{}/plots/test2.png".format(tmpdir)) 

460 # when files are passed without labels, assign to each label 

461 self.add_argument( 

462 [ 

463 "--add_existing_plot", "{}/test.png".format(tmpdir), 

464 "{}/test2.png".format(tmpdir) 

465 ] 

466 ) 

467 assert self.inputs.existing_plot == { 

468 "example": [real_path + "/plots/test.png", real_path + "/plots/test2.png"] 

469 } 

470 

471 # when a plot does not exist, do not add it 

472 self.add_argument( 

473 [ 

474 "--add_existing_plot", "{}/test.png".format(tmpdir), 

475 "{}/test3.png".format(tmpdir) 

476 ] 

477 ) 

478 assert self.inputs.existing_plot == { 

479 "example": real_path + "/plots/test.png" 

480 } 

481 # when no plots exist, return None 

482 self.add_argument(["--add_existing_plot", "does_not_exist.png"]) 

483 assert self.inputs.existing_plot is None 

484 

485 def test_preliminary_pages_for_multiple_analysis(self): 

486 """Test that preliminary watermarks are added when multiple analyses 

487 are not reproducible 

488 """ 

489 with open("{}/psd.dat".format(tmpdir), "w") as f: 

490 f.writelines(["1.00 3.44\n", "2.00 5.66\n", "3.00 4.56\n", "4.00 9.83\n"]) 

491 self.default_arguments = [ 

492 "--approximant", "IMRPhenomPv2", "IMRPhenomPv2", 

493 "--webdir", tmpdir, 

494 "--samples", "{}/bilby_example.h5".format(tmpdir), 

495 "{}/bilby_example.h5".format(tmpdir), 

496 "--email", "albert.einstein@ligo.org", 

497 "--gracedb", "Grace", "--no_conversion", 

498 "--labels", "example", "example2"] 

499 self.original_arguments = copy.deepcopy(self.default_arguments) 

500 self.make_input_object() 

501 # when neither a psd or config is passed for each file, add preliminary 

502 # watermark 

503 assert self.inputs.preliminary_pages["example"] == True 

504 assert self.inputs.preliminary_pages["example2"] == True 

505 # When a psd and config is provided to both analyses, add preliminary 

506 # watermark to both files 

507 self.add_argument(["--psd", "H1:{}/psd.dat".format(tmpdir)]) 

508 self.add_argument( 

509 ["--config", data_dir + "/config_lalinference.ini", 

510 data_dir + "/config_lalinference.ini"] 

511 ) 

512 assert self.inputs.preliminary_pages["example"] == False 

513 assert self.inputs.preliminary_pages["example2"] == False 

514 

515 def test_calibration(self): 

516 with open("{}/calibration.dat".format(tmpdir), "w") as f: 

517 f.writelines(["1.0 2.0 3.0 4.0 5.0 6.0 7.0\n"]) 

518 f.writelines(["1.0 2.0 3.0 4.0 5.0 6.0 7.0"]) 

519 assert self.inputs.calibration == {"example": {None: None}} 

520 self.add_argument(["--calibration", "{}/calibration.dat".format(tmpdir)]) 

521 assert self.inputs.calibration["example"] == {None: None} 

522 assert list(self.inputs.priors["calibration"]["example"].keys()) == ['calibration.dat'] 

523 

524 def test_custom_psd(self): 

525 with open("{}/psd.dat".format(tmpdir), "w") as f: 

526 f.writelines(["1.00 3.44\n", "2.00 5.66\n", "3.00 4.56\n", "4.00 9.83\n"]) 

527 parser = ArgumentParser() 

528 parser.add_all_known_options_to_parser() 

529 default_arguments = [ 

530 "--approximant", "IMRPhenomPv2", "IMRPhenomPv2", 

531 "--webdir", tmpdir, 

532 "--samples", "{}/bilby_example.h5".format(tmpdir), 

533 "{}/bilby_example.h5".format(tmpdir), 

534 "--email", "albert.einstein@ligo.org", 

535 "--gracedb", "Grace", 

536 "--labels", "test", "test2", 

537 "--test_psd", "L1:{}/psd.dat".format(tmpdir), 

538 "--test2_psd", "V1:{}/psd.dat".format(tmpdir), 

539 "--f_low", "1.0", "1.0", "--f_final", 

540 "3.0", "3.0", "--gw", "--no_conversion" 

541 ] 

542 opts, unknown = parser.parse_known_args(default_arguments) 

543 add_dynamic_PSD_to_namespace(opts, command_line=default_arguments) 

544 add_dynamic_calibration_to_namespace( 

545 opts, command_line=default_arguments 

546 ) 

547 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

548 assert sorted(list(inputs.psd.keys())) == sorted(["test", "test2"]) 

549 assert list(inputs.psd["test"].keys()) == ["L1"] 

550 assert list(inputs.psd["test2"].keys()) == ["V1"] 

551 np.testing.assert_almost_equal( 

552 inputs.psd["test"]["L1"], 

553 [[1.00, 3.44], [2.00, 5.66], [3.00, 4.56], [4.00, 9.83]] 

554 ) 

555 

556 def test_IFO_from_file_name(self): 

557 file_name = "IFO0.dat" 

558 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

559 file_name 

560 ) == "H1" 

561 file_name = "IFO1.dat" 

562 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

563 file_name 

564 ) == "L1" 

565 file_name = "IFO2.dat" 

566 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

567 file_name 

568 ) == "V1" 

569 

570 file_name = "IFO_H1.dat" 

571 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

572 file_name 

573 ) == "H1" 

574 file_name = "IFO_L1.dat" 

575 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

576 file_name 

577 ) == "L1" 

578 file_name = "IFO_V1.dat" 

579 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

580 file_name 

581 ) == "V1" 

582 

583 file_name = "example.dat" 

584 assert WebpagePlusPlottingPlusMetaFileInput.get_ifo_from_file_name( 

585 file_name 

586 ) == "example.dat" 

587 

588 def test_ignore_parameters(self): 

589 parser = ArgumentParser() 

590 parser.add_all_known_options_to_parser() 

591 default_arguments = [ 

592 "--approximant", "IMRPhenomPv2", 

593 "--webdir", tmpdir, 

594 "--samples", "{}/bilby_example.h5".format(tmpdir), 

595 "--labels", "example"] 

596 opts = parser.parse_args(default_arguments) 

597 original = WebpagePlusPlottingPlusMetaFileInput(opts) 

598 

599 parser = ArgumentParser() 

600 parser.add_all_known_options_to_parser() 

601 default_arguments = [ 

602 "--approximant", "IMRPhenomPv2", 

603 "--webdir", tmpdir, 

604 "--samples", "{}/bilby_example.h5".format(tmpdir), 

605 "--ignore_parameters", "cos*", 

606 "--labels", "example"] 

607 opts = parser.parse_args(default_arguments) 

608 ignored = WebpagePlusPlottingPlusMetaFileInput(opts) 

609 ignored_params = [ 

610 param for param in list(original.samples["example"].keys()) if 

611 "cos" in param 

612 ] 

613 assert all( 

614 param not in list(ignored.samples["example"].keys()) for param in 

615 ignored_params 

616 ) 

617 for param, samples in ignored.samples["example"].items(): 

618 np.testing.assert_almost_equal( 

619 samples, original.samples["example"][param] 

620 ) 

621 

622 def test_make_directories(self): 

623 assert os.path.isdir("{}/samples/samples".format(tmpdir)) == False 

624 self.replace_existing_argument("--webdir", "{}/samples".format(tmpdir)) 

625 self.inputs.make_directories() 

626 assert os.path.isdir("{}/samples/samples".format(tmpdir)) == True 

627 

628 def test_copy_files(self): 

629 if os.path.isdir("{}/samples".format(tmpdir)): 

630 shutil.rmtree("{}/samples".format(tmpdir)) 

631 assert os.path.isfile( 

632 "{}/samples/js/combine_corner.js".format(tmpdir) 

633 ) == False 

634 self.replace_existing_argument("--webdir", "{}/samples".format(tmpdir)) 

635 self.add_argument(["--config", data_dir + "/config_lalinference.ini"]) 

636 self.inputs.copy_files() 

637 assert os.path.isfile( 

638 "{}/samples/js/combine_corner.js".format(tmpdir) 

639 ) == True 

640 assert os.path.isfile( 

641 "{}/samples/config/example_config.ini".format(tmpdir) 

642 ) == True 

643 

644 def test_injection_data(self): 

645 assert sorted(list(self.inputs.injection_data["example"].keys())) == sorted( 

646 gw_parameters()) 

647 

648 def test_maxL_samples(self): 

649 ind = self.parameters.index("log_likelihood") 

650 likelihood = np.array(self.samples).T[ind] 

651 max_ind = np.argmax(likelihood) 

652 for param in self.parameters: 

653 ind = self.parameters.index(param) 

654 assert self.inputs.maxL_samples["example"][param] == self.samples.T[ind][max_ind] 

655 assert self.inputs.maxL_samples["example"]["approximant"] == "IMRPhenomPv2" 

656 

657 def test_same_parameters(self): 

658 parser = ArgumentParser() 

659 parser.add_all_known_options_to_parser() 

660 opts = parser.parse_args(["--approximant", "IMRPhenomPv2", 

661 "IMRPhenomPv2", "--webdir", tmpdir, "--samples", 

662 "{}/bilby_example.h5".format(tmpdir), 

663 "{}/lalinference_example.h5".format(tmpdir)]) 

664 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

665 assert sorted(inputs.same_parameters) == sorted(gw_parameters()) 

666 

667 def test_psd_labels(self): 

668 assert list(self.inputs.psd.keys()) == ["example"] 

669 assert self.inputs.psd["example"] == {} 

670 with open("{}/psd.dat".format(tmpdir), "w") as f: 

671 f.writelines(["1.00 3.44\n", "2.00 5.66\n", "3.00 4.56\n", "4.00 9.83\n"]) 

672 parser = ArgumentParser() 

673 parser.add_all_known_options_to_parser() 

674 opts = parser.parse_args(["--approximant", "IMRPhenomPv2", 

675 "IMRPhenomPv2", "--webdir", tmpdir, "--samples", 

676 "{}/bilby_example.h5".format(tmpdir), 

677 "{}/lalinference_example.h5".format(tmpdir), 

678 "--psd", "L1:{}/psd.dat".format(tmpdir), 

679 "L1:{}/psd.dat".format(tmpdir), 

680 "--labels", "example", "example2", "--f_low", "1.0", "1.0", "--f_final", 

681 "3.0", "3.0", "--gw", "--no_conversion"]) 

682 inputs = WebpagePlusPlottingPlusMetaFileInput(opts) 

683 assert sorted(list(inputs.psd["example"].keys())) == ["L1"] 

684 assert sorted(list(inputs.psd["example2"].keys())) == ["L1"]