Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add global meta data #873

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bilby/core/sampler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
command_line_args,
env_package_list,
get_entry_points,
global_meta_data,
loaded_modules_dict,
logger,
)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bilby/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
25 changes: 25 additions & 0 deletions bilby/core/utils/meta_data.py
Original file line number Diff line number Diff line change
@@ -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
})
4 changes: 4 additions & 0 deletions bilby/core/utils/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class Generator:


def seed(seed):
from .meta_data import global_meta_data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessarily have to hide these imports, the meta data will be initialized at import time anyway, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was running into circular imports but that may have been an old thing, I'll have a look

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if I make this a top-level import, then I hit circular import issues. I think this is because everything is imported in the __init__.py files in bilby, bilby.core and bilby.core.utils. We could make an exception for meta_data file.


Generator.rng = default_rng(seed)
global_meta_data.add_to_meta_data("rng", Generator.rng)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using this method instead of just doing global_meta_data[key] = value required, or some convention I haven't come across before?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we may want to add some form of check/validation when adding values, so this would make it easier in future. Happy to revert to something simpler though

global_meta_data.add_to_meta_data("seed", seed)


def generate_seeds(nseeds):
Expand Down
2 changes: 2 additions & 0 deletions bilby/gw/cosmology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading