Skip to content
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

Cosmo: validating and setting Parameters #12190

Merged
merged 11 commits into from Nov 3, 2021

Conversation

nstarman
Copy link
Member

@nstarman nstarman commented Sep 23, 2021

Parameter value validation and allowing parameters to be "fixed"/derived, e.g. setting Ode0 in FlatFLRWMixin.

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the Extra CI label.
  • Is a change log needed? If yes, did the change log check pass? If no, add the no-changelog-entry-needed label. If this is a manual backport, use the skip-changelog-checks label unless special changelog handling is necessary.
  • Is a milestone set? Milestone must be set but astropy-bot check might be missing; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate backport-X.Y.x label(s) before merge.

@nstarman nstarman added this to the v5.0 milestone Sep 23, 2021
@nstarman nstarman added this to In progress in Cosmology, the Expansion via automation Sep 23, 2021
@pep8speaks
Copy link

pep8speaks commented Sep 23, 2021

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-11-03 21:38:59 UTC

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this is looking, but looking at some of the after-setting tests that remain, I wonder whether it might make things easier if one had two options:

  1. A verifier, that just returns True if a value is OK, or False otherwise (with the default __set__ raising on False); could then do things like verifier=lambda x: x>=0. That said, in-line I'm suggesting fvalidate=float, so perhaps that has its uses too...
  2. A setter where the user simply is in full control (but with the once-only still enforced by __set__). The advantage is that this is expected to exist from things like (lazy)property.

Mostly bringing this up since it would be good to get this right.

astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/flrw.py Show resolved Hide resolved
astropy/cosmology/core.py Outdated Show resolved Hide resolved
astropy/cosmology/flrw.py Outdated Show resolved Hide resolved
astropy/cosmology/flrw.py Show resolved Hide resolved
astropy/cosmology/tests/test_core.py Outdated Show resolved Hide resolved
@mhvk
Copy link
Contributor

mhvk commented Sep 24, 2021

p.s. I'm wondering a bit whether it would make sense to in fact inherit from lazyproperty and add the validation (and override __set__, but still using super()). One nice bit with lazyproperty is that it was made thread-safe...

@nstarman
Copy link
Member Author

Rebased to fix merge conflicts.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments, but while going I realized I am perhaps reviewing at the wrong point, since you still refer to getter here while this is no longer allowed. My sense would be to similarly disallow setter and just use validate - at which point Parameter probably does not need to inherit from property after all...

astropy/cosmology/__init__.py Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
@nstarman nstarman force-pushed the cosmo_param_fixed branch 2 times, most recently from a9d9b30 to 84185e9 Compare October 29, 2021 21:57
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! A few small comments, mostly that I think one more piece can be removed.

p.s. My tendency would be to ask permission to still get this in 5.0, so the new Parameter does not have to be changed.

astropy/cosmology/io/tests/test_ecsv.py Show resolved Hide resolved
Function to get the value from instances of the cosmology class.
If None (default) returns the corresponding private attribute.
Often not set here, but as a decorator with ``getter``.
fvalidate : callable[[object, object, Any], Any] or {'default', 'float'}, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List of pre-defined validators is incomplete... My tendency would be to just write str here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM. I'm adding a property registered_validators(self): return self._registry_validators.keys() so it's easy to discover what's a valid str.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made it a classproperty

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


_registry_validators = {}

def __init__(self, fget=None, fvalidate="default", doc=None, *,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think fget is used at all anymore (and if it is, would not do anything), so remove!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a big oversight. Thanks for catching!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

astropy/cosmology/parameter.py Outdated Show resolved Hide resolved
@@ -262,6 +246,12 @@ def __init__(self, H0, Om0, Ode0, Tcmb0=0.0*u.K, Neff=3.04, m_nu=0.0*u.eV,
self._Tnu0 = 0.0 * u.K
self._Onu0 = 0.0

# now set m_nu Parameter (Note the value is calculated and not
# influenced by this value)
self.m_nu = m_nu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part still feels slightly odd. Somehow it seems cleaner to just do the calculation and set the private property. But no big deal!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just do self.m_nu = ... With the Ellipsis it feels less confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@nstarman nstarman force-pushed the cosmo_param_fixed branch 2 times, most recently from baa43c6 to bc1e7dc Compare November 3, 2021 01:12
@nstarman nstarman added the 💤 backport-v5.0.x on-merge: backport to v5.0.x label Nov 3, 2021
@nstarman nstarman modified the milestones: v5.1, v5.0 Nov 3, 2021
@astrofrog
Copy link
Member

I am ok with backporting this as it fixes an API being introduced in 5.0. However please make sure the CI is all green before merging, there have been too many CI breakages in the last week 😅. The CircleCI builds might need restarting.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but still some missing pieces in the documentation - can they be doctested?
Also, I'd not do the new classproperty - that is developer information so find to refer to a private attribute.

astropy/cosmology/flrw.py Show resolved Hide resolved
astropy/cosmology/flrw.py Show resolved Hide resolved
# Parameter details

@m_nu.validator
def m_nu(self, param, _):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above. The validator for Ob0, in contrast, feels just right.


return register

@classproperty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd not add this - it is useful for developers only and those can be pointed to the private attribute.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@lazyproperty # so only does expensive computation once
def m_nu(self):
""" Mass of neutrino species"""
@m_nu.setter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct any more, I think!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

docs/cosmology/dev.rst Show resolved Hide resolved
docs/cosmology/dev.rst Outdated Show resolved Hide resolved
docs/cosmology/dev.rst Outdated Show resolved Hide resolved
Co-authored-by: Marten van Kerkwijk <mhvk@astro.utoronto.ca>
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks all great! Just one remaining suggestion for the docs (plus a very small typo). I'll approve in the meantime.

...
return u.Quantity(numass, self.__class__.m_nu.unit)
return value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to copy the whole validator, as right now it misses the point that you can access the cosmology instance easily.

auto-locking), so ``self.H0 = H0`` will use this setter and put the value on
"._H0". The advantage of this method over direct assignment to the private
attribute is the use of validators. |Parameter| allows for custom value
validators, using the method-decorator ``validator``, that can check a values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a values -> a value's

@@ -237,7 +237,6 @@ class to use. Details about metadata treatment are in
``Cosmology.from_format.help("mapping")``.

.. code-block:: python
:emphasize-lines: 12,13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit unrelated but fine to get it in too!

Cosmology, the Expansion automation moved this from In progress (PR) to Reviewer approved Nov 3, 2021
@nstarman nstarman added the 💤 merge-when-ci-passes Do not use: We have auto-merge option now. label Nov 3, 2021
@nstarman
Copy link
Member Author

nstarman commented Nov 3, 2021

Thanks @mhvk for the multiple rounds of review! This refactoring of Parameter is a significant improvement over the original implementation. And thanks @astrofrog for getting this in v5.0!

use the simple unit munger.
Maybe in future compact all the long neutrino calculations into a breakout m_nu validator

Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
@nstarman nstarman merged commit da47582 into astropy:main Nov 3, 2021
Cosmology, the Expansion automation moved this from Reviewer approved to Done Nov 3, 2021
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Nov 3, 2021
@nstarman nstarman deleted the cosmo_param_fixed branch November 3, 2021 22:38
astrofrog added a commit that referenced this pull request Nov 4, 2021
…190-on-v5.0.x

Backport PR #12190 on branch v5.0.x (Cosmo: validating and setting Parameters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cosmology 💤 backport-v5.0.x on-merge: backport to v5.0.x 💤 merge-when-ci-passes Do not use: We have auto-merge option now.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants