pythonpython-2.7gpy

Cannot find plot function in GPy library (python)


I am using the GPy library in Python 2.7 to perform Gaussian Process regressions. I started by following the tutorial notebooks provided in the GitHub page.

Sample code :

import numpy as np
import matplotlib.pyplot as plt

f = lambda x : np.sin(x**2)

kernel = GPy.kern.RBF(input_dim=1, variance=1., lengthscale=1.)

X=np.random.rand(2,1)
Y=f(X)

m = GPy.models.GPRegression(X,Y,kernel)
m.optimize_restarts(num_restarts = 10,verbose=False)

fig=m.plot()
plt.show()

The weird thing I witnessed is that there is no plot function implemented in the GPRegression class (ok, it's just a small sub-class of GP.), nor in its super-class (GP), nor in its super-super-class (Model)... all located in GPy.core.

The plot function that is executed when I call m.plot() is in GPy.plotting.gpy_plot (which does not contain any class, but still uses the "self" keyword as function argument - but maybe it's just a "bad" name for a function argument ?).

I cannot see how a GPy.core.GP object can access this plot function (at first sight, there is no link whatsoever between the two python files - Ctrl+F "plot" in GPy/core/gp.py gives nothing for example).

When I call

vars(GPy.models.gp_regression.GP).keys()

, the plot function is indeed there, although not directly implemented in GPy.core.GP.

Same thing for : (Minimal reproducible example)

import GPy.core.gp
import GPy.likelihoods
import GPy.kern
import matplotlib.pyplot as plt

GPy.core.gp.GP.__dict__.keys()

Any idea of how GP calls the plot function in gpy_plot, and why it is coded this way ?


Solution

  • The plotting library gets "injected" in GPy/GPy/plotting/__init__.py's inject_plotting(). Here the line for plot():

        from ..core import GP
        ...
        GP.plot = gpy_plot.gp_plots.plot
    

    I assume the reason for this design was that it allows easily changing the plotting library on-the-fly via change_plotting_library().