-
Notifications
You must be signed in to change notification settings - Fork 79
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,11 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is using this method instead of just doing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 inbilby
,bilby.core
andbilby.core.utils
. We could make an exception formeta_data
file.