Skip to content

Provide a way to reinitialize the parameter values of a model#16812

Merged
nden merged 4 commits intoastropy:mainfrom
astrofrog:modeling-reinitialize-parameters
Aug 13, 2024
Merged

Provide a way to reinitialize the parameter values of a model#16812
nden merged 4 commits intoastropy:mainfrom
astrofrog:modeling-reinitialize-parameters

Conversation

@astrofrog
Copy link
Member

@astrofrog astrofrog commented Aug 9, 2024

This provides a (simple) public method to reinitialize parameters in a model either to default values, or to specific values in the same way as through the model initializer. For example:

In [1]: from astropy.modeling.models import Gaussian1D

In [3]: g = Gaussian1D(2, 3, 4)

In [4]: g.reset_parameters()

In [5]: g
Out[5]: <Gaussian1D(amplitude=1., mean=0., stddev=1.)>

In [6]: g.reset_parameters(5, 6, 7)

In [7]: g
Out[7]: <Gaussian1D(amplitude=5., mean=6., stddev=7.)>

In [8]: g.reset_parameters(mean=3)  # all other parameters reset to default

In [9]: g
Out[9]: <Gaussian1D(amplitude=1., mean=3., stddev=1.)>

In [10]: g.reset_parameters(amplitude=[1, 2, 3])  # setting to array

In [11]: g
Out[11]: <Gaussian1D(amplitude=[1., 2., 3.], mean=0., stddev=1.)>

In [14]: g.reset_parameters(amplitude=3 * u.Jy)  # adding units

In [15]: g
Out[15]: <Gaussian1D(amplitude=3. Jy, mean=0., stddev=1.)>

In [16]: g.reset_parameters(amplitude=3 * u.m)  # changing to different units

In [17]: g
Out[17]: <Gaussian1D(amplitude=3. m, mean=0., stddev=1.)>

This is an alternative to #16806 and #16710

Closes #16806
Closes #16710
Closes #16593

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@astrofrog astrofrog requested a review from a team as a code owner August 9, 2024 12:23
@astrofrog astrofrog added this to the v7.0.0 milestone Aug 9, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2024

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.

  • 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 instructions for rebase and squash.
  • 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. Codestyle issues can be fixed by the bot.
  • 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 this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • 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.

@astrofrog astrofrog added the skip-changelog-checks Tells bot to skip changlog checks label Aug 13, 2024
@astrofrog
Copy link
Member Author

astrofrog commented Aug 13, 2024

@nden - as you requested on Slack, I have now made the method private, and as a result I have removed the changelog entry. However, I have kept the tests as I think they are good to have and we may one day make this method public.

Copy link
Contributor

@nden nden left a comment

Choose a reason for hiding this comment

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

LGTM, discussed offline.

@nden
Copy link
Contributor

nden commented Aug 13, 2024

The precommit check is required but in this case I think it reformats a file. Can this be ignored? @pllim

@astrofrog
Copy link
Member Author

pre-commit.ci autofix

@astrofrog
Copy link
Member Author

@nden should be fixed now!

@nden nden merged commit e65f975 into astropy:main Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement modeling skip-changelog-checks Tells bot to skip changlog checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a way to make a copy of a model with different parameter values, or be more flexible with parameter shape

2 participants