1"""Introduction
2^^^^^^^^^^^^
3
4:py:class:`GPy.plotting` effectively extends models based on
5:py:class:`GPy.core.gp.GP` (and other classes) by adding methods to
6plot useful charts. 'matplotlib', 'plotly' (online) and 'plotly'
7(offline) are supported. The methods in :py:class:`GPy.plotting` (and
8child classes :py:class:`GPy.plotting.gpy_plot` and
9:py:class:`GPy.plotting.matplot_dep`) are not intended to be called
10directly, but rather are 'injected' into other classes (notably
11:py:class:`GPy.core.gp.GP`). Documentation describing plots is best
12found associated with the model being plotted
13e.g. :py:class:`GPy.core.gp.GP.plot_confidence`.
14
15"""
16
17# Copyright (c) 2014, GPy authors (see AUTHORS.txt).
18# Licensed under the BSD 3-clause license (see LICENSE.txt)
19current_lib = [None]
20
21supported_libraries = ['matplotlib', 'plotly', 'plotly_online', 'plotly_offline', 'none']
22error_suggestion = "Please make sure you specify your plotting library in your configuration file (<User>/.config/GPy/user.cfg).\n\n[plotting]\nlibrary = <library>\n\nCurrently supported libraries: {}".format(", ".join(supported_libraries))
23
24def change_plotting_library(lib, **kwargs):
25    try:
26        #===========================================================================
27        # Load in your plotting library here and
28        # save it under the name plotting_library!
29        # This is hooking the library in
30        # for the usage in GPy:
31        if lib not in supported_libraries:
32            raise ValueError("Warning: Plotting library {} not recognized, currently supported libraries are: \n {}".format(lib, ", ".join(supported_libraries)))
33        if lib == 'matplotlib':
34            import matplotlib
35            from .matplot_dep.plot_definitions import MatplotlibPlots
36            from .matplot_dep import visualize, mapping_plots, priors_plots, ssgplvm, svig_plots, variational_plots, img_plots
37            current_lib[0] = MatplotlibPlots()
38        if lib in ['plotly', 'plotly_online']:
39            import plotly
40            from .plotly_dep.plot_definitions import PlotlyPlotsOnline
41            current_lib[0] = PlotlyPlotsOnline(**kwargs)
42        if lib == 'plotly_offline':
43            import plotly
44            from .plotly_dep.plot_definitions import PlotlyPlotsOffline
45            current_lib[0] = PlotlyPlotsOffline(**kwargs)
46        if lib == 'none':
47            current_lib[0] = None
48        inject_plotting()
49        #===========================================================================
50    except (ImportError, NameError):
51        config.set('plotting', 'library', 'none')
52        raise
53        import warnings
54        warnings.warn(ImportWarning("You spevified {} in your configuration, but is not available. Install newest version of {} for plotting".format(lib, lib)))
55
56def inject_plotting():
57    if current_lib[0] is not None:
58        # Inject the plots into classes here:
59
60        # Already converted to new style:
61        from . import gpy_plot
62
63        from ..core import GP
64        GP.plot_data = gpy_plot.data_plots.plot_data
65        GP.plot_data_error = gpy_plot.data_plots.plot_data_error
66        GP.plot_errorbars_trainset = gpy_plot.data_plots.plot_errorbars_trainset
67        GP.plot_mean = gpy_plot.gp_plots.plot_mean
68        GP.plot_confidence = gpy_plot.gp_plots.plot_confidence
69        GP.plot_density = gpy_plot.gp_plots.plot_density
70        GP.plot_samples = gpy_plot.gp_plots.plot_samples
71        GP.plot = gpy_plot.gp_plots.plot
72        GP.plot_f = gpy_plot.gp_plots.plot_f
73        GP.plot_latent = gpy_plot.gp_plots.plot_f
74        GP.plot_noiseless = gpy_plot.gp_plots.plot_f
75        GP.plot_magnification = gpy_plot.latent_plots.plot_magnification
76
77        from ..models import StateSpace
78        StateSpace.plot_data = gpy_plot.data_plots.plot_data
79        StateSpace.plot_data_error = gpy_plot.data_plots.plot_data_error
80        StateSpace.plot_errorbars_trainset = gpy_plot.data_plots.plot_errorbars_trainset
81        StateSpace.plot_mean = gpy_plot.gp_plots.plot_mean
82        StateSpace.plot_confidence = gpy_plot.gp_plots.plot_confidence
83        StateSpace.plot_density = gpy_plot.gp_plots.plot_density
84        StateSpace.plot_samples = gpy_plot.gp_plots.plot_samples
85        StateSpace.plot = gpy_plot.gp_plots.plot
86        StateSpace.plot_f = gpy_plot.gp_plots.plot_f
87        StateSpace.plot_latent = gpy_plot.gp_plots.plot_f
88        StateSpace.plot_noiseless = gpy_plot.gp_plots.plot_f
89
90        from ..core import SparseGP
91        SparseGP.plot_inducing = gpy_plot.data_plots.plot_inducing
92
93        from ..models import GPLVM, BayesianGPLVM, bayesian_gplvm_minibatch, SSGPLVM, SSMRD
94        GPLVM.plot_latent = gpy_plot.latent_plots.plot_latent
95        GPLVM.plot_scatter = gpy_plot.latent_plots.plot_latent_scatter
96        GPLVM.plot_inducing = gpy_plot.latent_plots.plot_latent_inducing
97        GPLVM.plot_steepest_gradient_map = gpy_plot.latent_plots.plot_steepest_gradient_map
98        BayesianGPLVM.plot_latent = gpy_plot.latent_plots.plot_latent
99        BayesianGPLVM.plot_scatter = gpy_plot.latent_plots.plot_latent_scatter
100        BayesianGPLVM.plot_inducing = gpy_plot.latent_plots.plot_latent_inducing
101        BayesianGPLVM.plot_steepest_gradient_map = gpy_plot.latent_plots.plot_steepest_gradient_map
102        bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch.plot_latent = gpy_plot.latent_plots.plot_latent
103        bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch.plot_scatter = gpy_plot.latent_plots.plot_latent_scatter
104        bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch.plot_inducing = gpy_plot.latent_plots.plot_latent_inducing
105        bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch.plot_steepest_gradient_map = gpy_plot.latent_plots.plot_steepest_gradient_map
106        SSGPLVM.plot_latent = gpy_plot.latent_plots.plot_latent
107        SSGPLVM.plot_scatter = gpy_plot.latent_plots.plot_latent_scatter
108        SSGPLVM.plot_inducing = gpy_plot.latent_plots.plot_latent_inducing
109        SSGPLVM.plot_steepest_gradient_map = gpy_plot.latent_plots.plot_steepest_gradient_map
110
111        from ..models import TPRegression
112        TPRegression.plot_data = gpy_plot.data_plots.plot_data
113        TPRegression.plot = gpy_plot.gp_plots.plot
114        TPRegression.plot_data_error = gpy_plot.data_plots.plot_data_error
115        TPRegression.plot_errorbars_trainset = gpy_plot.data_plots.plot_errorbars_trainset
116        TPRegression.plot_mean = gpy_plot.gp_plots.plot_mean
117        TPRegression.plot_confidence = gpy_plot.gp_plots.plot_confidence
118        TPRegression.plot_density = gpy_plot.gp_plots.plot_density
119        TPRegression.plot_samples = gpy_plot.gp_plots.plot_samples
120        TPRegression.plot_f = gpy_plot.gp_plots.plot_f
121        TPRegression.plot_latent = gpy_plot.gp_plots.plot_f
122        TPRegression.plot_noiseless = gpy_plot.gp_plots.plot_f
123
124        from ..kern import Kern
125        Kern.plot_covariance = gpy_plot.kernel_plots.plot_covariance
126        def deprecate_plot(self, *args, **kwargs):
127            import warnings
128            warnings.warn(DeprecationWarning('Kern.plot is being deprecated and will not be available in the 1.0 release. Use Kern.plot_covariance instead'))
129            return self.plot_covariance(*args, **kwargs)
130        Kern.plot = deprecate_plot
131        Kern.plot_ARD = gpy_plot.kernel_plots.plot_ARD
132
133        from ..inference.optimization import Optimizer
134        Optimizer.plot = gpy_plot.inference_plots.plot_optimizer
135        # Variational plot!
136
137def plotting_library():
138    if current_lib[0] is None:
139        raise RuntimeError("No plotting library was loaded. \n{}".format(error_suggestion))
140    return current_lib[0]
141
142def show(figure, **kwargs):
143    """
144    Show the specific plotting library figure, returned by
145    add_to_canvas().
146
147    kwargs are the plotting library specific options
148    for showing/drawing a figure.
149    """
150    return plotting_library().show_canvas(figure, **kwargs)
151
152
153from ..util.config import config, NoOptionError
154try:
155    lib = config.get('plotting', 'library')
156    change_plotting_library(lib)
157except NoOptionError:
158    print("No plotting library was specified in config file. \n{}".format(error_suggestion))
159