GPy/GPy/testing/plotting_tests.py

177 lines
7.3 KiB
Python
Raw Normal View History

#===============================================================================
# Copyright (c) 2015, Max Zwiessele
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of GPy nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================
import numpy as np
import GPy, os
from nose import SkipTest
2015-10-04 23:41:06 +01:00
from matplotlib.testing.compare import compare_images
2015-10-05 14:10:06 +01:00
from matplotlib.testing.noseclasses import ImageComparisonFailure
try:
from matplotlib import cbook, pyplot as plt
import matplotlib
matplotlib.rcParams['text.usetex'] = False
except:
raise SkipTest("Matplotlib not installed, not testing plots")
extensions = ['png']
2015-10-05 14:10:06 +01:00
def _image_directories():
"""
Compute the baseline and result image directories for testing *func*.
Create the result directory if it doesn't exist.
"""
2015-10-05 14:10:06 +01:00
basedir = os.path.splitext(os.path.relpath(os.path.abspath(__file__)))[0]
#module_name = __init__.__module__
#mods = module_name.split('.')
#basedir = os.path.join(*mods)
result_dir = os.path.join(basedir, 'testresult')
baseline_dir = os.path.join(basedir, 'baseline')
if not os.path.exists(result_dir):
cbook.mkdirs(result_dir)
return baseline_dir, result_dir
2015-10-05 14:10:06 +01:00
def _sequenceEqual(a, b):
assert len(a) == len(b), "Sequences not same length"
for i, [x, y], in enumerate(zip(a, b)):
assert x == y, "element not matching {}".format(i)
2015-10-05 14:10:06 +01:00
def _notFound(path):
raise IOError('File {} not in baseline')
2015-10-05 14:10:06 +01:00
def _image_comparison(baseline_images, extensions=['pdf','svg','ong'], tol=1e-3):
baseline_dir, result_dir = _image_directories()
for num, base in zip(plt.get_fignums(), baseline_images):
for ext in extensions:
fig = plt.figure(num)
fig.axes[0].set_axis_off()
fig.set_frameon(False)
fig.canvas.draw()
fig.savefig(os.path.join(result_dir, "{}.{}".format(base, ext)))
2015-10-05 19:09:17 +01:00
plt.close('all')
2015-10-05 19:08:38 +01:00
for num, base in zip(plt.get_fignums(), baseline_images):
for ext in extensions:
#plt.close(num)
actual = os.path.join(result_dir, "{}.{}".format(base, ext))
expected = os.path.join(baseline_dir, "{}.{}".format(base, ext))
def do_test():
err = compare_images(expected, actual, tol)
try:
if not os.path.exists(expected):
raise ImageComparisonFailure(
'image does not exist: %s' % expected)
if err:
raise ImageComparisonFailure(
'images not close: {err[actual]!s} vs. {err[expected]!s} (RMS {err[rms]:.3f})'.format(err=err))
except ImageComparisonFailure:
pass
yield do_test
2015-10-05 14:10:06 +01:00
def test_plot(self=None):
np.random.seed(11111)
X = np.random.uniform(0, 1, (40, 1))
f = .2 * np.sin(1.3*X) + 1.3*np.cos(2*X)
Y = f+np.random.normal(0, .1, f.shape)
m = GPy.models.GPRegression(X, Y)
m.optimize()
m.plot_data()
m.plot_mean()
m.plot_confidence()
m.plot_density()
m.plot_errorbars_trainset()
2015-10-05 14:10:06 +01:00
m.plot_samples()
for do_test in _image_comparison(baseline_images=['gp_{}'.format(sub) for sub in ["data", "mean", 'conf', 'density', 'error', 'samples']], extensions=extensions):
yield (do_test, )
2015-10-05 14:10:06 +01:00
def test_plot_sparse(self=None):
np.random.seed(11111)
2015-10-05 14:10:06 +01:00
X = np.random.uniform(-1, 1, (40, 1))
f = .2 * np.sin(1.3*X) + 1.3*np.cos(2*X)
Y = f+np.random.normal(0, .1, f.shape)
m = GPy.models.SparseGPRegression(X, Y)
m.optimize()
m.plot_inducing()
2015-10-05 14:10:06 +01:00
for do_test in _image_comparison(baseline_images=['sparse_gp_{}'.format(sub) for sub in ['inducing']], extensions=extensions):
yield (do_test, )
2015-10-05 14:10:06 +01:00
def test_plot_classification(self=None):
np.random.seed(11111)
X = np.random.uniform(0, 1, (40, 1))
f = .2 * np.sin(1.3*X) + 1.3*np.cos(2*X)
Y = f+np.random.normal(0, .1, f.shape)
m = GPy.models.GPClassification(X, Y>Y.mean())
m.optimize()
m.plot()
m.plot(plot_raw=True)
m.plot(plot_raw=False, apply_link=True)
m.plot(plot_raw=True, apply_link=True)
2015-10-05 14:10:06 +01:00
for do_test in _image_comparison(baseline_images=['gp_class_{}'.format(sub) for sub in ["", "raw", 'link', 'raw_link']], extensions=extensions):
yield (do_test, )
2015-10-04 00:55:22 +01:00
2015-10-05 14:10:06 +01:00
def test_plot_sparse_classification(self=None):
2015-10-04 00:55:22 +01:00
np.random.seed(11111)
X = np.random.uniform(0, 1, (40, 1))
f = .2 * np.sin(1.3*X) + 1.3*np.cos(2*X)
Y = f+np.random.normal(0, .1, f.shape)
m = GPy.models.SparseGPClassification(X, Y>Y.mean())
2015-10-04 00:55:22 +01:00
m.optimize()
m.plot()
m.plot(plot_raw=True)
m.plot(plot_raw=False, apply_link=True)
m.plot(plot_raw=True, apply_link=True)
2015-10-05 14:10:06 +01:00
for do_test in _image_comparison(baseline_images=['sparse_gp_class_{}'.format(sub) for sub in ["", "raw", 'link', 'raw_link']], extensions=extensions):
yield (do_test, )
def test_gplvm_plot(self=None):
from ..examples.dimensionality_reduction import _simulate_matern
from ..kern import RBF
from ..models import GPLVM
Q = 3
_, _, Ylist = _simulate_matern(5, 1, 1, 100, num_inducing=5, plot_sim=False)
Y = Ylist[0]
k = RBF(Q, ARD=True) # + kern.white(Q, _np.exp(-2)) # + kern.bias(Q)
# k = kern.RBF(Q, ARD=True, lengthscale=10.)
m = GPLVM(Y, Q, init="PCA", kernel=k)
m.likelihood.variance = .1
m.optimize(messages=0)
labels = np.random.multinomial(1, np.random.dirichlet([.3333333, .3333333, .3333333]), size=(m.Y.shape[0])).nonzero()[1]
m.plot_prediction_fit(which_data_ycols=(0,1)) # ignore this test, as plotting is not consistent!!
plt.close('all')
m.plot_latent()
m.plot_magnification(labels=labels)
2015-10-05 18:47:54 +01:00
m.plot_steepest_gradient_map(resolution=5)
for do_test in _image_comparison(baseline_images=['gplvm_{}'.format(sub) for sub in ["latent", "magnification", 'gradient']], extensions=extensions):
2015-10-05 14:10:06 +01:00
yield (do_test, )
if __name__ == '__main__':
import nose
nose.main()