From 50c0ae24ae455b4684ecab55afc0722ce1a49630 Mon Sep 17 00:00:00 2001 From: Kyle Barbary Date: Fri, 12 May 2017 17:32:03 +0200 Subject: [PATCH] allow import with no fits reader installed --- .travis.yml | 20 +++++++++++--------- sfdmap.py | 13 ++++++++++--- test.py | 35 ++++++++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5e045bf..15366ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,22 +3,24 @@ language: python # Setting sudo to false opts in to Travis-CI container-based builds. sudo: false -python: - - 2.7 - - 3.5 - env: global: - - NUMPY_VERSION=1.11 + - PYTHON_VERSION=3.6 + - NUMPY_VERSION=1.12 - CMD='./test.py' - COVERAGE=0 - EXTRAS='fitsio' matrix: include: - # test without fitsio installed. - - python: 3.5 - env: CMD='./test.py --cov-report= --cov=sfdmap' COVERAGE=1 EXTRAS='' + - env: PYTHON_VERSION=3.4 NUMPY_VERSION=1.11 + - env: PYTHON_VERSION=2.7 + - env: PYTHON_VERSION=2.7 EXTRAS='astropy' + + # test with astropy instead of fitsio installed. + - env: CMD='./test.py --cov-report= --cov=sfdmap' COVERAGE=1 EXTRAS='astropy' + # test that import works with no FITS reader installed. + - env: EXTRAS='' before_install: - wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh @@ -30,7 +32,7 @@ before_install: # Note: leave numpy in all `conda install` commands to prevent conda from # upgrading it automatically. install: - - conda create -c openastronomy --yes -n test python=$TRAVIS_PYTHON_VERSION numpy=$NUMPY_VERSION astropy pytest $EXTRAS + - conda create -c openastronomy --yes -n test python=$PYTHON_VERSION numpy=$NUMPY_VERSION $EXTRAS pytest - source activate test - if [[ $COVERAGE ]]; then pip install pytest-cov coveralls; fi - source activate test diff --git a/sfdmap.py b/sfdmap.py index db9d510..932bc73 100644 --- a/sfdmap.py +++ b/sfdmap.py @@ -1,5 +1,7 @@ # Licensed under an MIT "Expat" license - See LICENSE -"""Get E(B-V) values from the Schlegel, Finkbeiner & Davis (1998) dust map.""" +"""Get E(B-V) values from the Schlegel, Finkbeiner & Davis (1998) dust map. + +Dust maps must be downloaded separately.""" import os @@ -13,7 +15,13 @@ try: from astropy.io.fits import getdata except ImportError: - raise ImportError("could not import fitsio or astropy.io.fits") + # If we don't have either reader, raise an error only when the function + # is called. This is so that we can import the module with just numpy + # installed (the minimum dependency). + def getdata(*args, **kwargs): + raise ImportError("Could not import fitsio or astropy.io.fits. " + "Install fitsio or astropy.") + __all__ = ['SFDMap', 'ebv'] __version__ = "0.1.0" @@ -21,7 +29,6 @@ def _isiterable(obj): """Returns `True` if the given object is iterable.""" - try: iter(obj) return True diff --git a/test.py b/test.py index 130e334..da1ecfe 100755 --- a/test.py +++ b/test.py @@ -4,16 +4,28 @@ import numpy as np from numpy.testing import assert_allclose -from astropy.coordinates import SkyCoord import pytest - +try: + from astropy.coordinates import SkyCoord + HAVE_ASTROPY = True +except ImportError: + HAVE_ASTROPY = False +try: + import fitsio + HAVE_FITSIO = True +except: + HAVE_FITSIO = False + import sfdmap -# ----------------------------------------------------------------------------- -# Test coordinate conversions -import os +# We end up skipping most of the tests if we don't have a FITS reader +# but we want to be able to run them anyway to test that importing works +# with minimal dependencies (numpy). +HAVE_FITS_READER = HAVE_FITSIO or HAVE_ASTROPY +# ----------------------------------------------------------------------------- +# Test coordinate conversions datapath = os.path.join(os.path.dirname(__file__), "testdata") TOL = 0.0001 # tolerance in arcseconds @@ -111,6 +123,8 @@ def test_bilinear_interpolate(): MINIMAP_SFD = sfdmap.SFDMap('testdata', north='SFD_dust_4096_ngp_cutout.fits', scaling=1.0) + +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_versus_ned(): """Test versus NED results""" @@ -125,7 +139,8 @@ def test_versus_ned(): ebv = MINIMAP_SFD.ebv(d['ra'], d['dec']) assert_allclose(ebv, d['ebv'], rtol=0.0, atol=0.001) - + +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_array_inputs(): """Test array inputs (values from NED test above).""" ra = np.array([204.17470, 205.40142]) @@ -134,6 +149,7 @@ def test_array_inputs(): assert_allclose(ebv, [0.077315, 0.0539752], rtol=0.0, atol=0.001) +@pytest.mark.skipif(not HAVE_ASTROPY, reason="no astropy") def test_skycoord(): """Test that skycoord gives same results""" @@ -144,7 +160,7 @@ def test_skycoord(): # only test locally when we have the full images. -@pytest.mark.skipif("SFD_DIR" not in os.environ, +@pytest.mark.skipif("SFD_DIR" not in os.environ or not HAVE_FITS_READER, reason="SFD_DIR environment variable not set") def test_boundaries(): """Test that interpolation=False works at b=0""" @@ -162,6 +178,7 @@ def test_repr(): assert "SFDMap(" in s +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_convenience_func(): ebv1 = MINIMAP.ebv(204.0, -30.0) ebv2 = sfdmap.ebv(204.0, -30.0, mapdir='testdata', @@ -170,6 +187,7 @@ def test_convenience_func(): assert ebv1 == ebv2 +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_interpolate_false(): """Test no interpolation (also tests fk5j2000).""" # position off-center of a pixel, but reference value is pixel value. @@ -178,12 +196,14 @@ def test_interpolate_false(): assert_allclose(ebv, 0.0552888, atol=0.0000001, rtol=0.0) +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_tuple_arg(): """Test that passing (ra, dec) as a tuple works.""" assert MINIMAP.ebv(204.0, -30.0) == MINIMAP.ebv((204.0, -30.0)) +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_argument_exceptions(): """Test wrong numbers or types of arguments""" @@ -197,6 +217,7 @@ def test_argument_exceptions(): MINIMAP.ebv(0., 0., 0.) +@pytest.mark.skipif(not HAVE_FITS_READER, reason="no FITS reader") def test_input_options(): """Test unit and frame options."""