question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HDUList.header returns incorrect values for .keys() and .items()

See original GitHub issue

Description

HDUList.header returns incorrect values for .keys() and .items()

In the example below, hdulist[0].header, shows a key/value pair of

OBJECT = ‘TILEID: 1106’

hdulist[0].header['OBJECT'] returns the value from the key/value pair.

However, both hdulist[0].header.keys() and hdulist[0].header.items() indicate a key of ‘OBJECT.TILEID’. Using the returned key from .keys() or .items() returns a bogus value:

hdulist[0].header['OBJECT.TILEID'] => 1106.0

Expected behavior

Expect ‘OBJECT’ but not ‘OBJECT.TILEID’ to be listed in header.keys() and header.items().

Actual behavior

Got bad values for a key in hdulist[0].header. Seems that the value of a key/value became part of the key. See “Reproduce”

Steps to Reproduce

>>> import astropy.io.fits
>>> hdulist = astropy.io.fits.open("https://astroarchive.noirlab.edu/api/retrieve/00b228b9384ab0e358fd12b3aa1586d0/")
>>> 'OBJECT' in hdulist[0].header        # => True
>>> 'OBJECT' in hdulist[0].header.keys() # => False  # but used as key next
>>> hdulist[0].header['OBJECT']          # => 'TILEID: 1106'
>>> hdulist[0].header['OBJECT.TILEID']   # => 1106.0   # BOGUS key and value
>>> hdulist[0].header                    # Shows 'OBJECT' but not 'OBJECT.TILEID'

System Details

Linux-5.4.0-113-generic-x86_64-with-glibc2.31 Python 3.9.7 (default, Sep 6 2021, 18:25:07) [GCC 9.3.0] Numpy 1.22.4 pyerfa 2.0.0.1 astropy 5.1 Scipy 1.8.1 Matplotlib 3.5.2

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
pothierscommented, May 27, 2022

@dhomeier Your direct pip install from hash worked. Yes, your branch resolves my problem. Thanks! I’ll leave my monkey-patch fix in my code until your change works its way into a production release.

1reaction
pothierscommented, May 27, 2022

Treat this as a proposed simple fix for this bug.

To get around the items(), keys() bug I have monkey-patched my code with the following. If this change (or equivalent) is applied to Astropy, I’ll remove my monkey-patch and all should continue to work the same.

def NEW_items(self):
    """Like :meth:`dict.items`."""

    for k in self._rvkc_indices:
        v = self.get('OBJECT')
        yield k, None if v == UNDEFINED else v

    for card in self._cards:
        yield card.keyword, None if card.value == UNDEFINED else card.value


def NEW_keys(self):
    """
    Like :meth:`dict.keys`--iterating directly over the `Header`
    instance has the same behavior.
    """

    for k in self._rvkc_indices:
        yield k

    for card in self._cards:
        yield card.keyword

astropy.io.fits.header.Header.items = NEW_items
astropy.io.fits.header.Header.keys = NEW_keys
Read more comments on GitHub >

github_iconTop Results From Across the Web

HDU Lists — Astropy v5.2
When a FITS file is opened, a HDUList object is returned. ... An integer value of index indicates the position from which pop()...
Read more >
python - What can I do for my program not to throw a KeyError ...
What can I do for my program not to throw a KeyError for a Header not existing in a .fits file? · 1....
Read more >
FITS File handling (astropy.io.fits)
The open function returns an object called an HDUList which is a list -like collection of HDU objects. An HDU (Header Data Unit)...
Read more >
FITS Headers — Astropy v1.0.4
PrimaryHDU() >>> hdu.header # show the all of the header cards SIMPLE = T / conforms to FITS standard BITPIX = 8 /...
Read more >
Source code for sunraster.instr.spice - SunPy
If False, returns regular windows. ... sequence_class([v[0] for v in value], common_axis=-1)) for key, value in cube_lists.items() ] else: # If only one ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found