Skip to content

Fix Kernel normalization - #13299

Merged
pllim merged 8 commits into
astropy:mainfrom
larrybradley:kernel-normalization
Jun 3, 2022
Merged

pllim merged 8 commits into
astropy:mainfrom
larrybradley:kernel-normalization

Conversation

@larrybradley

@larrybradley larrybradley commented Jun 2, 2022 •

Copy link
Copy Markdown
Member

Description

The documentation for kernels states that the "generated kernel is normalized so that it integrates to 1", but this is not true for many of the kernels. For example:

>>> from astropy.convolution import Gaussian2DKernel
>>> kernel = Gaussian2DKernel(3, x_size=5, y_size=5)
>>> kernel.array.sum()
0.3570479753738957

This PR ensures that the returned kernels are normalized.

This PR also fixes the Kernel truncation attribute. Currently, its value is set in the kernel initializer and thus is not updated after subsequent modifications to the kernel array (e.g., kernel.normalize(mode='peak') changes the normalization, but the truncation is not updated). This PR returns the correct truncation value for the kernel array.

This PR also includes some docstring improvements and some style fixes.

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

@pllim

pllim commented Jun 2, 2022

Copy link
Copy Markdown
Member

Seems like a pretty serious bug. You sure this don't need to go all the way to LTS (5.0.x)?

@larrybradley

Copy link
Copy Markdown
Member Author

@pllim I re-milestoned to v5.0.5

@keflavich keflavich 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 definitely strikes me as an important fix. I suspect I never encountered it b/c I always make kernels with sizes much larger than the shape being encompassed; a truncated Gaussian is not a good approximation of a Gaussian.

I'm surprised there is only one test hit by this, though.

@pllim

pllim commented Jun 2, 2022

Copy link
Copy Markdown
Member

there is only one test hit by this

Does this mean we need to add more tests to this PR? 😸

@pllim

pllim commented Jun 2, 2022

Copy link
Copy Markdown
Member

@larrybradley , for future reference, we need one backport label per backport branch, so I readded the label you removed. FYI.

@larrybradley
larrybradley force-pushed the kernel-normalization branch from 0819ccd to 4a01648 Compare June 2, 2022 15:37
@pllim
pllim merged commit 69a4823 into astropy:main Jun 3, 2022
@lumberbot-app

lumberbot-app Bot commented Jun 3, 2022

Copy link
Copy Markdown

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v5.0.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 69a4823386c189a98b05802c813232c81b7c48ad
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #13299: Fix Kernel normalization'
  1. Push to a named branch:
git push YOURFORK v5.0.x:auto-backport-of-pr-13299-on-v5.0.x
  1. Create a PR against branch v5.0.x, I would have named this PR:

"Backport PR #13299 on branch v5.0.x (Fix Kernel normalization)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@lumberbot-app

lumberbot-app Bot commented Jun 3, 2022

Copy link
Copy Markdown

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout v5.1.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 69a4823386c189a98b05802c813232c81b7c48ad
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #13299: Fix Kernel normalization'
  1. Push to a named branch:
git push YOURFORK v5.1.x:auto-backport-of-pr-13299-on-v5.1.x
  1. Create a PR against branch v5.1.x, I would have named this PR:

"Backport PR #13299 on branch v5.1.x (Fix Kernel normalization)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@pllim

pllim commented Jun 3, 2022

Copy link
Copy Markdown
Member

Ops, @larrybradley , maybe v5.0.x backport is impossible after all. If you do not want to manually backport this to v5.0.x, please change the milestone back. I apologize for any inconvenience caused.

@pllim

pllim commented Jun 3, 2022

Copy link
Copy Markdown
Member

Oh wait... v5.1.x auto backport failed too. How?

@pllim

pllim commented Jun 11, 2022

Copy link
Copy Markdown
Member

Auto backport failed, so I have moved the milestone to v5.2 for now. If you ever get around to manual backports, please update the milestone. Thanks!

@keflavich

Copy link
Copy Markdown
Contributor

Revisiting this for a minute: the new way guarantees that kernel.array.sum() == 1 (to numerical precision), while the old way properly evaluated the normalized Gaussian function. We may need to clarify this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants