Skip to content

Commit 977760a

Browse files
committed
Made some more simplifications in the fitting code by also keeping around an array called self._param_indices, which are the indices into the self.model.parameters array of the parameter values being fitted (in particular excluding fixed and tied parameters). The values of tied parameters are updated manually. This fixed a couple bugs, particularly that came up in the LinearLSQFitter and should fix #1624.
1 parent 3552d36 commit 977760a

1 file changed

Lines changed: 28 additions & 45 deletions

File tree

‎astropy/modeling/fitting.py‎

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def __init__(self, model):
9494
self.ineqcons = []
9595

9696
self._set_constraints()
97-
self._fitparams, self._fitparam_indices = self._model_to_fit_params()
97+
self._fitparams, self._fitparam_indices, self._param_indices = \
98+
self._model_to_fit_params()
9899
self._validate_constraints()
99100
self._weights = None
100101

@@ -148,31 +149,12 @@ def fitparams(self, fps):
148149
self._fitparams[:] = fps
149150

150151
if any(self.fixed) or any(self.tied):
151-
mparams = []
152-
fitparams = list(fps)
153-
for i, name in enumerate(self.model.param_names):
154-
if self.fixed[i] == True:
155-
param = getattr(self.model, name)
156-
if len(param.shape) == 0:
157-
mparams.extend([param.value])
158-
else:
159-
mparams.extend(param.value)
160-
elif self.tied[i] != False:
161-
value = self.tied[i](self.model)
162-
if isinstance(value, numbers.Number):
163-
mparams.append(value)
164-
else:
165-
mparams.extend(value)
166-
else:
167-
# The parameters being fitted are in the fps list in the
168-
# same order they appear in the model parameters array, so
169-
# just pop the values the current parameter off the end of
170-
# the list
152+
self.model.parameters[self._param_indices] = fps
153+
for idx, name in enumerate(self.model.param_names):
154+
if self.tied[idx] != False:
155+
value = self.tied[idx](self.model)
171156
slice_ = self.model._param_metrics[name][0]
172-
param_len = slice_.stop - slice_.start
173-
mparams.extend(fitparams[:param_len])
174-
del fitparams[:param_len]
175-
self.model.parameters = mparams
157+
self.model.parameters[slice_] = value
176158
elif any([tuple(b) != (-1E12, 1E12) for b in self.bounds]):
177159
self._set_bounds(fps)
178160
else:
@@ -186,19 +168,20 @@ def _model_to_fit_params(self):
186168
constant or tied.
187169
"""
188170

189-
fitparam_indices = []
171+
fitparam_indices = range(len(self.model.param_names))
172+
param_indices = range(len(self.model.parameters))
190173
if any(self.model.fixed.values()) or any(self.model.tied.values()):
191174
params = list(self.model.parameters)
192-
for idx, name in enumerate(self.model.param_names[::-1]):
175+
for idx, name in list(enumerate(self.model.param_names))[::-1]:
193176
if self.model.fixed[name] or self.model.tied[name]:
194-
sl = self.model._param_metrics[name][0].start
177+
sl = self.model._param_metrics[name][0]
195178
del params[sl]
196-
else:
197-
fitparam_indices.append(idx)
198-
return np.array(params), fitparam_indices
179+
del param_indices[sl]
180+
del fitparam_indices[idx]
181+
return np.array(params), fitparam_indices, param_indices
199182
else:
200-
return (self.model.parameters.copy(),
201-
range(len(self.model.param_names)))
183+
return (self.model.parameters.copy(), fitparam_indices,
184+
param_indices)
202185

203186
def _set_bounds(self, pars):
204187
"""
@@ -296,7 +279,8 @@ def weights(self, val):
296279

297280
def _update_constraints(self):
298281
self._set_constraints()
299-
self._fitparams, self._fitparam_indices = self._model_to_fit_params()
282+
self._fitparams, self._fitparam_indices, self._param_indices = \
283+
self._model_to_fit_params()
300284

301285
@abc.abstractmethod
302286
def __call__(self):
@@ -343,17 +327,14 @@ def __init__(self, model):
343327

344328
def _deriv_with_constraints(self, params=None, x=None, y=None):
345329
if y is None:
346-
d = self.model.deriv(x=x)
330+
d = np.array(self.model.deriv(x=x))
331+
else:
332+
d = np.array(self.model.deriv(x=x, y=y))
333+
334+
if self.model.col_deriv:
335+
return d[self._fitparam_indices]
347336
else:
348-
d = self.model.deriv(x=x, y=y)
349-
fixed = [name for name in self.model.fixed if
350-
self.model.fixed[name]]
351-
ind = range(len(self.model.param_names))
352-
for name in fixed:
353-
index = self.model.param_names.index(name)
354-
ind.remove(index)
355-
res = d[:, ind]
356-
return res
337+
return d[:, self._fitparam_indices]
357338

358339
def _map_domain_window(self, x, y=None):
359340
"""
@@ -480,7 +461,9 @@ def __call__(self, x, y, z=None, weights=None, rcond=None):
480461
# carefully; for now it's not actually allowed
481462
lacoef = (lacoef.T / scl).T
482463
self.fit_info['params'] = lacoef
483-
if rank != self.model._order:
464+
# TODO: Only Polynomial models currently have an _order attribute;
465+
# maybe change this to read isinstance(self.model, PolynomialBase)
466+
if hasattr(self.model, '_order') and rank != self.model._order:
484467
warnings.warn("The fit may be poorly conditioned\n",
485468
AstropyUserWarning)
486469
self.fitparams = lacoef.flatten()

0 commit comments

Comments
 (0)