ENH: add global meta data#873
Conversation
ColmTalbot
left a comment
There was a problem hiding this comment.
This looks like a good start to me.
| 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.
Is using this method instead of just doing global_meta_data[key] = value required, or some convention I haven't come across before?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
You can in principle override the get/set item: https://stackoverflow.com/questions/2390827/how-to-properly-subclass-dict-and-override-getitem-setitem
However, my experience when doing this before is that can introduce unexpected behaviour. But, on the other hand it may be a good idea so that any checks are uniformly applied (e.g. if someone utilises the get/set method instead of using add_to_metadata
There was a problem hiding this comment.
That's a good idea @GregoryAshton. I've used UserDict as the base class, since this is less error-prone than inheriting directly from dict. This does mean, however, I've had to add encodings for UserDict (I add UserList as well since they can share the same if statement).
Let me know what you both think
|
The tests are going to fail until #867 is merged, since it adds support for properly saving the |
aabeafd to
45fbd9e
Compare
|
In what seems to be a reoccurring theme, I realised that adding the |
b788d37 to
1a808c9
Compare
a1a1a72 to
a93dea1
Compare
ColmTalbot
left a comment
There was a problem hiding this comment.
This looks good to me now.
asb5468
left a comment
There was a problem hiding this comment.
This looks good to me and the tests cover the new functionality suitably.
Both classes have `data` attribute that is a dict (list) that can be saved as normal.
a93dea1 to
d545b5b
Compare
Based on the discussion in #867, I've made a start on a possible implementation of a global meta data object.
For now, it's just a singleton dictionary that, by default, tracks the
rng. Things like thecosmologycan then be added when they're set.Motivation
This change makes it easier to keep track of any global variables and allows us to ensure they're saved in the result file after running a sampler.
Changes (updated 27/01/25)
meta_datasubmodule andGlobalMetaDataclassseed,set_cosmologyand_set_default_cosmologyto update the global meta data when called.UserDictandUserListsubclasses. These rely on the fact both classes have adataattribute, see the Python docsTesting
These changes are mostly covered by the existing tests save result files, but I added a test for the encoding/decoding change and for the global meta data class.