Skip to content

Commit

Permalink
Merge pull request #2145 from cdeil/gh-2144
Browse files Browse the repository at this point in the history
Make tests run without GAMMAPY_DATA
  • Loading branch information
cdeil committed May 19, 2019
2 parents fcf37b5 + 4cdda67 commit a04ea54
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Expand Up @@ -46,8 +46,6 @@ env:

- ASTROPY_USE_SYSTEM_PYTEST=1

matrix:
- PYTHON_VERSION=3.5 SETUP_CMD='egg_info' FETCH_GAMMAPY_DATA=false

matrix:

Expand All @@ -73,7 +71,8 @@ matrix:

# Run tests without GAMMAPY_EXTRA available
- os: linux
env: FETCH_GAMMAPY_EXTRA=false PYTHON_VERSION=3.7 SETUP_CMD='test -V'
env: FETCH_GAMMAPY_DATA=false PYTHON_VERSION=3.7 SETUP_CMD='test -V'
FETCH_GAMMAPY_EXTRA=false
CONDA_DEPENDENCIES=$CONDA_DEPENDENCIES

# Build docs
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Expand Up @@ -30,7 +30,7 @@ dependencies:
- healpy
- reproject
- sherpa
- iminuit>=1.3.2
- iminuit
- pytest
- pytest-cov
- sphinx
Expand Down
1 change: 1 addition & 0 deletions gammapy/astro/darkmatter/tests/test_spectra.py
Expand Up @@ -16,6 +16,7 @@ def test_primary_flux():
assert_quantity_allclose(actual, desired)


@requires_data("gammapy-data")
def test_DMAnnihilation():

channel = "b"
Expand Down
3 changes: 3 additions & 0 deletions gammapy/cube/tests/test_background.py
Expand Up @@ -142,6 +142,7 @@ def test_make_map_background_irf(bkg_3d, pars, fixed_pointing_info):
assert_allclose(m.data.sum(), pars["sum"], rtol=1e-5)


@requires_data("gammapy-data")
def test_make_map_background_irf_constant(fixed_pointing_info_aligned):
m = make_map_background_irf_with_symmetry(
fpi=fixed_pointing_info_aligned, symmetry="constant"
Expand All @@ -155,6 +156,7 @@ def test_make_map_background_irf_constant(fixed_pointing_info_aligned):
assert_allclose(d[:, 1], d[0, 1])


@requires_data("gammapy-data")
def test_make_map_background_irf_sym(fixed_pointing_info_aligned):
m = make_map_background_irf_with_symmetry(
fpi=fixed_pointing_info_aligned, symmetry="symmetric"
Expand All @@ -164,6 +166,7 @@ def test_make_map_background_irf_sym(fixed_pointing_info_aligned):
assert_allclose(d[0, 1], d[2, 1], rtol=1e-4) # Symmetric along lat


@requires_data("gammapy-data")
def test_make_map_background_irf_asym(fixed_pointing_info_aligned):
m = make_map_background_irf_with_symmetry(
fpi=fixed_pointing_info_aligned, symmetry="asymmetric"
Expand Down
1 change: 1 addition & 0 deletions gammapy/cube/tests/test_make.py
Expand Up @@ -110,6 +110,7 @@ def test_map_maker(pars, observations, keepdims):
assert_allclose(background.data.sum(), pars["background"], rtol=1e-5)


@requires_data("gammapy-data")
def test_map_maker_ring(observations):
ring_bkg = RingBackgroundEstimator(r_in="0.5 deg", width="0.4 deg")
geomd = geom(ebounds=[0.1, 1, 10])
Expand Down
5 changes: 3 additions & 2 deletions gammapy/cube/tests/test_simulate.py
Expand Up @@ -3,6 +3,7 @@
from numpy.testing import assert_allclose
from astropy.coordinates import SkyCoord
import astropy.units as u
from ...utils.testing import requires_data
from ...irf import load_cta_irfs
from ...maps import WcsGeom, MapAxis
from ...spectrum.models import PowerLaw
Expand All @@ -12,11 +13,11 @@
from ..simulate import simulate_dataset


@requires_data("gammapy-data")
def test_simulate():
filename = (
irfs = load_cta_irfs(
"$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
)
irfs = load_cta_irfs(filename)

# Define sky model to simulate the data
spatial_model = SkyGaussian(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")
Expand Down
36 changes: 24 additions & 12 deletions gammapy/spectrum/tests/test_flux_point_estimator.py
Expand Up @@ -4,7 +4,7 @@
from numpy.testing import assert_allclose
from astropy import units as u
from astropy.coordinates import SkyCoord
from ...utils.testing import requires_dependency
from ...utils.testing import requires_dependency, requires_data
from ...irf import EffectiveAreaTable, load_cta_irfs
from ..models import PowerLaw, ExponentialCutoffPowerLaw
from ..simulation import SpectrumSimulation
Expand Down Expand Up @@ -40,29 +40,34 @@ def create_fpe(model):


def simulate_map_dataset():
filename = (
irfs = load_cta_irfs(
"$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
)
irfs = load_cta_irfs(filename)

skydir = SkyCoord("0 deg", "0 deg", frame="galactic")
edges = np.logspace(-1, 2, 15) * u.TeV
energy_axis = MapAxis.from_edges(edges=edges, name="energy")

geom = WcsGeom.create(skydir=skydir, width=(4, 4), binsz=0.1, axes=[energy_axis], coordsys="GAL")
geom = WcsGeom.create(
skydir=skydir, width=(4, 4), binsz=0.1, axes=[energy_axis], coordsys="GAL"
)

gauss = SkyGaussian("0 deg", "0 deg", "0.4 deg", frame="galactic")
pwl = PowerLaw(amplitude="1e-11 cm-2 s-1 TeV-1")
skymodel = SkyModel(spatial_model=gauss, spectral_model=pwl, name="source")
dataset = simulate_dataset(skymodel=skymodel, geom=geom, pointing=skydir, irfs=irfs, random_state=0)
dataset = simulate_dataset(
skymodel=skymodel, geom=geom, pointing=skydir, irfs=irfs, random_state=0
)
return dataset


@pytest.fixture(scope="session")
def fpe_map_pwl():
dataset = simulate_map_dataset()
e_edges = [0.1, 1, 10, 100] * u.TeV
return FluxPointsEstimator(datasets=[dataset], e_edges=e_edges, norm_n_values=3, source="source")
return FluxPointsEstimator(
datasets=[dataset], e_edges=e_edges, norm_n_values=3, source="source"
)


@pytest.fixture(scope="session")
Expand All @@ -72,7 +77,13 @@ def fpe_map_pwl_reoptimize():
dataset.parameters["lon_0"].frozen = True
dataset.parameters["lat_0"].frozen = True
dataset.parameters["index"].frozen = True
return FluxPointsEstimator(datasets=[dataset], e_edges=e_edges, norm_values=[1], reoptimize=True, source="source")
return FluxPointsEstimator(
datasets=[dataset],
e_edges=e_edges,
norm_values=[1],
reoptimize=True,
source="source",
)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -124,29 +135,30 @@ def test_run_ecpl(fpe_ecpl):
fp = fpe_ecpl.estimate_flux_point(fpe_ecpl.e_groups[1])
assert_allclose(fp["norm"], 1, rtol=1e-1)


@staticmethod
@requires_dependency("iminuit")
@requires_data("gammapy-data")
def test_run_map_pwl(fpe_map_pwl):
fp = fpe_map_pwl.run(steps=["err", "norm-scan", "ts"])

actual = fp.table["norm"].data
assert_allclose(actual, [0.97922 , 0.94081 , 1.074426], rtol=1e-3)
assert_allclose(actual, [0.97922, 0.94081, 1.074426], rtol=1e-3)

actual = fp.table["norm_err"].data
assert_allclose(actual, [0.069967, 0.052631, 0.093025], rtol=1e-3)
assert_allclose(actual, [0.069963, 0.052605, 0.09297], rtol=1e-3)

actual = fp.table["sqrt_ts"].data
assert_allclose(actual, [16.165811, 27.121425, 22.040969], rtol=1e-3)
assert_allclose(actual, [16.165806, 27.121415, 22.04104], rtol=1e-3)

actual = fp.table["norm_scan"][0]
assert_allclose(actual, [0.2, 1, 5], rtol=1e-3)

actual = fp.table["dloglike_scan"][0] - fp.table["loglike"][0]
assert_allclose(actual, [1.536460e+02, 8.756689e-02, 1.883420e+03], rtol=1e-3)
assert_allclose(actual, [1.536452e02, 8.762343e-02, 1.883447e03], rtol=1e-3)

@staticmethod
@requires_dependency("iminuit")
@requires_data("gammapy-data")
def test_run_map_pwl_reoptimize(fpe_map_pwl_reoptimize):
fp = fpe_map_pwl_reoptimize.run(steps=["err", "norm-scan", "ts"])

Expand Down

0 comments on commit a04ea54

Please sign in to comment.