aperiosoftware / astropy

Repository for the Astropy core package
www.astropy.org
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Refactor CompImageHDU to inherit from ImageHDU #60

Closed astrofrog closed 3 months ago

astrofrog commented 1 year ago

This pull request addresses https://github.com/astropy/astropy/issues/9238 by refactoring CompImageHDU to inherit from ImageHDU instead of BinTableHDU. This includes some subtle API changes that cannot be warned about in advance using deprecation warnings or future warnings, so this PR, if accepted, would definitely warrant a major version bump. Ideally this would go in v6.0 but it depends on whether we can achieve consensus and stamp out remaining issues.

This PR isn't 100% ready (see 'Outstanding issues' below and FIXME comments in code) but I wanted to try and open this now in case it is something we can consider getting in before the v6.0 feature freeze. I doubt that the outstanding issues would cause significant code changes so it should be safe to already review things as-is.

Background

Compressed image HDUs are stored in FITS files as binary tables. The CompImageHDU was therefore developed as a a subclass of BinTableHDU. However, that implementation is quite complex because it tries to look to users like an ImageHDU - for example .header is meant to be the image header, and .data is meant to be the decompressed image data. To quote comments from the code:

# TODO: The difficulty of implementing this screams a need to rewrite this module
Bypasses `BinTableHDU._writeheader()` which updates the header with
metadata about the data that is meaningless here; another reason
why this class maybe shouldn't inherit directly from BinTableHDU...

Issue https://github.com/astropy/astropy/issues/9238 by @mhvk also suggested that CompImageHDU would be simpler if it inherited from ImageHDU.

This PR does that refactoring, and represents CompImageHDU as an ImageHDU subclass that internally stores a BinTableHDU representing the compressed data. Instead of always keeping the image and table header in sync, headers are only converted at I/O time which should in principle make things faster.

Overall, this results in a few hundred lines of code being removed. For now CompImageHeader is kept to avoid breaking API so that warnings about reserved keywords are emitted as soon as they are set. However, if we would be happy to 'break' this behavior and only warn at I/O time or when calling verify, we could get rid of the custom header subclass. If we want to keep the behavior, we can nevertheless try and avoid code duplication with the Header class by ssimplifying the header validation (see https://github.com/astropy/astropy/pull/15293).

API changes/fixes

I have tried to minimize API changes as much as possible. However, it is impossible to not have any changes at all:

Modified tests

I've tried to change tests as little as possible but there were some unavoidable changes:

Outstanding issues/open questions

github-actions[bot] commented 1 year ago

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.

github-actions[bot] commented 1 year ago

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

astrofrog commented 3 months ago

Closing as the astropy PR is now open.