Skip to content

Instantly share code, notes, and snippets.

@cdeil
Created February 11, 2014 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdeil/8936825 to your computer and use it in GitHub Desktop.
Save cdeil/8936825 to your computer and use it in GitHub Desktop.
______________________________________________________________ TestMaps.test_make_derived_maps _______________________________________________________________
self = <gammapy.background.tests.test_maps.TestMaps testMethod=test_make_derived_maps>
def setUp(self):
"""Make an example file containing a Maps object."""
# Parameters
shape = (300, 300)
background = 1
pos1 = (100, 100)
signal1 = 100
pos2 = (200, 200)
signal2 = 100
a_on = 1
exclusion_dist = 10
# Make an example on map containing background and two sources
n_on_data = background * np.ones(shape)
n_on_data[pos1] += signal1
n_on_data[pos2] += signal2
n_on_hdu = fits.ImageHDU(n_on_data, name='n_on')
# Make and example onexposure map
onexposure_data = a_on * np.ones(shape)
onexposure_hdu = fits.ImageHDU(onexposure_data, name='a_on')
# Make an example exclusion map that excludes source 1,
# but not source 2
y, x = np.indices(shape)
dist1 = np.sqrt((x - pos1[0]) ** 2 + (y - pos1[1]) ** 2)
exclusion_data = np.where(dist1 < exclusion_dist, 0, 1)
exclusion_hdu = fits.ImageHDU(exclusion_data, name='exclusion')
# Make a BgMaps object and write it to FITS file
maps = Maps([n_on_hdu, onexposure_hdu, exclusion_hdu])
> maps.writeto(self.filename_basic, clobber=True)
gammapy/background/tests/test_maps.py:53:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = [<astropy.io.fits.hdu.image.PrimaryHDU object at 0x113f5d910>, <astropy.io.fit...ct at 0x113f64390>, <astropy.io.fits.hdu.image.ImageHDU object at 0x113f643d0>]
fileobj = <astropy.io.fits.file._File <open file '/tmp/maps_basic.fits', mode 'wb' at 0x113f68780>>, output_verify = 'exception', clobber = True
checksum = False
def writeto(self, fileobj, output_verify='exception', clobber=False,
checksum=False):
"""
Write the `HDUList` to a new file.
Parameters
----------
fileobj : file path, file object or file-like object
File to write to. If a file object, must be opened in a
writeable mode.
output_verify : str
Output verification option. Must be one of ``"fix"``,
``"silentfix"``, ``"ignore"``, ``"warn"``, or
``"exception"``. See :ref:`verify` for more info.
clobber : bool
When `True`, overwrite the output file if exists.
checksum : bool
When `True` adds both ``DATASUM`` and ``CHECKSUM`` cards
to the headers of all HDU's written to the file.
"""
if (len(self) == 0):
warnings.warn("There is nothing to write.", AstropyUserWarning)
return
self.verify(option=output_verify)
# make sure the EXTEND keyword is there if there is extension
self.update_extend()
# make note of whether the input file object is already open, in which
# case we should not close it after writing (that should be the job
# of the caller)
closed = fileobj_closed(fileobj)
# writeto is only for writing a new file from scratch, so the most
# sensible mode to require is 'ostream'. This can accept an open
# file object that's open to write only, or in append/update modes
# but only if the file doesn't exist.
fileobj = _File(fileobj, mode='ostream', clobber=clobber)
> hdulist = self.fromfile(fileobj)
/Users/deil/Library/Python/2.7/lib/python/site-packages/astropy-0.4.dev7301-py2.7-macosx-10.9-x86_64.egg/astropy/io/fits/hdu/hdulist.py:649:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'gammapy.background.maps.Maps'>, fileobj = <astropy.io.fits.file._File <open file '/tmp/maps_basic.fits', mode 'wb' at 0x113f68780>>, mode = None
memmap = False, save_backup = False, kwargs = {}
@classmethod
def fromfile(cls, fileobj, mode=None, memmap=False,
save_backup=False, **kwargs):
"""
Creates an HDUList instance from a file-like object.
The actual implementation of :func:`fitsopen`, and generally shouldn't
be used directly. Use :func:`open` instead (and see its
documentation for details of the parameters accepted by this method).
"""
return cls._readfrom(fileobj=fileobj, mode=mode, memmap=memmap,
> save_backup=save_backup, **kwargs)
/Users/deil/Library/Python/2.7/lib/python/site-packages/astropy-0.4.dev7301-py2.7-macosx-10.9-x86_64.egg/astropy/io/fits/hdu/hdulist.py:247:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'gammapy.background.maps.Maps'>, fileobj = <astropy.io.fits.file._File <open file '/tmp/maps_basic.fits', mode 'wb' at 0x113f68780>>, data = None
mode = 'ostream', memmap = False, save_backup = False, kwargs = {}
ffo = <astropy.io.fits.file._File <open file '/tmp/maps_basic.fits', mode 'wb' at 0x113f68780>>
@classmethod
def _readfrom(cls, fileobj=None, data=None, mode=None,
memmap=False, save_backup=False, **kwargs):
"""
Provides the implementations from HDUList.fromfile and
HDUList.fromstring, both of which wrap this method, as their
implementations are largely the same.
"""
if fileobj is not None:
if not isinstance(fileobj, _File):
# instantiate a FITS file object (ffo)
ffo = _File(fileobj, mode=mode, memmap=memmap)
else:
ffo = fileobj
# The pyfits mode is determined by the _File initializer if the
# supplied mode was None
mode = ffo.mode
> hdulist = cls(file=ffo)
/Users/deil/Library/Python/2.7/lib/python/site-packages/astropy-0.4.dev7301-py2.7-macosx-10.9-x86_64.egg/astropy/io/fits/hdu/hdulist.py:764:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = [], hdus = [], file = <astropy.io.fits.file._File <open file '/tmp/maps_basic.fits', mode 'wb' at 0x113f68780>>, rename_hdus = None
is_off_correlated = True, theta = None, theta_pix = 0
def __init__(self, hdus=[], file=None, rename_hdus=None,
is_off_correlated=True, theta=None, theta_pix=0):
super(Maps, self).__init__(hdus, file)
#import IPython; IPython.embed()
#if rename_hdus is not None:
# for name, number in rename_hdus.items():
# self[number].name = name
hdu_names = [hdu.name.lower() for hdu in self]
print(hdu_names)
# Check that there is at least one of the basic_maps present.
# This is required so that the map geometry is defined.
existing_basic_maps = [name for name in BASIC_MAP_NAMES
if name in hdu_names]
nonexisting_basic_maps = [name for name in BASIC_MAP_NAMES
if name not in hdu_names]
if not existing_basic_maps:
logging.error('hdu_names =', hdu_names)
logging.error('BASIC_MAP_NAMES = ', BASIC_MAP_NAMES)
> raise IndexError('hdus must contain at least one of the BASIC_MAP_NAMES')
E IndexError: hdus must contain at least one of the BASIC_MAP_NAMES
gammapy/background/maps.py:71: IndexError
---------------------------------------------------------------------- Captured stdout -----------------------------------------------------------------------
['n_on', 'a_on', 'exclusion']
[]
---------------------------------------------------------------------- Captured stderr -----------------------------------------------------------------------
DEBUG - Adding missing basic maps: ['n_off', 'a_off', 'exposure']
DEBUG - is_off_correlated: True
DEBUG - theta: 0
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 851, in emit
msg = self.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 724, in format
return fmt.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format
record.message = record.getMessage()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file maps.py, line 69
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 851, in emit
msg = self.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 724, in format
return fmt.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format
record.message = record.getMessage()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file maps.py, line 70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment