ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 10 Nov 2023 20:31:42 +0100Cannot index an array - causing key error , context astropy.iohttps://ask.sagemath.org/question/74269/cannot-index-an-array-causing-key-error-context-astropyio/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!
PatPatBFri, 10 Nov 2023 20:31:42 +0100https://ask.sagemath.org/question/74269/subs() function gives KeyError when keyword is a list memberhttps://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/ I have a multivariate Boolean polynomial my_poly in xi (x0,x1,...,etc) and want to substitute one of the variables (say e.g. x0=0). my_poly subs(x0=0) works but I need to determine the exact variable (left hand side of '=') and value (right hand side of '=') at run-time depending on some conditions. The problem is subs() function do not accept expression on the left hand side of '=' and I have many xi variables, so if else is not practical. How can I solve this issue?adnanbaysalWed, 03 Feb 2016 10:25:06 +0100https://ask.sagemath.org/question/32426/