From 6751515c1998a03c25fcb78fc9ef68b9e28c8496 Mon Sep 17 00:00:00 2001 From: Aditya Saini <49980787+adityasaini70@users.noreply.github.com> Date: Tue, 18 May 2021 22:44:58 +0530 Subject: [PATCH 01/16] Fix docstring for gp.py::GP.__init__() (#915) Removed indication in docstring that was incorrect: `kernel` is a required arg, but docstring stated it was optional. Now, docstring does not say it's optional. --- GPy/core/gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/core/gp.py b/GPy/core/gp.py index 66d62f62..0704c6a6 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -21,7 +21,7 @@ class GP(Model): :param X: input observations :param Y: output observations - :param kernel: a GPy kernel, defaults to rbf+white + :param kernel: a GPy kernel :param likelihood: a GPy likelihood :param inference_method: The :class:`~GPy.inference.latent_function_inference.LatentFunctionInference` inference method to use for this GP :rtype: model object From cc00de7d41f97ab352c5cc06324759c0b3fa21b0 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 17:38:21 +0100 Subject: [PATCH 02/16] Update visualize.py Due to this issue: https://github.com/matplotlib/matplotlib/issues/17172 setting aspect equal doesn't work on 3D axes. Removing for the moment. --- GPy/plotting/matplot_dep/visualize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/plotting/matplot_dep/visualize.py b/GPy/plotting/matplot_dep/visualize.py index d4442dc7..0c20933b 100644 --- a/GPy/plotting/matplot_dep/visualize.py +++ b/GPy/plotting/matplot_dep/visualize.py @@ -411,7 +411,7 @@ class mocap_data_show(matplotlib_show): def __init__(self, vals, axes=None, connect=None, color='b'): if axes==None: fig = plt.figure() - axes = fig.add_subplot(111, projection='3d', aspect='equal') + axes = fig.add_subplot(111, projection='3d') #, aspect='equal') aspect equal not implemented in 3D plots currently see this issue: https://github.com/matplotlib/matplotlib/issues/17172 super(mocap_data_show, self).__init__(vals, axes) self.color = color From f822caaf2c2cc8ef00f613756929b979f9a8df12 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 17:43:52 +0100 Subject: [PATCH 03/16] Remove == None and replace with is --- GPy/plotting/matplot_dep/visualize.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/GPy/plotting/matplot_dep/visualize.py b/GPy/plotting/matplot_dep/visualize.py index 0c20933b..26f62777 100644 --- a/GPy/plotting/matplot_dep/visualize.py +++ b/GPy/plotting/matplot_dep/visualize.py @@ -39,7 +39,7 @@ class vpython_show(data_show): super(vpython_show, self).__init__(vals) # If no axes are defined, create some. - if scene==None: + if scene is None: self.scene = visual.display(title='Data Visualization') else: self.scene = scene @@ -57,7 +57,7 @@ class matplotlib_show(data_show): super(matplotlib_show, self).__init__(vals) # If no axes are defined, create some. - if axes==None: + if axes is None: fig = plt.figure() self.axes = fig.add_subplot(111) else: @@ -190,7 +190,7 @@ class lvm_subplots(lvm): def __init__(self, vals, Model, data_visualize, latent_axes=None, sense_axes=None): self.nplots = int(np.ceil(Model.input_dim/2.))+1 assert len(latent_axes)==self.nplots - if vals==None: + if vals is None: vals = Model.X[0, :] self.latent_values = vals @@ -215,9 +215,9 @@ class lvm_dimselect(lvm): """ def __init__(self, vals, model, data_visualize, latent_axes=None, sense_axes=None, latent_index=[0, 1], labels=None): - if latent_axes==None and sense_axes==None: + if latent_axes is None and sense_axes is None: self.fig,(latent_axes,self.sense_axes) = plt.subplots(1,2) - elif sense_axes==None: + elif sense_axes is None: fig=plt.figure() self.sense_axes = fig.add_subplot(111) else: @@ -300,7 +300,7 @@ class image_show(matplotlib_show): self.set_image(self.vals) if not self.palette == []: # Can just show the image (self.set_image() took care of setting the palette) self.handle = self.axes.imshow(self.vals, interpolation='nearest') - elif cmap==None: # Use a jet map. + elif cmap is None: # Use a jet map. self.handle = self.axes.imshow(self.vals, cmap=plt.cm.jet, interpolation='nearest') # @UndefinedVariable else: # Use the selected map. self.handle = self.axes.imshow(self.vals, cmap=cmap, interpolation='nearest') # @UndefinedVariable @@ -368,7 +368,7 @@ class mocap_data_show_vpython(vpython_show): def draw_edges(self): self.rods = [] self.line_handle = [] - if not self.connect==None: + if self.connect is not None: self.I, self.J = np.nonzero(self.connect) for i, j in zip(self.I, self.J): pos, axis = self.pos_axis(i, j) @@ -380,7 +380,7 @@ class mocap_data_show_vpython(vpython_show): def modify_edges(self): self.line_handle = [] - if not self.connect==None: + if self.connect is not None: self.I, self.J = np.nonzero(self.connect) for rod, i, j in zip(self.rods, self.I, self.J): rod.pos, rod.axis = self.pos_axis(i, j) @@ -409,7 +409,7 @@ class mocap_data_show(matplotlib_show): """Base class for visualizing motion capture data.""" def __init__(self, vals, axes=None, connect=None, color='b'): - if axes==None: + if axes is None: fig = plt.figure() axes = fig.add_subplot(111, projection='3d') #, aspect='equal') aspect equal not implemented in 3D plots currently see this issue: https://github.com/matplotlib/matplotlib/issues/17172 super(mocap_data_show, self).__init__(vals, axes) @@ -428,7 +428,7 @@ class mocap_data_show(matplotlib_show): def draw_edges(self): self.line_handle = [] - if not self.connect==None: + if self.connect is not None: x = [] y = [] z = [] From 4dfc22277f27e4b0236721655e7dd82ebd1d96c2 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 17:51:00 +0100 Subject: [PATCH 04/16] Give latent axes proper key. --- GPy/plotting/matplot_dep/visualize.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GPy/plotting/matplot_dep/visualize.py b/GPy/plotting/matplot_dep/visualize.py index 26f62777..9895131b 100644 --- a/GPy/plotting/matplot_dep/visualize.py +++ b/GPy/plotting/matplot_dep/visualize.py @@ -111,11 +111,11 @@ class lvm(matplotlib_show): self.cid = latent_axes.figure.canvas.mpl_connect('axes_leave_event', self.on_leave) self.cid = latent_axes.figure.canvas.mpl_connect('axes_enter_event', self.on_enter) else: - self.cid = latent_axes[0].figure.canvas.mpl_connect('button_press_event', self.on_click) + self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('button_press_event', self.on_click) if not disable_drag: - self.cid = latent_axes[0].figure.canvas.mpl_connect('motion_notify_event', self.on_move) - self.cid = latent_axes[0].figure.canvas.mpl_connect('axes_leave_event', self.on_leave) - self.cid = latent_axes[0].figure.canvas.mpl_connect('axes_enter_event', self.on_enter) + self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('motion_notify_event', self.on_move) + self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('axes_leave_event', self.on_leave) + self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('axes_enter_event', self.on_enter) self.data_visualize = data_visualize self.model = model From 20750d7a5ee6bdf8276ecee63717831d03cb6264 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 20:15:17 +0100 Subject: [PATCH 05/16] Fix cmu_mocap demo. --- GPy/examples/dimensionality_reduction.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index fe84df72..ec5dbf01 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -78,7 +78,8 @@ def gplvm_oil_100(optimize=True, verbose=1, plot=True): m = GPy.models.GPLVM(Y, 6, kernel=kernel) m.data_labels = data['Y'].argmax(axis=1) if optimize: m.optimize('scg', messages=verbose) - if plot: m.plot_latent(labels=m.data_labels) + if plot: + m.plot_latent(labels=m.data_labels) return m def sparse_gplvm_oil(optimize=True, verbose=0, plot=True, N=100, Q=6, num_inducing=15, max_iters=50): @@ -689,7 +690,9 @@ def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose if optimize: m.optimize(messages=verbose, max_f_eval=10000) if plot: - ax = m.plot_latent() + fig, (latent_axes, sense_axes) = plt.subplots(1, 2) + m.plot_latent(ax=latent_axes) + ax = gca y = m.Y[0, :] data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel']) lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=ax) From 9686a58b35181236495863f9026936a4a8b8084b Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 20:18:23 +0100 Subject: [PATCH 06/16] Fix cmu_mocap demo. --- GPy/examples/dimensionality_reduction.py | 1 + 1 file changed, 1 insertion(+) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index ec5dbf01..f0639ca3 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -676,6 +676,7 @@ def stick_bgplvm(model=None, optimize=True, verbose=True, plot=True): def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose=True, plot=True): + import matplotlib.pyplot as plt import GPy import pods From 67834da1950819c23a68b06dfaea1e493f2a781e Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 20:22:22 +0100 Subject: [PATCH 07/16] Fix cmu_mocap demo. --- GPy/examples/dimensionality_reduction.py | 1 - 1 file changed, 1 deletion(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index f0639ca3..c66f2f92 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -693,7 +693,6 @@ def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose if plot: fig, (latent_axes, sense_axes) = plt.subplots(1, 2) m.plot_latent(ax=latent_axes) - ax = gca y = m.Y[0, :] data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel']) lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=ax) From fbe0a09506b1eb7df6f9905dafa2c1e1c89aff90 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 20:24:18 +0100 Subject: [PATCH 08/16] Fix cmu_mocap demo. --- GPy/plotting/matplot_dep/visualize.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GPy/plotting/matplot_dep/visualize.py b/GPy/plotting/matplot_dep/visualize.py index 9895131b..26f62777 100644 --- a/GPy/plotting/matplot_dep/visualize.py +++ b/GPy/plotting/matplot_dep/visualize.py @@ -111,11 +111,11 @@ class lvm(matplotlib_show): self.cid = latent_axes.figure.canvas.mpl_connect('axes_leave_event', self.on_leave) self.cid = latent_axes.figure.canvas.mpl_connect('axes_enter_event', self.on_enter) else: - self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('button_press_event', self.on_click) + self.cid = latent_axes[0].figure.canvas.mpl_connect('button_press_event', self.on_click) if not disable_drag: - self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('motion_notify_event', self.on_move) - self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('axes_leave_event', self.on_leave) - self.cid = latent_axes['scatter'].figure.canvas.mpl_connect('axes_enter_event', self.on_enter) + self.cid = latent_axes[0].figure.canvas.mpl_connect('motion_notify_event', self.on_move) + self.cid = latent_axes[0].figure.canvas.mpl_connect('axes_leave_event', self.on_leave) + self.cid = latent_axes[0].figure.canvas.mpl_connect('axes_enter_event', self.on_enter) self.data_visualize = data_visualize self.model = model From 8b098ec59bafb7e8db0c0f0925d6bb567e59f9f7 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Tue, 18 May 2021 20:27:16 +0100 Subject: [PATCH 09/16] Fix cmu_mocap demo. --- GPy/examples/dimensionality_reduction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index c66f2f92..634f9790 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -695,7 +695,7 @@ def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose m.plot_latent(ax=latent_axes) y = m.Y[0, :] data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel']) - lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=ax) + lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=latent_axes) input('Press enter to finish') lvm_visualizer.close() data_show.close() From 5c71aa45c7be64c134eb544428feca3425bf6414 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 09:45:21 +0100 Subject: [PATCH 10/16] Update GPLVM class to use metadata and output normalizers. --- GPy/models/gplvm.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/GPy/models/gplvm.py b/GPy/models/gplvm.py index cdc0ab47..3d0b0d5c 100644 --- a/GPy/models/gplvm.py +++ b/GPy/models/gplvm.py @@ -14,7 +14,7 @@ class GPLVM(GP): """ - def __init__(self, Y, input_dim, init='PCA', X=None, kernel=None, name="gplvm"): + def __init__(self, Y, input_dim, init='PCA', X=None, kernel=None, name="gplvm", Y_metadata=None, normalizer=False): """ :param Y: observed data @@ -23,6 +23,11 @@ class GPLVM(GP): :type input_dim: int :param init: initialisation method for the latent space :type init: 'PCA'|'random' + :param normalizer: + normalize the outputs Y. + If normalizer is True, we will normalize using Standardize. + If normalizer is False (the default), no normalization will be done. + :type normalizer: bool """ if X is None: from ..util.initialization import initialize_latent @@ -34,7 +39,7 @@ class GPLVM(GP): likelihood = Gaussian() - super(GPLVM, self).__init__(X, Y, kernel, likelihood, name='GPLVM') + super(GPLVM, self).__init__(X, Y, kernel, likelihood, name='GPLVM', Y_metadata=Y_metadata, normalizer=normalizer) self.X = Param('latent_mean', X) self.link_parameter(self.X, index=0) From 11f806df3f2cc35d84d4a0022ca011304699e136 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 09:50:51 +0100 Subject: [PATCH 11/16] Fix normalizer to catch when output scale is zero. --- GPy/util/normalizer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GPy/util/normalizer.py b/GPy/util/normalizer.py index 2e6d991a..4fe9815c 100644 --- a/GPy/util/normalizer.py +++ b/GPy/util/normalizer.py @@ -90,6 +90,9 @@ class Standardize(_Norm): Y = np.ma.masked_invalid(Y, copy=False) self.mean = Y.mean(0).view(np.ndarray) self.std = Y.std(0).view(np.ndarray) + if np.any(self.std) == 0: + self.std[np.where(Y_std==0)]=1. + warnings.warn("Some values of Y have standard deviation of zero. Resetting to 1.0 to avoid divide by zero errors.") def normalize(self, Y): super(Standardize, self).normalize(Y) From 943be980d4b4625d8d663dbd829049cef581202f Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 09:55:13 +0100 Subject: [PATCH 12/16] Fix normalizer to catch when output scale is zero. --- GPy/util/normalizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/util/normalizer.py b/GPy/util/normalizer.py index 4fe9815c..07716bc9 100644 --- a/GPy/util/normalizer.py +++ b/GPy/util/normalizer.py @@ -90,7 +90,7 @@ class Standardize(_Norm): Y = np.ma.masked_invalid(Y, copy=False) self.mean = Y.mean(0).view(np.ndarray) self.std = Y.std(0).view(np.ndarray) - if np.any(self.std) == 0: + if np.any(self.std == 0): self.std[np.where(Y_std==0)]=1. warnings.warn("Some values of Y have standard deviation of zero. Resetting to 1.0 to avoid divide by zero errors.") From 95426aed34a41b54c52cd60937d0875707d91454 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 09:58:16 +0100 Subject: [PATCH 13/16] Fix normalizer to catch when output scale is zero. --- GPy/util/normalizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/util/normalizer.py b/GPy/util/normalizer.py index 07716bc9..83c79b32 100644 --- a/GPy/util/normalizer.py +++ b/GPy/util/normalizer.py @@ -91,8 +91,8 @@ class Standardize(_Norm): self.mean = Y.mean(0).view(np.ndarray) self.std = Y.std(0).view(np.ndarray) if np.any(self.std == 0): - self.std[np.where(Y_std==0)]=1. warnings.warn("Some values of Y have standard deviation of zero. Resetting to 1.0 to avoid divide by zero errors.") + self.std[np.where(self.std==0)]=1. def normalize(self, Y): super(Standardize, self).normalize(Y) From 5f68d8a7c34a52b57be4d4cef44e646a81c2c931 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 10:00:41 +0100 Subject: [PATCH 14/16] Fix normalizer to catch when output scale is zero. --- GPy/util/normalizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/util/normalizer.py b/GPy/util/normalizer.py index 83c79b32..243f81c7 100644 --- a/GPy/util/normalizer.py +++ b/GPy/util/normalizer.py @@ -4,7 +4,7 @@ Created on Aug 27, 2014 @author: Max Zwiessele ''' import numpy as np - +import warnings class _Norm(object): def __init__(self): From ef90137c4c4251cccffc27811fc2f341a0116e40 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 10:06:40 +0100 Subject: [PATCH 15/16] Fix dimensionality reduction example to work with notebook. --- GPy/examples/dimensionality_reduction.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index 634f9790..9218a0e8 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -685,17 +685,23 @@ def cmu_mocap(subject='35', motion=['01'], in_place=True, optimize=True, verbose # Make figure move in place. data['Y'][:, 0:3] = 0.0 Y = data['Y'] - Y_mean = Y.mean(0) - Y_std = Y.std(0) - m = GPy.models.GPLVM((Y - Y_mean) / Y_std, 2) + m = GPy.models.GPLVM(Y, 2, normalizer=True) if optimize: m.optimize(messages=verbose, max_f_eval=10000) if plot: - fig, (latent_axes, sense_axes) = plt.subplots(1, 2) + fig, _ = plt.subplots(figsize=(8, 5)) + latent_axes = fig.add_subplot(131) + sense_axes = fig.add_subplot(132) + viz_axes = fig.add_subplot(133, projection='3d') + + m.plot_latent(ax=latent_axes) + latent_axes.set_aspect('equal') + y = m.Y[0, :] - data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel']) - lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=latent_axes) + data_show = GPy.plotting.matplot_dep.visualize.skeleton_show(y[None, :], data['skel'], viz_axes) + + lvm_visualizer = GPy.plotting.matplot_dep.visualize.lvm(m.X[0].copy(), m, data_show, latent_axes=latent_axes, sense_axes=sense_axes) input('Press enter to finish') lvm_visualizer.close() data_show.close() From 714ad858aa5df0851b72e40b5af2e53ccc347f47 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Wed, 19 May 2021 18:31:52 +0100 Subject: [PATCH 16/16] Update GPy/util/normalizer.py --- GPy/util/normalizer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GPy/util/normalizer.py b/GPy/util/normalizer.py index 243f81c7..d04e7324 100644 --- a/GPy/util/normalizer.py +++ b/GPy/util/normalizer.py @@ -92,7 +92,8 @@ class Standardize(_Norm): self.std = Y.std(0).view(np.ndarray) if np.any(self.std == 0): warnings.warn("Some values of Y have standard deviation of zero. Resetting to 1.0 to avoid divide by zero errors.") - self.std[np.where(self.std==0)]=1. + # Choice of setting to 1.0 is somewhat arbitrary. It avoids a divide by zero error, but setting to EPS would also do this. Don't have strong reasons for choosing 1.0, it was just first instinct + self.std[np.where(self.std==0)]=1. def normalize(self, Y): super(Standardize, self).normalize(Y)