Skip to content

Commit f5c2d26

Browse files
committed
TST: improve result save/read tests
1 parent 96ed380 commit f5c2d26

1 file changed

Lines changed: 101 additions & 14 deletions

File tree

test/core/result_test.py

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class TestResult(unittest.TestCase):
5959
def init_outdir(self, tmp_path):
6060
# Use pytest's tmp_path fixture to create a temporary directory
6161
self.outdir = str(tmp_path / "test")
62+
# Outdirs not used in the result object
63+
self.other_outdir = str(tmp_path / "test2")
64+
self.other_outdir2 = str(tmp_path / "test3")
6265

6366
def setUp(self):
6467
np.random.seed(7)
@@ -137,16 +140,25 @@ def test_result_file_name_pickle(self):
137140
"{}/{}_result.pkl".format(outdir, label),
138141
)
139142

140-
def test_fail_save_and_load(self):
141-
with self.assertRaises(ValueError):
143+
def test_fail_save_and_load_missing_inputs(self):
144+
with self.assertRaises(
145+
ValueError, msg="No information given to load file"
146+
):
142147
bilby.core.result.read_in_result()
143148

144-
with self.assertRaises(ValueError):
149+
def test_fail_save_and_load_no_extension(self):
150+
with self.assertRaises(ValueError, msg="No file extension provided."):
145151
bilby.core.result.read_in_result(filename="no_file_extension")
146152

153+
def test_fail_save_and_load_invalid_extension(self):
154+
with self.assertRaises(ValueError, msg="Filetype .invalid not understood."):
155+
bilby.core.result.read_in_result(filename="file.invalid")
156+
157+
def test_fail_save_and_load_invalid_file(self):
147158
with self.assertRaises(IOError):
148159
bilby.core.result.read_in_result(filename="not/a/file.json")
149160

161+
def test_fail_save_and_load_incomplete_json(self):
150162
with self.assertRaises(IOError):
151163
incomplete_json = """
152164
{
@@ -287,14 +299,35 @@ def _save_with_outdir_and_filename(self, filename, outdir, template):
287299
self.result.save_to_file(filename=filename, outdir=outdir, extension="json", gzip=False)
288300
self.assertTrue(os.path.isfile(template))
289301

290-
def test_save_with_outdir_and_filename(self):
291-
self._save_with_outdir_and_filename("out/result", "out2", "out2/result")
292-
self._save_with_outdir_and_filename("out/result", None, "out/result")
293-
self._save_with_outdir_and_filename("result", "out", "out/result")
302+
def test_save_with_outdir_and_filename_different_outdir(self):
303+
self._save_with_outdir_and_filename(
304+
f"{self.other_outdir}/result", self.other_outdir2, f"{self.other_outdir2}/result"
305+
)
306+
307+
def test_save_with_outdir_and_filename_same_outdir(self):
308+
self._save_with_outdir_and_filename(
309+
f"{self.other_outdir}/result", None, f"{self.other_outdir}/result"
310+
)
311+
312+
def test_save_with_outdir_and_filename_no_outdir_in_filename(self):
294313
self._save_with_outdir_and_filename(
295-
"result", None, os.path.join(self.result.outdir, "result"))
314+
"result", self.other_outdir, f"{self.other_outdir}/result"
315+
)
316+
317+
def test_save_with_filename_only(self):
318+
self._save_with_outdir_and_filename(
319+
"result", None, os.path.join(self.result.outdir, "result")
320+
)
321+
322+
def test_save_with_outdir_no_filename(self):
323+
self._save_with_outdir_and_filename(
324+
None, self.other_outdir, os.path.join(self.other_outdir, f"{self.result.label}_result.json")
325+
)
326+
327+
def test_save_no_filename_or_outdir(self):
296328
self._save_with_outdir_and_filename(
297-
None, "out", os.path.join("out", f"{self.result.label}_result.json"))
329+
None, None, os.path.join(self.result.outdir, f"{self.result.label}_result.json")
330+
)
298331

299332
def test_save_and_overwrite_json(self):
300333
self._save_and_overwrite_test(extension='json')
@@ -779,6 +812,11 @@ def test_sanity_check_labels(self):
779812

780813
class TestPPPlots(unittest.TestCase):
781814

815+
@pytest.fixture(autouse=True)
816+
def init_outdir(self, tmp_path):
817+
# Use pytest's tmp_path fixture to create a temporary directory
818+
self.outdir = str(tmp_path / "test_pp_plots")
819+
782820
def setUp(self):
783821
priors = bilby.core.prior.PriorDict(dict(
784822
a=bilby.core.prior.Uniform(0, 1, latex_label="$a$"),
@@ -787,7 +825,7 @@ def setUp(self):
787825
self.results = [
788826
bilby.core.result.Result(
789827
label=str(ii),
790-
outdir='.',
828+
outdir=self.outdir,
791829
search_parameter_keys=list(priors.keys()),
792830
priors=priors,
793831
injection_parameters=priors.sample(),
@@ -876,11 +914,58 @@ def test_reweight_different_likelihood_weights_correct(self):
876914
self.assertLess(abs(new.log_evidence - self.result.log_evidence), 0.05)
877915
self.assertNotEqual(new.log_evidence, self.result.log_evidence)
878916

879-
def test_save_to_file_filename_with_extension_and_extension_none(self):
917+
918+
class TestResultSaveAndRead(unittest.TestCase):
919+
920+
@pytest.fixture(autouse=True)
921+
def init_outdir(self, tmp_path):
922+
# Use pytest's tmp_path fixture to create a temporary directory
923+
self.outdir = str(tmp_path / "test_result_save_and_read")
924+
925+
def setUp(self):
926+
np.random.seed(7)
927+
bilby.utils.command_line_args.bilby_test_mode = False
928+
priors = bilby.prior.PriorDict(
929+
dict(
930+
x=bilby.prior.Uniform(0, 1, "x", latex_label="$x$", unit="s"),
931+
y=bilby.prior.Uniform(0, 1, "y", latex_label="$y$", unit="m"),
932+
c=1,
933+
d=2,
934+
)
935+
)
936+
result = bilby.core.result.Result(
937+
label="label",
938+
outdir=self.outdir,
939+
sampler="nestle",
940+
search_parameter_keys=["x", "y"],
941+
fixed_parameter_keys=["c", "d"],
942+
priors=priors,
943+
sampler_kwargs=dict(test="test", func=lambda x: x),
944+
injection_parameters=dict(x=0.5, y=0.5),
945+
meta_data=dict(test="test"),
946+
sampling_time=100.0,
947+
)
948+
949+
n = 100
950+
posterior = pd.DataFrame(
951+
dict(x=np.random.normal(0, 1, n), y=np.random.normal(0, 1, n))
952+
)
953+
result.posterior = posterior
954+
result.log_evidence = 10
955+
result.log_evidence_err = 11
956+
result.log_bayes_factor = 12
957+
result.log_noise_evidence = 13
958+
self.result = result
959+
960+
@parameterized.parameterized.expand([
961+
".h5", ".hdf5", ".json", ".pkl", ".pickle",
962+
])
963+
def test_save_and_read_filename_with_extension_and_extension_none(self, ext):
880964
# Should use the extension from filename
881-
filename = os.path.join(self.result.outdir, "custom_name.hdf5")
965+
filename = os.path.join(self.result.outdir, f"custom_name.{ext}")
882966
self.result.save_to_file(filename=filename, extension=None)
883967
self.assertTrue(os.path.isfile(filename))
968+
bilby.core.result.read_in_result(filename=filename)
884969
os.remove(filename)
885970

886971
@parameterized.parameterized.expand([
@@ -889,7 +974,7 @@ def test_save_to_file_filename_with_extension_and_extension_none(self):
889974
("pickle",),
890975
(True,),
891976
])
892-
def test_save_to_file_filename_with_extension_and_extension(self, extension):
977+
def test_save_and_read_filename_with_extension_and_extension(self, extension):
893978
"""Test all the extensions that are support when the filename is provided"""
894979
filename = os.path.join(self.result.outdir, "custom_name.hdf5")
895980
expected = filename
@@ -902,12 +987,13 @@ def test_save_to_file_filename_with_extension_and_extension(self, extension):
902987
bilby.core.result.read_in_result(filename=expected, extension=extension)
903988
os.remove(expected)
904989

905-
def test_save_to_file_filename_without_extension_and_extension_none(self):
990+
def test_save_and_read_filename_without_extension_and_extension_none(self):
906991
# Should use the default extension (json)
907992
filename = os.path.join(self.result.outdir, "custom_name_noext")
908993
expected = filename
909994
self.result.save_to_file(filename=filename, extension=None)
910995
self.assertTrue(os.path.isfile(expected))
996+
bilby.core.result.read_in_result(filename=expected, extension="json")
911997
os.remove(expected)
912998

913999
def test_save_to_file_defaults_to_pickle_with_incorrect_extension(self):
@@ -917,6 +1003,7 @@ def test_save_to_file_defaults_to_pickle_with_incorrect_extension(self):
9171003
self.result.save_to_file(filename=filename, extension="bar")
9181004
self.assertTrue(os.path.isfile(expected))
9191005
self.assertFalse(os.path.isfile(filename))
1006+
bilby.core.result.read_in_result(filename=expected)
9201007
os.remove(expected)
9211008

9221009

0 commit comments

Comments
 (0)