Coverage for pesummary/tests/read_test.py: 86.6%
969 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-09 22:34 +0000
« 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
3import os
4import shutil
5import numpy as np
7from .base import make_result_file, testing_dir
8import pesummary
9from pesummary.gw.file.read import read as GWRead
10from pesummary.core.file.read import read as Read
11from pesummary.io import read, write
12import glob
13import tempfile
15tmpdir = tempfile.TemporaryDirectory(prefix=".", dir=".").name
16__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"]
19class BaseRead(object):
20 """Base class to test the core functions in the Read and GWRead functions
21 """
22 def test_parameters(self, true, pesummary=False):
23 """Test the parameter property
24 """
25 if pesummary:
26 assert all(i in self.result.parameters[0] for i in true)
27 assert all(i in true for i in self.result.parameters[0])
28 else:
29 assert all(i in self.result.parameters for i in true)
30 assert all(i in true for i in self.result.parameters)
32 def test_samples(self, true, pesummary=False):
33 """Test the samples property
34 """
35 if pesummary:
36 assert len(self.result.samples[0]) == 1000
37 assert len(self.result.samples[0][0]) == 18
38 samples = self.result.samples[0]
39 parameters = self.result.parameters[0]
40 else:
41 assert len(self.result.samples) == 1000
42 assert len(self.result.samples[0]) == 18
43 samples = self.result.samples
44 parameters = self.result.parameters
46 idxs = [self.parameters.index(i) for i in parameters]
47 np.testing.assert_almost_equal(
48 np.array(samples), np.array(self.samples)[:, idxs]
49 )
50 for ind, param in enumerate(parameters):
51 samp = np.array(samples).T[ind]
52 idx = self.parameters.index(param)
53 np.testing.assert_almost_equal(samp, np.array(self.samples).T[idx])
55 def test_samples_dict(self, true):
56 """Test the samples_dict property
57 """
58 parameters = true[0]
59 samples = true[1]
61 for num, param in enumerate(parameters):
62 specific_samples = [i[num] for i in samples]
63 drawn_samples = self.result.samples_dict[param]
64 np.testing.assert_almost_equal(drawn_samples, specific_samples)
66 def test_version(self, true=None):
67 """Test the version property
68 """
69 if true is None:
70 assert self.result.input_version == "No version information found"
71 else:
72 assert self.result.input_version == true
74 def test_extra_kwargs(self, true=None):
75 """Test the extra_kwargs property
76 """
77 if true is None:
78 assert self.result.extra_kwargs == {
79 "sampler": {"nsamples": 1000}, "meta_data": {}
80 }
81 else:
82 assert sorted(self.result.extra_kwargs) == sorted(true)
84 def test_injection_parameters(self, true, pesummary=False):
85 """Test the injection_parameters property
86 """
87 if true is None:
88 assert self.result.injection_parameters is None
89 else:
90 import math
92 assert all(i in list(true.keys()) for i in self.parameters)
93 assert all(i in self.parameters for i in list(true.keys()))
95 if not pesummary:
96 for i in true.keys():
97 if math.isnan(true[i]):
98 assert math.isnan(self.result.injection_parameters[i])
99 else:
100 assert true[i] == self.result.injection_parameters[i]
102 def test_to_dat(self):
103 """Test the to_dat method
104 """
105 self.result.to_dat(outdir=tmpdir, label="label")
106 assert os.path.isfile(os.path.join(tmpdir, "pesummary_label.dat"))
107 data = np.genfromtxt(
108 os.path.join(tmpdir, "pesummary_label.dat"), names=True)
109 assert all(i in self.parameters for i in list(data.dtype.names))
110 assert all(i in list(data.dtype.names) for i in self.parameters)
111 for param in self.parameters:
112 assert np.testing.assert_almost_equal(
113 data[param], self.result.samples_dict[param], 8
114 ) is None
116 def test_file_format_read(self, path, file_format, _class, function=Read):
117 """Test that when the file_format is specified, that correct class is used
118 """
119 result = function(path, file_format=file_format)
120 assert isinstance(result, _class)
122 def test_downsample(self):
123 """Test the .downsample method. This includes testing that the
124 .downsample method downsamples to the specified number of samples,
125 that it only takes samples that are currently in the posterior
126 table and that it maintains concurrent samples.
127 """
128 old_samples_dict = self.result.samples_dict
129 nsamples = 50
130 self.result.downsample(nsamples)
131 new_samples_dict = self.result.samples_dict
132 assert new_samples_dict.number_of_samples == nsamples
133 for param in self.parameters:
134 assert all(
135 samp in old_samples_dict[param] for samp in
136 new_samples_dict[param]
137 )
138 for num in range(nsamples):
139 samp_inds = [
140 old_samples_dict[param].tolist().index(
141 new_samples_dict[param][num]
142 ) for param in self.parameters
143 ]
144 assert len(set(samp_inds)) == 1
147class GWBaseRead(BaseRead):
148 """Base class to test the GWRead specific functions
149 """
150 def test_parameters(self, true, pesummary=False):
151 """Test the parameter property
152 """
153 super(GWBaseRead, self).test_parameters(true, pesummary=pesummary)
154 from .base import gw_parameters
155 full_parameters = gw_parameters()
157 self.result.generate_all_posterior_samples()
158 assert all(i in self.result.parameters for i in full_parameters)
159 assert all(i in full_parameters for i in self.result.parameters)
161 def test_injection_parameters(self, true):
162 """Test the injection_parameters property
163 """
164 import math
166 super(GWBaseRead, self).test_injection_parameters(true)
167 self.result.add_injection_parameters_from_file(testing_dir + "/main_injection.xml", conversion=False)
168 true = {
169 'dec': [1.949725], 'geocent_time': [1186741861], 'spin_2x': [0.],
170 'spin_2y': [0.], 'spin_2z': [0.], 'luminosity_distance': [139.7643],
171 'ra': [-1.261573], 'spin_1y': [0.], 'spin_1x': [0.], 'spin_1z': [0.],
172 'psi': [1.75], 'phase': [0.], 'iota': [1.0471976],
173 'mass_1': [53.333332], 'mass_2': [26.666668],
174 'symmetric_mass_ratio': [0.22222222], 'a_1': float('nan'),
175 'a_2': float('nan'), 'tilt_1': float('nan'), 'tilt_2': float('nan'),
176 'phi_jl': float('nan'), 'phi_12': float('nan'),
177 'theta_jn': float('nan'), 'redshift': float('nan'),
178 'mass_1_source': float('nan'), 'mass_2_source': float('nan'),
179 'log_likelihood': float('nan')
180 }
181 assert all(i in list(true.keys()) for i in self.parameters)
182 for i in true.keys():
183 if not isinstance(true[i], list) and math.isnan(true[i]):
184 assert math.isnan(self.result.injection_parameters[i])
185 else:
186 np.testing.assert_almost_equal(
187 true[i], self.result.injection_parameters[i], 5
188 )
190 def test_calibration_data_in_results_file(self):
191 """Test the calibration_data_in_results_file property
192 """
193 pass
195 def test_add_injection_parameters_from_file(self):
196 """Test the add_injection_parameters_from_file method
197 """
198 pass
200 def test_add_fixed_parameters_from_config_file(self):
201 """Test the add_fixed_parameters_from_config_file method
202 """
203 pass
205 def test_to_lalinference_dat(self):
206 """Test the to_lalinference dat=True method
207 """
208 from pesummary.gw.file.standard_names import lalinference_map
210 self.result.to_lalinference(dat=True, outdir=tmpdir,
211 filename="lalinference_label.dat")
212 assert os.path.isfile(os.path.join(tmpdir, "lalinference_label.dat"))
213 data = np.genfromtxt(
214 os.path.join(tmpdir, "lalinference_label.dat"), names=True)
215 for param in data.dtype.names:
216 if param not in self.result.parameters:
217 pesummary_param = lalinference_map[param]
218 else:
219 pesummary_param = param
220 assert np.testing.assert_almost_equal(
221 data[param], self.result.samples_dict[pesummary_param], 8
222 ) is None
224 def test_file_format_read(self, path, file_format, _class):
225 """Test that when the file_format is specified, that correct class is used
226 """
227 super(GWBaseRead, self).test_file_format_read(
228 path, file_format, _class, function=GWRead
229 )
232class TestCoreJsonFile(BaseRead):
233 """Class to test loading in a JSON file with the core Read function
234 """
235 def setup_method(self):
236 """Setup the TestCoreJsonFile class
237 """
238 if not os.path.isdir(tmpdir):
239 os.mkdir(tmpdir)
240 self.parameters, self.samples = make_result_file(
241 outdir=tmpdir, extension="json", gw=False
242 )
243 self.path = os.path.join(tmpdir, "test.json")
244 self.result = Read(self.path)
246 def teardown_method(self):
247 """Remove all files and directories created from this class
248 """
249 if os.path.isdir(tmpdir):
250 shutil.rmtree(tmpdir)
252 def test_class_name(self):
253 """Test the class used to load in this file
254 """
255 assert isinstance(
256 self.result, pesummary.core.file.formats.default.SingleAnalysisDefault
257 )
259 def test_parameters(self):
260 """Test the parameter property of the default class
261 """
262 super(TestCoreJsonFile, self).test_parameters(self.parameters)
264 def test_samples(self):
265 """Test the samples property of the default class
266 """
267 super(TestCoreJsonFile, self).test_samples(self.samples)
269 def test_samples_dict(self):
270 true = [self.parameters, self.samples]
271 super(TestCoreJsonFile, self).test_samples_dict(true)
273 def test_version(self):
274 """Test the version property of the default class
275 """
276 super(TestCoreJsonFile, self).test_version()
278 def test_extra_kwargs(self):
279 """Test the extra_kwargs property of the default class
280 """
281 super(TestCoreJsonFile, self).test_extra_kwargs()
283 def test_injection_parameters(self):
284 """Test the injection_parameters property
285 """
286 true = {par: float("nan") for par in self.parameters}
287 super(TestCoreJsonFile, self).test_injection_parameters(true)
289 def test_to_dat(self):
290 """Test the to_dat method
291 """
292 super(TestCoreJsonFile, self).test_to_dat()
294 def test_file_format_read(self):
295 """Test that when the file_format is specified, that correct class is used
296 """
297 from pesummary.core.file.formats.default import SingleAnalysisDefault
299 super(TestCoreJsonFile, self).test_file_format_read(
300 self.path, "json", SingleAnalysisDefault
301 )
303 def test_downsample(self):
304 """Test that the posterior table is correctly downsampled
305 """
306 super(TestCoreJsonFile, self).test_downsample()
309class TestCoreHDF5File(BaseRead):
310 """Class to test loading in an HDF5 file with the core Read function
311 """
312 def setup_method(self):
313 """Setup the TestCoreHDF5File class
314 """
315 if not os.path.isdir(tmpdir):
316 os.mkdir(tmpdir)
317 self.parameters, self.samples = make_result_file(
318 outdir=tmpdir, extension="hdf5", gw=False
319 )
320 self.path = os.path.join(tmpdir, "test.h5")
321 self.result = Read(self.path)
323 def teardown_method(self):
324 """Remove the files and directories created from this class
325 """
326 if os.path.isdir(tmpdir):
327 shutil.rmtree(tmpdir)
329 def test_class_name(self):
330 """Test the class used to load in this file
331 """
332 assert isinstance(
333 self.result, pesummary.core.file.formats.default.SingleAnalysisDefault
334 )
336 def test_parameters(self):
337 """Test the parameter property of the default class
338 """
339 super(TestCoreHDF5File, self).test_parameters(self.parameters)
341 def test_samples(self):
342 """Test the samples property of the default class
343 """
344 super(TestCoreHDF5File, self).test_samples(self.samples)
346 def test_samples_dict(self):
347 """Test the samples_dict property of the default class
348 """
349 true = [self.parameters, self.samples]
350 super(TestCoreHDF5File, self).test_samples_dict(true)
352 def test_version(self):
353 """Test the version property of the default class
354 """
355 super(TestCoreHDF5File, self).test_version()
357 def test_extra_kwargs(self):
358 """Test the extra_kwargs property of the default class
359 """
360 super(TestCoreHDF5File, self).test_extra_kwargs()
362 def test_injection_parameters(self):
363 """Test the injection_parameters property
364 """
365 true = {par: float("nan") for par in self.parameters}
366 super(TestCoreHDF5File, self).test_injection_parameters(true)
368 def test_to_dat(self):
369 """Test the to_dat method
370 """
371 super(TestCoreHDF5File, self).test_to_dat()
373 def test_file_format_read(self):
374 """Test that when the file_format is specified, that correct class is used
375 """
376 from pesummary.core.file.formats.default import SingleAnalysisDefault
378 super(TestCoreHDF5File, self).test_file_format_read(self.path, "hdf5", SingleAnalysisDefault)
380 def test_downsample(self):
381 """Test that the posterior table is correctly downsampled
382 """
383 super(TestCoreHDF5File, self).test_downsample()
386class TestCoreCSVFile(BaseRead):
387 """Class to test loading in a csv file with the core Read function
388 """
389 def setup_method(self):
390 """Setup the TestCoreCSVFile class
391 """
392 if not os.path.isdir(tmpdir):
393 os.mkdir(tmpdir)
394 self.parameters, self.samples = make_result_file(
395 extension="csv", outdir=tmpdir, gw=False
396 )
397 self.path = os.path.join(tmpdir, "test.csv")
398 self.result = Read(self.path)
400 def teardown_method(self):
401 """Remove the files and directories created from this class
402 """
403 if os.path.isdir(tmpdir):
404 shutil.rmtree(tmpdir)
406 def test_class_name(self):
407 """Test the class used to load in this file
408 """
409 assert isinstance(
410 self.result, pesummary.core.file.formats.default.SingleAnalysisDefault
411 )
413 def test_parameters(self):
414 """Test the parameter property of the default class
415 """
416 super(TestCoreCSVFile, self).test_parameters(self.parameters)
418 def test_samples(self):
419 """Test the samples property of the default class
420 """
421 super(TestCoreCSVFile, self).test_samples(self.samples)
423 def test_samples_dict(self):
424 """Test the samples_dict property of the default class
425 """
426 true = [self.parameters, self.samples]
427 super(TestCoreCSVFile, self).test_samples_dict(true)
429 def test_version(self):
430 """Test the version property of the default class
431 """
432 super(TestCoreCSVFile, self).test_version()
434 def test_extra_kwargs(self):
435 """Test the extra_kwargs property of the default class
436 """
437 super(TestCoreCSVFile, self).test_extra_kwargs()
439 def test_injection_parameters(self):
440 """Test the injection_parameters property
441 """
442 true = {par: float("nan") for par in self.parameters}
443 super(TestCoreCSVFile, self).test_injection_parameters(true)
445 def test_to_dat(self):
446 """Test the to_dat method
447 """
448 super(TestCoreCSVFile, self).test_to_dat()
450 def test_file_format_read(self):
451 """Test that when the file_format is specified, that correct class is used
452 """
453 from pesummary.core.file.formats.default import SingleAnalysisDefault
455 super(TestCoreCSVFile, self).test_file_format_read(self.path, "csv", SingleAnalysisDefault)
457 def test_downsample(self):
458 """Test that the posterior table is correctly downsampled
459 """
460 super(TestCoreCSVFile, self).test_downsample()
463class TestCoreNumpyFile(BaseRead):
464 """Class to test loading in a numpy file with the core Read function
465 """
466 def setup_method(self):
467 """Setup the TestCoreNumpyFile class
468 """
469 if not os.path.isdir(tmpdir):
470 os.mkdir(tmpdir)
471 self.parameters, self.samples = make_result_file(
472 outdir=tmpdir, extension="npy", gw=False
473 )
474 self.path = os.path.join(tmpdir, "test.npy")
475 self.result = Read(self.path)
477 def teardown_method(self):
478 """Remove the files and directories created from this class
479 """
480 if os.path.isdir(tmpdir):
481 shutil.rmtree(tmpdir)
483 def test_class_name(self):
484 """Test the class used to load in this file
485 """
486 assert isinstance(
487 self.result, pesummary.core.file.formats.default.SingleAnalysisDefault
488 )
490 def test_parameters(self):
491 """Test the parameter property of the default class
492 """
493 super(TestCoreNumpyFile, self).test_parameters(self.parameters)
495 def test_samples(self):
496 """Test the samples property of the default class
497 """
498 super(TestCoreNumpyFile, self).test_samples(self.samples)
500 def test_samples_dict(self):
501 """Test the samples_dict property of the default class
502 """
503 true = [self.parameters, self.samples]
504 super(TestCoreNumpyFile, self).test_samples_dict(true)
506 def test_version(self):
507 """Test the version property of the default class
508 """
509 super(TestCoreNumpyFile, self).test_version()
511 def test_extra_kwargs(self):
512 """Test the extra_kwargs property of the default class
513 """
514 super(TestCoreNumpyFile, self).test_extra_kwargs()
516 def test_injection_parameters(self):
517 """Test the injection_parameters property
518 """
519 true = {par: float("nan") for par in self.parameters}
520 super(TestCoreNumpyFile, self).test_injection_parameters(true)
522 def test_to_dat(self):
523 """Test the to_dat method
524 """
525 super(TestCoreNumpyFile, self).test_to_dat()
527 def test_file_format_read(self):
528 """Test that when the file_format is specified, that correct class is used
529 """
530 from pesummary.core.file.formats.default import SingleAnalysisDefault
532 super(TestCoreNumpyFile, self).test_file_format_read(self.path, "numpy", SingleAnalysisDefault)
534 def test_downsample(self):
535 """Test that the posterior table is correctly downsampled
536 """
537 super(TestCoreNumpyFile, self).test_downsample()
540class TestCoreDatFile(BaseRead):
541 """Class to test loading in an dat file with the core Read function
542 """
543 def setup_method(self):
544 """Setup the TestCoreDatFile class
545 """
546 if not os.path.isdir(tmpdir):
547 os.mkdir(tmpdir)
548 self.parameters, self.samples = make_result_file(
549 outdir=tmpdir, extension="dat", gw=False
550 )
551 self.path = os.path.join(tmpdir, "test.dat")
552 self.result = Read(self.path)
554 def teardown_method(self):
555 """Remove the files and directories created from this class
556 """
557 if os.path.isdir(tmpdir):
558 shutil.rmtree(tmpdir)
560 def test_class_name(self):
561 """Test the class used to load in this file
562 """
563 assert isinstance(
564 self.result, pesummary.core.file.formats.default.SingleAnalysisDefault
565 )
567 def test_parameters(self):
568 """Test the parameter property of the default class
569 """
570 super(TestCoreDatFile, self).test_parameters(self.parameters)
572 def test_samples(self):
573 """Test the samples property of the default class
574 """
575 super(TestCoreDatFile, self).test_samples(self.samples)
577 def test_samples_dict(self):
578 """Test the samples_dict property of the default class
579 """
580 true = [self.parameters, self.samples]
581 super(TestCoreDatFile, self).test_samples_dict(true)
583 def test_version(self):
584 """Test the version property of the default class
585 """
586 super(TestCoreDatFile, self).test_version()
588 def test_extra_kwargs(self):
589 """Test the extra_kwargs property of the default class
590 """
591 super(TestCoreDatFile, self).test_extra_kwargs()
593 def test_injection_parameters(self):
594 """Test the injection_parameters property
595 """
596 true = {par: float("nan") for par in self.parameters}
597 super(TestCoreDatFile, self).test_injection_parameters(true)
599 def test_to_dat(self):
600 """Test the to_dat method
601 """
602 super(TestCoreDatFile, self).test_to_dat()
604 def test_file_format_read(self):
605 """Test that when the file_format is specified, that correct class is used
606 """
607 from pesummary.core.file.formats.default import SingleAnalysisDefault
609 super(TestCoreDatFile, self).test_file_format_read(self.path, "dat", SingleAnalysisDefault)
611 def test_downsample(self):
612 """Test that the posterior table is correctly downsampled
613 """
614 super(TestCoreDatFile, self).test_downsample()
617class BilbyFile(BaseRead):
618 """Base class to test loading in a bilby file with the core Read function
619 """
620 def test_class_name(self):
621 """Test the class used to load in this file
622 """
623 assert isinstance(self.result, pesummary.core.file.formats.bilby.Bilby)
625 def test_parameters(self):
626 """Test the parameter property of the bilby class
627 """
628 super(BilbyFile, self).test_parameters(self.parameters)
630 def test_samples(self):
631 """Test the samples property of the bilby class
632 """
633 super(BilbyFile, self).test_samples(self.samples)
635 def test_samples_dict(self):
636 """Test the samples_dict property of the bilby class
637 """
638 true = [self.parameters, self.samples]
639 super(BilbyFile, self).test_samples_dict(true)
641 def test_version(self):
642 """Test the version property of the bilby class
643 """
644 true = "bilby=0.5.3:"
645 super(BilbyFile, self).test_version(true)
647 def test_extra_kwargs(self):
648 """Test the extra_kwargs property of the default class
649 """
650 true = {"sampler": {
651 "log_bayes_factor": 0.5,
652 "log_noise_evidence": 0.1,
653 "log_evidence": 0.2,
654 "log_evidence_err": 0.1},
655 "meta_data": {'time_marginalization': True},
656 "other": {"likelihood": {"time_marginalization": "True"}}
657 }
658 super(BilbyFile, self).test_extra_kwargs(true)
660 def test_injection_parameters(self, true):
661 """Test the injection_parameters property
662 """
663 super(BilbyFile, self).test_injection_parameters(true)
665 def test_file_format_read(self, path, file_format):
666 """Test that when the file_format is specified, that correct class is used
667 """
668 from pesummary.core.file.formats.bilby import Bilby
670 super(BilbyFile, self).test_file_format_read(path, file_format, Bilby)
672 def test_priors(self, read_function=Read):
673 """Test that the priors are correctly extracted from the bilby result
674 file
675 """
676 for param, prior in self.result.priors["samples"].items():
677 assert isinstance(prior, np.ndarray)
678 f = read_function(self.path, disable_prior=True)
679 assert not len(f.priors["samples"])
680 f = read_function(self.path, nsamples_for_prior=200)
681 params = list(f.priors["samples"].keys())
682 assert len(f.priors["samples"][params[0]]) == 200
686class TestCoreJsonBilbyFile(BilbyFile):
687 """Class to test loading in a bilby json file with the core Read function
688 """
689 def setup_method(self):
690 """Setup the TestCoreBilbyFile class
691 """
692 if not os.path.isdir(tmpdir):
693 os.mkdir(tmpdir)
694 self.parameters, self.samples = make_result_file(
695 outdir=tmpdir, extension="json", gw=False, bilby=True
696 )
697 self.path = os.path.join(tmpdir, "test.json")
698 self.result = Read(self.path)
700 def teardown_method(self):
701 """Remove the files and directories created from this class
702 """
703 if os.path.isdir(tmpdir):
704 shutil.rmtree(tmpdir)
706 def test_class_name(self):
707 """Test the class used to load in this file
708 """
709 super(TestCoreJsonBilbyFile, self).test_class_name()
711 def test_parameters(self):
712 """Test the parameter property of the bilby class
713 """
714 super(TestCoreJsonBilbyFile, self).test_parameters()
716 def test_samples(self):
717 """Test the samples property of the bilby class
718 """
719 super(TestCoreJsonBilbyFile, self).test_samples()
721 def test_samples_dict(self):
722 """Test the samples_dict property of the bilby class
723 """
724 super(TestCoreJsonBilbyFile, self).test_samples_dict()
726 def test_version(self):
727 """Test the version property of the default class
728 """
729 super(TestCoreJsonBilbyFile, self).test_version()
731 def test_extra_kwargs(self):
732 """Test the extra_kwargs property of the default class
733 """
734 super(TestCoreJsonBilbyFile, self).test_extra_kwargs()
736 def test_injection_parameters(self):
737 """Test the injection_parameters property
738 """
739 true = {par: 1. for par in self.parameters}
740 super(TestCoreJsonBilbyFile, self).test_injection_parameters(true)
742 def test_to_dat(self):
743 """Test the to_dat method
744 """
745 super(TestCoreJsonBilbyFile, self).test_to_dat()
747 def test_file_format_read(self):
748 """Test that when the file_format is specified, that correct class is used
749 """
750 super(TestCoreJsonBilbyFile, self).test_file_format_read(self.path, "bilby")
752 def test_downsample(self):
753 """Test that the posterior table is correctly downsampled
754 """
755 super(TestCoreJsonBilbyFile, self).test_downsample()
757 def test_priors(self):
758 """Test that the priors are correctly extracted from the bilby result
759 file
760 """
761 super(TestCoreJsonBilbyFile, self).test_priors()
765class TestCoreHDF5BilbyFile(BilbyFile):
766 """Class to test loading in a bilby hdf5 file with the core Read function
767 """
768 def setup_method(self):
769 """Setup the TestCoreBilbyFile class
770 """
771 if not os.path.isdir(tmpdir):
772 os.mkdir(tmpdir)
773 self.parameters, self.samples = make_result_file(
774 outdir=tmpdir, extension="hdf5", gw=False, bilby=True
775 )
776 self.path = os.path.join(tmpdir, "test.hdf5")
777 self.result = Read(self.path)
779 def teardown_method(self):
780 """Remove the files and directories created from this class
781 """
782 if os.path.isdir(tmpdir):
783 shutil.rmtree(tmpdir)
785 def test_class_name(self):
786 """Test the class used to load in this file
787 """
788 super(TestCoreHDF5BilbyFile, self).test_class_name()
790 def test_parameters(self):
791 """Test the parameter property of the bilby class
792 """
793 super(TestCoreHDF5BilbyFile, self).test_parameters()
795 def test_samples(self):
796 """Test the samples property of the bilby class
797 """
798 super(TestCoreHDF5BilbyFile, self).test_samples()
800 def test_samples_dict(self):
801 """Test the samples_dict property of the bilby class
802 """
803 super(TestCoreHDF5BilbyFile, self).test_samples_dict()
805 def test_version(self):
806 """Test the version property of the default class
807 """
808 super(TestCoreHDF5BilbyFile, self).test_version()
810 def test_extra_kwargs(self):
811 """Test the extra_kwargs property of the default class
812 """
813 super(TestCoreHDF5BilbyFile, self).test_extra_kwargs()
815 def test_injection_parameters(self):
816 """Test the injection_parameters property
817 """
818 true = {par: 1. for par in self.parameters}
819 super(TestCoreHDF5BilbyFile, self).test_injection_parameters(true)
821 def test_to_dat(self):
822 """Test the to_dat method
823 """
824 super(TestCoreHDF5BilbyFile, self).test_to_dat()
826 def test_file_format_read(self):
827 """Test that when the file_format is specified, that correct class is used
828 """
829 super(TestCoreHDF5BilbyFile, self).test_file_format_read(self.path, "bilby")
831 def test_downsample(self):
832 """Test that the posterior table is correctly downsampled
833 """
834 super(TestCoreHDF5BilbyFile, self).test_downsample()
836 def test_priors(self):
837 """Test that the priors are correctly extracted from the bilby result
838 file
839 """
840 super(TestCoreHDF5BilbyFile, self).test_priors(read_function=Read)
843class PESummaryFile(BaseRead):
844 """Base class to test loading in a PESummary file with the core Read function
845 """
847 def test_class_name(self):
848 """Test the class used to load in this file
849 """
850 assert isinstance(self.result, pesummary.core.file.formats.pesummary.PESummary)
852 def test_parameters(self):
853 """Test the parameter property of the PESummary class
854 """
855 super(PESummaryFile, self).test_parameters(
856 self.parameters, pesummary=True)
858 def test_samples(self):
859 """Test the samples property of the PESummary class
860 """
861 super(PESummaryFile, self).test_samples(
862 self.samples, pesummary=True)
864 def test_version(self):
865 """Test the version property of the default class
866 """
867 true = ["No version information found"]
868 super(PESummaryFile, self).test_version(true)
870 def test_extra_kwargs(self):
871 """Test the extra_kwargs property of the default class
872 """
873 true = [{"sampler": {"log_evidence": 0.5}, "meta_data": {}}]
874 super(PESummaryFile, self).test_extra_kwargs(true)
876 def test_samples_dict(self):
877 """Test the samples_dict property
878 """
879 assert list(self.result.samples_dict.keys()) == ["label"]
881 parameters = self.parameters
882 samples = self.samples
883 for num, param in enumerate(parameters):
884 specific_samples = [i[num] for i in samples]
885 drawn_samples = self.result.samples_dict["label"][param]
886 np.testing.assert_almost_equal(drawn_samples, specific_samples)
888 def test_to_bilby(self):
889 """Test the to_bilby method
890 """
891 from pesummary.core.file.read import is_bilby_json_file
893 bilby_object = self.result.to_bilby(save=False)["label"]
894 bilby_object.save_to_file(
895 filename=os.path.join(tmpdir, "bilby.json"))
896 assert is_bilby_json_file(os.path.join(tmpdir, "bilby.json"))
898 def test_to_dat(self):
899 """Test the to_dat method
900 """
901 self.result.to_dat(
902 outdir=tmpdir, filenames={"label": "pesummary_label.dat"}
903 )
904 assert os.path.isfile(os.path.join(tmpdir, "pesummary_label.dat"))
905 data = np.genfromtxt(
906 os.path.join(tmpdir, "pesummary_label.dat"), names=True)
907 assert all(i in self.parameters for i in list(data.dtype.names))
908 assert all(i in list(data.dtype.names) for i in self.parameters)
910 def test_downsample(self):
911 """Test the .downsample method
912 """
913 old_samples_dict = self.result.samples_dict
914 nsamples = 50
915 self.result.downsample(nsamples)
916 for num, label in enumerate(self.result.labels):
917 assert self.result.samples_dict[label].number_of_samples == nsamples
918 for param in self.parameters[num]:
919 assert all(
920 samp in old_samples_dict[label][param] for samp in
921 self.result.samples_dict[label][param]
922 )
923 for idx in range(nsamples):
924 samp_inds = [
925 old_samples_dict[label][param].tolist().index(
926 self.result.samples_dict[label][param][idx]
927 ) for param in self.parameters[num]
928 ]
929 assert len(set(samp_inds)) == 1
933class TestCoreJsonPESummaryFile(PESummaryFile):
934 """Class to test loading in a PESummary json file with the core Read
935 function
936 """
937 def setup_method(self):
938 """Setup the TestCorePESummaryFile class
939 """
940 if not os.path.isdir(tmpdir):
941 os.mkdir(tmpdir)
942 self.parameters, self.samples = make_result_file(
943 outdir=tmpdir, extension="json", gw=False, pesummary=True
944 )
945 self.result = Read(os.path.join(tmpdir, "test.json"))
947 def teardown_method(self):
948 """Remove the files and directories created from this class
949 """
950 if os.path.isdir(tmpdir):
951 shutil.rmtree(tmpdir)
953 def test_class_name(self):
954 """Test the class used to load in this file
955 """
956 super(TestCoreJsonPESummaryFile, self).test_class_name()
958 def test_parameters(self):
959 """Test the parameter property of the PESummary class
960 """
961 super(TestCoreJsonPESummaryFile, self).test_parameters()
963 def test_samples(self):
964 """Test the samples property of the PESummary class
965 """
966 super(TestCoreJsonPESummaryFile, self).test_samples()
968 def test_samples_dict(self):
969 """Test the samples_dict property
970 """
971 super(TestCoreJsonPESummaryFile, self).test_samples_dict()
973 def test_version(self):
974 """Test the version property of the default class
975 """
976 super(TestCoreJsonPESummaryFile, self).test_version()
978 def test_extra_kwargs(self):
979 """Test the extra_kwargs property of the default class
980 """
981 super(TestCoreJsonPESummaryFile, self).test_extra_kwargs()
983 def test_injection_parameters(self):
984 """Test the injection_parameters property
985 """
986 true = {par: float("nan") for par in self.parameters}
987 super(TestCoreJsonPESummaryFile, self).test_injection_parameters(
988 true, pesummary=True)
990 def test_to_bilby(self):
991 """Test the to_bilby method
992 """
993 super(TestCoreJsonPESummaryFile, self).test_to_bilby()
995 def test_to_dat(self):
996 """Test the to_dat method
997 """
998 super(TestCoreJsonPESummaryFile, self).test_to_dat()
1000 def test_file_format_read(self):
1001 """Test that when the file_format is specified, that correct class is used
1002 """
1003 pass
1005 def test_downsample(self):
1006 """Test that the posterior table is correctly downsampled
1007 """
1008 super(TestCoreJsonPESummaryFile, self).test_downsample()
1011class TestCoreHDF5PESummaryFile(PESummaryFile):
1012 """Class to test loading in a PESummary hdf5 file with the core Read
1013 function
1014 """
1015 def setup_method(self):
1016 """Setup the TestCorePESummaryFile class
1017 """
1018 if not os.path.isdir(tmpdir):
1019 os.mkdir(tmpdir)
1020 self.parameters, self.samples = make_result_file(
1021 outdir=tmpdir, extension="hdf5", gw=False, pesummary=True
1022 )
1023 self.result = Read(os.path.join(tmpdir, "test.h5"))
1025 def teardown_method(self):
1026 """Remove the files and directories created from this class
1027 """
1028 if os.path.isdir(tmpdir):
1029 shutil.rmtree(tmpdir)
1031 def test_class_name(self):
1032 """Test the class used to load in this file
1033 """
1034 super(TestCoreHDF5PESummaryFile, self).test_class_name()
1036 def test_parameters(self):
1037 """Test the parameter property of the PESummary class
1038 """
1039 super(TestCoreHDF5PESummaryFile, self).test_parameters()
1041 def test_samples(self):
1042 """Test the samples property of the PESummary class
1043 """
1044 super(TestCoreHDF5PESummaryFile, self).test_samples()
1046 def test_samples_dict(self):
1047 """Test the samples_dict property
1048 """
1049 super(TestCoreHDF5PESummaryFile, self).test_samples_dict()
1051 def test_version(self):
1052 """Test the version property of the default class
1053 """
1054 super(TestCoreHDF5PESummaryFile, self).test_version()
1056 def test_extra_kwargs(self):
1057 """Test the extra_kwargs property of the default class
1058 """
1059 super(TestCoreHDF5PESummaryFile, self).test_extra_kwargs()
1061 def test_injection_parameters(self):
1062 """Test the injection_parameters property
1063 """
1064 true = {par: float("nan") for par in self.parameters}
1065 super(TestCoreHDF5PESummaryFile, self).test_injection_parameters(
1066 true, pesummary=True)
1068 def test_to_bilby(self):
1069 """Test the to_bilby method
1070 """
1071 super(TestCoreHDF5PESummaryFile, self).test_to_bilby()
1073 def test_to_dat(self):
1074 """Test the to_dat method
1075 """
1076 super(TestCoreHDF5PESummaryFile, self).test_to_dat()
1078 def test_file_format_read(self):
1079 """Test that when the file_format is specified, that correct class is used
1080 """
1081 pass
1083 def test_downsample(self):
1084 """Test that the posterior table is correctly downsampled
1085 """
1086 super(TestCoreHDF5PESummaryFile, self).test_downsample()
1089class TestGWCSVFile(GWBaseRead):
1090 """Class to test loading in a csv file with the core Read function
1091 """
1092 def setup_method(self):
1093 """Setup the TestGWCSVFile class
1094 """
1095 if not os.path.isdir(tmpdir):
1096 os.mkdir(tmpdir)
1097 self.parameters, self.samples = make_result_file(
1098 outdir=tmpdir, extension="csv", gw=True
1099 )
1100 self.path = os.path.join(tmpdir, "test.csv")
1101 self.result = GWRead(self.path)
1103 def teardown_method(self):
1104 """Remove the files and directories created from this class
1105 """
1106 if os.path.isdir(tmpdir):
1107 shutil.rmtree(tmpdir)
1109 def test_class_name(self):
1110 """Test the class used to load in this file
1111 """
1112 assert isinstance(
1113 self.result, pesummary.gw.file.formats.default.SingleAnalysisDefault
1114 )
1116 def test_parameters(self):
1117 """Test the parameter property of the default class
1118 """
1119 super(TestGWCSVFile, self).test_parameters(self.parameters)
1121 def test_samples(self):
1122 """Test the samples property of the default class
1123 """
1124 super(TestGWCSVFile, self).test_samples(self.samples)
1126 def test_samples_dict(self):
1127 """Test the samples_dict property of the default class
1128 """
1129 true = [self.parameters, self.samples]
1130 super(TestGWCSVFile, self).test_samples_dict(true)
1132 def test_version(self):
1133 """Test the version property of the default class
1134 """
1135 super(TestGWCSVFile, self).test_version()
1137 def test_extra_kwargs(self):
1138 """Test the extra_kwargs property of the default class
1139 """
1140 super(TestGWCSVFile, self).test_extra_kwargs()
1142 def test_injection_parameters(self):
1143 """Test the injection_parameters property
1144 """
1145 true = {par: float("nan") for par in self.parameters}
1146 super(TestGWCSVFile, self).test_injection_parameters(true)
1148 def test_to_dat(self):
1149 """Test the to_dat method
1150 """
1151 super(TestGWCSVFile, self).test_to_dat()
1153 def test_to_lalinference_dat(self):
1154 """Test the to_lalinference dat=True method
1155 """
1156 super(TestGWCSVFile, self).test_to_lalinference_dat()
1158 def test_file_format_read(self):
1159 """Test that when the file_format is specified, that correct class is used
1160 """
1161 from pesummary.gw.file.formats.default import SingleAnalysisDefault
1163 super(TestGWCSVFile, self).test_file_format_read(
1164 self.path, "csv", SingleAnalysisDefault
1165 )
1168class TestGWNumpyFile(GWBaseRead):
1169 """Class to test loading in a npy file with the core Read function
1170 """
1171 def setup_method(self):
1172 """Setup the TestGWNumpyFile class
1173 """
1174 if not os.path.isdir(tmpdir):
1175 os.mkdir(tmpdir)
1176 self.parameters, self.samples = make_result_file(
1177 outdir=tmpdir, extension="npy", gw=True
1178 )
1179 self.path = os.path.join(tmpdir, "test.npy")
1180 self.result = GWRead(self.path)
1182 def teardown_method(self):
1183 """Remove the files and directories created from this class
1184 """
1185 if os.path.isdir(tmpdir):
1186 shutil.rmtree(tmpdir)
1188 def test_class_name(self):
1189 """Test the class used to load in this file
1190 """
1191 assert isinstance(
1192 self.result, pesummary.gw.file.formats.default.SingleAnalysisDefault
1193 )
1195 def test_parameters(self):
1196 """Test the parameter property of the default class
1197 """
1198 super(TestGWNumpyFile, self).test_parameters(self.parameters)
1200 def test_samples(self):
1201 """Test the samples property of the default class
1202 """
1203 super(TestGWNumpyFile, self).test_samples(self.samples)
1205 def test_samples_dict(self):
1206 """Test the samples_dict property of the default class
1207 """
1208 true = [self.parameters, self.samples]
1209 super(TestGWNumpyFile, self).test_samples_dict(true)
1211 def test_version(self):
1212 """Test the version property of the default class
1213 """
1214 super(TestGWNumpyFile, self).test_version()
1216 def test_extra_kwargs(self):
1217 """Test the extra_kwargs property of the default class
1218 """
1219 super(TestGWNumpyFile, self).test_extra_kwargs()
1221 def test_injection_parameters(self):
1222 """Test the injection_parameters property
1223 """
1224 true = {par: float("nan") for par in self.parameters}
1225 super(TestGWNumpyFile, self).test_injection_parameters(true)
1227 def test_to_dat(self):
1228 """Test the to_dat method
1229 """
1230 super(TestGWNumpyFile, self).test_to_dat()
1232 def test_to_lalinference_dat(self):
1233 """Test the to_lalinference dat=True method
1234 """
1235 super(TestGWNumpyFile, self).test_to_lalinference_dat()
1237 def test_file_format_read(self):
1238 """Test that when the file_format is specified, that correct class is used
1239 """
1240 from pesummary.gw.file.formats.default import SingleAnalysisDefault
1242 super(TestGWNumpyFile, self).test_file_format_read(
1243 self.path, "numpy", SingleAnalysisDefault
1244 )
1247class TestGWDatFile(GWBaseRead):
1248 """Class to test loading in an dat file with the core Read function
1249 """
1250 def setup_method(self):
1251 """Setup the TestGWDatFile class
1252 """
1253 if not os.path.isdir(tmpdir):
1254 os.mkdir(tmpdir)
1255 self.parameters, self.samples = make_result_file(
1256 outdir=tmpdir, extension="dat", gw=True
1257 )
1258 self.path = os.path.join(tmpdir, "test.dat")
1259 self.result = GWRead(self.path)
1261 def teardown_method(self):
1262 """Remove the files and directories created from this class
1263 """
1264 if os.path.isdir(tmpdir):
1265 shutil.rmtree(tmpdir)
1267 def test_class_name(self):
1268 """Test the class used to load in this file
1269 """
1270 assert isinstance(
1271 self.result, pesummary.gw.file.formats.default.SingleAnalysisDefault
1272 )
1274 def test_parameters(self):
1275 """Test the parameter property of the default class
1276 """
1277 super(TestGWDatFile, self).test_parameters(self.parameters)
1279 def test_samples(self):
1280 """Test the samples property of the default class
1281 """
1282 super(TestGWDatFile, self).test_samples(self.samples)
1284 def test_samples_dict(self):
1285 """Test the samples_dict property of the default class
1286 """
1287 true = [self.parameters, self.samples]
1288 super(TestGWDatFile, self).test_samples_dict(true)
1290 def test_version(self):
1291 """Test the version property of the default class
1292 """
1293 super(TestGWDatFile, self).test_version()
1295 def test_extra_kwargs(self):
1296 """Test the extra_kwargs property of the default class
1297 """
1298 super(TestGWDatFile, self).test_extra_kwargs()
1300 def test_injection_parameters(self):
1301 """Test the injection_parameters property
1302 """
1303 true = {par: float("nan") for par in self.parameters}
1304 super(TestGWDatFile, self).test_injection_parameters(true)
1306 def test_to_dat(self):
1307 """Test the to_dat method
1308 """
1309 super(TestGWDatFile, self).test_to_dat()
1311 def test_to_lalinference_dat(self):
1312 """Test the to_lalinference dat=True method
1313 """
1314 super(TestGWDatFile, self).test_to_lalinference_dat()
1316 def test_file_format_read(self):
1317 """Test that when the file_format is specified, that correct class is used
1318 """
1319 from pesummary.gw.file.formats.default import SingleAnalysisDefault
1321 super(TestGWDatFile, self).test_file_format_read(
1322 self.path, "dat", SingleAnalysisDefault
1323 )
1325 def test_downsample(self):
1326 """Test that the posterior table is correctly downsampled
1327 """
1328 super(TestGWDatFile, self).test_downsample()
1331class TestGWHDF5File(GWBaseRead):
1332 """Class to test loading in an HDF5 file with the gw Read function
1333 """
1334 def setup_method(self):
1335 """Setup the TestCoreHDF5File class
1336 """
1337 if not os.path.isdir(tmpdir):
1338 os.mkdir(tmpdir)
1339 self.parameters, self.samples = make_result_file(
1340 outdir=tmpdir, extension="hdf5", gw=True
1341 )
1342 self.path = os.path.join(tmpdir, "test.h5")
1343 self.result = GWRead(self.path)
1345 def teardown_method(self):
1346 """Remove the files and directories created from this class
1347 """
1348 if os.path.isdir(tmpdir):
1349 shutil.rmtree(tmpdir)
1351 def test_class_name(self):
1352 """Test the class used to load in this file
1353 """
1354 assert isinstance(
1355 self.result, pesummary.gw.file.formats.default.SingleAnalysisDefault
1356 )
1358 def test_parameters(self):
1359 """Test the parameter property of the default class
1360 """
1361 super(TestGWHDF5File, self).test_parameters(self.parameters)
1363 def test_samples(self):
1364 """Test the samples property of the default class
1365 """
1366 super(TestGWHDF5File, self).test_samples(self.samples)
1368 def test_samples_dict(self):
1369 """Test the samples_dict property of the default class
1370 """
1371 true = [self.parameters, self.samples]
1372 super(TestGWHDF5File, self).test_samples_dict(true)
1374 def test_version(self):
1375 """Test the version property of the default class
1376 """
1377 super(TestGWHDF5File, self).test_version()
1379 def test_extra_kwargs(self):
1380 """Test the extra_kwargs property of the default class
1381 """
1382 super(TestGWHDF5File, self).test_extra_kwargs()
1384 def test_injection_parameters(self):
1385 """Test the injection_parameters property
1386 """
1387 true = {par: float("nan") for par in self.parameters}
1388 super(TestGWHDF5File, self).test_injection_parameters(true)
1390 def test_to_dat(self):
1391 """Test the to_dat method
1392 """
1393 super(TestGWHDF5File, self).test_to_dat()
1395 def test_to_lalinference_dat(self):
1396 """Test the to_lalinference dat=True method
1397 """
1398 super(TestGWHDF5File, self).test_to_lalinference_dat()
1400 def test_file_format_read(self):
1401 """Test that when the file_format is specified, that correct class is used
1402 """
1403 from pesummary.gw.file.formats.default import SingleAnalysisDefault
1405 super(TestGWHDF5File, self).test_file_format_read(
1406 self.path, "hdf5", SingleAnalysisDefault
1407 )
1409 def test_downsample(self):
1410 """Test that the posterior table is correctly downsampled
1411 """
1412 super(TestGWHDF5File, self).test_downsample()
1415class TestGWJsonFile(GWBaseRead):
1416 """Class to test loading in an json file with the gw Read function
1417 """
1418 def setup_method(self):
1419 """Setup the TestGWDatFile class
1420 """
1421 if not os.path.isdir(tmpdir):
1422 os.mkdir(tmpdir)
1423 self.parameters, self.samples = make_result_file(
1424 outdir=tmpdir, extension="json", gw=True
1425 )
1426 self.path = os.path.join(tmpdir, "test.json")
1427 self.result = GWRead(self.path)
1429 def teardown_method(self):
1430 """Remove the files and directories created from this class
1431 """
1432 if os.path.isdir(tmpdir):
1433 shutil.rmtree(tmpdir)
1435 def test_class_name(self):
1436 """Test the class used to load in this file
1437 """
1438 assert isinstance(
1439 self.result, pesummary.gw.file.formats.default.SingleAnalysisDefault
1440 )
1442 def test_parameters(self):
1443 """Test the parameter property of the default class
1444 """
1445 super(TestGWJsonFile, self).test_parameters(self.parameters)
1447 def test_samples(self):
1448 """Test the samples property of the default class
1449 """
1450 super(TestGWJsonFile, self).test_samples(self.samples)
1452 def test_samples_dict(self):
1453 """Test the samples_dict property of the default class
1454 """
1455 true = [self.parameters, self.samples]
1456 super(TestGWJsonFile, self).test_samples_dict(true)
1458 def test_version(self):
1459 """Test the version property of the default class
1460 """
1461 super(TestGWJsonFile, self).test_version()
1463 def test_extra_kwargs(self):
1464 """Test the extra_kwargs property of the default class
1465 """
1466 super(TestGWJsonFile, self).test_extra_kwargs()
1468 def test_injection_parameters(self):
1469 """Test the injection_parameters property
1470 """
1471 true = {par: float("nan") for par in self.parameters}
1472 super(TestGWJsonFile, self).test_injection_parameters(true)
1474 def test_to_dat(self):
1475 """Test the to_dat method
1476 """
1477 super(TestGWJsonFile, self).test_to_dat()
1479 def test_to_lalinference_dat(self):
1480 """Test the to_lalinference dat=True method
1481 """
1482 super(TestGWJsonFile, self).test_to_lalinference_dat()
1484 def test_file_format_read(self):
1485 """Test that when the file_format is specified, that correct class is used
1486 """
1487 from pesummary.gw.file.formats.default import SingleAnalysisDefault
1489 super(TestGWJsonFile, self).test_file_format_read(
1490 self.path, "json", SingleAnalysisDefault
1491 )
1493 def test_downsample(self):
1494 """Test that the posterior table is correctly downsampled
1495 """
1496 super(TestGWJsonFile, self).test_downsample()
1499class TestGWJsonBilbyFile(GWBaseRead):
1500 """Class to test loading in a bilby json file with the gw Read function
1501 """
1502 def setup_method(self):
1503 """Setup the TestCoreBilbyFile class
1504 """
1505 if not os.path.isdir(tmpdir):
1506 os.mkdir(tmpdir)
1507 self.parameters, self.samples = make_result_file(
1508 outdir=tmpdir, extension="json", gw=True, bilby=True
1509 )
1510 self.path = os.path.join(tmpdir, "test.json")
1511 self.result = GWRead(self.path, disable_prior=True)
1513 def teardown_method(self):
1514 """Remove the files and directories created from this class
1515 """
1516 if os.path.isdir(tmpdir):
1517 shutil.rmtree(tmpdir)
1519 def test_class_name(self):
1520 """Test the class used to load in this file
1521 """
1522 assert isinstance(self.result, pesummary.gw.file.formats.bilby.Bilby)
1524 def test_parameters(self):
1525 """Test the parameter property of the bilby class
1526 """
1527 super(TestGWJsonBilbyFile, self).test_parameters(self.parameters)
1529 def test_samples(self):
1530 """Test the samples property of the bilby class
1531 """
1532 super(TestGWJsonBilbyFile, self).test_samples(self.samples)
1534 def test_samples_dict(self):
1535 """Test the samples_dict property of the bilby class
1536 """
1537 true = [self.parameters, self.samples]
1538 super(TestGWJsonBilbyFile, self).test_samples_dict(true)
1540 def test_version(self):
1541 """Test the version property of the default class
1542 """
1543 true = "bilby=0.5.3:"
1544 super(TestGWJsonBilbyFile, self).test_version(true)
1546 def test_extra_kwargs(self):
1547 """Test the extra_kwargs property of the default class
1548 """
1549 true = {"sampler": {
1550 "log_bayes_factor": 0.5,
1551 "log_noise_evidence": 0.1,
1552 "log_evidence": 0.2,
1553 "log_evidence_err": 0.1},
1554 "meta_data": {"time_marginalization": True},
1555 "other": {"likelihood": {"time_marginalization": "True"}}
1556 }
1557 super(TestGWJsonBilbyFile, self).test_extra_kwargs(true)
1559 def test_injection_parameters(self):
1560 """Test the injection_parameters property
1561 """
1562 true = {par: 1. for par in self.parameters}
1563 super(TestGWJsonBilbyFile, self).test_injection_parameters(true)
1565 def test_to_dat(self):
1566 """Test the to_dat method
1567 """
1568 super(TestGWJsonBilbyFile, self).test_to_dat()
1570 def test_to_lalinference_dat(self):
1571 """Test the to_lalinference dat=True method
1572 """
1573 super(TestGWJsonBilbyFile, self).test_to_lalinference_dat()
1575 def test_file_format_read(self):
1576 """Test that when the file_format is specified, that correct class is used
1577 """
1578 from pesummary.gw.file.formats.bilby import Bilby
1580 super(TestGWJsonBilbyFile, self).test_file_format_read(self.path, "bilby", Bilby)
1582 def test_downsample(self):
1583 """Test that the posterior table is correctly downsampled
1584 """
1585 super(TestGWJsonBilbyFile, self).test_downsample()
1587 def test_priors(self, read_function=GWRead):
1588 """Test that the priors are correctly extracted from the bilby result
1589 file
1590 """
1591 self.result = GWRead(self.path)
1592 assert "final_mass_source_non_evolved" not in self.result.parameters
1593 for param, prior in self.result.priors["samples"].items():
1594 assert isinstance(prior, np.ndarray)
1595 assert "final_mass_source_non_evolved" in self.result.priors["samples"].keys()
1596 f = read_function(self.path, disable_prior_conversion=True)
1597 assert "final_mass_source_non_evolved" not in f.priors["samples"].keys()
1598 f = read_function(self.path, disable_prior=True)
1599 assert not len(f.priors["samples"])
1600 f = read_function(self.path, nsamples_for_prior=200)
1601 params = list(f.priors["samples"].keys())
1602 assert len(f.priors["samples"][params[0]]) == 200
1605class TestGWLALInferenceFile(GWBaseRead):
1606 """Class to test loading in a LALInference file with the gw Read function
1607 """
1608 def setup_method(self):
1609 """Setup the TestCoreBilbyFile class
1610 """
1611 if not os.path.isdir(tmpdir):
1612 os.mkdir(tmpdir)
1613 self.parameters, self.samples = make_result_file(
1614 outdir=tmpdir, extension="hdf5", gw=True, lalinference=True
1615 )
1616 self.path = os.path.join(tmpdir, "test.hdf5")
1617 self.result = GWRead(self.path)
1619 def teardown_method(self):
1620 """Remove the files and directories created from this class
1621 """
1622 if os.path.isdir(tmpdir):
1623 shutil.rmtree(tmpdir)
1625 def test_hdf5_dataset_to_list(self):
1626 """Test method to convert hdf5 dataset to list
1627 """
1628 import h5py
1629 f = h5py.File(self.path)
1630 path_to_samples = "lalinference/lalinference_nest/posterior_samples"
1631 parameters = f[path_to_samples].dtype.names
1632 old = [
1633 [float(i[parameters.index(j)]) for j in parameters] for
1634 i in f[path_to_samples]
1635 ]
1636 new = np.array(f[path_to_samples]).view((float, len(parameters))).tolist()
1637 for n in range(len(old)):
1638 np.testing.assert_almost_equal(old[n], new[n])
1640 def test_class_name(self):
1641 """Test the class used to load in this file
1642 """
1643 assert isinstance(
1644 self.result, pesummary.gw.file.formats.lalinference.LALInference)
1646 def test_parameters(self):
1647 """Test the parameter property of the bilby class
1648 """
1649 super(TestGWLALInferenceFile, self).test_parameters(self.parameters)
1651 def test_samples(self):
1652 """Test the samples property of the bilby class
1653 """
1654 super(TestGWLALInferenceFile, self).test_samples(self.samples)
1656 def test_samples_dict(self):
1657 """Test the samples_dict property of the bilby class
1658 """
1659 true = [self.parameters, self.samples]
1660 super(TestGWLALInferenceFile, self).test_samples_dict(true)
1662 def test_version(self):
1663 """Test the version property of the default class
1664 """
1665 super(TestGWLALInferenceFile, self).test_version()
1667 def test_extra_kwargs(self):
1668 """Test the extra_kwargs property of the default class
1669 """
1670 true = {"sampler": {"nsamples": 1000}, "meta_data": {}, "other": {}}
1671 super(TestGWLALInferenceFile, self).test_extra_kwargs(true=true)
1673 def test_injection_parameters(self):
1674 """Test the injection_parameters property
1675 """
1676 super(TestGWLALInferenceFile, self).test_injection_parameters(None)
1678 def test_to_dat(self):
1679 """Test the to_dat method
1680 """
1681 super(TestGWLALInferenceFile, self).test_to_dat()
1683 def test_to_lalinference_dat(self):
1684 """Test the to_lalinference dat=True method
1685 """
1686 super(TestGWLALInferenceFile, self).test_to_lalinference_dat()
1688 def test_file_format_read(self):
1689 """Test that when the file_format is specified, that correct class is used
1690 """
1691 from pesummary.gw.file.formats.lalinference import LALInference
1693 super(TestGWLALInferenceFile, self).test_file_format_read(
1694 self.path, "lalinference", LALInference
1695 )
1697 def test_downsample(self):
1698 """Test that the posterior table is correctly downsampled
1699 """
1700 super(TestGWLALInferenceFile, self).test_downsample()
1703class TestPublicPycbc(object):
1704 """Test that data files produced by Nitz et al.
1705 (https://github.com/gwastro/2-ogc) can be read in correctly.
1706 """
1707 def setup_method(self):
1708 """Setup the TestCoreBilbyFile class
1709 """
1710 if not os.path.isdir(tmpdir):
1711 os.mkdir(tmpdir)
1713 def teardown_method(self):
1714 """Remove the files and directories created from this class
1715 """
1716 if os.path.isdir(tmpdir):
1717 shutil.rmtree(tmpdir)
1719 def _pycbc_check(self, filename):
1720 """Test a public pycbc posterior samples file
1722 Parameters
1723 ----------
1724 filename: str
1725 url of pycbc posterior samples file you wish to download, read and
1726 test
1727 """
1728 from pesummary.core.fetch import download_and_read_file
1729 from pesummary.gw.file.standard_names import standard_names
1730 import h5py
1731 self.file = download_and_read_file(
1732 filename, read_file=False, outdir=tmpdir
1733 )
1734 self.result = GWRead(self.file, path_to_samples="samples")
1735 samples = self.result.samples_dict
1736 fp = h5py.File(self.file, 'r')
1737 fp_samples = fp["samples"]
1738 for param in fp_samples.keys():
1739 np.testing.assert_almost_equal(
1740 fp_samples[param], samples[standard_names.get(param, param)]
1741 )
1742 fp.close()
1744 def test_2_OGC(self):
1745 """Test the samples released as part of the 2-OGC catalog
1746 """
1747 self._pycbc_check(
1748 "https://github.com/gwastro/2-ogc/raw/master/posterior_samples/"
1749 "H1L1V1-EXTRACT_POSTERIOR_150914_09H_50M_45UTC-0-1.hdf"
1750 )
1752 def test_3_OGC(self):
1753 """Test the samples released as part of the 3-OGC catalog
1754 """
1755 self._pycbc_check(
1756 "https://github.com/gwastro/3-ogc/raw/master/posterior/"
1757 "GW150914_095045-PYCBC-POSTERIOR-XPHM.hdf"
1758 )
1761class TestPublicPrincetonO1O2(object):
1762 """Test that data files produced by Venumadhav et al.
1763 (https://github.com/jroulet/O2_samples) can be read in correctly
1764 """
1765 def setup_method(self):
1766 """Setup the TestCoreBilbyFile class
1767 """
1768 from pesummary.core.fetch import download_and_read_file
1769 if not os.path.isdir(".outdir"):
1770 os.mkdir(".outdir")
1771 self.file = download_and_read_file(
1772 "https://github.com/jroulet/O2_samples/raw/master/GW150914.npy",
1773 read_file=False, outdir=".outdir"
1774 )
1775 self.result = GWRead(self.file, file_format="princeton")
1777 def teardown_method(self):
1778 """Remove the files and directories created from this class
1779 """
1780 if os.path.isdir(".outdir"):
1781 shutil.rmtree(".outdir")
1783 def test_samples_dict(self):
1784 """
1785 """
1786 data = np.load(self.file)
1787 samples = self.result.samples_dict
1788 map = {
1789 "mchirp": "chirp_mass", "eta": "symmetric_mass_ratio",
1790 "s1z": "spin_1z", "s2z": "spin_2z", "RA": "ra", "DEC": "dec",
1791 "psi": "psi", "iota": "iota", "vphi": "phase", "tc": "geocent_time",
1792 "DL": "luminosity_distance"
1793 }
1794 columns = [
1795 'mchirp', 'eta', 's1z', 's2z', 'RA', 'DEC', 'psi', 'iota', 'vphi',
1796 'tc', 'DL'
1797 ]
1798 for num, param in enumerate(columns):
1799 np.testing.assert_almost_equal(data.T[num], samples[map[param]])
1802class TestMultiAnalysis(object):
1803 """Class to test that a file which contains multiple analyses can be read
1804 in appropiately
1805 """
1806 def setup_method(self):
1807 """Setup the TestMultiAnalysis class
1808 """
1809 from pesummary.utils.samples_dict import MultiAnalysisSamplesDict
1810 from pesummary.io import write
1812 if not os.path.isdir(tmpdir):
1813 os.mkdir(tmpdir)
1814 self.data = MultiAnalysisSamplesDict(
1815 {"label1": {
1816 "mass_1": np.random.uniform(20, 100, 10),
1817 "mass_2": np.random.uniform(5, 20, 10),
1818 }, "label2": {
1819 "mass_1": np.random.uniform(20, 100, 10),
1820 "mass_2": np.random.uniform(5, 20, 10)
1821 }}
1822 )
1823 write(
1824 self.data, file_format="sql", filename="multi_analysis.db",
1825 outdir=tmpdir, overwrite=True, delete_existing=True
1826 )
1827 self.result = read(
1828 os.path.join(tmpdir, "multi_analysis.db"),
1829 add_zero_likelihood=False, remove_row_column="ROW"
1830 )
1831 self.samples_dict = self.result.samples_dict
1833 def teardown_method(self):
1834 """Remove all files and directories created from this class
1835 """
1836 if os.path.isdir(tmpdir):
1837 shutil.rmtree(tmpdir)
1839 def test_multi_analysis_db(self):
1840 """Test that an sql database with more than one set of samples can
1841 be read in appropiately
1842 """
1843 assert sorted(self.samples_dict.keys()) == sorted(self.data.keys())
1844 for key in self.samples_dict.keys():
1845 assert sorted(self.samples_dict[key].keys()) == sorted(
1846 self.data[key].keys()
1847 )
1848 for param in self.samples_dict[key].keys():
1849 np.testing.assert_almost_equal(
1850 self.samples_dict[key][param], self.data[key][param]
1851 )
1852 self.result.generate_all_posterior_samples()
1853 self.samples_dict = self.result.samples_dict
1854 for key in self.samples_dict.keys():
1855 assert "total_mass" in self.samples_dict[key].keys()
1856 np.testing.assert_almost_equal(
1857 self.data[key]["mass_1"] + self.data[key]["mass_2"],
1858 self.samples_dict[key]["total_mass"]
1859 )
1862class TestSingleAnalysisChangeFormat(object):
1863 """Test that when changing file format through the 'write' method, the
1864 samples are conserved
1865 """
1866 def setup_method(self):
1867 """Setup the TestChangeFormat class
1868 """
1869 if not os.path.isdir(tmpdir):
1870 os.mkdir(tmpdir)
1871 self.parameters = ["log_likelihood", "mass_1", "mass_2"]
1872 self.samples = np.array(
1873 [
1874 np.random.uniform(20, 100, 1000),
1875 np.random.uniform(5, 10, 1000), np.random.uniform(0, 1, 1000)
1876 ]
1877 ).T
1878 write(
1879 self.parameters, self.samples, outdir=tmpdir, filename="test.dat",
1880 overwrite=True
1881 )
1882 self.result = read(os.path.join(tmpdir, "test.dat"))
1884 def teardown_method(self):
1885 """Remove all files and directories created from this class
1886 """
1887 if os.path.isdir(tmpdir):
1888 shutil.rmtree(tmpdir)
1890 def save_and_check(
1891 self, file_format, bilby=False, pesummary=False, lalinference=False
1892 ):
1893 """Save the result file and check the contents
1894 """
1895 if bilby:
1896 filename = "test_bilby.json"
1897 elif pesummary or lalinference:
1898 filename = "test_pesummary.h5"
1899 else:
1900 filename = "test.{}".format(file_format)
1901 self.result.write(
1902 file_format=file_format, outdir=tmpdir, filename=filename
1903 )
1904 result = read(os.path.join(tmpdir, filename), disable_prior=True)
1905 if pesummary:
1906 assert result.parameters[0] == self.parameters
1907 np.testing.assert_almost_equal(result.samples[0], self.samples)
1908 else:
1909 original = result.parameters
1910 sorted_params = sorted(result.parameters)
1911 idxs = [original.index(i) for i in sorted_params]
1912 assert sorted(result.parameters) == self.parameters
1913 np.testing.assert_almost_equal(
1914 np.array(result.samples)[:, idxs], self.samples
1915 )
1917 def test_to_bilby(self):
1918 """Test saving to bilby format
1919 """
1920 self.save_and_check("bilby", bilby=True)
1922 def test_to_hdf5(self):
1923 """Test saving to hdf5
1924 """
1925 self.save_and_check("hdf5")
1927 def test_to_json(self):
1928 """Test saving to json
1929 """
1930 self.save_and_check("json")
1932 def test_to_sql(self):
1933 """Test saving to sql
1934 """
1935 self.save_and_check("sql")
1937 def test_to_pesummary(self):
1938 self.save_and_check("pesummary", pesummary=True)
1940 def test_to_lalinference(self):
1941 self.save_and_check("lalinference", lalinference=True)
1944class TestMultipleAnalysisChangeFormat(object):
1945 """Test that when changing file format through the 'write' method, the
1946 samples are conserved
1947 """
1948 def setup_method(self):
1949 """Setup the TestMultiplAnalysisChangeFormat class
1950 """
1951 if not os.path.isdir(tmpdir):
1952 os.mkdir(tmpdir)
1953 self.parameters = [
1954 ["log_likelihood", "mass_1", "mass_2"],
1955 ["chirp_mass", "log_likelihood", "total_mass"]
1956 ]
1957 self.samples = np.array(
1958 [np.array(
1959 [
1960 np.random.uniform(20, 100, 1000),
1961 np.random.uniform(5, 10, 1000),
1962 np.random.uniform(0, 1, 1000)
1963 ]
1964 ).T, np.array(
1965 [
1966 np.random.uniform(20, 100, 1000),
1967 np.random.uniform(5, 10, 1000),
1968 np.random.uniform(0, 1, 1000)
1969 ]
1970 ).T]
1971 )
1972 write(
1973 self.parameters, self.samples, outdir=tmpdir, filename="test.db",
1974 overwrite=True, file_format="sql"
1975 )
1976 self.result = read(os.path.join(tmpdir, "test.db"))
1978 def teardown_method(self):
1979 """Remove all files and directories created from this class
1980 """
1981 if os.path.isdir(tmpdir):
1982 shutil.rmtree(tmpdir)
1984 def save_and_check(
1985 self, file_format, bilby=False, pesummary=False, lalinference=False,
1986 multiple_files=True
1987 ):
1988 """Save the result file and check the contents
1989 """
1990 if bilby:
1991 filename = "test_bilby.json"
1992 elif pesummary or lalinference:
1993 filename = "test_pesummary.h5"
1994 else:
1995 filename = "test.{}".format(file_format)
1996 self.result.write(
1997 file_format=file_format, outdir=tmpdir, filename=filename
1998 )
1999 if multiple_files:
2000 files = sorted(glob.glob(tmpdir + "/{}_*.{}".format(*filename.split("."))))
2001 assert len(files) == 2
2002 for num, _file in enumerate(files):
2003 result = read(_file, disable_prior=True)
2004 original = result.parameters
2005 sorted_params = sorted(result.parameters)
2006 idxs = [original.index(i) for i in sorted_params]
2007 assert sorted(result.parameters) == self.parameters[num]
2008 np.testing.assert_almost_equal(
2009 np.array(result.samples)[:, idxs], self.samples[num]
2010 )
2011 else:
2012 result = read(os.path.join(tmpdir, filename), disable_prior=True)
2013 original = result.parameters
2014 sorted_params = sorted(result.parameters)
2015 idxs = [original.index(i) for i in sorted_params]
2016 for ii in range(len(original)):
2017 assert result.parameters[ii] == self.parameters[ii]
2018 np.testing.assert_almost_equal(
2019 np.array(result.samples), self.samples
2020 )
2022 def test_to_bilby(self):
2023 """Test saving to bilby
2024 """
2025 self.save_and_check("bilby", bilby=True)
2027 def test_to_dat(self):
2028 """Test saving to dat
2029 """
2030 self.save_and_check("dat")
2032 def test_to_hdf5(self):
2033 """Test saving to hdf5
2034 """
2035 self.save_and_check("hdf5")
2037 def test_to_json(self):
2038 """Test saving to json
2039 """
2040 self.save_and_check("json")
2042 def test_to_sql(self):
2043 """Test saving to sql
2044 """
2045 self.save_and_check("sql", multiple_files=False)
2047 def test_to_pesummary(self):
2048 self.save_and_check("pesummary", pesummary=True, multiple_files=False)
2050 def test_to_lalinference(self):
2051 self.save_and_check("lalinference", lalinference=True)
2054def test_remove_nan_likelihoods():
2055 """Test that samples with 'nan' log_likelihoods are removed from the
2056 posterior table
2057 """
2058 from pesummary.utils.samples_dict import MultiAnalysisSamplesDict
2059 import math
2061 if not os.path.isdir(tmpdir):
2062 os.mkdir(tmpdir)
2063 parameters = ["a", "b", "log_likelihood"]
2064 likelihoods = np.random.uniform(0, 1, 1000)
2065 inds = np.random.choice(len(likelihoods), size=100, replace=False)
2066 likelihoods[inds] = float('nan')
2067 samples = np.array([
2068 np.random.uniform(10, 5, 1000), np.random.uniform(10, 5, 1000),
2069 likelihoods
2070 ]).T
2071 write(parameters, samples, filename="test.dat", outdir=tmpdir)
2072 f = read("{}/test.dat".format(tmpdir), remove_nan_likelihood_samples=False)
2073 read_samples = f.samples_dict
2074 for param in parameters:
2075 assert len(read_samples[param]) == 1000
2076 for num, param in enumerate(parameters):
2077 np.testing.assert_almost_equal(read_samples[param], samples.T[num])
2078 f = read("{}/test.dat".format(tmpdir), remove_nan_likelihood_samples=True)
2079 read_samples = f.samples_dict
2080 for param in parameters:
2081 assert len(read_samples[param]) == 900
2082 inds = np.array([math.isnan(_) for _ in likelihoods], dtype=bool)
2083 for num, param in enumerate(parameters):
2084 np.testing.assert_almost_equal(
2085 read_samples[param], samples.T[num][~inds]
2086 )
2087 likelihoods = np.random.uniform(0, 1, 2000).reshape(2, 1000)
2088 inds = np.random.choice(1000, size=100, replace=False)
2089 likelihoods[0][inds] = float('nan')
2090 inds = np.random.choice(1000, size=500, replace=False)
2091 likelihoods[1][inds] = float('nan')
2092 samples = {
2093 "one": {
2094 "a": np.random.uniform(1, 5, 1000), "b": np.random.uniform(1, 2, 1000),
2095 "log_likelihood": likelihoods[0]
2096 }, "two": {
2097 "c": np.random.uniform(1, 5, 1000), "d": np.random.uniform(1, 2, 1000),
2098 "log_likelihood": likelihoods[1]
2099 }
2100 }
2101 data = MultiAnalysisSamplesDict(samples)
2102 write(
2103 data, file_format="pesummary", filename="multi.h5", outdir=tmpdir,
2104 )
2105 f = read("{}/multi.h5".format(tmpdir), remove_nan_likelihood_samples=True)
2106 _samples_dict = f.samples_dict
2107 for num, label in enumerate(["one", "two"]):
2108 inds = np.array([math.isnan(_) for _ in likelihoods[num]], dtype=bool)
2109 if num == 0:
2110 assert len(_samples_dict["one"]["a"]) == 900
2111 else:
2112 assert len(_samples_dict["two"]["c"]) == 500
2113 for param in samples[label].keys():
2114 np.testing.assert_almost_equal(
2115 _samples_dict[label][param], samples[label][param][~inds]
2116 )
2117 if os.path.isdir(tmpdir):
2118 shutil.rmtree(tmpdir)
2121def test_add_log_likelihood():
2122 """Test that zero log likelihood samples are added when the posterior table
2123 does not include likelihood samples
2124 """
2125 from pesummary.utils.samples_dict import MultiAnalysisSamplesDict
2127 if not os.path.isdir(tmpdir):
2128 os.mkdir(tmpdir)
2129 parameters = ["a", "b"]
2130 samples = np.array([
2131 np.random.uniform(10, 5, 1000), np.random.uniform(10, 5, 1000)
2132 ]).T
2133 write(parameters, samples, filename="test.dat", outdir=tmpdir)
2134 f = read("{}/test.dat".format(tmpdir))
2135 _samples_dict = f.samples_dict
2136 assert sorted(f.parameters) == ["a", "b", "log_likelihood"]
2137 np.testing.assert_almost_equal(
2138 _samples_dict["log_likelihood"], np.zeros(1000)
2139 )
2140 np.testing.assert_almost_equal(_samples_dict["a"], samples.T[0])
2141 np.testing.assert_almost_equal(_samples_dict["b"], samples.T[1])
2142 parameters = [["a", "b"], ["c", "d"]]
2143 samples = [
2144 np.array([np.random.uniform(1, 5, 1000), np.random.uniform(1, 2, 1000)]).T,
2145 np.array([np.random.uniform(1, 5, 1000), np.random.uniform(1, 2, 1000)]).T
2146 ]
2147 data = MultiAnalysisSamplesDict({
2148 "one": {
2149 "a": np.random.uniform(1, 5, 1000), "b": np.random.uniform(1, 2, 1000)
2150 }, "two": {
2151 "c": np.random.uniform(1, 5, 1000), "d": np.random.uniform(1, 2, 1000)
2152 }
2153 })
2154 write(
2155 data, file_format="pesummary", filename="multi.h5", outdir=tmpdir,
2156 )
2157 f = read("{}/multi.h5".format(tmpdir))
2158 _samples_dict = f.samples_dict
2159 np.testing.assert_almost_equal(
2160 _samples_dict["one"]["log_likelihood"], np.zeros(1000)
2161 )
2162 np.testing.assert_almost_equal(
2163 _samples_dict["two"]["log_likelihood"], np.zeros(1000)
2164 )
2165 if os.path.isdir(tmpdir):
2166 shutil.rmtree(tmpdir)