Skip to content

Commit

Permalink
Merge pull request #928 from lawrennd/devel
Browse files Browse the repository at this point in the history
Make examples files check if matplotlib is available.
  • Loading branch information
lawrennd authored May 29, 2021
2 parents 4089a66 + 06f6071 commit 111727e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 118 deletions.
35 changes: 15 additions & 20 deletions GPy/examples/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"""
Gaussian Processes classification examples
"""
MPL_AVAILABLE = True
try:
import matplotlib.pyplot as plt
except ImportError:
MPL_AVAILABLE = False

import GPy

default_seed = 10000
Expand Down Expand Up @@ -78,9 +84,7 @@ def toy_linear_1d_classification(seed=default_seed, optimize=True, plot=True):
# m.pseudo_EM()

# Plot
if plot:
from matplotlib import pyplot as plt

if MPL_AVAILABLE and plot:
fig, axes = plt.subplots(2, 1)
m.plot_f(ax=axes[0])
m.plot(ax=axes[1])
Expand Down Expand Up @@ -117,15 +121,12 @@ def toy_linear_1d_classification_laplace(seed=default_seed, optimize=True, plot=

# Optimize
if optimize:
try:
m.optimize("scg", messages=1)
except Exception as e:
return m
m.optimize("scg", messages=True)

# Plot
if plot:
from matplotlib import pyplot as plt
return m

# Plot
if MPL_AVAILABLE and plot:
fig, axes = plt.subplots(2, 1)
m.plot_f(ax=axes[0])
m.plot(ax=axes[1])
Expand Down Expand Up @@ -162,9 +163,7 @@ def sparse_toy_linear_1d_classification(
m.optimize()

# Plot
if plot:
from matplotlib import pyplot as plt

if MPL_AVAILABLE and plot:
fig, axes = plt.subplots(2, 1)
m.plot_f(ax=axes[0])
m.plot(ax=axes[1])
Expand Down Expand Up @@ -207,9 +206,7 @@ def sparse_toy_linear_1d_classification_uncertain_input(
m.optimize()

# Plot
if plot:
from matplotlib import pyplot as plt

if MPL_AVAILABLE and plot:
fig, axes = plt.subplots(2, 1)
m.plot_f(ax=axes[0])
m.plot(ax=axes[1])
Expand Down Expand Up @@ -259,9 +256,7 @@ def toy_heaviside(seed=default_seed, max_iters=100, optimize=True, plot=True):
print(m)

# Plot
if plot:
from matplotlib import pyplot as plt

if MPL_AVAILABLE and plot:
fig, axes = plt.subplots(2, 1)
m.plot_f(ax=axes[0])
m.plot(ax=axes[1])
Expand Down Expand Up @@ -314,7 +309,7 @@ def crescent_data(
if optimize:
m.optimize(messages=1)

if plot:
if MPL_AVAILABLE and plot:
m.plot()

print(m)
Expand Down
19 changes: 12 additions & 7 deletions GPy/examples/non_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import GPy
import numpy as np
from GPy.util import datasets

MPL_AVAILABLE = True
try:
import matplotlib.pyplot as plt
except:
pass
except ImportError:
MPL_AVAILABLE = False


def student_t_approx(optimize=True, plot=True):
Expand Down Expand Up @@ -102,7 +102,7 @@ def student_t_approx(optimize=True, plot=True):
print("Corrupt student t")
m4.optimize(optimizer, messages=1)

if plot:
if MPL_AVAILABLE and plot:
plt.figure(1)
plt.suptitle("Gaussian likelihood")
ax = plt.subplot(211)
Expand Down Expand Up @@ -141,7 +141,12 @@ def boston_example(optimize=True, plot=True):

optimizer = "bfgs"
messages = 0
data = datasets.boston_housing()
try:
import pods
except ImportError:
print("pods unavailable, see https://github.com/sods/ods for example datasets")
return
data = pods.datasets.boston_housing()
degrees_freedoms = [3, 5, 8, 10]
X = data["X"].copy()
Y = data["Y"].copy()
Expand Down Expand Up @@ -251,7 +256,7 @@ def rmse(Y, Ystar):
print(pred_density)
print(mstu_t)

if plot:
if MPL_AVAILABLE and plot:
plt.figure()
plt.scatter(X_test[:, data_axis_plot], Y_test_pred[0])
plt.scatter(X_test[:, data_axis_plot], Y_test, c="r", marker="x")
Expand All @@ -270,7 +275,7 @@ def rmse(Y, Ystar):
print("Average scores: {}".format(np.mean(score_folds, 1)))
print("Average pred density: {}".format(np.mean(pred_density, 1)))

if plot:
if MPL_AVAILABLE and plot:
# Plotting
stu_t_legends = ["Student T, df={}".format(df) for df in degrees_freedoms]
legends = ["Baseline", "Gaussian", "Laplace Approx Gaussian"] + stu_t_legends
Expand Down
Loading

0 comments on commit 111727e

Please sign in to comment.