Skip to content

Commit

Permalink
allow import with no fits reader installed
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarbary committed May 13, 2017
1 parent 64ea213 commit 50c0ae2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions sfdmap.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -13,15 +15,20 @@
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"


def _isiterable(obj):
"""Returns `True` if the given object is iterable."""

try:
iter(obj)
return True
Expand Down
35 changes: 28 additions & 7 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""

Expand All @@ -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])
Expand All @@ -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"""

Expand All @@ -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"""
Expand All @@ -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',
Expand All @@ -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.
Expand All @@ -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"""

Expand All @@ -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."""

Expand Down

0 comments on commit 50c0ae2

Please sign in to comment.