Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #41 from anizami/new-image-tests
Browse files Browse the repository at this point in the history
code for generating and comparing images
  • Loading branch information
astrofrog committed Jun 16, 2014
2 parents 8cad9b8 + 870dfd5 commit a8ecf2f
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 23 deletions.
26 changes: 13 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ matrix:
# development version of Astropy, which fixes some issues with
# coverage testing in affiliated packages.
- python: 2.7
env: ASTROPY_VERSION=development SETUP_CMD='test --coverage'
env: ASTROPY_VERSION=development SETUP_CMD='test --coverage --remote-data'

# Check for sphinx doc build warnings - we do this first because it
# may run for a long time
Expand All @@ -34,27 +34,27 @@ matrix:

# Try Astropy development version
- python: 2.7
env: ASTROPY_VERSION=development SETUP_CMD='test'
env: ASTROPY_VERSION=development SETUP_CMD='test --remote-data'
- python: 3.3
env: ASTROPY_VERSION=development SETUP_CMD='test'
env: ASTROPY_VERSION=development SETUP_CMD='test --remote-data'

# Try all python versions with the latest numpy
- python: 2.6
env: SETUP_CMD='test'
env: SETUP_CMD='test --remote-data'
- python: 2.7
env: SETUP_CMD='test'
env: SETUP_CMD='test --remote-data'
- python: 3.3
env: SETUP_CMD='test'
env: SETUP_CMD='test --remote-data'
- python: 3.4
env: SETUP_CMD='test'
env: SETUP_CMD='test --remote-data'

# Try older numpy versions
- python: 2.7
env: NUMPY_VERSION=1.7 SETUP_CMD='test'
env: NUMPY_VERSION=1.7 SETUP_CMD='test --remote-data'
- python: 2.7
env: NUMPY_VERSION=1.6 SETUP_CMD='test'
env: NUMPY_VERSION=1.6 SETUP_CMD='test --remote-data'
- python: 2.7
env: NUMPY_VERSION=1.5 SETUP_CMD='test'
env: NUMPY_VERSION=1.5 SETUP_CMD='test --remote-data'

before_install:

Expand Down Expand Up @@ -93,7 +93,7 @@ install:
# conda for packages available through conda, or pip for any other
# packages. You should leave the `numpy=$NUMPY_VERSION` in the `conda`
# install since this ensures Numpy does not get automatically upgraded.
- if [[ $SETUP_CMD != egg_info ]]; then $CONDA_INSTALL numpy=$NUMPY_VERSION matplotlib ; fi
- if [[ $SETUP_CMD != egg_info ]]; then $CONDA_INSTALL numpy=$NUMPY_VERSION nose matplotlib ; fi
# - if [[ $SETUP_CMD != egg_info ]]; then $PIP_INSTALL ...; fi

# DOCUMENTATION DEPENDENCIES
Expand All @@ -103,11 +103,11 @@ install:
- if [[ $SETUP_CMD == build_sphinx* ]]; then $CONDA_INSTALL numpy=$NUMPY_VERSION Sphinx matplotlib; fi

# COVERAGE DEPENDENCIES
- if [[ $SETUP_CMD == 'test --coverage' ]]; then $PIP_INSTALL coverage coveralls; fi
- if [[ $SETUP_CMD == 'test --coverage --remote-data' ]]; then $PIP_INSTALL coverage coveralls; fi

script:
- python setup.py $SETUP_CMD

after_success:
# Uncomment line below if coveralls.io is set up for this package.
- if [[ $SETUP_CMD == 'test --coverage' ]]; then coveralls; fi
- if [[ $SETUP_CMD == 'test --coverage --remote-data' ]]; then coveralls; fi
18 changes: 16 additions & 2 deletions wcsaxes/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
# no matter how it is invoked within the source tree.

from astropy.tests.pytest_plugins import *
from astropy.tests.pytest_plugins import pytest_addoption as astropy_pytest_addoption

## Uncomment the following line to treat all DeprecationWarnings as
## exceptions
# Uncomment the following line to treat all DeprecationWarnings as
# exceptions
# enable_deprecations_as_exceptions()

import os
from astropy.tests.helper import pytest


def pytest_addoption(parser):
parser.addoption('--generate-reference', help="generate reference images", action="store_true")
return astropy_pytest_addoption(parser)


@pytest.fixture
def generate(request):
return request.config.getoption("--generate-reference")
4 changes: 2 additions & 2 deletions wcsaxes/formatter_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def formatter(self, values, spacing):
sep = None
else:
if unit == u.degree:
sep=(six.u('\xb0'), "'", '"')[:fields]
sep=(six.u('\xb0'), "'", '"')
else:
sep=('h', 'm', 's')[:fields]
sep=('h', 'm', 's')

angles = Angle(np.asarray(values), unit=u.deg)
string = angles.to_string(unit=unit,
Expand Down
4 changes: 2 additions & 2 deletions wcsaxes/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def transform(self, value):
for axis in self:
self[axis].transform = value

def update(self):
def update_limits(self):

xmin, xmax = self.parent_axes.get_xlim()
ymin, ymax = self.parent_axes.get_ylim()
Expand All @@ -110,7 +110,7 @@ def update(self):

def sample(self, n_samples):

self.update()
self.update_limits()

spines = OrderedDict()

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wcsaxes/tests/baseline_images/image_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wcsaxes/tests/baseline_images/ticks_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions wcsaxes/tests/setup_package.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
def get_package_data():
return {
_ASTROPY_PACKAGE_NAME_ + '.tests': ['coveragerc']}
return {'wcsaxes.tests': ['coveragerc', 'baseline_images/*.png']}
156 changes: 156 additions & 0 deletions wcsaxes/tests/test_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import pytest
import os
import numpy as np
from astropy import units as u
import matplotlib.pyplot as plt
from matplotlib.testing.compare import compare_images
from matplotlib.patches import Circle
from astropy.wcs import WCS
from astropy.io import fits
from wcsaxes import WCSAxes
from matplotlib import cbook
from astropy.tests.helper import pytest
from astropy.utils.data import download_file
from astropy.tests.helper import remote_data


class TestImages(object):

@classmethod
def setup_class(cls):
cls._filedir = os.path.abspath(__file__)
cls._basedir = os.path.split(cls._filedir)[0]
cls._baseline_images_dir = os.path.join(cls._basedir, 'baseline_images')
cls._result_dir = os.path.abspath(os.path.join(cls._basedir, 'test_result_images'))

if not os.path.exists(cls._result_dir):
cbook.mkdirs(cls._result_dir)

if not os.path.exists(cls._baseline_images_dir):
cbook.mkdirs(cls._baseline_images_dir)

cls._tolerance = 1

cls._image1 = download_file("http://astrofrog.github.io/wcsaxes-datasets/msx.fits", timeout=10)
cls._image2 = download_file("http://astrofrog.github.io/wcsaxes-datasets/rosat.fits", timeout=10)
cls._image3 = download_file("http://astrofrog.github.io/wcsaxes-datasets/2MASS_k.fits", timeout=10)
cls._data_cube = download_file("http://astrofrog.github.io/wcsaxes-datasets/L1448_13CO.fits", timeout=10)

# method to create baseline or test images
def generate_or_test(self, generate, figure, image, test_image=None, baseline_image=None):
baseline_image = os.path.abspath(os.path.join(self._baseline_images_dir, image))
test_image = os.path.abspath(os.path.join(self._result_dir, image))
if generate:
figure.savefig(baseline_image)
pytest.skip("Skipping test, since generating data")
else:
figure.savefig(test_image)
msg = compare_images(baseline_image, test_image, tol=self._tolerance)
assert msg is None

# Test for plotting image and also setting values of ticks
@remote_data
def test_image_plot(self, generate):
hdu = fits.open(self._image1)[0]
fig = plt.figure(figsize=(6, 6))
ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=WCS(hdu.header))
fig.add_axes(ax)
ax.imshow(hdu.data, vmin=-1e-5, vmax=1e-4, origin='lower')
ax.coords[0].set_ticks([-0.30, 0., 0.20] * u.degree)

self.generate_or_test(generate, fig, 'image_plot.png')

# Test for overlaying contours on images
@remote_data
def test_contour_overlay(self, generate):
hdu = fits.open(self._image3)[0]

hdu_msx = fits.open(self._image1)[0]
wcs_msx = WCS(hdu_msx.header)

fig = plt.figure(figsize=(6, 6))
ax = WCSAxes(fig, [0.15, 0.15, 0.8, 0.8], wcs=WCS(hdu.header))
fig.add_axes(ax)
ax.imshow(hdu.data, vmin=-100, vmax=3000, origin='lower')
# Overplot contour
ax.contour(hdu_msx.data, transform=ax.get_transform(wcs_msx), colors='orange', levels=[2.5e-5, 5e-5, 1.e-4])
ax.set_xlim(0., 720.)
ax.set_ylim(0., 720.)

self.generate_or_test(generate, fig, 'contour_overlay.png')

# Test for overlaying grid, changing format of ticks, setting spacing and number of ticks
@remote_data
def test_overlay_features_image(self, generate):
hdu = fits.open(self._image1)[0]
fig = plt.figure(figsize=(6, 6))
ax = WCSAxes(fig, [0.25, 0.25, 0.65, 0.65], wcs=WCS(hdu.header))
fig.add_axes(ax)
# Change the format of the ticks
ax.coords[0].set_major_formatter('dd:mm:ss')
ax.coords[1].set_major_formatter('dd:mm:ss.ssss')

# Overlay grid on image
ax.grid(color='red', alpha=1.0, lw=1, linestyle='dashed')

# Set the spacing of ticks on the 'glon' axis to 4 arcsec
ax.coords['glon'].set_ticks(spacing=4 * u.arcsec)
# Set the number of ticks on the 'glat' axis to 9
ax.coords['glat'].set_ticks(number=9)
# Set labels on axes
ax.coords['glon'].set_axislabel('Galactic Longitude')
ax.coords['glat'].set_axislabel('Galactic Latitude')

self.generate_or_test(generate, fig, 'overlay_features_image.png')

# Overlay curvilinear grid and patches on image
@remote_data
def test_curvilinear_grid_patches_image(self, generate):
hdu = fits.open(self._image2)[0]
fig = plt.figure(figsize=(8, 8))
ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=WCS(hdu.header))
fig.add_axes(ax)
ax.imshow(hdu.data, vmax=1000, origin='lower')
ax.grid(color='white', alpha=1.0, lw=1, linestyle='dashed')
p = Circle((300, 100), radius=40, ec='yellow', fc='none')
ax.add_patch(p)
p = Circle((30., 20.), radius=20., ec='orange', fc='none', transform=ax.get_transform('world'))
ax.add_patch(p)
p = Circle((60., 50.), radius=20., ec='red', fc='none', transform=ax.get_transform('fk5'))
ax.add_patch(p)

self.generate_or_test(generate, fig, 'curvlinear_grid_patches_image.png')

@remote_data
def test_cube_slice_image(self, generate):
image = fits.getdata(self._data_cube)
w = WCS(self._data_cube)
fig = plt.figure()
ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], w, slices=(50, 'y', 'x'))
fig.add_axes(ax)
ax.imshow(image[:, :, 100].transpose(), cmap=plt.cm.gist_heat)
ax.coords[2].set_axislabel('Velocity m/s')

self.generate_or_test(generate, fig, 'cube_slice_image.png')

# Test for axes and ticks sizes, labels etc
def test_ticks_labels(self, generate):
fig = plt.figure(figsize=(6, 6))
ax = WCSAxes(fig, [0.1, 0.1, 0.7, 0.7], wcs=None)
fig.add_axes(ax)
ax.set_xlim(-0.5, 2)
ax.set_ylim(-0.5, 2)
ax.coords[0].set_ticks(size=10, color='blue', alpha=0.2)
ax.coords[1].set_ticks(size=20, color='red', alpha=0.9)
ax.coords[0].set_ticks_position('all')
ax.coords[1].set_ticks_position('all')
ax.coords[0].set_axislabel('X-axis', size=20)
ax.coords[1].set_axislabel('Y-axis', color='green', size=25, weight='regular', style='normal', family='monospace')
ax.coords[0].set_axislabel_position('t')
ax.coords[1].set_axislabel_position('r')
ax.coords[0].set_ticklabel(color='purple', size=15, alpha=1, weight='light', style='normal', family='sans-serif')
ax.coords[1].set_ticklabel(color='black', size=18, alpha=0.9, weight='bold', family='serif')
ax.coords[0].set_ticklabel_position('all')
ax.coords[1].set_ticklabel_position('r')

self.generate_or_test(generate, fig, 'ticks_labels.png')
7 changes: 5 additions & 2 deletions wcsaxes/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# coordinates, but also world-to-world).

import abc

import astropy
import numpy as np
from matplotlib.path import Path
from matplotlib.transforms import Transform
Expand Down Expand Up @@ -184,7 +184,10 @@ def transform(self, input_coords):

c_out = c_in.transform_to(self.output_system)

return np.concatenate((c_out.lonangle.deg[:, np.newaxis], c_out.latangle.deg[:, np.newaxis]), 1)
if astropy.__version__ < '0.4':
return np.concatenate((c_out.lonangle.deg[:, np.newaxis], c_out.latangle.deg[:, np.newaxis]), 1)
else:
return np.concatenate((c_out.spherical.lon.deg[:, np.newaxis], c_out.spherical.lat.deg[:, np.newaxis]), 1)

transform_non_affine = transform

Expand Down

0 comments on commit a8ecf2f

Please sign in to comment.