Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Cannot index an array - causing key error , context astropy.io

There seems to be a problem indexing the object class 'astropy.io.fits.hdu.hdulist.HDUList

When I try to index it, I get a key Error as if it were a dictionary: fits_image_filename = "/tmp/chiCyGQHY.fits" from astropy.io import fits hdul = fits.open(fits_image_filename)

print(type(hdul)) len(hdul) <class 'astropy.io.fits.hdu.hdulist.hdulist'=""> 1

I need to extract hdul[0]. I succeed ONLY if I do hdul.pop()

hdu=hdul.pop()

hdu

<astropy.io.fits.hdu.image.primaryhdu object="" at="" 0x7f707f89c510="">

However if instead I do hdu = hdul[0] KeyError Traceback (most recent call last) <ipython-input-25-e993ae5dfe6a> in <module>() ----> 1 hdul[Integer(0)]

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in __getitem__(self, key) 299 try: 300 return self._try_while_unread_hdus(super(HDUList, self).__getitem__, --> 301 self._positive_index_of(key)) 302 except IndexError as e: 303 # Raise a more helpful IndexError if the file was not fully read.

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _positive_index_of(self, key) 719 """ 720 --> 721 index = self.index_of(key) 722 723 if index >= 0:

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in index_of(self, key) 684 '{} indices must be integers, extension names as strings, ' 685 'or (extname, version) tuples; got {}' --> 686 ''.format(self.__class__.__name__, _key)) 687 688 _key = (_key.strip()).upper()

KeyError: 'HDUList indices must be integers, extension names as strings, or (extname, version) tuples; got 0'

Question: What causes this confusion in sage between dictionaries and lists. Is it the wrapper Integer() ? If so, what is a more elegant work-around ?

Thanks to all you mavens out there!

Pat

Cannot index an array - causing key error , context astropy.io

There seems to be a problem indexing the object class 'astropy.io.fits.hdu.hdulist.HDUList

When I try to index it, I get a key Error as if it were a dictionary: dictionary:

fits_image_filename = "/tmp/chiCyGQHY.fits"
from astropy.io import fits
hdul = fits.open(fits_image_filename)

fits.open(fits_image_filename) print(type(hdul)) len(hdul) <class 'astropy.io.fits.hdu.hdulist.hdulist'=""> 1

'astropy.io.fits.hdu.hdulist.HDUList'> 1

I need to extract hdul[0]. I succeed ONLY if I do hdul.pop()

hdu=hdul.pop()

hdu

<astropy.io.fits.hdu.image.primaryhdu object="" at="" 0x7f707f89c510="">

hdu=hdul.pop()

hdu

<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f707f89c510>

However if instead I do hdu = hdul[0]
KeyError **KeyError**                                  Traceback (most recent call last)
<ipython-input-25-e993ae5dfe6a> in <module>()
----> 1 hdul[Integer(0)]

hdul**[Integer(0)]** /home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in __getitem__(self, key) 299 try: 300 return self._try_while_unread_hdus(super(HDUList, self).__getitem__, --> 301 self._positive_index_of(key)) 302 except IndexError as e: 303 # Raise a more helpful IndexError if the file was not fully read.

read. /home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _positive_index_of(self, key) 719 """ 720 --> 721 index = self.index_of(key) 722 723 if index >= 0:

0: /home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in index_of(self, key) 684 '{} indices must be integers, extension names as strings, ' 685 'or (extname, version) tuples; got {}' --> 686 ''.format(self.__class__.__name__, _key)) 687 688 _key = (_key.strip()).upper()

(_key.strip()).upper() KeyError: 'HDUList indices must be integers, extension names as strings, or (extname, version) tuples; got 0'

0'

Question: What causes this confusion in sage between dictionaries and lists. Is it the wrapper Integer() ? If so, what is a more elegant work-around ?

Thanks to all you mavens out there!

Pat

click to hide/show revision 3
retagged

Cannot index an array - causing key error , context astropy.io

There seems to be a problem indexing the object class 'astropy.io.fits.hdu.hdulist.HDUList

When I try to index it, I get a key Error as if it were a dictionary:

fits_image_filename = "/tmp/chiCyGQHY.fits"
from astropy.io import fits
hdul = fits.open(fits_image_filename)

print(type(hdul))
len(hdul)
<class 'astropy.io.fits.hdu.hdulist.HDUList'>
1

I need to extract hdul[0]. I succeed ONLY if I do hdul.pop()

hdu=hdul.pop()

hdu

<astropy.io.fits.hdu.image.PrimaryHDU object at 0x7f707f89c510>

However if instead I do hdu = hdul[0]
**KeyError**                                  Traceback (most recent call last)
<ipython-input-25-e993ae5dfe6a> in <module>()
----> 1 hdul**[Integer(0)]**

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in __getitem__(self, key)
    299         try:
    300             return self._try_while_unread_hdus(super(HDUList, self).__getitem__,
--> 301                                             self._positive_index_of(key))
    302         except IndexError as e:
    303             # Raise a more helpful IndexError if the file was not fully read.

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _positive_index_of(self, key)
    719         """
    720 
--> 721         index = self.index_of(key)
    722 
    723         if index >= 0:

/home/patricia/SageMath/local/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in index_of(self, key)
    684                 '{} indices must be integers, extension names as strings, '
    685                 'or (extname, version) tuples; got {}'
--> 686                 ''.format(self.__class__.__name__, _key))
    687 
    688         _key = (_key.strip()).upper()

KeyError: 'HDUList indices must be integers, extension names as strings, or (extname, version) tuples; got 0'

Question: What causes this confusion in sage between dictionaries and lists. Is it the wrapper Integer() ? If so, what is a more elegant work-around ?

Thanks to all you mavens out there!

Pat