Merge branch 'devel' of github.com:SheffieldML/GPy into devel

This commit is contained in:
Alan Saul 2013-06-06 10:15:18 +01:00
commit b142b68876
4 changed files with 241 additions and 85 deletions

View file

@ -151,7 +151,7 @@ def BGPLVM_oil(optimize=True, N=200, Q=10, num_inducing=15, max_f_eval=50, plot=
data_show = GPy.util.visualize.vector_show(y)
lvm_visualizer = GPy.util.visualize.lvm_dimselect(m.X[0, :], m, data_show, latent_axes=latent_axes) # , sense_axes=sense_axes)
raw_input('Press enter to finish')
plt.close('all')
plt.close(fig)
return m
def oil_100():
@ -304,75 +304,72 @@ def mrd_simulation(optimize=True, plot=True, plot_sim=True, **kw):
m.plot_scales("MRD Scales")
return m
# # Commented out because dataset is missing
# def brendan_faces():
# from GPy import kern
# data = GPy.util.datasets.brendan_faces()
# Q = 2
# Y = data['Y'][0:-1:10, :]
# # Y = data['Y']
# Yn = Y - Y.mean()
# Yn /= Yn.std()
def brendan_faces():
from GPy import kern
data = GPy.util.datasets.brendan_faces()
Q = 2
Y = data['Y'][0:-1:10, :]
# Y = data['Y']
Yn = Y - Y.mean()
Yn /= Yn.std()
# m = GPy.models.GPLVM(Yn, Q)
# # m = GPy.models.BayesianGPLVM(Yn, Q, num_inducing=100)
m = GPy.models.GPLVM(Yn, Q)
# m = GPy.models.BayesianGPLVM(Yn, Q, num_inducing=100)
# # optimize
# m.constrain('rbf|noise|white', GPy.core.transformations.logexp_clipped())
# optimize
m.constrain('rbf|noise|white', GPy.core.transformations.logexp_clipped())
# m.ensure_default_constraints()
# m.optimize('scg', messages=1, max_f_eval=10000)
m.ensure_default_constraints()
m.optimize('scg', messages=1, max_f_eval=10000)
# ax = m.plot_latent(which_indices=(0, 1))
# y = m.likelihood.Y[0, :]
# data_show = GPy.util.visualize.image_show(y[None, :], dimensions=(20, 28), transpose=True, invert=False, scale=False)
# lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
# raw_input('Press enter to finish')
# plt.close('all')
ax = m.plot_latent(which_indices=(0, 1))
y = m.likelihood.Y[0, :]
data_show = GPy.util.visualize.image_show(y[None, :], dimensions=(20, 28), transpose=True, invert=False, scale=False)
lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
raw_input('Press enter to finish')
lvm_visualizer.close()
# return m
return m
# # Commented out because dataset is missing
# def stick():
# data = GPy.util.datasets.stick()
# m = GPy.models.GPLVM(data['Y'], 2)
def stick():
data = GPy.util.datasets.stick()
m = GPy.models.GPLVM(data['Y'], 2)
# # optimize
# m.ensure_default_constraints()
# m.optimize(messages=1, max_f_eval=10000)
# m._set_params(m._get_params())
# optimize
m.ensure_default_constraints()
m.optimize(messages=1, max_f_eval=10000)
m._set_params(m._get_params())
# ax = m.plot_latent()
# y = m.likelihood.Y[0, :]
# data_show = GPy.util.visualize.stick_show(y[None, :], connect=data['connect'])
# lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
# raw_input('Press enter to finish')
# plt.close('all')
ax = m.plot_latent()
y = m.likelihood.Y[0, :]
data_show = GPy.util.visualize.stick_show(y[None, :], connect=data['connect'])
lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
raw_input('Press enter to finish')
lvm_visualizer.close()
# return m
return m
# # Commented out because dataset is missing
# def cmu_mocap(subject='35', motion=['01'], in_place=True):
def cmu_mocap(subject='35', motion=['01'], in_place=True):
# data = GPy.util.datasets.cmu_mocap(subject, motion)
# Y = data['Y']
# if in_place:
# # Make figure move in place.
# data['Y'][:, 0:3] = 0.0
# m = GPy.models.GPLVM(data['Y'], 2, normalize_Y=True)
data = GPy.util.datasets.cmu_mocap(subject, motion)
Y = data['Y']
if in_place:
# Make figure move in place.
data['Y'][:, 0:3] = 0.0
m = GPy.models.GPLVM(data['Y'], 2, normalize_Y=True)
# # optimize
# m.ensure_default_constraints()
# m.optimize(messages=1, max_f_eval=10000)
# optimize
m.ensure_default_constraints()
m.optimize(messages=1, max_f_eval=10000)
# ax = m.plot_latent()
# y = m.likelihood.Y[0, :]
# data_show = GPy.util.visualize.skeleton_show(y[None, :], data['skel'])
# lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
# raw_input('Press enter to finish')
# plt.close('all')
ax = m.plot_latent()
y = m.likelihood.Y[0, :]
data_show = GPy.util.visualize.skeleton_show(y[None, :], data['skel'])
lvm_visualizer = GPy.util.visualize.lvm(m.X[0, :].copy(), m, data_show, ax)
raw_input('Press enter to finish')
lvm_visualizer.close()
# return m
return m
# def BGPLVM_oil():
# data = GPy.util.datasets.oil()

View file

@ -9,6 +9,61 @@ import urllib2 as url
data_path = os.path.join(os.path.dirname(__file__), 'datasets')
default_seed = 10000
neil_url = 'http://staffwww.dcs.shef.ac.uk/people/N.Lawrence/'
def prompt_user():
# raw_input returns the empty string for "enter"
yes = set(['yes', 'y'])
no = set(['no','n'])
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("Please respond with 'yes', 'y' or 'no', 'n'")
return prompt_user()
def download_data(dataset_name=None):
"""Helper function which contains the resource locations for each data set in one place"""
# Note: there may be a better way of doing this. One of the pythonistas will need to take a look. Neil
data_resources = {'oil': {'urls' : [neil_url + 'oil_data/'],
'files' : [['DataTrnLbls.txt', 'DataTrn.txt']],
'citation' : 'Bishop, C. M. and G. D. James (1993). Analysis of multiphase flows using dual-energy gamma densitometry and neural networks. Nuclear Instruments and Methods in Physics Research A327, 580-593',
'details' : """The three phase oil data used initially for demonstrating the Generative Topographic mapping.""",
'agreement' : None},
'brendan_faces' : {'url' : ['http://www.cs.nyu.edu/~roweis/data/'],
'files': [['frey_rawface.mat']],
'citation' : 'Frey, B. J., Colmenarez, A and Huang, T. S. Mixtures of Local Linear Subspaces for Face Recognition. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition 1998, 32-37, June 1998. Computer Society Press, Los Alamitos, CA.',
'details' : """A video of Brendan Frey's face popularized as a benchmark for visualization by the Locally Linear Embedding.""",
'agreement': None}
}
print('Acquiring resource: ' + dataset_name)
# TODO, check resource is in dictionary!
dr = data_resources[dataset_name]
print('Details of data: ')
print(dr['details'])
if dr['citation']:
print('Please cite:')
print(dr['citation'])
if dr['agreement']:
print('You must also agree to the following:')
print(dr['agreement'])
print('Do you wish to proceed with the download? [yes/no]')
if prompt_user()==False:
return False
for url, files in zip(dr['urls'], dr['files']):
for file in files:
download_resource(url + file)
return True
# Some general utilities.
def sample_class(f):
@ -17,7 +72,7 @@ def sample_class(f):
c = np.where(c, 1, -1)
return c
def fetch_dataset(resource, save_name = None, save_file = True, messages = True):
def download_resource(resource, save_name = None, save_file = True, messages = True):
if messages:
print "Downloading resource: " , resource, " ... ",
response = url.urlopen(resource)
@ -57,14 +112,19 @@ def simulation_BGPLVM():
# The data sets
def oil():
fid = open(os.path.join(data_path, 'oil', 'DataTrn.txt'))
#if download_data('oil'):
oil_train_file = os.path.join(data_path, 'oil', 'DataTrn.txt')
oil_trainlbls_file = os.path.join(data_path, 'oil', 'DataTrnLbls.txt')
fid = open(oil_train_file)
X = np.fromfile(fid, sep='\t').reshape((-1, 12))
fid.close()
fid = open(os.path.join(data_path, 'oil', 'DataTrnLbls.txt'))
fid = open(oil_trainlbls_file)
Y = np.fromfile(fid, sep='\t').reshape((-1, 3)) * 2. - 1.
fid.close()
return {'X': X, 'Y': Y, 'info': "The oil data from Bishop and James (1993)."}
#else:
# throw an error
def oil_100(seed=default_seed):
np.random.seed(seed=seed)
data = oil()
@ -111,10 +171,13 @@ def silhouette():
return {'X': X, 'Y': Y, 'Xtest': Xtest, 'Ytest': Ytest, 'info': "Artificial silhouette simulation data developed from Agarwal and Triggs (2004)."}
def stick():
#if download_data('stick'):
Y, connect = GPy.util.mocap.load_text_data('run1', data_path)
Y = Y[0:-1:4, :]
lbls = 'connect'
return {'Y': Y, 'connect' : connect, 'info': "Stick man data from Ohio."}
# else:
# throw an error.
def swiss_roll_generated(N=1000, sigma=0.0):
with open(os.path.join(data_path, 'swiss_roll.pickle')) as f:
@ -283,6 +346,10 @@ def cmu_mocap(subject, train_motions, test_motions=[], sample_every=4):
# Load in subject skeleton.
subject_dir = os.path.join(data_path, 'mocap', 'cmu', subject)
# Make sure the data is downloaded.
mocap.fetch_cmu(([subject], [train_motions]), skel_store_dir=subject_dir,motion_store_dir=subject_dir)
skel = GPy.util.mocap.acclaim_skeleton(os.path.join(subject_dir, subject + '.asf'))
# Set up labels for each sequence

View file

@ -693,7 +693,7 @@ skel = acclaim_skeleton()
def fetch_data(base_url = 'http://mocap.cs.cmu.edu:8080/subjects', skel_store_dir = '.', motion_store_dir = '.', subj_motions = None, store_motions = True, return_motions = True, messages = True):
def fetch_cmu(subj_motions, base_url = 'http://mocap.cs.cmu.edu:8080/subjects', skel_store_dir = '.', motion_store_dir = '.', store_motions = True, return_motions = True, messages = True):
'''
Download and store the skel. and motions indicated in a tuple (A,B) where A is a list of skeletons and B
the corresponding 2-D list of motions, ie B_ij is the j-th motion to download for skeleton A_i
@ -702,9 +702,9 @@ def fetch_data(base_url = 'http://mocap.cs.cmu.edu:8080/subjects', skel_store_di
e.g.
# Download the data, do not return anything
GPy.util.mocap.fetch_data(subj_motions = ([35],[[1,2,3]]), return_motions = False)
GPy.util.mocap.fetch_cmu(subj_motions = ([35],[[1,2,3]]), return_motions = False)
# Fetch and return the data in a list. Do not store them anywhere
GPy.util.mocap.fetch_data(subj_motions = ([35],[[1,2,3]]), return_motions = True, store_motions = False)
GPy.util.mocap.fetch_cmu(subj_motions = ([35],[[1,2,3]]), return_motions = True, store_motions = False)
In both cases above, if the data do exist in the given skel_store_dir and motion_store_dir, they are just loaded from there.
'''
@ -752,7 +752,7 @@ def fetch_data(base_url = 'http://mocap.cs.cmu.edu:8080/subjects', skel_store_di
os.mkdir(cur_skel_dir)
if not os.path.isdir(motion_store_dir + cur_skel_suffix):
os.mkdir(motion_store_dir + cur_skel_suffix)
cur_skel_data = dat.fetch_dataset(cur_skel_url, cur_skel_file, store_motions, messages)
cur_skel_data = dat.download_resource(cur_skel_url, cur_skel_file, store_motions, messages)
if return_motions:
all_skels.append(cur_skel_data)
@ -765,7 +765,7 @@ def fetch_data(base_url = 'http://mocap.cs.cmu.edu:8080/subjects', skel_store_di
if return_motions:
cur_motion_data = f.read()
else:
cur_motion_data = dat.fetch_dataset(cur_motion_url, cur_motion_file, store_motions, messages)
cur_motion_data = dat.download_resource(cur_motion_url, cur_motion_file, store_motions, messages)
if return_motions:
all_motions[i].append(cur_motion_data)

View file

@ -5,34 +5,69 @@ import numpy as np
import matplotlib as mpl
import time
import Image
import visual
class data_show:
"""
The data show class is a base class which describes how to visualize a
The data_show class is a base class which describes how to visualize a
particular data set. For example, motion capture data can be plotted as a
stick figure, or images are shown using imshow. This class enables latent
to data visualizations for the GP-LVM.
"""
def __init__(self, vals, axes=None):
def __init__(self, vals):
self.vals = vals.copy()
# If no axes are defined, create some.
def modify(self, vals):
raise NotImplementedError, "this needs to be implemented to use the data_show class"
def close(self):
raise NotImplementedError, "this needs to be implemented to use the data_show class"
class vpython_show(data_show):
"""
the vpython_show class is a base class for all visualization methods that use vpython to display. It is initialized with a scene. If the scene is set to None it creates a scene window.
"""
def __init__(self, vals, scene=None):
data_show.__init__(self, vals)
# If no axes are defined, create some.
if scene==None:
self.scene = visual.display(title='Data Visualization')
else:
self.scene = scene
def close(self):
self.scene.exit()
class matplotlib_show(data_show):
"""
the matplotlib_show class is a base class for all visualization methods that use matplotlib. It is initialized with an axis. If the axis is set to None it creates a figure window.
"""
def __init__(self, vals, axes=None):
data_show.__init__(self, vals)
# If no axes are defined, create some.
if axes==None:
fig = plt.figure()
self.axes = fig.add_subplot(111)
else:
self.axes = axes
def modify(self, vals):
raise NotImplementedError, "this needs to be implemented to use the data_show class"
def close(self):
plt.close(self.axes.get_figure())
class vector_show(data_show):
class vector_show(matplotlib_show):
"""
A base visualization class that just shows a data vector as a plot of
vector elements alongside their indices.
"""
def __init__(self, vals, axes=None):
data_show.__init__(self, vals, axes)
matplotlib_show.__init__(self, vals, axes)
self.handle = self.axes.plot(np.arange(0, len(vals))[:, None], self.vals.T)[0]
def modify(self, vals):
@ -42,7 +77,7 @@ class vector_show(data_show):
self.axes.figure.canvas.draw()
class lvm(data_show):
class lvm(matplotlib_show):
def __init__(self, vals, model, data_visualize, latent_axes=None, sense_axes=None, latent_index=[0,1]):
"""Visualize a latent variable model
@ -54,7 +89,7 @@ class lvm(data_show):
if vals == None:
vals = model.X[0]
data_show.__init__(self, vals, axes=latent_axes)
matplotlib_show.__init__(self, vals, axes=latent_axes)
if isinstance(latent_axes,mpl.axes.Axes):
self.cid = latent_axes.figure.canvas.mpl_connect('button_press_event', self.on_click)
@ -204,10 +239,10 @@ class lvm_dimselect(lvm):
class image_show(data_show):
class image_show(matplotlib_show):
"""Show a data vector as an image."""
def __init__(self, vals, axes=None, dimensions=(16,16), transpose=False, invert=False, scale=False, palette=[], presetMean = 0., presetSTD = -1., selectImage=0):
data_show.__init__(self, vals, axes)
matplotlib_show.__init__(self, vals, axes)
self.dimensions = dimensions
self.transpose = transpose
self.invert = invert
@ -266,14 +301,72 @@ class image_show(data_show):
self.vals = Image.fromarray(self.vals.astype('uint8'))
self.vals.putpalette(self.palette) # palette is a list, must be loaded before calling this function
class mocap_data_show(data_show):
class mocap_data_show_vpython(vpython_show):
"""Base class for visualizing motion capture data using visual module."""
def __init__(self, vals, scene=None, connect=None, radius=0.1):
vpython_show.__init__(self, vals, scene)
self.radius = radius
self.connect = connect
self.process_values()
self.draw_edges()
self.draw_vertices()
def draw_vertices(self):
self.spheres = []
for i in range(self.vals.shape[0]):
self.spheres.append(visual.sphere(pos=(self.vals[i, 0], self.vals[i, 2], self.vals[i, 1]), radius=self.radius))
self.scene.visible=True
def draw_edges(self):
self.rods = []
self.line_handle = []
if not self.connect==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)
self.rods.append(visual.cylinder(pos=pos, axis=axis, radius=self.radius))
def modify_vertices(self):
for i in range(self.vals.shape[0]):
self.spheres[i].pos = (self.vals[i, 0], self.vals[i, 2], self.vals[i, 1])
def modify_edges(self):
self.line_handle = []
if not self.connect==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)
def pos_axis(self, i, j):
pos = []
axis = []
pos.append(self.vals[i, 0])
axis.append(self.vals[j, 0]-self.vals[i,0])
pos.append(self.vals[i, 2])
axis.append(self.vals[j, 2]-self.vals[i,2])
pos.append(self.vals[i, 1])
axis.append(self.vals[j, 1]-self.vals[i,1])
return pos, axis
def modify(self, vals):
self.vals = vals.copy()
self.process_values()
self.modify_edges()
self.modify_vertices()
def process_values(self):
raise NotImplementedError, "this needs to be implemented to use the data_show class"
class mocap_data_show(matplotlib_show):
"""Base class for visualizing motion capture data."""
def __init__(self, vals, axes=None, connect=None):
if axes==None:
fig = plt.figure()
axes = fig.add_subplot(111, projection='3d')
data_show.__init__(self, vals, axes)
matplotlib_show.__init__(self, vals, axes)
self.connect = connect
self.process_values()
@ -342,17 +435,17 @@ class mocap_data_show(data_show):
self.axes.set_zlim(self.z_lim)
class stick_show(mocap_data_show):
class stick_show(mocap_data_show_vpython):
"""Show a three dimensional point cloud as a figure. Connect elements of the figure together using the matrix connect."""
def __init__(self, vals, axes=None, connect=None):
mocap_data_show.__init__(self, vals, axes, connect)
def __init__(self, vals, connect=None, scene=None):
mocap_data_show_vpython.__init__(self, vals, scene=scene, connect=connect, radius=0.04)
def process_values(self):
self.vals = self.vals.reshape((3, self.vals.shape[1]/3)).T
class skeleton_show(mocap_data_show):
class skeleton_show(mocap_data_show_vpython):
"""data_show class for visualizing motion capture data encoded as a skeleton with angles."""
def __init__(self, vals, skel, padding=0, axes=None):
def __init__(self, vals, skel, scene=None, padding=0):
"""data_show class for visualizing motion capture data encoded as a skeleton with angles.
:param vals: set of modeled angles to use for printing in the axis when it's first created.
:type vals: np.array
@ -364,8 +457,7 @@ class skeleton_show(mocap_data_show):
self.skel = skel
self.padding = padding
connect = skel.connection_matrix()
mocap_data_show.__init__(self, vals, axes, connect)
mocap_data_show_vpython.__init__(self, vals, scene=scene, connect=connect, radius=0.4)
def process_values(self):
"""Takes a set of angles and converts them to the x,y,z coordinates in the internal prepresentation of the class, ready for plotting.