@@ -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 )
0 commit comments