Skip to content

Cosmo: validating and setting Parameters - #12190

Merged
nstarman merged 11 commits into
astropy:mainfrom
nstarman:cosmo_param_fixed
Nov 3, 2021
Merged

nstarman merged 11 commits into
astropy:mainfrom
nstarman:cosmo_param_fixed

Conversation

@nstarman

@nstarman nstarman commented Sep 23, 2021 •

Copy link
Copy Markdown
Member

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
@pep8speaks

pep8speaks commented Sep 23, 2021 •

Copy link
Copy Markdown

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

@nstarman
nstarman force-pushed the cosmo_param_fixed branch 2 times, most recently from 134c62c to 9052da9 Compare September 23, 2021 20:40
@nstarman
nstarman requested a review from mhvk September 23, 2021 21:00

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/flrw.py Resolved
Comment thread astropy/cosmology/core.py Resolved
Comment thread astropy/cosmology/flrw.py Resolved
Comment thread astropy/cosmology/flrw.py Resolved
Comment thread astropy/cosmology/tests/test_core.py Resolved
@mhvk

mhvk commented Sep 24, 2021

Copy link
Copy Markdown
Contributor

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
nstarman force-pushed the cosmo_param_fixed branch 10 times, most recently from 6554129 to 3005a30 Compare October 14, 2021 02:55
@nstarman
nstarman requested review from adrn and mhvk October 19, 2021 19:30
@nstarman

Copy link
Copy Markdown
Member Author

Rebased to fix merge conflicts.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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...

Comment thread astropy/cosmology/__init__.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/parameter.py Resolved
@nstarman
nstarman force-pushed the cosmo_param_fixed branch 2 times, most recently from a9d9b30 to 84185e9 Compare October 29, 2021 21:57
@nstarman
nstarman requested a review from mhvk October 29, 2021 22:01
so can have pre-registered options

Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread astropy/cosmology/io/tests/test_ecsv.py Resolved
Comment thread astropy/cosmology/parameter.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread astropy/cosmology/parameter.py Outdated

_registry_validators = {}

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread astropy/cosmology/parameter.py Resolved
Comment thread astropy/cosmology/flrw.py Outdated

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 requested a review from astrofrog November 3, 2021 01:19
@nstarman nstarman modified the milestones: v5.1, v5.0 Nov 3, 2021
@astrofrog

Copy link
Copy Markdown
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.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread astropy/cosmology/flrw.py Resolved
Comment thread astropy/cosmology/flrw.py Resolved
Comment thread astropy/cosmology/flrw.py Outdated
# Parameter details

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

Copy link
Copy Markdown
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.

Comment thread astropy/cosmology/parameter.py Outdated

return register

@classproperty

Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed.

Comment thread docs/cosmology/dev.rst Outdated
@lazyproperty # so only does expensive computation once
def m_nu(self):
""" Mass of neutrino species"""
@m_nu.setter

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Comment thread docs/cosmology/dev.rst Resolved
Comment thread docs/cosmology/dev.rst Resolved
Comment thread docs/cosmology/dev.rst Resolved
Co-authored-by: Marten van Kerkwijk <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
@nstarman
nstarman requested a review from mhvk November 3, 2021 18:30

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread docs/cosmology/dev.rst
return value
...
return u.Quantity(numass, self.__class__.m_nu.unit)
return value

Copy link
Copy Markdown
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.

Comment thread docs/cosmology/dev.rst Outdated
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
Copy Markdown
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

Comment thread docs/cosmology/io.rst
``Cosmology.from_format.help("mapping")``.

.. code-block:: python
:emphasize-lines: 12,13

Copy link
Copy Markdown
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!

@nstarman nstarman added the zzz 💤 merge-when-ci-passes Do not use: We have auto-merge option now. label Nov 3, 2021
@nstarman

nstarman commented Nov 3, 2021

Copy link
Copy Markdown
Member Author

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) <[email protected]>
@nstarman
nstarman merged commit da47582 into astropy:main 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 zzz 💤 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.

4 participants