-
Notifications
You must be signed in to change notification settings - Fork 80
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 cosmology to CBC result #867
Conversation
I don't think this is enough to actually record the cosmology in the file, the simplest way would be to add it to the metadata and then access it like the other properties. |
I've updated the PR to added support for saving astropy cosmologies to our Whilst doing this, also updated the code to use |
bilby/core/sampler/__init__.py
Outdated
@@ -251,6 +251,14 @@ 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) | |||
# Add the cosmology if available |
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'd rather not add this here, I think it sets a bad precedent of adding application-specific logic into core. Is there some reason adding this to the CBCResult
is not sufficient? I think "users didn't specify the result type" is not a sufficient reason here.
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 agree that 'users didn't specify the result type' shouldn't be reason enough in principle, but I'm slightly concerned the documentation isn't sufficiently clear on this (AFAICT, only two of all our GW examples specify it). Therefore, anyone that's used one of the examples that doesn't have it as a starting point may not have specified the class. It's also different to e.g. the likelihood metadata which is always saved irrespective of the results class, meaning it can be recovered even if the wrong result class is used.
I do see your point, as this is clearly GW code. This may be out-of-scope for this MR, but I could imagine having an object that keeps track of some of the global variables, e.g. rng
? This could then be saved in the metadata. If we had something like this, calling set_cosmology
could just add the cosmology to that object?
Either way, I think we should update the GW examples to include the correct result class.
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 agree with all of this, especially updating examples (and docs). I think some kind of generic metadata object in the background is a good idea, adding to this in set_cosmology
also sounds great. Another thing we can think about is setting a default cosmology via environment variable, I think astropy does similar, we could even piggyback off theirs.
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.
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've also moved cosmology
to the global_meta_data
so it is consistent with #873
89e7779
to
96714cd
Compare
96714cd
to
62a6d06
Compare
@@ -264,6 +352,13 @@ def encode_for_hdf5(key, item): | |||
""" | |||
from ..prior.dict import PriorDict | |||
|
|||
try: | |||
from astropy import cosmology as cosmo, units |
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'm not wild about having this import here, I think it could be avoided, but since it is only a debug message I'm happy to let it slide for a while.
unrelated: We could consider changing this function to use multiple dispatch at some point down the road, that would avoid ugly import tests and also allow downstream users to add their own encoding/decoding.
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, I agree. At the time, I couldn't come up with a clean alternative.
On multiple dispatch: that's an interesting option, might be something to bring up on a dev call.
Following from the discussion on the last dev call, I checked some existing files. For GWTC-3 and GWTC-4 files it seems to work: >>> import bilby
>>> bilby.gw.result.CBCResult.from_json("/home/pe.o3/o3a_final/run_directories/S190706a/ProdF9/result/ProdF9_data0_1246487219-346191_analysis_H1L1V1_dynesty_merge_result.json")
<bilby.gw.result.CompactBinaryCoalescenceResult object at 0x7fb556a93ed0>
>>> bilby.gw.result.CBCResult.from_hdf5("/home/pe.o4/GWTC4/working/S230608as/bilby-IMRPhenomXPHM-SpinTaylor/final_result/bilby-IMRPhenomXPHM-SpinTaylor_data0_1370292665-161092_analysis_H1L1_merge_result.hdf5")
<bilby.gw.result.CompactBinaryCoalescenceResult object at 0x7fb5324a7f10> However, for a bilby GWTC-1 file, it's current breaking but works with 2.4.0. I'll see if I can figure out why and fix it. |
I've fixed this is my last commit. The issue was that the |
26efc7e
to
b676477
Compare
@mj-will overall this looks like a good. But, it does add a lot of overhead and tying into astropy. Is there any risk that we will end up unable to read/write results because of an astropy version change? E.g., if an astropy cosmology gets added/changed is this robust? |
@GregoryAshton I agree this does increase our dependence on astropy but, to me, this makes us more robust to astropy changes since we're using their own API to read/write objects rather than relying on introspection. The As for overhead, other than the initial import, I'd hope most of the operations are pretty lightweight. Is there a particular call/operation you know to be slow? |
Okay - in that case it sounds like you have thought through the issues. For the fallback, I guess one could just store the name. However, usually that is a bad idea as we then loose information silently, so I am happy to agree we don't need a fallback.
Nothing in particular, just curious. |
Thanks, @GregoryAshton, given that are you happy to approve this? |
b676477
to
1a274bb
Compare
Add
cosmology
to theCompactBinaryCoalescenceResult
.If we run analyses with a different cosmology, I think this should be recorded in the result file. My proposal is to add the
cosmology
in a new dictionary withinmeta_data
calledglobal_meta_data
. This is inline with the changes proposed in #873. I've implemented it following the same pattern used for other properties, such as the waveform arguments.Whilst making these changes, I realised that for this to work, the encoding/decoding on astropy objects wasn't working correctly for
hdf5
files. So a lot of changes are related to this.Changes
cosmology
to the meta data dictionary when calling run samplercosmology
to the meta data dictionary when making an instance ofCompactBinaryCoalescenceResult
encode/decode_astropy_cosmology/quantity
to make use of built-in astropy functions and to more closely match theJsonCustomEncode
implemented in astropy. Thedecode
functions should be backwards compatible.encode/decode_astropy_unit
decode_hdf5_dict
to ensure astropy types are decoded