Repository navigation
Cosmo as a dataclass - #15484
Cosmo as a dataclass#15484
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
|
👋 Thank you for your draft pull request! Do you know that you can use |
6be68d6 to
ee557a6
Compare
5e03f6a to
6728751
Compare
Except for the time required to upgrade the code I submit the opposite, why would we not make a class a dataclass? |
A concern I heard about data classes is startup time overhead. |
|
@eerovaher @WilliamJamieson @neutrinoceros, now that #15603 is in, this PR is ready as well! It will be great to finally upgrade the architecture of |
neutrinoceros
left a comment
There was a problem hiding this comment.
I'll set aside some time to finish this review tomorrow, in the mean time here are a couple (very) small questions.
| Any | ||
| Results of evaluating the Cosmology method. | ||
| """ | ||
| # TODO: speed up using ``replace`` |
There was a problem hiding this comment.
Should this be done in this PR ? Otherwise, why not ?
There was a problem hiding this comment.
It's just out of scope. This function has to comply with requirements for making Astropy Models.
neutrinoceros
left a comment
There was a problem hiding this comment.
This time I was able to do a complete read through. This looks outstanding !
A broad (but very minor) comment is that I'm not sure about the name dataclass_decorator. How about maybe something more descriptive of its application domain like cosmology_dataclass ?
| # Derived quantities: | ||
| # Dark matter density; matter - baryons, if latter is not None. | ||
| self._Odm0 = None if Ob0 is None else (self._Om0 - self._Ob0) | ||
| object.__setattr__( |
There was a problem hiding this comment.
I want to collect all these object.__setattr__ ancillary variables into a more efficient storage object. But I think that should be done in a future PR.
neutrinoceros
left a comment
There was a problem hiding this comment.
With your latest batch of replies, I'm happy to sign this off !
|
@eerovaher @WilliamJamieson, the feature freeze is coming up. What do y'all think of this PR? |
|
I agree we should try to get this merged before the feature freeze. I'll make time to have a look at it tomorrow. Luckily @neutrinoceros has already reviewed it, so I don't expect that any big changes are going to be needed. |
eerovaher
left a comment
There was a problem hiding this comment.
I do have a few comments, but it should be possible to address them before the feature freeze.
Do you plan editing the commit history yourself? Currently there are two different commits that both Make Cosmology a dataclass, so a normal merge doesn't seem to be the right thing to do, but you don't want this to be automatically squashed either.
| all_vars["name"].__set__(self, name) | ||
| all_vars["meta"].__set__(self, OrderedDict(meta or {})) | ||
|
|
||
| def __post_init__(self): # noqa: B027 |
There was a problem hiding this comment.
The current docstring is very strongly suggesting that this should be an abstract method (which would also get rid of the noqa), so the docstring should be rewritten.
| ) | ||
| from astropy.cosmology.parameter._core import MISSING | ||
|
|
||
| ############################################################################## |
| now a :func:`~dataclasses.dataclass`. This means that the :mod:`dataclasses` machinery | ||
| can be used to work with :class:`~astropy.cosmology.Cosmology` objects. For example:: | ||
|
|
||
| .. doctest-skip:: |
There was a problem hiding this comment.
Why are the doctests being skipped?
Yes, this is a product of an interactive rebase. I'll fix it up. |
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]> Signed-off-by: nstarman <[email protected]>
py3.13+ will have the `converter` arg, eliminating this class. Co-authored-by: Clément Robert <[email protected]> Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]> Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]> Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
Signed-off-by: nstarman <[email protected]>
|
🎉 thanks all for the reviews!! |
This PR changes
Cosmologyand its subclasses todataclasses.dataclassobjects for versions py3.10+.Why change?
Well, this has long been the intended plan.
Cosmologyis structured very similarly to the dataclass paradigm (seecosmology.Parameter) but small differences meant that they were not compatible.Dataclasses bring a number of advantages over normal classes, particularly for classes that are defined by their attributes (so not
dict-like things).fieldsandasdict, etc.