Skip to content

Commit 25f2a1b

Browse files
committed
Resolves some basic issues with fitting multiple models sets (for linear fits only, which is the only place that is supported for now). Right now this only supports fitting with *one* input across all models (such as when evaluating the model with model_set_axis=False). This is actually in line with what was previously supported. The only major difference is that it's no longer to transpose the 'y' input before passing to the fitter. This is, as I see it, an internal detail of how the fitting algorithm works and not something users should be responsible for. I'll clean this up further once I've merged this work #2580, which will make further improvements easier.
1 parent eddb367 commit 25f2a1b

7 files changed

Lines changed: 85 additions & 48 deletions

File tree

‎astropy/modeling/core.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,14 @@ def _check_param_broadcast(self, params, max_ndim, model_set_axis):
651651
all_shapes = []
652652
param_names = []
653653

654-
for name, value in six.iteritems(params):
654+
for name in self.param_names:
655+
# Previously this just used iteritems(params), but we loop over all
656+
# param_names instead just to ensure some determinism in the
657+
# ordering behavior
658+
if name not in params:
659+
continue
660+
661+
value = params[name]
655662
param_names.append(name)
656663
# We've already checked that each parameter array is compatible in
657664
# the model_set_axis dimension, but now we need to check the

‎astropy/modeling/fitting.py‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,29 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
217217
model_copy : `~astropy.modeling.FittableModel`
218218
a copy of the input model with parameters set by the fitter
219219
"""
220+
220221
if not model.fittable:
221222
raise ValueError("Model must be a subclass of FittableModel")
223+
222224
if not model.linear:
223225
raise ModelLinearityError('Model is not linear in parameters, '
224226
'linear fit methods should not be used.')
225227

226-
if any(model.tied.values()) \
227-
or any([tuple(b) != (None, None) for b in model.bounds.values()]) \
228-
or model.eqcons or model.ineqcons:
229-
raise ValueError("LinearFitter supports only fixed constraints.")
228+
_validate_constraints(self.supported_constraints, model)
229+
230230
multiple = False
231231
model_copy = model.copy()
232232
_, fitparam_indices = _model_to_fit_params(model_copy)
233+
233234
if model_copy.n_inputs == 2 and z is None:
234235
raise ValueError("Expected x, y and z for a 2 dimensional model.")
235236

236-
farg = _convert_input(x, y, z)
237+
farg = _convert_input(x, y, z, n_models=len(model_copy),
238+
model_set_axis=model_copy.model_set_axis)
237239

238-
# TODO: This code needs to respect model_set_axis
239240
if len(farg) == 2:
240241
x, y = farg
241-
if y.ndim == 2:
242-
if y.shape[1] != len(model_copy):
243-
raise ValueError(
244-
"Number of data sets (y array is expected to equal "
245-
"the number of parameter sets)")
242+
246243
# map domain into window
247244
if hasattr(model_copy, 'domain'):
248245
x = self._map_domain_window(model_copy, x)
@@ -271,14 +268,17 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
271268
fitparam_indices, x=x, y=y)
272269
else:
273270
lhs = model_copy.fit_deriv(x, y, *model_copy.parameters)
271+
274272
if len(z.shape) == 3:
275273
rhs = np.array([i.flatten() for i in z]).T
276274
multiple = z.shape[0]
277275
else:
278276
rhs = z.flatten()
277+
279278
# If the derivative is defined along rows (as with non-linear models)
280279
if model_copy.col_fit_deriv:
281280
lhs = np.asarray(lhs).T
281+
282282
if weights is not None:
283283
weights = np.asarray(weights, dtype=np.float)
284284
if len(x) != len(weights):
@@ -305,20 +305,15 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
305305
self.fit_info['rank'] = rank
306306
self.fit_info['singular_values'] = sval
307307

308-
# If y.n_inputs > model.n_inputs we are doing a simultanious 1D fitting
309-
# of several 1D arrays. Otherwise the model is 2D.
310-
# if y.n_inputs > self.model.n_inputs:
311-
if multiple and len(model_copy) != multiple:
312-
# TODO: This should be reworked so that model_copy has the correct
313-
# number of model instances to begin with
314-
model_copy._n_models = multiple
315308
lacoef = (lacoef.T / scl).T
316309
self.fit_info['params'] = lacoef
310+
317311
# TODO: Only Polynomial models currently have an _order attribute;
318312
# maybe change this to read isinstance(model, PolynomialBase)
319313
if hasattr(model_copy, '_order') and rank != model_copy._order:
320314
warnings.warn("The fit may be poorly conditioned\n",
321315
AstropyUserWarning)
316+
322317
_fitter_to_model_params(model_copy, lacoef.flatten())
323318
return model_copy
324319

@@ -776,17 +771,38 @@ def __call__(self, *args):
776771
model.parameters = np.array(mparams)
777772

778773

779-
def _convert_input(x, y, z=None):
774+
def _convert_input(x, y, z=None, n_models=1, model_set_axis=0):
780775
"""Convert inputs to float arrays."""
781776

782777
x = np.asarray(x, dtype=np.float)
783778
y = np.asarray(y, dtype=np.float)
779+
780+
if z is not None:
781+
z = np.asarray(z, dtype=np.float)
782+
783+
# For compatibility with how the linear fitter code currently expects to
784+
# work, shift the dependent variable's axes to the expected locations
785+
if n_models > 1 and z is None:
786+
if y.shape[model_set_axis] != n_models:
787+
raise ValueError(
788+
"Number of data sets (y array is expected to equal "
789+
"the number of parameter sets)")
790+
# For a 1-D model the y coordinate's model-set-axis is expected to be
791+
# last, so that its first dimension is the same length as the x
792+
# coordinates. This is in line with the expectations of
793+
# numpy.linalg.lstsq:
794+
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html
795+
# That is, each model should be represented by a column.
796+
# TODO: Obviously this is a detail of np.linalg.lstsq and should be
797+
# handled specifically by any fitters that use it...
798+
y = np.rollaxis(y, model_set_axis, y.ndim)
799+
784800
if x.shape[0] != y.shape[0]:
785801
raise ValueError("x and y should have the same shape")
802+
786803
if z is None:
787804
farg = (x, y)
788805
else:
789-
z = np.asarray(z, dtype=np.float)
790806
if x.shape != z.shape:
791807
raise ValueError("x, y and z should have the same shape")
792808
farg = (x, y, z)

‎astropy/modeling/tests/test_fitters.py‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,42 @@ def errfunc(p, x1, y1, x2, y2):
160160

161161

162162
class TestLinearLSQFitter(object):
163-
164-
def setup_class(self):
163+
def test_chebyshev1D(self):
164+
"""Tests fitting a 1D Chebyshev polynomial to some real world data."""
165165
test_file = get_pkg_data_filename(os.path.join('data',
166166
'idcompspec.fits'))
167-
f = open(test_file)
168-
lines = f.read()
169-
reclist = lines.split('begin')
170-
f.close()
167+
with open(test_file) as f:
168+
lines = f.read()
169+
reclist = lines.split('begin')
170+
171171
record = irafutil.IdentifyRecord(reclist[1])
172-
self.icoeff = record.coeff
172+
coeffs = record.coeff
173173
order = int(record.fields['order'])
174-
self.model = models.Chebyshev1D(order - 1)
175-
self.model.domain = record.get_range()
176-
self.lf = LinearLSQFitter()
177-
self.x = record.x
178-
self.y = record.z
179-
self.yy = np.array([record.z, record.z])
180174

181-
def test_chebyshev1D(self):
182-
new_model = self.lf(self.model, self.x, self.y)
183-
assert_allclose(new_model.parameters, np.array(self.icoeff),
184-
rtol=10E-2)
175+
initial_model = models.Chebyshev1D(order - 1,
176+
domain=record.get_range())
177+
fitter = LinearLSQFitter()
178+
179+
fitted_model = fitter(initial_model, record.x, record.z)
180+
assert_allclose(fitted_model.parameters, np.array(coeffs),
181+
rtol=10e-2)
182+
183+
def test_linear_fit_model_set(self):
184+
"""Tests fitting multiple models simultaneously."""
185+
186+
init_model = models.Polynomial1D(degree=2, c0=[1, 1], n_models=2)
187+
x = np.arange(10)
188+
y_expected = init_model(x, model_set_axis=False)
189+
assert y_expected.shape == (2, 10)
190+
191+
# Add a bit of random noise
192+
with NumpyRNGContext(0x01010101):
193+
y = y_expected + np.random.normal(0, 0.01, size=y_expected.shape)
194+
195+
fitter = LinearLSQFitter()
196+
fitted_model = fitter(init_model, x, y)
197+
assert_allclose(fitted_model(x, model_set_axis=False), y_expected,
198+
rtol=1e-1)
185199

186200

187201
@pytest.mark.skipif('not HAS_SCIPY')

‎astropy/modeling/tests/test_input.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_linear_fitter_Nset(self):
104104
p1 = models.Polynomial1D(3, model_set_axis=0, **params)
105105
y1 = p1(self.x1, model_set_axis=False)
106106
pfit = fitting.LinearLSQFitter()
107-
model = pfit(p1, self.x1, y1.T)
107+
model = pfit(p1, self.x1, y1)
108108
assert_allclose(model.param_sets, expected, atol=10 ** (-7))
109109

110110
def test_linear_fitter_1dcheb(self):
@@ -182,7 +182,7 @@ def test_wrong_pset(self):
182182
p1 = models.Polynomial1D(5, model_set_axis=0, **params)
183183
y1 = p1(self.x1, model_set_axis=False)
184184
pfit = fitting.LinearLSQFitter()
185-
model = pfit(p1, self.x1, y1.T)
185+
model = pfit(p1, self.x1, y1)
186186
assert_allclose(model.param_sets, expected, atol=10 ** (-7))
187187

188188
@pytest.mark.skipif('not HAS_SCIPY')

‎docs/modeling/fitting.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Fitting examples
3232
>>> p1 = models.Polynomial1D(3)
3333
>>> p1.c0 = 1
3434
>>> p1.c1 = 2
35-
>>> print p1
35+
>>> print(p1)
3636
Model: Polynomial1D
3737
Inputs: 1
3838
Outputs: 1
@@ -44,11 +44,11 @@ Fitting examples
4444
1.0 2.0 0.0 0.0
4545
>>> x = np.arange(10)
4646
>>> y = p1(x)
47-
>>> yy = np.array([y, y]).T
47+
>>> yy = np.array([y, y])
4848
>>> p2 = models.Polynomial1D(3, n_models=2)
4949
>>> pfit = fitting.LinearLSQFitter()
5050
>>> new_model = pfit(p2, x, yy)
51-
>>> print(new_model) # doctest: +SKIP
51+
>>> print(new_model)
5252
Model: Polynomial1D
5353
Inputs: 1
5454
Outputs: 1

‎docs/modeling/models.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ error::
5959
>>> g = Gaussian1D(amplitude=1, mean=[1, 2], stddev=[1, 2, 3])
6060
Traceback (most recent call last):
6161
...
62-
InputParameterError: Parameter 'stddev' of shape (3,) cannot be broadcast
63-
with parameter 'mean' of shape (2,). All parameter arrays must have shapes
64-
that are mutually compatible according to the broadcasting rules.
62+
InputParameterError: Parameter 'mean' of shape (2,) cannot be broadcast
63+
with parameter 'stddev' of shape (3,). All parameter arrays must have
64+
shapes that are mutually compatible according to the broadcasting rules.
6565

6666

6767
Model Sets

‎docs/modeling/parameters.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,6 @@ Parameter examples
165165
... c2=[4.0, 5.0, 6.0])
166166
Traceback (most recent call last):
167167
...
168-
InputParameterError: Parameter u'c2' of shape (3,) cannot be broadcast
169-
with parameter u'c1' of shape (2,). All parameter arrays must have
168+
InputParameterError: Parameter u'c1' of shape (2,) cannot be broadcast
169+
with parameter u'c2' of shape (3,). All parameter arrays must have
170170
shapes that are mutually compatible according to the broadcasting rules.

0 commit comments

Comments
 (0)