Skip to content

Instantly share code, notes, and snippets.

@cdeil
Created February 20, 2018 11:56
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/ff27083f3afa01de418c6fef1d49f1ec to your computer and use it in GitHub Desktop.
Save cdeil/ff27083f3afa01de418c6fef1d49f1ec to your computer and use it in GitHub Desktop.
_______________________________________________________________________________ test_fill_cube _______________________________________________________________________________
event_lists = [<gammapy.data.event_list.EventList object at 0x112bab320>, <gammapy.data.event_list.EventList object at 0x112babcc0>, <gammapy.data.event_list.EventList object at 0x112babc18>, <gammapy.data.event_list.EventList object at 0x112c799b0>]
@requires_data('gammapy-extra')
def test_fill_cube(event_lists):
> array = read_cube()
gammapy/background/tests/test_fov_cube.py:120:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gammapy/background/tests/test_fov_cube.py:19: in read_cube
cube = FOVCube.read(filename, format='table', scheme=scheme, hdu='BACKGROUND')
gammapy/background/fov_cube.py:383: in read
return cls.from_fits_table(hdu, scheme)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cls = <class 'gammapy.background.fov_cube.FOVCube'>, hdu = <astropy.io.fits.hdu.table.BinTableHDU object at 0x112d7bbe0>, scheme = 'bg_cube'
@classmethod
def from_fits_table(cls, hdu, scheme=None):
"""Read cube from a FITS binary table.
Parameters
----------
hdu : `~astropy.io.fits.BinTableHDU`
HDU binary table for the cube.
scheme : str, optional
String identifying parameter naming scheme for FITS files and plots.
Returns
-------
cube : `~gammapy.background.FOVCube`
FOVCube object.
"""
header = hdu.header
data = hdu.data
scheme_dict = cls.define_scheme(scheme)
x_name_lo = scheme_dict['coordx_fits_name'] + '_LO'
x_name_hi = scheme_dict['coordx_fits_name'] + '_HI'
y_name_lo = scheme_dict['coordy_fits_name'] + '_LO'
y_name_hi = scheme_dict['coordy_fits_name'] + '_HI'
e_name_lo = scheme_dict['energy_fits_name'] + '_LO'
e_name_hi = scheme_dict['energy_fits_name'] + '_HI'
# check correct axis order: 1st X, 2nd Y, 3rd energy, 4th data
if (header['TTYPE1'] != x_name_lo) or (header['TTYPE2'] != x_name_hi):
raise ValueError("Expecting X axis in first 2 places, not ({0}, {1})"
.format(header['TTYPE1'], header['TTYPE2']))
if (header['TTYPE3'] != y_name_lo) or (header['TTYPE4'] != y_name_hi):
raise ValueError("Expecting Y axis in second 2 places, not ({0}, {1})"
.format(header['TTYPE3'], header['TTYPE4']))
if (header['TTYPE5'] != e_name_lo) or (header['TTYPE6'] != e_name_hi):
raise ValueError("Expecting E axis in third 2 places, not ({0}, {1})"
.format(header['TTYPE5'], header['TTYPE6']))
if (header['TTYPE7'] != scheme_dict['data_fits_name']):
raise ValueError("Expecting data axis ({0}) in fourth place, not ({1})"
> .format(scheme_dict['data_fits_name'], header['TTYPE7']))
E ValueError: Expecting data axis (BKG) in fourth place, not (Bgd)
gammapy/background/fov_cube.py:246: ValueError
________________________________________________________________________ test_spectrum_analysis_iact _________________________________________________________________________
tmpdir = local('/private/var/folders/sb/4qv5j4m90pz1rw7m70rj1b1r0000gn/T/pytest-of-deil/pytest-28/test_spectrum_analysis_iact0')
@requires_data('gammapy-extra')
@requires_dependency('scipy')
@requires_dependency('sherpa')
def test_spectrum_analysis_iact(tmpdir):
config = get_config()
config['outdir'] = tmpdir
analysis = SpectrumAnalysisIACT(observations=obs_list(), config=config)
> analysis.run()
gammapy/scripts/tests/test_spectrum_pipe.py:37:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gammapy/scripts/spectrum_pipe.py:62: in run
self.run_fit()
gammapy/scripts/spectrum_pipe.py:94: in run_fit
self.flux_point_estimator.compute_points()
gammapy/spectrum/flux_point.py:608: in compute_points
row = self.compute_flux_point(group)
gammapy/spectrum/flux_point.py:632: in compute_flux_point
energy_ref=energy_ref,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <gammapy.spectrum.flux_point.FluxPointEstimator object at 0x166d55ba8>, model = PowerLaw()
energy_group = SpectrumEnergyGroup(energy_group_idx=1, bin_idx_min=36, bin_idx_max=42, bin_type='normal', energy_min=<Quantity 1. TeV>, energy_max=<Quantity 2.44843675 TeV>)
energy_ref = <Quantity 1.56474814 TeV>, sqrt_ts_threshold = 1
def fit_point(self, model, energy_group, energy_ref, sqrt_ts_threshold=1):
from .fit import SpectrumFit
energy_min = energy_group.energy_min
energy_max = energy_group.energy_max
# Set reference and remove min amplitude
model.parameters['reference'].value = energy_ref.to('TeV').value
fit = SpectrumFit(self.obs, model)
# TODO: Notice channels contained in energy_group
fit.fit_range = energy_min, energy_max
log.debug(
'Calling Sherpa fit for flux point '
' in energy range:\n{}'.format(fit)
)
fit.fit()
fit.est_errors()
# compute TS value for all observations
stat_best_fit = np.sum([res.statval for res in fit.result])
> dnde, dnde_err = res.model.evaluate_error(energy_ref)
E NameError: name 'res' is not defined
gammapy/spectrum/flux_point.py:809: NameError
____________________________________________________________________________ test_flux_points[pl] ____________________________________________________________________________
config = {'dnde': <Quantity 2.7465e-11 1 / (cm2 s TeV)>, 'dnde_err': <Quantity 4.7555e-12 1 / (cm2 s TeV)>, 'dnde_errn': <Quantity 4.5333e-12 1 / (cm2 s TeV)>, 'dnde_errp': <Quantity 5.005e-12 1 / (cm2 s TeV)>, ...}
@requires_data('gammapy-extra')
@requires_dependency('sherpa')
@requires_dependency('matplotlib')
@requires_dependency('scipy')
@pytest.mark.parametrize('config', ['pl', 'ecpl'])
def test_flux_points(config):
# TODO: replace this with a simple test case in a fixture
filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
obs = SpectrumObservation.read(filename)
seg = SpectrumEnergyGroupMaker(obs=obs)
ebounds = [0.3, 1, 3, 10, 30] * u.TeV
seg.compute_range_safe()
seg.compute_groups_fixed(ebounds=ebounds)
if config == 'pl':
config = dict(
model=PowerLaw(
index=Quantity(2, ''),
amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
reference=Quantity(1, 'TeV')
),
obs=obs,
seg=seg,
dnde=2.7465e-11 * u.Unit('cm-2 s-1 TeV-1'),
dnde_err=4.7555e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errn=4.5333e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errp=5.0050e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_ul=3.7998e-11 * u.Unit('cm-2 s-1 TeV-1'),
res=-0.1126,
res_err=0.1536,
)
elif config == 'ecpl':
config = dict(
model=ExponentialCutoffPowerLaw(
index=Quantity(2, ''),
amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
reference=Quantity(1, 'TeV'),
lambda_= Quantity(0.1, 'TeV-1')
),
obs=obs,
seg=seg,
dnde=2.7465e-11 * u.Unit('cm-2 s-1 TeV-1'),
dnde_err=4.7555e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errn=4.5333e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errp=5.0050e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_ul=3.7998e-11 * u.Unit('cm-2 s-1 TeV-1'),
res=-0.057955,
res_err=0.1634,
)
> tester = FluxPointTester(config)
gammapy/spectrum/tests/test_flux_point.py:202:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gammapy/spectrum/tests/test_flux_point.py:214: in __init__
self.setup()
gammapy/spectrum/tests/test_flux_point.py:226: in setup
self.fpe.compute_points()
gammapy/spectrum/flux_point.py:608: in compute_points
row = self.compute_flux_point(group)
gammapy/spectrum/flux_point.py:632: in compute_flux_point
energy_ref=energy_ref,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <gammapy.spectrum.flux_point.FluxPointEstimator object at 0x166c13d30>, model = PowerLaw()
energy_group = SpectrumEnergyGroup(energy_group_idx=1, bin_idx_min=33, bin_idx_max=35, bin_type='normal', energy_min=<Quantity 6.81292069e+08 keV>, energy_max=<Quantity 1.e+09 keV>)
energy_ref = <Quantity 8.25404185e+08 keV>, sqrt_ts_threshold = 1
def fit_point(self, model, energy_group, energy_ref, sqrt_ts_threshold=1):
from .fit import SpectrumFit
energy_min = energy_group.energy_min
energy_max = energy_group.energy_max
# Set reference and remove min amplitude
model.parameters['reference'].value = energy_ref.to('TeV').value
fit = SpectrumFit(self.obs, model)
# TODO: Notice channels contained in energy_group
fit.fit_range = energy_min, energy_max
log.debug(
'Calling Sherpa fit for flux point '
' in energy range:\n{}'.format(fit)
)
fit.fit()
fit.est_errors()
# compute TS value for all observations
stat_best_fit = np.sum([res.statval for res in fit.result])
> dnde, dnde_err = res.model.evaluate_error(energy_ref)
E NameError: name 'res' is not defined
gammapy/spectrum/flux_point.py:809: NameError
___________________________________________________________________________ test_flux_points[ecpl] ___________________________________________________________________________
config = {'dnde': <Quantity 2.7465e-11 1 / (cm2 s TeV)>, 'dnde_err': <Quantity 4.7555e-12 1 / (cm2 s TeV)>, 'dnde_errn': <Quantity 4.5333e-12 1 / (cm2 s TeV)>, 'dnde_errp': <Quantity 5.005e-12 1 / (cm2 s TeV)>, ...}
@requires_data('gammapy-extra')
@requires_dependency('sherpa')
@requires_dependency('matplotlib')
@requires_dependency('scipy')
@pytest.mark.parametrize('config', ['pl', 'ecpl'])
def test_flux_points(config):
# TODO: replace this with a simple test case in a fixture
filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
obs = SpectrumObservation.read(filename)
seg = SpectrumEnergyGroupMaker(obs=obs)
ebounds = [0.3, 1, 3, 10, 30] * u.TeV
seg.compute_range_safe()
seg.compute_groups_fixed(ebounds=ebounds)
if config == 'pl':
config = dict(
model=PowerLaw(
index=Quantity(2, ''),
amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
reference=Quantity(1, 'TeV')
),
obs=obs,
seg=seg,
dnde=2.7465e-11 * u.Unit('cm-2 s-1 TeV-1'),
dnde_err=4.7555e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errn=4.5333e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errp=5.0050e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_ul=3.7998e-11 * u.Unit('cm-2 s-1 TeV-1'),
res=-0.1126,
res_err=0.1536,
)
elif config == 'ecpl':
config = dict(
model=ExponentialCutoffPowerLaw(
index=Quantity(2, ''),
amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
reference=Quantity(1, 'TeV'),
lambda_= Quantity(0.1, 'TeV-1')
),
obs=obs,
seg=seg,
dnde=2.7465e-11 * u.Unit('cm-2 s-1 TeV-1'),
dnde_err=4.7555e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errn=4.5333e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_errp=5.0050e-12 * u.Unit('cm-2 s-1 TeV-1'),
dnde_ul=3.7998e-11 * u.Unit('cm-2 s-1 TeV-1'),
res=-0.057955,
res_err=0.1634,
)
> tester = FluxPointTester(config)
gammapy/spectrum/tests/test_flux_point.py:202:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
gammapy/spectrum/tests/test_flux_point.py:214: in __init__
self.setup()
gammapy/spectrum/tests/test_flux_point.py:226: in setup
self.fpe.compute_points()
gammapy/spectrum/flux_point.py:608: in compute_points
row = self.compute_flux_point(group)
gammapy/spectrum/flux_point.py:632: in compute_flux_point
energy_ref=energy_ref,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <gammapy.spectrum.flux_point.FluxPointEstimator object at 0x166fd8f28>, model = ExponentialCutoffPowerLaw()
energy_group = SpectrumEnergyGroup(energy_group_idx=1, bin_idx_min=33, bin_idx_max=35, bin_type='normal', energy_min=<Quantity 6.81292069e+08 keV>, energy_max=<Quantity 1.e+09 keV>)
energy_ref = <Quantity 8.25404185e+08 keV>, sqrt_ts_threshold = 1
def fit_point(self, model, energy_group, energy_ref, sqrt_ts_threshold=1):
from .fit import SpectrumFit
energy_min = energy_group.energy_min
energy_max = energy_group.energy_max
# Set reference and remove min amplitude
model.parameters['reference'].value = energy_ref.to('TeV').value
fit = SpectrumFit(self.obs, model)
# TODO: Notice channels contained in energy_group
fit.fit_range = energy_min, energy_max
log.debug(
'Calling Sherpa fit for flux point '
' in energy range:\n{}'.format(fit)
)
fit.fit()
fit.est_errors()
# compute TS value for all observations
stat_best_fit = np.sum([res.statval for res in fit.result])
> dnde, dnde_err = res.model.evaluate_error(energy_ref)
E NameError: name 'res' is not defined
gammapy/spectrum/flux_point.py:809: NameError
================================================= 4 failed, 1246 passed, 28 skipped, 35 xfailed, 4 xpassed in 332.18 seconds =================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment