From 3a97eaf6842d04e6fb980a24796e32b8fa6e4323 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Mon, 14 Oct 2013 17:59:04 +0100 Subject: [PATCH] warning messages optional for re-constraining --- GPy/core/model.py | 4 ++-- GPy/core/parameterized.py | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 17619e27..9f67cef3 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -250,7 +250,7 @@ class Model(Parameterized): else: self._set_params_transformed(initial_parameters) - def ensure_default_constraints(self): + def ensure_default_constraints(self, warning=True): """ Ensure that any variables which should clearly be positive have been constrained somehow. The method performs a regular @@ -268,7 +268,7 @@ class Model(Parameterized): if not (i in currently_constrained): to_make_positive.append(i) if len(to_make_positive): - self.constrain_positive(np.asarray(to_make_positive)) + self.constrain_positive(np.asarray(to_make_positive), warning=warning) def objective_function(self, x): """ diff --git a/GPy/core/parameterized.py b/GPy/core/parameterized.py index 09cb374a..7aa21535 100644 --- a/GPy/core/parameterized.py +++ b/GPy/core/parameterized.py @@ -193,17 +193,17 @@ class Parameterized(object): else: self.fixed_indices, self.fixed_values = [], [] - def constrain_negative(self, regexp): + def constrain_negative(self, regexp, warning=True): """ Set negative constraints. """ - self.constrain(regexp, transformations.NegativeLogexp()) + self.constrain(regexp, transformations.NegativeLogexp(), warning) - def constrain_positive(self, regexp): + def constrain_positive(self, regexp, warning=True): """ Set positive constraints. """ - self.constrain(regexp, transformations.Logexp()) + self.constrain(regexp, transformations.Logexp(), warning) - def constrain_bounded(self, regexp, lower, upper): + def constrain_bounded(self, regexp, lower, upper, warning=True): """ Set bounded constraints. """ - self.constrain(regexp, transformations.Logistic(lower, upper)) + self.constrain(regexp, transformations.Logistic(lower, upper), warning) def all_constrained_indices(self): if len(self.constrained_indices) or len(self.fixed_indices): @@ -211,17 +211,18 @@ class Parameterized(object): else: return np.empty(shape=(0,)) - def constrain(self, regexp, transform): + def constrain(self, regexp, transform, warning=True): assert isinstance(transform, transformations.Transformation) matches = self.grep_param_names(regexp) overlap = set(matches).intersection(set(self.all_constrained_indices())) if overlap: self.unconstrain(np.asarray(list(overlap))) - print 'Warning: re-constraining these parameters' - pn = self._get_param_names() - for i in overlap: - print pn[i] + if warning: + print 'Warning: re-constraining these parameters' + pn = self._get_param_names() + for i in overlap: + print pn[i] self.constrained_indices.append(matches) self.constraints.append(transform) @@ -229,7 +230,7 @@ class Parameterized(object): x[matches] = transform.initialize(x[matches]) self._set_params(x) - def constrain_fixed(self, regexp, value=None): + def constrain_fixed(self, regexp, value=None, warning=True): """ :param regexp: which parameters need to be fixed. @@ -249,10 +250,11 @@ class Parameterized(object): overlap = set(matches).intersection(set(self.all_constrained_indices())) if overlap: self.unconstrain(np.asarray(list(overlap))) - print 'Warning: re-constraining these parameters' - pn = self._get_param_names() - for i in overlap: - print pn[i] + if warning: + print 'Warning: re-constraining these parameters' + pn = self._get_param_names() + for i in overlap: + print pn[i] self.fixed_indices.append(matches) if value != None: