print_all function removed, print m works as before.

This commit is contained in:
Ricardo 2013-09-16 12:24:37 +01:00
parent 4bb2ea9606
commit 1931e447f4
5 changed files with 53 additions and 43 deletions

View file

@ -31,8 +31,8 @@ class Model(Parameterized):
def getstate(self): def getstate(self):
""" """
Get the current state of the class. Get the current state of the class.
Inherited from Parameterized, so add those parameters to the state Inherited from Parameterized, so add those parameters to the state
:return: list of states from the model. :return: list of states from the model.
""" """
@ -46,7 +46,7 @@ class Model(Parameterized):
call Parameterized with the rest of the state call Parameterized with the rest of the state
:param state: the state of the model. :param state: the state of the model.
:type state: list as returned from getstate. :type state: list as returned from getstate.
""" """
self.preferred_optimizer = state.pop() self.preferred_optimizer = state.pop()
self.sampling_runs = state.pop() self.sampling_runs = state.pop()
@ -397,17 +397,20 @@ class Model(Parameterized):
return np.nan return np.nan
return 0.5 * self._get_params().size * np.log(2 * np.pi) + self.log_likelihood() - hld return 0.5 * self._get_params().size * np.log(2 * np.pi) + self.log_likelihood() - hld
def __str__(self, names=None): def __str__(self):
if names is None: s = Parameterized.__str__(self).split('\n')
names = self._get_print_names() #def __str__(self, names=None):
s = Parameterized.__str__(self, names=names).split('\n') # if names is None:
# names = self._get_print_names()
#s = Parameterized.__str__(self, names=names).split('\n')
# add priors to the string # add priors to the string
if self.priors is not None: if self.priors is not None:
strs = [str(p) if p is not None else '' for p in self.priors] strs = [str(p) if p is not None else '' for p in self.priors]
else: else:
strs = [''] * len(self._get_param_names()) strs = [''] * len(self._get_params())
name_indices = self.grep_param_names("|".join(names)) # strs = [''] * len(self._get_param_names())
strs = np.array(strs)[name_indices] # name_indices = self.grep_param_names("|".join(names))
# strs = np.array(strs)[name_indices]
width = np.array(max([len(p) for p in strs] + [5])) + 4 width = np.array(max([len(p) for p in strs] + [5])) + 4
log_like = self.log_likelihood() log_like = self.log_likelihood()

View file

@ -27,9 +27,9 @@ class Parameterized(object):
def _get_param_names(self): def _get_param_names(self):
raise NotImplementedError, "this needs to be implemented to use the Parameterized class" raise NotImplementedError, "this needs to be implemented to use the Parameterized class"
def _get_print_names(self): #def _get_print_names(self):
""" Override for which names to print out, when using print m """ # """ Override for which names to print out, when using print m """
return self._get_param_names() # return self._get_param_names()
def pickle(self, filename, protocol=None): def pickle(self, filename, protocol=None):
if protocol is None: if protocol is None:
@ -63,10 +63,10 @@ class Parameterized(object):
""" """
Get the current state of the class, Get the current state of the class,
here just all the indices, rest can get recomputed here just all the indices, rest can get recomputed
For inheriting from Parameterized: For inheriting from Parameterized:
Allways append the state of the inherited object
and call down to the inherited object in setstate!! Allways append the state of the inherited object
and call down to the inherited object in setstate!!
""" """
return [self.tied_indices, return [self.tied_indices,
self.fixed_indices, self.fixed_indices,
@ -336,26 +336,30 @@ class Parameterized(object):
n = [nn for i, nn in enumerate(n) if not i in remove] n = [nn for i, nn in enumerate(n) if not i in remove]
return n return n
@property #@property
def all(self): #def all(self):
return self.__str__(self._get_param_names()) # return self.__str__(self._get_param_names())
def __str__(self, names=None, nw=30): #def __str__(self, names=None, nw=30):
def __str__(self, nw=30):
""" """
Return a string describing the parameter names and their ties and constraints Return a string describing the parameter names and their ties and constraints
""" """
if names is None: names = self._get_param_names()
names = self._get_print_names() #if names is None:
name_indices = self.grep_param_names("|".join(names)) # names = self._get_print_names()
#name_indices = self.grep_param_names("|".join(names))
N = len(names) N = len(names)
if not N: if not N:
return "This object has no free parameters." return "This object has no free parameters."
header = ['Name', 'Value', 'Constraints', 'Ties'] header = ['Name', 'Value', 'Constraints', 'Ties']
values = self._get_params()[name_indices] # map(str,self._get_params()) values = self._get_params() # map(str,self._get_params())
#values = self._get_params()[name_indices] # map(str,self._get_params())
# sort out the constraints # sort out the constraints
constraints = [''] * len(self._get_param_names()) constraints = [''] * len(names)
#constraints = [''] * len(self._get_param_names())
for i, t in zip(self.constrained_indices, self.constraints): for i, t in zip(self.constrained_indices, self.constraints):
for ii in i: for ii in i:
constraints[ii] = t.__str__() constraints[ii] = t.__str__()

View file

@ -208,8 +208,8 @@ class SparseGP(GPBase):
return sum([['iip_%i_%i' % (i, j) for j in range(self.Z.shape[1])] for i in range(self.Z.shape[0])], [])\ return sum([['iip_%i_%i' % (i, j) for j in range(self.Z.shape[1])] for i in range(self.Z.shape[0])], [])\
+ self.kern._get_param_names_transformed() + self.likelihood._get_param_names() + self.kern._get_param_names_transformed() + self.likelihood._get_param_names()
def _get_print_names(self): #def _get_print_names(self):
return self.kern._get_param_names_transformed() + self.likelihood._get_param_names() # return self.kern._get_param_names_transformed() + self.likelihood._get_param_names()
def update_likelihood_approximation(self): def update_likelihood_approximation(self):
""" """

View file

@ -66,8 +66,8 @@ class BayesianGPLVM(SparseGP, GPLVM):
S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], []) S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
return (X_names + S_names + SparseGP._get_param_names(self)) return (X_names + S_names + SparseGP._get_param_names(self))
def _get_print_names(self): #def _get_print_names(self):
return SparseGP._get_print_names(self) # return SparseGP._get_print_names(self)
def _get_params(self): def _get_params(self):
""" """

View file

@ -25,11 +25,11 @@ class MRD(Model):
:param input_dim: latent dimensionality :param input_dim: latent dimensionality
:type input_dim: int :type input_dim: int
:param initx: initialisation method for the latent space : :param initx: initialisation method for the latent space :
* 'concat' - PCA on concatenation of all datasets * 'concat' - PCA on concatenation of all datasets
* 'single' - Concatenation of PCA on datasets, respectively * 'single' - Concatenation of PCA on datasets, respectively
* 'random' - Random draw from a normal * 'random' - Random draw from a normal
:type initx: ['concat'|'single'|'random'] :type initx: ['concat'|'single'|'random']
:param initz: initialisation method for inducing inputs :param initz: initialisation method for inducing inputs
:type initz: 'permute'|'random' :type initz: 'permute'|'random'
@ -163,28 +163,31 @@ class MRD(Model):
self._init_X(initx, self.likelihood_list) self._init_X(initx, self.likelihood_list)
self._init_Z(initz, self.X) self._init_Z(initz, self.X)
def _get_latent_param_names(self): #def _get_latent_param_names(self):
def _get_param_names(self):
n1 = self.gref._get_param_names() n1 = self.gref._get_param_names()
n1var = n1[:self.NQ * 2 + self.MQ] n1var = n1[:self.NQ * 2 + self.MQ]
return n1var # return n1var
#
#def _get_kernel_names(self):
def _get_kernel_names(self):
map_names = lambda ns, name: map(lambda x: "{1}_{0}".format(*x), map_names = lambda ns, name: map(lambda x: "{1}_{0}".format(*x),
itertools.izip(ns, itertools.izip(ns,
itertools.repeat(name))) itertools.repeat(name)))
kernel_names = (map_names(SparseGP._get_param_names(g)[self.MQ:], n) for g, n in zip(self.bgplvms, self.names)) return list(itertools.chain(n1var, *(map_names(\
return kernel_names SparseGP._get_param_names(g)[self.MQ:], n) \
for g, n in zip(self.bgplvms, self.names))))
# kernel_names = (map_names(SparseGP._get_param_names(g)[self.MQ:], n) for g, n in zip(self.bgplvms, self.names))
# return kernel_names
def _get_param_names(self): #def _get_param_names(self):
# X_names = sum([['X_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], []) # X_names = sum([['X_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
# S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], []) # S_names = sum([['X_variance_%i_%i' % (n, q) for q in range(self.input_dim)] for n in range(self.num_data)], [])
n1var = self._get_latent_param_names() # n1var = self._get_latent_param_names()
kernel_names = self._get_kernel_names() # kernel_names = self._get_kernel_names()
return list(itertools.chain(n1var, *kernel_names)) # return list(itertools.chain(n1var, *kernel_names))
def _get_print_names(self): #def _get_print_names(self):
return list(itertools.chain(*self._get_kernel_names())) # return list(itertools.chain(*self._get_kernel_names()))
def _get_params(self): def _get_params(self):
""" """