Repository navigation
Cosmo Unified I/O - #11559
Cosmo Unified I/O#11559nstarman wants to merge 20 commits into
Conversation
|
Hello @nstarman 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style. There are no PEP8 style issues with this pull request - thanks! 🎉 Comment last updated at 2021-07-01 20:27:31 UTC |
|
👋 Thank you for your draft pull request! Do you know that you can use |
08e28fe to
dc9f5d4
Compare
|
@nstarman - if you could point me to where the |
|
Thanks!! In this branch the following will result in a Actually, the following formats all have this problem: |
|
OK, so at least And |
|
@nstarman - I see this PR is using the generalized From my perspective if you are looking to invent a new way of serializing the cosmology, then less is more and you should pick exactly one format (noting that most of the Table formats don't support serializing units or metadata). Disclaimer: I've never actually used the |
My original intent was to only implement ECSV and JSON. When I switched from a home-brewed solution to using the generalized The cosmology package has been careful to allow for float and Quantity input for all arguments. If a user is not interested in roundtrip-ability (on the metadata) nor automatic unit handling, then basically any Table format is technically valid input, even if not advised. I agree that less is more, but by subclassing `TableRead The ECSV is most definitely the preferred format as it allows for saving citations. Edit: Perhaps a good solution would be to have I think this is a good solution to allow for flexible I/O and also keep the recommended formats prominent. |
|
@nstarman - some thoughts... First, the 🤞 plan for supporting JSON won't easily work in general since I see that Is there really a strong use-case for tables of cosmologies such that it needs to be supported as a code one-liner instead of potentially writing a few lines of code? In particular I think current behavior for I'm not sure that using this unified I/O read infrastructure with formats like latex and HTML will be useful in the wild. Basically it seems like the column names are likely to differ enough (esp. in the latex case) to end up requiring some by-hand munging. Then you just have e.g. I recommend against including functionality that is "not advised" or partially working, since somebody will start doing that and then come to report problems. Ideally everything that goes into the core should be completely robust and bulletproof. About human readability, I think the ECSV is sufficiently readable: With astropy installed a quick I've really come around to a philosophy of favoring minimal solutions that are simple. Then wait to see if users to submit issues like "I really need a fixed-format output for cosmology parameters".
The recent case of QDP is a counter-example, where a new ASCII format got added that doesn't work with Cosmology. But more to the the point, I don't think of At the end my vote would be to support exactly two formats, each of which can serialize a single cosmology:
|
|
I've not thought through much of this, but @taldcroft's comment that cosmology is not a table did make me think that perhaps one should instead think of cosmology as an object that can be serialized and thus can be meta data on anything else. Conceptually, that seems to make more sense; e.g., it could be meta data on a table of supernova observations and inferred luminosity distances, etc. I'm not quite sure how to extend that to also serialize just the cosmology. I huess it would need an Anyway, overall very much 👍 to not reinventing the wheel on a yet another serialization method that is |
|
@mhvk - yes, putting As far as a universally-accessible serialization format for a cosmology... First is there any prior art? Beyond that, honestly a simple JSON dict seems like the best choice, mostly driven by array-valued parameters. Simple metadata like references can be easily stored. With ECSV there is the problem of |
Yeah in further testing this is proving troublesome. Before I switched to the TableRead/Write I had a simple to/from_json using the built-in
I think this is the fine balance between over-engineering and not wanting to back myself into a corner later on. In prior research I've had to run cosmological MCMC chains. Many programs then allow one to move along the chain, performing calculations. While these chains are usually specific to the program running them because the parameters aren't labelled, if re-represented as a Table my thought is
Good points both. I think that this really applies to the
It's very readable for single cosmologies. Less so for many rows with different numbers of decimal places.
👍
I'm convinced. My proposal is this:
For later, when
I really like this! I'm definitely interested in implementing this. Correct me if I'm misinterpreting, but this would be in addition to the above serialization implementation (esp for a "vectorized" Cosmology)? I think the
I have not encountered a universally-accessible serialization format. I think they are all bespoke. CLASS uses a |
876ee8c to
c38c9d9
Compare
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
It was used multiple times, but now it only appears in 1 place. Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Co-authored-by: @taldcroct Co-authored-by: @mhvk Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Co-authored-by: Tom Aldcroft <[email protected]> Co-authored-by: Marten van Kerkwijk <[email protected]> Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Co-authored-by: Tom Aldcroft <[email protected]> Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
90f1fea to
8044836
Compare
temporarily turn off tests Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
|
@mhvk @taldcroft. Thanks for the detailed reviews and the tough questions. I appreciate all the insight into the I’m going to try to tackle all points (#11559 (review) and similar) in one long-form response Goals
Solutions
Everything will use the Unified I/O methods, including transforming to/from objects like dictionaries and Tables. The descriptors on For example (the property names class Cosmology:
# Unified I/O object transformation methods from ``astropy.cosmology._io``
# The 'from_' method is NotImplemented in subclasses.
from_ = UnifiedReadWriteMethod(CosmologyFrom) # can’t use ‘from =’
to_ = UnifiedReadWriteMethod(CosmologyTo)
# Unified I/O read and write methods from ``astropy.cosmology._io``
# The 'read' method is NotImplemented in subclasses.
read = UnifiedReadWriteMethod(CosmologyRead)
write = UnifiedReadWriteMethod(CosmologyWrite)Now converting to/from a mapping/table is as easy as from astropy.cosmology import Cosmology, Planck18
mapping = Planck18.to_("mapping")
cosmo = Cosmology.from_(mapping)
assert cosmo == Planck18 # True
table = Planck18.to_("table")
cosmo = Cosmology.from_(table)
assert cosmo == Planck18 # True
from astropy.table import vstack
newtable = vstack([table, table])
cosmo = Cosmology.from_(newtable, index=1)
assert cosmo == Planck18 # TrueAnd new formats can be easily added, without adding anything to the Cosmology object itself! Also it’s quite literally a registry so 3rd party packages can contribute formats — e.g. This PR now contains a reference implementation of the above. Discussion
Cosmology file format requirements are different than that of Tables. For one, other popular packages employ formats like
Agreed, this PR has become too large. I’m going to split it up into pieces, if we agree that the above is the right approach. This PR branch is called
I would like to support this in addition to base JSON. For a followup.
Done on the first part and ECSV will be the preferred format here. JSON is need for interoperability with non-astropy-supporting packages
I will be adding an
Agreed! I want to eventually make stuff like
Thanks! But I think third time's the charm :).
I wanted them for 2 reasons:
I agree that having 2 ways of doing things was an implementation mistake. Now it’s all in the class method. |
|
@nstarman - thanks, that really helps! You definitely completely sold me on the For JSON, I can see the case too (and it fits with read/write anyway), but mostly if there is already some use outside astropy - if there isn't, I think there should first be some discussion with other packages, including a careful look at whether there is an existing format that is "good enough" (obligatory link: https://xkcd.com/927/). The use case for the methods is fairly clear but the implementation I'm much less sure about; name pollution depends on the number of Anyway, for what it is worth, my suggested ordering would be to start with a slightly narrowed down PR from this one with just read/write. Next may depend on whether there is an existing human-readable standard that could encapsulate all our cosmological parameters. If so, incorporate it and use it to store ours. If not, use ECSV. p.s. I'll add that I really appreciate your willingness to discuss and re-do - @taldcroft and I often similarly take our time to converge to a solution (like |
Same! And the former.
Could you elaborate about the drawbacks? Re name pollution, I expect to have |
|
It is partially names - Ideally, of course, you have some form of interchange format - perhaps something like |
Agreed. The
My hope is to actually do none/few/only the 'from_' of these in Astropy itself, but work with package developers to register in a reader/writer/identifier. The
I like that as an initializer. I think the constructor might look something like this (pseudo) code.
Unfortunately, though many support |
|
Ah, yes, now I finally see the connection with readers... A registration mechanism would definitely seem the way to go! |
|
This PR will be implemented in a number of smaller PRs. Thanks @mhvk and @taldcroft for the great discussion and feedback that will be motivating these PRs. |
Description
Use the
UnifiedReadWritearchitecture with theio_registryto enable reading and writing cosmology instances from ECSV and JSON.Part of #10673 -> #11777
Blocking #11517, #11570
Blocked by
#11530,#11540,#11542!,#11903,#11906