From f1b0bace79d9068afd4cf0da38d580de0e1e8ade Mon Sep 17 00:00:00 2001 From: mj-will Date: Fri, 6 Dec 2024 17:38:48 +0000 Subject: [PATCH 1/4] ENH: add global meta data --- bilby/core/sampler/__init__.py | 2 ++ bilby/core/utils/__init__.py | 1 + bilby/core/utils/meta_data.py | 25 +++++++++++++++++++++++++ bilby/core/utils/random.py | 3 +++ 4 files changed, 31 insertions(+) create mode 100644 bilby/core/utils/meta_data.py diff --git a/bilby/core/sampler/__init__.py b/bilby/core/sampler/__init__.py index ef65ce1d..d0f7f838 100644 --- a/bilby/core/sampler/__init__.py +++ b/bilby/core/sampler/__init__.py @@ -7,6 +7,7 @@ command_line_args, env_package_list, get_entry_points, + global_meta_data, loaded_modules_dict, logger, ) @@ -251,6 +252,7 @@ def run_sampler( meta_data["likelihood"] = likelihood.meta_data meta_data["loaded_modules"] = loaded_modules_dict() meta_data["environment_packages"] = env_package_list(as_dataframe=True) + meta_data["global_meta_data"] = global_meta_data if command_line_args.bilby_zero_likelihood_mode: from bilby.core.likelihood import ZeroLikelihood diff --git a/bilby/core/utils/__init__.py b/bilby/core/utils/__init__.py index bb599156..80f0b69b 100644 --- a/bilby/core/utils/__init__.py +++ b/bilby/core/utils/__init__.py @@ -10,6 +10,7 @@ from .introspection import * from .io import * from .log import * +from .meta_data import * from .plotting import * from .samples import * from .series import * diff --git a/bilby/core/utils/meta_data.py b/bilby/core/utils/meta_data.py new file mode 100644 index 00000000..db1f00e2 --- /dev/null +++ b/bilby/core/utils/meta_data.py @@ -0,0 +1,25 @@ +from collections import UserDict + +from . import random + + +class GlobalMetaData(UserDict): + """A class to store global meta data. + + This class is a singleton, meaning that only one instance can exist at a time. + """ + + _instance = None + + def add_to_meta_data(self, key, value): + self[key] = value + + def __new__(cls, *args, **kwargs): + if cls._instance is None: + cls._instance = super().__new__(cls) + return cls._instance + + +global_meta_data = GlobalMetaData({ + "rng": random.rng +}) diff --git a/bilby/core/utils/random.py b/bilby/core/utils/random.py index dbee5b09..f6f24868 100644 --- a/bilby/core/utils/random.py +++ b/bilby/core/utils/random.py @@ -11,7 +11,10 @@ class Generator: def seed(seed): + from .meta_data import global_meta_data + Generator.rng = default_rng(seed) + global_meta_data.add_to_meta_data("rng", Generator.rng) def generate_seeds(nseeds): From 8aea58f632fedecbd771c218bf8ed7dd66e4832e Mon Sep 17 00:00:00 2001 From: mj-will Date: Fri, 6 Dec 2024 17:38:58 +0000 Subject: [PATCH 2/4] ENH: add cosmology to global meta data --- bilby/gw/cosmology.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bilby/gw/cosmology.py b/bilby/gw/cosmology.py index 3e15eb1c..e4c73ce2 100644 --- a/bilby/gw/cosmology.py +++ b/bilby/gw/cosmology.py @@ -8,10 +8,12 @@ def _set_default_cosmology(): from astropy import cosmology as cosmo + from ..core.utils.meta_data import global_meta_data global DEFAULT_COSMOLOGY, COSMOLOGY if DEFAULT_COSMOLOGY is None: DEFAULT_COSMOLOGY = cosmo.Planck15 COSMOLOGY = [DEFAULT_COSMOLOGY, DEFAULT_COSMOLOGY.name] + global_meta_data.add_to_meta_data("cosmology", COSMOLOGY) def get_available_cosmologies(): From 0ac165bc03117d3b13dc879a5b2c1fd896abb346 Mon Sep 17 00:00:00 2001 From: mj-will Date: Fri, 6 Dec 2024 17:52:18 +0000 Subject: [PATCH 3/4] ENH: add seed to meta data --- bilby/core/utils/random.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bilby/core/utils/random.py b/bilby/core/utils/random.py index f6f24868..54a357d0 100644 --- a/bilby/core/utils/random.py +++ b/bilby/core/utils/random.py @@ -15,6 +15,7 @@ def seed(seed): Generator.rng = default_rng(seed) global_meta_data.add_to_meta_data("rng", Generator.rng) + global_meta_data.add_to_meta_data("seed", seed) def generate_seeds(nseeds): From aabeafd2e3b6a129eb3c9dd3f96929c9b9373195 Mon Sep 17 00:00:00 2001 From: mj-will Date: Mon, 9 Dec 2024 13:17:24 +0000 Subject: [PATCH 4/4] MAINT: only specify cosmology object in the global meta data --- bilby/gw/cosmology.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bilby/gw/cosmology.py b/bilby/gw/cosmology.py index e4c73ce2..e4d7eae4 100644 --- a/bilby/gw/cosmology.py +++ b/bilby/gw/cosmology.py @@ -13,7 +13,7 @@ def _set_default_cosmology(): if DEFAULT_COSMOLOGY is None: DEFAULT_COSMOLOGY = cosmo.Planck15 COSMOLOGY = [DEFAULT_COSMOLOGY, DEFAULT_COSMOLOGY.name] - global_meta_data.add_to_meta_data("cosmology", COSMOLOGY) + global_meta_data.add_to_meta_data("cosmology", COSMOLOGY[0]) def get_available_cosmologies():