changed the behaviour of checkgrad

checkgrad usd to check the passed string (for name matching) against the
list of _get_param_names(). Then it would index along
_get_param_names_transformed()!

this led to inconsistensies when fixed or tied variables were used,
which screwed up the ordering of the variable names.

We now match against _get_param_names_transformed().
This commit is contained in:
James Hensman 2013-06-05 12:08:33 +01:00
parent 903e66486d
commit dea4359b4e
3 changed files with 48 additions and 29 deletions

View file

@ -392,7 +392,11 @@ class model(parameterised):
if target_param is None:
param_list = range(len(x))
else:
param_list = self.grep_param_names(target_param)
param_list = self.grep_param_names(target_param, transformed=True, search=True)
if not param_list:
print "No free parameters to check"
return
for i in param_list:
xx = x.copy()

View file

@ -119,7 +119,7 @@ class parameterised(object):
"""Unties all parameters by setting tied_indices to an empty list."""
self.tied_indices = []
def grep_param_names(self, regexp):
def grep_param_names(self, regexp, transformed=False, search=False):
"""
:param regexp: regular expression to select parameter names
:type regexp: re | str | int
@ -129,13 +129,21 @@ class parameterised(object):
Other objects are passed through - i.e. integers which weren't meant for grepping
"""
if transformed:
names = self._get_param_names_transformed()
else:
names = self._get_param_names()
if type(regexp) in [str, np.string_, np.str]:
regexp = re.compile(regexp)
return np.nonzero([regexp.match(name) for name in self._get_param_names()])[0]
elif type(regexp) is re._pattern_type:
return np.nonzero([regexp.match(name) for name in self._get_param_names()])[0]
pass
else:
return regexp
if search:
return np.nonzero([regexp.search(name) for name in names])[0]
else:
return np.nonzero([regexp.match(name) for name in names])[0]
def Nparam_transformed(self):
removed = 0
@ -223,7 +231,14 @@ class parameterised(object):
To fix multiple parameters to the same value, simply pass a regular expression which matches both parameter names, or pass both of the indexes
"""
matches = self.grep_param_names(regexp)
assert not np.any(matches[:, None] == self.all_constrained_indices()), "Some indices are already constrained"
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]
self.fixed_indices.append(matches)
if value != None:
self.fixed_values.append(value)