From 8fa890639e6d0d5fc0323c81cd5a01541170b08b Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Thu, 16 Oct 2014 04:06:03 +0100 Subject: [PATCH] Display of models and params for the notebook. --- GPy/core/model.py | 10 ++++++++ GPy/core/parameterization/param.py | 23 +++++++++++++++++ GPy/core/parameterization/parameterized.py | 29 ++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/GPy/core/model.py b/GPy/core/model.py index dc0a9f5e..8da52358 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -381,6 +381,16 @@ class Model(Parameterized): self.optimizer_array = x return ret + def _repr_html_(self): + """Representation of the model in html for notebook display.""" + model_details = [['Model', self.name + '
'], + ['Log-likelihood', '{}
'.format(float(self.log_likelihood()))], + ["Number of Parameters", '{}
'.format(self.size)]] + from operator import itemgetter + to_print = [""] + ["{}: {}".format(name, detail) for name, detail in model_details] + ["
Parameters:"] + to_print.append(super(Model, self)._repr_html_()) + return "\n".join(to_print) + def __str__(self): model_details = [['Name', self.name], ['Log-likelihood', '{}'.format(float(self.log_likelihood()))], diff --git a/GPy/core/parameterization/param.py b/GPy/core/parameterization/param.py index cb9b0cfe..d205c9e4 100644 --- a/GPy/core/parameterization/param.py +++ b/GPy/core/parameterization/param.py @@ -249,6 +249,29 @@ class Param(Parameterizable, ObsAr): if ind.size > 4: indstr = ','.join(map(str, ind[:2])) + "..." + ','.join(map(str, ind[-2:])) else: indstr = ','.join(map(str, ind)) return name + '[' + indstr + ']' + + def _repr_html_(self, constr_matrix=None, indices=None, prirs=None, ties=None): + """Representation of the parameter in html for notebook display.""" + filter_ = self._current_slice_ + vals = self.flat + if indices is None: indices = self._indices(filter_) + ravi = self._raveled_index(filter_) + if constr_matrix is None: constr_matrix = self.constraints.properties_for(ravi) + if prirs is None: prirs = self.priors.properties_for(ravi) + if ties is None: ties = self._ties_for(ravi) + ties = [' '.join(map(lambda x: x, t)) for t in ties] + header_format = """ + + {i} + {x} + {c} + {p} + {t} +""" + header = header_format.format(x=self.hierarchy_name(), c=__constraints_name__, i=__index_name__, t=__tie_name__, p=__priors_name__) # nice header for printing + if not ties: ties = itertools.cycle(['']) + return "\n".join([''] + [header] + ["".format(x=x, c=" ".join(map(str, c)), p=" ".join(map(str, p)), t=(t or ''), i=i) for i, x, c, t, p in itertools.izip(indices, vals, constr_matrix, ties, prirs)] + ["
{i}{x}{c}{p}{t}
"]) + def __str__(self, constr_matrix=None, indices=None, prirs=None, ties=None, lc=None, lx=None, li=None, lp=None, lt=None, only_name=False): filter_ = self._current_slice_ vals = self.flat diff --git a/GPy/core/parameterization/parameterized.py b/GPy/core/parameterization/parameterized.py index cf621ad9..562c6096 100644 --- a/GPy/core/parameterization/parameterized.py +++ b/GPy/core/parameterization/parameterized.py @@ -357,6 +357,35 @@ class Parameterized(Parameterizable): @property def _ties_str(self): return [','.join(x._ties_str) for x in self.flattened_parameters] + + def _repr_html_(self, header=True): + """Representation of the parameters in html for notebook display.""" + name = adjust_name_for_printing(self.name) + "." + constrs = self._constraints_str; + ts = self._ties_str + prirs = self._priors_str + desc = self._description_str; names = self.parameter_names() + nl = max([len(str(x)) for x in names + [name]]) + sl = max([len(str(x)) for x in desc + ["Value"]]) + cl = max([len(str(x)) if x else 0 for x in constrs + ["Constraint"]]) + tl = max([len(str(x)) if x else 0 for x in ts + ["Tied to"]]) + pl = max([len(str(x)) if x else 0 for x in prirs + ["Prior"]]) + format_spec = "{{name:<{0}s}}{{desc:>{1}s}}{{const:^{2}s}}{{pri:^{3}s}}{{t:^{4}s}}".format(nl, sl, cl, pl, tl) + to_print = [] + for n, d, c, t, p in itertools.izip(names, desc, constrs, ts, prirs): + to_print.append(format_spec.format(name=n, desc=d, const=c, t=t, pri=p)) + sep = '-' * (nl + sl + cl + + pl + tl + 8 * 2 + 3) + if header: + header = """ + + {name} + Value + Constraint + Prior + Tied to""".format(name=name) + to_print.insert(0, header) + return '' + '\n'.format(sep).join(to_print) + '\n
' + def __str__(self, header=True): name = adjust_name_for_printing(self.name) + "." constrs = self._constraints_str;