From e9059de2775482e36aa100a423ce1a89f8f31f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Zabalza?= Date: Fri, 8 Nov 2019 12:59:08 +0000 Subject: [PATCH 1/2] Move tests out of source tree --- {src/naima/tests => tests}/__init__.py | 0 src/naima/tests/fixtures.py => tests/conftest.py | 2 +- {src/naima/tests => tests}/coveragerc | 0 .../tests => tests}/data/CrabNebula_Fake_Xray.dat | 0 .../tests => tests}/data/CrabNebula_HESS_ipac.dat | 0 .../data/CrabNebula_HESS_ipac_energy_edges.dat | 0 .../data/CrabNebula_HESS_ipac_energy_error.dat | 0 .../data/CrabNebula_HESS_ipac_energy_errors.dat | 0 .../data/CrabNebula_HESS_ipac_energy_width.dat | 0 .../data/CrabNebula_HESS_ipac_symmetric.dat | 0 {src/naima/tests => tests}/data/Fake_ipac_sed.dat | 0 {src/naima/tests => tests}/setup_package.py | 0 {src/naima/tests => tests}/test_functionfit.py | 2 +- {src/naima/tests => tests}/test_interactive.py | 4 ++-- {src/naima/tests => tests}/test_models.py | 10 +++++----- {src/naima/tests => tests}/test_plotting.py | 6 ++---- {src/naima/tests => tests}/test_saveread.py | 11 ++++------- {src/naima/tests => tests}/test_sherpamod.py | 12 ++++++------ {src/naima/tests => tests}/test_utils.py | 2 +- tox.ini | 2 +- 20 files changed, 23 insertions(+), 28 deletions(-) rename {src/naima/tests => tests}/__init__.py (100%) rename src/naima/tests/fixtures.py => tests/conftest.py (98%) rename {src/naima/tests => tests}/coveragerc (100%) rename {src/naima/tests => tests}/data/CrabNebula_Fake_Xray.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac_energy_edges.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac_energy_error.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac_energy_errors.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac_energy_width.dat (100%) rename {src/naima/tests => tests}/data/CrabNebula_HESS_ipac_symmetric.dat (100%) rename {src/naima/tests => tests}/data/Fake_ipac_sed.dat (100%) rename {src/naima/tests => tests}/setup_package.py (100%) rename {src/naima/tests => tests}/test_functionfit.py (99%) rename {src/naima/tests => tests}/test_interactive.py (96%) rename {src/naima/tests => tests}/test_models.py (98%) rename {src/naima/tests => tests}/test_plotting.py (95%) rename {src/naima/tests => tests}/test_saveread.py (91%) rename {src/naima/tests => tests}/test_sherpamod.py (86%) rename {src/naima/tests => tests}/test_utils.py (99%) diff --git a/src/naima/tests/__init__.py b/tests/__init__.py similarity index 100% rename from src/naima/tests/__init__.py rename to tests/__init__.py diff --git a/src/naima/tests/fixtures.py b/tests/conftest.py similarity index 98% rename from src/naima/tests/fixtures.py rename to tests/conftest.py index 062e04a4..5cff931f 100644 --- a/src/naima/tests/fixtures.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ from astropy.tests.helper import pytest from astropy.utils.data import get_pkg_data_filename -from ..core import run_sampler, uniform_prior +from naima.core import run_sampler, uniform_prior # Read data fname = get_pkg_data_filename("data/CrabNebula_HESS_ipac.dat") diff --git a/src/naima/tests/coveragerc b/tests/coveragerc similarity index 100% rename from src/naima/tests/coveragerc rename to tests/coveragerc diff --git a/src/naima/tests/data/CrabNebula_Fake_Xray.dat b/tests/data/CrabNebula_Fake_Xray.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_Fake_Xray.dat rename to tests/data/CrabNebula_Fake_Xray.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac.dat b/tests/data/CrabNebula_HESS_ipac.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac.dat rename to tests/data/CrabNebula_HESS_ipac.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac_energy_edges.dat b/tests/data/CrabNebula_HESS_ipac_energy_edges.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac_energy_edges.dat rename to tests/data/CrabNebula_HESS_ipac_energy_edges.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac_energy_error.dat b/tests/data/CrabNebula_HESS_ipac_energy_error.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac_energy_error.dat rename to tests/data/CrabNebula_HESS_ipac_energy_error.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac_energy_errors.dat b/tests/data/CrabNebula_HESS_ipac_energy_errors.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac_energy_errors.dat rename to tests/data/CrabNebula_HESS_ipac_energy_errors.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac_energy_width.dat b/tests/data/CrabNebula_HESS_ipac_energy_width.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac_energy_width.dat rename to tests/data/CrabNebula_HESS_ipac_energy_width.dat diff --git a/src/naima/tests/data/CrabNebula_HESS_ipac_symmetric.dat b/tests/data/CrabNebula_HESS_ipac_symmetric.dat similarity index 100% rename from src/naima/tests/data/CrabNebula_HESS_ipac_symmetric.dat rename to tests/data/CrabNebula_HESS_ipac_symmetric.dat diff --git a/src/naima/tests/data/Fake_ipac_sed.dat b/tests/data/Fake_ipac_sed.dat similarity index 100% rename from src/naima/tests/data/Fake_ipac_sed.dat rename to tests/data/Fake_ipac_sed.dat diff --git a/src/naima/tests/setup_package.py b/tests/setup_package.py similarity index 100% rename from src/naima/tests/setup_package.py rename to tests/setup_package.py diff --git a/src/naima/tests/test_functionfit.py b/tests/test_functionfit.py similarity index 99% rename from src/naima/tests/test_functionfit.py rename to tests/test_functionfit.py index b5db3c4c..1108e994 100644 --- a/src/naima/tests/test_functionfit.py +++ b/tests/test_functionfit.py @@ -7,7 +7,7 @@ from astropy.tests.helper import pytest from astropy.utils.data import get_pkg_data_filename -from ..core import ( +from naima.core import ( get_sampler, lnprob, normal_prior, diff --git a/src/naima/tests/test_interactive.py b/tests/test_interactive.py similarity index 96% rename from src/naima/tests/test_interactive.py rename to tests/test_interactive.py index e89ab8c8..1730d2e0 100644 --- a/src/naima/tests/test_interactive.py +++ b/tests/test_interactive.py @@ -7,8 +7,8 @@ from astropy.tests.helper import pytest from astropy.utils.data import get_pkg_data_filename -from ..model_fitter import InteractiveModelFitter -from ..models import ExponentialCutoffPowerLaw +from naima.model_fitter import InteractiveModelFitter +from naima.models import ExponentialCutoffPowerLaw try: import matplotlib diff --git a/src/naima/tests/test_models.py b/tests/test_models.py similarity index 98% rename from src/naima/tests/test_models.py rename to tests/test_models.py index 1e9ce16a..7dd987d0 100644 --- a/src/naima/tests/test_models.py +++ b/tests/test_models.py @@ -7,7 +7,7 @@ from astropy.tests.helper import pytest from numpy.testing import assert_allclose -from ..models import ( +from naima.models import ( Bremsstrahlung, BrokenPowerLaw, EblAbsorptionModel, @@ -20,8 +20,8 @@ Synchrotron, TableModel, ) -from ..radiative import PionDecayKelner06 -from ..utils import trapz_loglog +from naima.radiative import PionDecayKelner06 +from naima.utils import trapz_loglog try: import scipy @@ -454,7 +454,7 @@ def test_pion_decay_no_nuc_enh(particle_dists): """ test PionDecayKelner06 """ - from ..radiative import PionDecay + from naima.radiative import PionDecay ECPL, PL, BPL = particle_dists @@ -542,7 +542,7 @@ def test_tablemodel(): assert_allclose(tm(e3).value, 0.0) # use tablemodel as pdist - from ..radiative import Synchrotron, InverseCompton, PionDecay + from naima.radiative import Synchrotron, InverseCompton, PionDecay SY = Synchrotron(tm) _ = SY.flux(e / 10) diff --git a/src/naima/tests/test_plotting.py b/tests/test_plotting.py similarity index 95% rename from src/naima/tests/test_plotting.py rename to tests/test_plotting.py index f39cd5bf..a577daad 100644 --- a/src/naima/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -8,10 +8,8 @@ from astropy.tests.helper import pytest from astropy.utils.data import get_pkg_data_filename -from ..analysis import save_diagnostic_plots, save_results_table -from ..core import run_sampler, uniform_prior -from ..plot import plot_chain, plot_data, plot_fit -from .fixtures import sampler +from naima.analysis import save_diagnostic_plots, save_results_table +from naima.plot import plot_chain, plot_data, plot_fit try: import matplotlib diff --git a/src/naima/tests/test_saveread.py b/tests/test_saveread.py similarity index 91% rename from src/naima/tests/test_saveread.py rename to tests/test_saveread.py index 674f891e..ffc63b93 100644 --- a/src/naima/tests/test_saveread.py +++ b/tests/test_saveread.py @@ -7,13 +7,10 @@ from astropy.tests.helper import pytest from astropy.utils.data import get_pkg_data_filename -from ..analysis import read_run, save_run -from ..core import run_sampler, uniform_prior -from ..model_fitter import InteractiveModelFitter -from ..models import ExponentialCutoffPowerLaw -from ..plot import plot_chain, plot_data, plot_fit -from ..utils import validate_data_table -from .fixtures import simple_sampler as sampler +from naima.analysis import read_run, save_run +from naima.model_fitter import InteractiveModelFitter +from naima.plot import plot_chain, plot_data, plot_fit +from naima.utils import validate_data_table try: import matplotlib diff --git a/src/naima/tests/test_sherpamod.py b/tests/test_sherpamod.py similarity index 86% rename from src/naima/tests/test_sherpamod.py rename to tests/test_sherpamod.py index f403ad84..2919bdce 100644 --- a/src/naima/tests/test_sherpamod.py +++ b/tests/test_sherpamod.py @@ -2,7 +2,7 @@ import numpy as np from astropy.tests.helper import pytest -from ..utils import trapz_loglog +from naima.utils import trapz_loglog try: from sherpa import ui @@ -31,7 +31,7 @@ def test_electron_models(): test import """ - from ..sherpa_models import InverseCompton, Synchrotron, Bremsstrahlung + from naima.sherpa_models import InverseCompton, Synchrotron, Bremsstrahlung for modelclass in [InverseCompton, Synchrotron, Bremsstrahlung]: model = modelclass() @@ -42,13 +42,13 @@ def test_electron_models(): print(model) # point calc - output = model.calc([p.val for p in model.pars], energies) + model.calc([p.val for p in model.pars], energies) # test as well ECPL model.cutoff = 100 # integrated - output = model.calc([p.val for p in model.pars], elo, xhi=ehi) + model.calc([p.val for p in model.pars], elo, xhi=ehi) if modelclass is InverseCompton: # Perform a fit to fake data @@ -77,7 +77,7 @@ def test_proton_model(): test import """ - from ..sherpa_models import PionDecay + from naima.sherpa_models import PionDecay model = PionDecay() @@ -88,7 +88,7 @@ def test_proton_model(): output = model.calc([p.val for p in model.pars], energies) # integrated - output = model.calc([p.val for p in model.pars], elo, xhi=ehi) + model.calc([p.val for p in model.pars], elo, xhi=ehi) # test as well ECPL model.cutoff = 1000 diff --git a/src/naima/tests/test_utils.py b/tests/test_utils.py similarity index 99% rename from src/naima/tests/test_utils.py rename to tests/test_utils.py index 97849619..5b706ab4 100644 --- a/src/naima/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,7 +6,7 @@ from astropy.utils.data import get_pkg_data_filename from numpy.testing import assert_allclose -from ..utils import ( +from naima.utils import ( build_data_table, estimate_B, generate_energy_edges, diff --git a/tox.ini b/tox.ini index 91abcfa6..5f99df4b 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ sitepackages = False deps = pytest commands = - pytest {posargs:--verbose src/naima/tests} + pytest {posargs:--verbose tests} [testenv:flake8] skip_install = True From ddfb287dd1497d6010b4491ffa780fb2002adfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Zabalza?= Date: Thu, 28 Nov 2019 00:28:42 +0000 Subject: [PATCH 2/2] use tmpdir fixture --- tests/test_saveread.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/tests/test_saveread.py b/tests/test_saveread.py index ffc63b93..c2452d0a 100644 --- a/tests/test_saveread.py +++ b/tests/test_saveread.py @@ -35,10 +35,11 @@ @pytest.mark.skipif("not HAS_EMCEE") -def test_roundtrip(sampler): - save_run("test_chain.h5", sampler, clobber=True) - assert os.path.exists("test_chain.h5") - nresult = read_run("test_chain.h5") +def test_roundtrip(tmpdir, sampler): + path = os.path.join(tmpdir, "test_chain.h5") + save_run(path, sampler, clobber=True) + assert os.path.exists(path) + nresult = read_run(path) assert np.allclose(sampler.chain, nresult.chain) assert np.allclose(sampler.flatchain, nresult.flatchain) @@ -81,9 +82,10 @@ def test_roundtrip(sampler): @pytest.mark.skipif("not HAS_MATPLOTLIB or not HAS_EMCEE") -def test_plot_fit(sampler): - save_run("test_chain.h5", sampler, clobber=True) - nresult = read_run("test_chain.h5", modelfn=sampler.modelfn) +def test_plot_fit(tmpdir, sampler): + path = os.path.join(tmpdir, "test_chain.h5") + save_run(path, sampler, clobber=True) + nresult = read_run(path, modelfn=sampler.modelfn) plot_data(nresult) plot_fit(nresult, 0) @@ -93,9 +95,10 @@ def test_plot_fit(sampler): @pytest.mark.skipif("not HAS_MATPLOTLIB or not HAS_EMCEE") -def test_plot_chain(sampler): - save_run("test_chain.h5", sampler, clobber=True) - nresult = read_run("test_chain.h5", modelfn=sampler.modelfn) +def test_plot_chain(tmpdir, sampler): + path = os.path.join(tmpdir, "test_chain.h5") + save_run(path, sampler, clobber=True) + nresult = read_run(path, modelfn=sampler.modelfn) for i in range(nresult.chain.shape[2]): plot_chain(nresult, i) @@ -103,9 +106,10 @@ def test_plot_chain(sampler): @pytest.mark.skipif("not HAS_MATPLOTLIB or not HAS_EMCEE") -def test_imf(sampler): - save_run("test_chain.h5", sampler, clobber=True) - nresult = read_run("test_chain.h5", modelfn=sampler.modelfn) +def test_imf(tmpdir, sampler): + path = os.path.join(tmpdir, "test_chain.h5") + save_run(path, sampler, clobber=True) + nresult = read_run(path, modelfn=sampler.modelfn) imf = InteractiveModelFitter( nresult.modelfn, nresult.chain[-1][-1], nresult.data