Skip to content

Update astropy.io.fits.CompImageHDU header when setting data - #12058

Closed
ConorMacBride wants to merge 2 commits into
astropy:mainfrom
ConorMacBride:compimagehdu-update-header
Closed

ConorMacBride wants to merge 2 commits into
astropy:mainfrom
ConorMacBride:compimagehdu-update-header

Conversation

@ConorMacBride

@ConorMacBride ConorMacBride commented Aug 16, 2021 •

Copy link
Copy Markdown
Member

Description

This pull request is to address the following unexpected behaviours:

  1. If a HDU is created with e.g. hdu = astropy.io.fits.CompImageHDU(), when new data is set (e.g. hdu.data = np.random.rand(10, 20, 30)) hdu.header does not match the shape of the new data. However, with astropy.io.fits.ImageHDU it would.

  2. If the header is updated with the astropy.io.fits.CompImageHDU._update_header_data method, hdu.header is still not always updated. This is because astropy.io.fits.CompImageHDU.header is a @lazyproperty that caches its first return value.

The first commit in this PR implements an astropy.io.fits.CompImageHDU.update_header method (for symmetry with astropy.io.fits.ImageHDU.update_header). This method calls the _update_header_data method and clears the cached @lazyproperty. This resolves the second unexpected behaviour above.

The second commit in this PR calls the new astropy.io.fits.CompImageHDU.update_header method in the astropy.io.fits.CompImageHDU.data setter method (just like in astropy.io.fits.ImageHDU). This resolves the first unexpected behaviour above.

  • While these updates fix the issues I was having, eight of the astropy/io/fits/tests/test_checksum.py and astropy/io/fits/tests/test_image.py tests are failing when the header is updated while setting data. I'm pretty sure some of these are failing because tests are not expecting manually removed headers such as ZNAXIS to be re-added when the data attribute is updated. Before I investigate this further, does what I'm trying to do here make sense?*

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

@github-actions github-actions Bot 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.

Welcome to Astropy 👋 and congratulations on your first pull request! 🎉

A project member will respond to you as soon as possible; in the meantime, please have a look over the Checklist for Contributed Code and make sure you've addressed as many of the questions there as possible.

If you feel that this pull request has not been responded to in a timely manner, please leave a comment mentioning our software support engineer @embray, or send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail [email protected].

raise TypeError('CompImageHDU data has incorrect type:{}; '
'dtype.fields = {}'.format(
type(data), data.dtype.fields))
if hasattr(self, '_data_first_run') and not self._data_first_run:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems weird to only set a new attribute here...

@pllim
pllim requested review from embray and saimn August 16, 2021 13:16
@pllim pllim added this to the v5.0 milestone Aug 16, 2021
@pllim

pllim commented Aug 16, 2021

Copy link
Copy Markdown
Member

Thanks! If subpackage maintainers think this should move forward, this PR will also need test(s) and change log. And maybe also user-facing doc on how/when to use it.

@pllim

pllim commented Aug 16, 2021

Copy link
Copy Markdown
Member

p.s. If this PR fixes a particular existing GitHub issue, please fill in Fixes # above. Thanks!

@embray

embray commented Aug 16, 2021

Copy link
Copy Markdown
Member

Thanks @ConorMacBride for the patch. I think before we move forward on this I want to take a closer look at exactly what the issue is. There are a few known issues with CompImageHDU some of which I was working on fixing a couple weeks ago (but have been taken away from for other things). So I want to be very careful about additional patches we make to this.

IIRC lazyproperty also supports some mechanisms for this don't necessitate this workaround. For example, it might be sufficient, for the use case described in this issue, to add a @data.setter method.

@astrofrog

Copy link
Copy Markdown
Member

Should this be treated as a bug fix? (just asking in terms of updating the milestone now that we've branched v5.0.x)

@saimn saimn modified the milestones: v5.0, v5.0.1 Nov 2, 2021
@saimn

saimn commented Nov 2, 2021

Copy link
Copy Markdown
Contributor

Yes, this is a bug fix so moving the milestone to 5.0.1

@github-actions github-actions Bot added the Close? Tell stale bot that this issue/PR is stale label Jan 14, 2022
@github-actions

Copy link
Copy Markdown
Contributor

Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

@astrofrog astrofrog modified the milestones: v5.0.1, v5.0.2 Jan 27, 2022
@github-actions github-actions Bot added the closed-by-bot Closed by stale bot label Feb 14, 2022
@github-actions

Copy link
Copy Markdown
Contributor

I'm going to close this pull request as per my previous message. If you think what is being added/fixed here is still important, please remember to open an issue to keep track of it. Thanks!

If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here.

@github-actions github-actions Bot closed this Feb 14, 2022
@saimn saimn reopened this Feb 15, 2022
@saimn saimn added keep-open and removed Close? Tell stale bot that this issue/PR is stale closed-by-bot Closed by stale bot labels Feb 15, 2022
@pllim pllim modified the milestones: v5.0.7, v5.0.8 Aug 15, 2023
@pllim pllim modified the milestones: v5.0.8, v5.0.9 Sep 7, 2023
@saimn saimn modified the milestones: v5.0.9, v6.0.1 Nov 6, 2023
@saimn saimn modified the milestones: v6.0.1, v6.0.2 Mar 25, 2024
@astrofrog astrofrog modified the milestones: v6.0.2, v6.1.1 Apr 4, 2024
@saimn saimn modified the milestones: v6.1.1, v6.1.2 Jun 17, 2024
@astrofrog astrofrog modified the milestones: v6.1.2, v6.1.3 Jul 25, 2024
@pllim pllim modified the milestones: v6.1.3, v6.1.4 Aug 30, 2024
@pllim pllim modified the milestones: v6.1.4, v6.1.5 Sep 27, 2024
@saimn saimn removed the keep-open label Sep 30, 2024
@saimn

saimn commented Sep 30, 2024

Copy link
Copy Markdown
Contributor

@ConorMacBride - #15474 is now merged and I think things now work as expected. Please reopen if I missed something.

@saimn saimn closed this Sep 30, 2024
@saimn

saimn commented Sep 30, 2024

Copy link
Copy Markdown
Contributor

@ConorMacBride - #15474 is now merged and I think things now work as expected. Please reopen if I missed something.

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.

6 participants