Commit 4b071243 authored by Leo Pound Singer's avatar Leo Pound Singer

Port image tests to pytest and reinstate them

Original: e1367d086c6c2c09104a4f411fb60ef8c912097c
parent 6e8b8e1a
......@@ -83,6 +83,7 @@ src/stamp-h1
src/stamp-h2
src/version.c
swig/swiglal*
test/.cache
test/LALInferenceGenerateROQTest
test/LALInferenceHDF5Test
test/LALInferenceInjectionTest
......@@ -101,5 +102,4 @@ test/test_bayestar_realize_coincs.xml
test/test_bayestar_sample_model_psd.xml
test/test_bayestar_sim_to_tmpltbank.xml
test/test_cubic_interp
test/test_result_images
test/test_vot.xml
......@@ -30,6 +30,7 @@ AC_CONFIG_FILES([ \
src/version.c \
swig/Makefile \
test/Makefile \
test/baseline/Makefile
])
AM_INIT_AUTOMAKE([ \
1.11 \
......
EXTRA_DIST =
include $(top_srcdir)/gnuscripts/lalsuite_test.am
SUBDIRS = baseline
# Add compiled test programs to this variable
test_programs += LALInferenceTest
test_programs += LALInferencePriorTest
......@@ -16,7 +18,7 @@ test_programs += test_cubic_interp
# Disable test_multiband.sh for now
#test_scripts = test_multiband.sh
if SWIG_BUILD_PYTHON
# test_scripts += test_plot.py
test_scripts += test_plot.py
test_scripts += test_detframe.py
if HAVE_CHEALPIX
test_scripts += doctests.py
......@@ -59,15 +61,9 @@ MOSTLYCLEANFILES = \
test_bayestar_prune_neighborhood_tmpltbank.xml \
test_bayestar_littlehope.xml \
test_bayestar_lattice_tmpltbank.xml \
test_result_images/pp_plot_default.png \
test_result_images/pp_plot_lines.png \
test_result_images/pp_plot_steps.png \
$(END_OF_LIST)
EXTRA_DIST += \
LALInferenceTest.h \
baseline_images/pp_plot_default.png \
baseline_images/pp_plot_lines.png \
baseline_images/pp_plot_steps.png \
HL-INJECTIONS_1_TEST-1000000000-10.xml \
$(END_OF_LIST)
EXTRA_DIST = \
test_astro_mollweide_axes.png \
test_mollweide_axes.png \
test_pp_plot_default.png \
test_pp_plot_lines.png \
test_pp_plot_steps.png \
$(END_OF_LIST)
from __future__ import division
from __future__ import print_function
import matplotlib
matplotlib.rcdefaults()
matplotlib.use('agg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cbook
from matplotlib.testing.compare import compare_images
import functools
import unittest
import os
import sys
from pkg_resources import require, DistributionNotFound, VersionConflict
try:
from astropy.tests.helper import pytest
require('pytest_mpl')
except (ImportError, DistributionNotFound, VersionConflict):
print('these tests require pytest and pytest-mpl', file=sys.stderr)
raise SystemExit(77)
import lalinference.plot
def image_comparison(testfunc, filename=None, tolerance=1):
# Construct paths to baseline and result image directories.
filedir = os.path.dirname(os.path.abspath(__file__))
baseline_dir = os.path.join(filedir, 'baseline_images')
result_dir = os.path.join(os.getcwd(), 'test_result_images')
# Default test result filename: test function name, stripped of the
# 'test_' prefix, and with '.png' appended
if filename is None:
filename = testfunc.__name__.replace('test_', '') + '.png'
# Construct full paths to baseline and test images.
baseline_path = os.path.join(baseline_dir, filename)
result_path = os.path.join(result_dir, filename)
@functools.wraps(testfunc)
def test(*args, **kwargs):
# Run test function
fig = testfunc(*args, **kwargs)
# Create directories if needed
cbook.mkdirs(baseline_dir)
cbook.mkdirs(result_dir)
if os.path.exists(baseline_path):
fig.savefig(result_path)
msg = compare_images(baseline_path, result_path, tolerance)
if msg is not None:
raise AssertionError(msg)
else:
fig.savefig(baseline_path)
raise unittest.SkipTest(
"Generated baseline image, {0}".format(baseline_path))
return test
class TestPPPlot(unittest.TestCase):
def setUp(self):
# Re-initialize the random seed to make the unit test repeatable
np.random.seed(0)
self.fig = plt.figure(figsize=(3, 3), dpi=72)
self.ax = self.fig.add_subplot(111, projection='pp_plot')
# self.ax = lalinference.plot.PPPlot(self.fig, [0.1, 0.1, 0.8, 0.8])
# self.fig.add_axes(self.ax)
self.p_values = np.arange(1, 20) / 20
@image_comparison
def test_pp_plot_steps(self):
"""Test P--P plot with drawstyle='steps'."""
self.ax.add_confidence_band(len(self.p_values))
self.ax.add_diagonal()
self.ax.add_lightning(len(self.p_values), 20, drawstyle='steps')
self.ax.add_series(self.p_values, drawstyle='steps')
return self.fig
@image_comparison
def test_pp_plot_lines(self):
"""Test P--P plot with drawstyle='steps'."""
self.ax.add_confidence_band(len(self.p_values))
self.ax.add_diagonal()
self.ax.add_lightning(len(self.p_values), 20, drawstyle='lines')
self.ax.add_series(self.p_values, drawstyle='lines')
self.ax.add_diagonal()
return self.fig
@image_comparison
def test_pp_plot_default(self):
"""Test P--P plot with drawstyle='steps'."""
self.ax.add_confidence_band(len(self.p_values))
self.ax.add_diagonal()
self.ax.add_lightning(len(self.p_values), 20)
self.ax.add_series(self.p_values)
return self.fig
class TestMollweideAxes(unittest.TestCase):
@image_comparison
def test_mollweide_axes(self):
"""Test HEALPix heat map on 'mollweide' axes"""
fig = plt.figure(figsize=(6, 4), dpi=72)
ax = fig.add_subplot(111, projection='mollweide')
lalinference.plot.healpix_heatmap(np.arange(12), nest=True)
ax.grid()
return fig
@image_comparison
def test_astro_mollweide_axes(self):
"""Test HEALPix heat map on 'astro hours mollweide' axes"""
fig = plt.figure(figsize=(6, 4), dpi=72)
ax = fig.add_subplot(111, projection='astro hours mollweide')
lalinference.plot.healpix_heatmap(np.arange(12), nest=True)
ax.grid()
return fig
def pp_plot():
# Re-initialize the random seed to make the unit test repeatable
np.random.seed(0)
fig = plt.figure(figsize=(3, 3), dpi=72)
ax = fig.add_subplot(111, projection='pp_plot')
p_values = np.arange(1, 20) / 20
return fig, ax, p_values
@pytest.mark.mpl_image_compare
def test_pp_plot_steps():
"""Test P--P plot with drawstyle='steps'."""
fig, ax, p_values = pp_plot()
ax.add_confidence_band(len(p_values))
ax.add_diagonal()
ax.add_lightning(len(p_values), 20, drawstyle='steps')
ax.add_series(p_values, drawstyle='steps')
return fig
@pytest.mark.mpl_image_compare
def test_pp_plot_lines():
"""Test P--P plot with drawstyle='steps'."""
fig, ax, p_values = pp_plot()
ax.add_confidence_band(len(p_values))
ax.add_diagonal()
ax.add_lightning(len(p_values), 20, drawstyle='lines')
ax.add_series(p_values, drawstyle='lines')
ax.add_diagonal()
return fig
@pytest.mark.mpl_image_compare
def test_pp_plot_default():
"""Test P--P plot with drawstyle='steps'."""
fig, ax, p_values = pp_plot()
ax.add_confidence_band(len(p_values))
ax.add_diagonal()
ax.add_lightning(len(p_values), 20)
ax.add_series(p_values)
return fig
@pytest.mark.mpl_image_compare
def test_mollweide_axes():
"""Test HEALPix heat map on 'mollweide' axes"""
fig = plt.figure(figsize=(6, 4), dpi=72)
ax = fig.add_subplot(111, projection='mollweide')
lalinference.plot.healpix_heatmap(np.arange(12), nest=True)
return fig
@pytest.mark.mpl_image_compare
def test_astro_mollweide_axes():
"""Test HEALPix heat map on 'astro hours mollweide' axes"""
fig = plt.figure(figsize=(6, 4), dpi=72)
ax = fig.add_subplot(111, projection='astro hours mollweide')
lalinference.plot.healpix_heatmap(np.arange(12), nest=True)
return fig
if __name__ == '__main__':
import unittest
unittest.main()
raise SystemExit(pytest.main(['-v', '--mpl', __file__]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment