
    IR-enF                       d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
ZddlmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ dd
lmZmZmZmZ ddlmZ ddlm Z m!Z! g dZ"egZ#eegZ$ G d de%          Z& G d d          Z' G d d          Z( G d de)          Z* G d de*          Z+ G d de*e,          Z- G d dej.                  Z/d Z0 G d de/          Z1 G d  d!e/          Z2 G d" d#          Z3 G d$ d%e/          Z4 G d& d'e4          Z5 G d( d)e4          Z6 G d* d+e6          Z7 G d, d-e6          Z8 G d. d/e6          Z9 G d0 d1e1          Z: G d2 d3e1          Z; G d4 d5e/          Z<dDd6Z=dEd8Z> ed9d:;          d<             Z?d= Z@ ed9d>;          d?             ZAd@ ZBdA ZCdB ZDdC ZE eE             dS )Fa  
This module implements classes (called Fitters) which combine optimization
algorithms (typically from `scipy.optimize`) with statistic functions to perform
fitting. Fitters are implemented as callable classes. In addition to the data
to fit, the ``__call__`` method takes an instance of
`~astropy.modeling.core.FittableModel` as input, and returns a copy of the
model with its parameters determined by the optimizer.

Optimization algorithms, called "optimizers" are implemented in
`~astropy.modeling.optimizers` and statistic functions are in
`~astropy.modeling.statistic`. The goal is to provide an easy to extend
framework and allow users to easily create new fitters by combining statistics
with optimizers.

There are two exceptions to the above scheme.
`~astropy.modeling.fitting.LinearLSQFitter` uses Numpy's `~numpy.linalg.lstsq`
function.  `~astropy.modeling.fitting.LevMarLSQFitter` uses
`~scipy.optimize.leastsq` which combines optimization and statistic in one
implementation.
    N)reducewraps)entry_points)Quantity)
deprecated)AstropyUserWarning   )DEFAULT_ACCDEFAULT_EPSDEFAULT_MAXITERSLSQPSimplex)SplineExactKnotsFitterSplineInterpolateFitterSplineSmoothingFitterSplineSplrepFitter)leastsquare)_combine_equivalency_dictpoly_map_domain)LinearLSQFitterLevMarLSQFitterTRFLSQFitterDogBoxLSQFitterLMLSQFitterFittingWithOutlierRemovalSLSQPLSQFitterSimplexLSQFitterJointFitterFitterModelLinearityErrorModelsErrorr   r   r   r   c                       e Zd ZdZdS )NonFiniteValueErrorz=
    Error raised when attempting to a non-finite value.
    N__name__
__module____qualname____doc__     8lib/python3.11/site-packages/astropy/modeling/fitting.pyr#   r#   K              r*   r#   c                   *    e Zd ZdZd Zd Zd Zd ZdS )
Covariancez1Class for covariance matrix calculated by fitter.c                 "    || _         || _        d S N)
cov_matrixparam_namesselfr1   r2   s      r+   __init__zCovariance.__init__T   s    $&r*   c                    t          d | j        D                       }d}dd| d}t          | j                  D ]\  }}||dz
  k    r~| j        |         }||                    dt          |          z  |d                              t          t          j	        |d |dz            |                    dd	                   z  }|d
z  }|
                                S )Nc              3   4   K   | ]}t          |          V  d S r0   len.0xs     r+   	<genexpr>z$Covariance.pprint.<locals>.<genexpr>[   (      <<a3q66<<<<<<r*   z#parameter variances / covariances 
 z <z| {0}
r	       ...)maxr2   	enumerater1   replacer9   formatreprnproundrstrip)	r4   	max_lines	round_vallongest_nameret_strfstringirowparams	            r+   pprintzCovariance.pprintX   s     <<4+;<<<<<83<3333300 	! 	!FAsIM!!(+7??3U+;UAFFMM#gAg,	::;;AbDA   5 ~~r*   c                 0    |                      dd          S N
      )rL   rM   rT   r4   s    r+   __repr__zCovariance.__repr__h       {{R1{555r*   c                 |   t          |          dk    rt          d          t          d |D                       rA| j                            |d                   | j                            |d                   }}n.t          d |D                       r|\  }}nt          d          | j        |         |         S )N   z)Covariance must be indexed by two values.c              3   @   K   | ]}t          |t                    V  d S r0   )
isinstancestrr;   items     r+   r=   z)Covariance.__getitem__.<locals>.<genexpr>o   s,      88z$$$888888r*   r   r	   c              3   @   K   | ]}t          |t                    V  d S r0   )r`   intrb   s     r+   r=   z)Covariance.__getitem__.<locals>.<genexpr>s   s,      ::4D#&&::::::r*   zDCovariance can be indexed by two parameter names or integer indices.)r9   
ValueErrorallr2   index	TypeErrorr1   )r4   paramsi1i2s       r+   __getitem__zCovariance.__getitem__k   s    v;;!HIII8888888 		%++F1I668H8N8Nq	9 9BB ::6::::: 	FBV   r"2&&r*   N)r%   r&   r'   r(   r5   rT   r[   rm   r)   r*   r+   r.   r.   Q   sV        ;;' ' '      6 6 6' ' ' ' 'r*   r.   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )StandardDeviationsz Class for fitting uncertainties.c                 H    || _         |                     |          | _        d S r0   )r2   
_calc_stdsstdsr3   s      r+   r5   zStandardDeviations.__init__   s!    &OOJ//			r*   c                 B    d t          j        |          D             }|S )Nc                 F    g | ]}|d k    rt          j        |          ndS )r   N)rI   sqrtr:   s     r+   
<listcomp>z1StandardDeviations._calc_stds.<locals>.<listcomp>   s,    KKK!a!ee


KKKr*   )rI   diag)r4   r1   rr   s      r+   rq   zStandardDeviations._calc_stds   s'     LKrwz7J7JKKKr*   c           	      <   t          d | j        D                       }d}t          | j                  D ]T\  }}||dz
  k    rA| j        |         }|| d|t	          |          z
  z   dt          j        ||           dz  }O|dz  }U|                                S )Nc              3   4   K   | ]}t          |          V  d S r0   r8   r:   s     r+   r=   z,StandardDeviations.pprint.<locals>.<genexpr>   r>   r*   zstandard deviations
r	   r@   z| 
rC   )rD   r2   rE   rr   r9   rI   rJ   rK   )r4   rL   rM   rN   rO   rQ   stdrS   s           r+   rT   zStandardDeviations.pprint   s    <<4+;<<<<<)	** 	! 	!FAsIM!!(+ 4c\CJJ%>? 4 4xY//4 4 4
 5 ~~r*   c                 0    |                      dd          S rV   rY   rZ   s    r+   r[   zStandardDeviations.__repr__   r\   r*   c                     t          |t                    r| j                            |          }n't          |t                    r|}nt          d          | j        |         S )Nz?Standard deviation can be indexed by parameter name or integer.)r`   ra   r2   rh   re   ri   rr   )r4   rS   rQ   s      r+   rm   zStandardDeviations.__getitem__   sh    eS!! 	 &&u--AAs## 	AAQ   y|r*   N)	r%   r&   r'   r(   r5   rq   rT   r[   rm   r)   r*   r+   ro   ro   |   se        **0 0 0       6 6 6	 	 	 	 	r*   ro   c                       e Zd ZdZdS )r!   z Base class for model exceptions.Nr$   r)   r*   r+   r!   r!      s        ****r*   r!   c                       e Zd ZdZdS )r    z<Raised when a non-linear model is passed to a linear fitter.Nr$   r)   r*   r+   r    r       s        FFFFr*   r    c                       e Zd ZdZdS )UnsupportedConstraintErrorzE
    Raised when a fitter does not support a type of constraint.
    Nr$   r)   r*   r+   r   r      r,   r*   r   c                   6     e Zd ZdZ e            Z fdZ xZS )_FitterMetazD
    Currently just provides a registry for all Fitter classes.
    c                     t                                          | |||          }t          j        |          s/|                    d          s| j                            |           |S )N_)super__new__inspect
isabstract
startswithregistryadd)mclsnamebasesmemberscls	__class__s        r+   r   z_FitterMeta.__new__   s_    ggoodD%99!#&& 	#ts/C/C 	#Mc"""
r*   )r%   r&   r'   r(   setr   r   __classcell__r   s   @r+   r   r      sO          suuH        r*   r   c                 >     t                     d fd	            }|S )a&  
    This is a decorator that can be used to add support for dealing with
    quantities to any __call__ method on a fitter which may not support
    quantities itself. This is done by temporarily removing units from all
    parameters then adding them back once the fitting has completed.
    Nc                 F   |                     dd           }t          |t                    p)t          |t                    pt          |t                    }|j        }|s|r2|j        rt          |j        ||j                  }	|j        t          |t                    r>|	                    |j        |j        d                  |	|j        d                            }t          |t                    r@|>|	                    |j        |j        d                  |	|j        d                            }|j        d         |i}
|!||
|j
        d         <   ||
|j        d         <   n||
|j
        d         <   d |
d<    |j        di |
}t          |t                    r|d         |
d<   |d         |
d<   |d         }d	}t          |t                    r
d
}|j        }nt          j        |          }t          |t                    r
d
}|j        }nt          j        |          }|3t          |t                    r
d
}|j        }nt          j        |          }| | |||fi |}n | ||||fi |}|r |j        di |
}|S t#          d           | |||fd|i|S )Nequivalenciesr   )r   r	   z_left_kwargsr^   _right_kwargsFTz9This model does not support being fit to data with units.r)   )popr`   r   
_has_units_supports_unit_fittingr   inputsinput_units_equivalenciesinput_unitstooutputswithout_units_for_datatuplevaluerI   asarraywith_units_from_dataNotImplementedError)r4   modelr<   yr   kwargsr   data_has_unitsmodel_has_unitsr   rename_dataadd_back_unitsxdataydatazdata	model_newfuncs                   r+   wrapperz$fitter_unit_support.<locals>.wrapper   s   

?D99 q(## '!X&&'!X&& 	  * U	:_ U	:+ Q -FL-1P- -) $0!!X.. DD!-el1o>*CELQRO*T !   "!X.. 1=DD!-el1o>*CELQRO*T !    %|A2=45Ka 0134KQ0045Ka 01'+K$ 54CC{CCeU++ %27(K/388K0!!HE "'a** *%)NGEEJqMMEa** *%)NGEEJqMME=!!X.. .)- ! "
19 $T5% I I& I III $T5% P P P PI " N >	 > M M M MI   *O  
 4eQ99Q9&999r*   r0   )r   )r   r   s   ` r+   fitter_unit_supportr      sA     4[[`: `: `: `: `: [`:D Nr*   c                   X    e Zd ZdZg Zd Zd Zed             Ze	j
        d             ZdS )r   z
    Base class for all fitters.

    Parameters
    ----------
    optimizer : callable
        A callable implementing an optimization algorithm
    statistic : callable
        Statistic function

    c                 @   |t          d          |t          d          t          j        |          r |            | _        n+t          j        |          r|| _        nt          d          t          j        |          r |            | _        d S || _        d S )NzExpected an optimizer.zExpected a statistic function.z8Expected optimizer to be a callable class or a function.)rf   r   isclass_opt_method
isfunction_stat_method)r4   	optimizer	statistics      r+   r5   zFitter.__init__A  s    5666=>>>?9%% 	Y(y{{D	** 	Y(DWXXX?9%% 	* )	D )Dr*   c                 t    |d         }|d         }t          ||            | j        ||g|dd         R  }|S )a  
        Function to minimize.

        Parameters
        ----------
        fps : list
            parameters returned by the fitter
        args : list
            [model, [other_args], [input coordinates]]
            other_args may include weights or any other quantities specific for
            a statistic

        Notes
        -----
        The list of arguments (args) is set in the `__call__` method.
        Fitters may overwrite this method, e.g. when statistic functions
        require other arguments.

        r   r	   )fitter_to_model_paramsr   )r4   fpsargsr   measress         r+   objective_functionzFitter.objective_functionR  sO    ( QBxuc***de9d1R4j999
r*   c                      dS )z
        When available, calculate and sets the parameter covariance matrix
        (model.cov_matrix) and standard deviations (model.stds).
        Nr)   r   s    r+   _add_fitting_uncertaintiesz!Fitter._add_fitting_uncertaintiesl  s	     tr*   c                      t          d          )z
        This method performs the actual fitting and modifies the parameter list
        of a model.
        Fitter subclasses should implement this method.
        z(Subclasses should implement this method.)r   rZ   s    r+   __call__zFitter.__call__t  s     ""LMMMr*   N)r%   r&   r'   r(   supported_constraintsr5   r   staticmethodr   abcabstractmethodr   r)   r*   r+   r   r   2  s        
 
 * * *"  4   \ 	N N N N Nr*   r   )	metaclassc                   z    e Zd ZdZdgZdZddZed             ZddZ	edd	            Z
dd
Zedd            ZdS )r   a_  
    A class performing a linear least square fitting.
    Uses `numpy.linalg.lstsq` to do the fitting.
    Given a model and data, fits the model to the data and changes the
    model's parameters. Keeps a dictionary of auxiliary fitting information.

    Notes
    -----
    Note that currently LinearLSQFitter does not support compound models.
    fixedTFc                 ,    d d d d d| _         || _        d S )N)	residualsranksingular_valuesrj   )fit_info_calc_uncertainties)r4   calc_uncertaintiess     r+   r5   zLinearLSQFitter.__init__  s,    #	
 
 $6   r*   c                     | j         d         | j         d         k    rdS t          j                            |           | j         d         k     rdS dS )z+Check if inverse of matrix can be obtained.r   r	   FT)shaperI   linalgmatrix_rank)ms    r+   _is_invertiblezLinearLSQFitter._is_invertible  sK     71:##59  ##agaj005tr*   Nc           	         t          j        |j        |          }t          |d          }	|                     |          sS t           j                            |          |Vt                    dk    rJd}
|	r|j        }
t           j	        
                    ||
          }d|                                |z
  z  |z  g}t                    dk    rg }t          t                              D ]}d}
|	r!|j        d|f                                         }
t           j	        
                    ||
          } |d          }t          j        |j                  |         }|                    d|                                |z
  z  t          j        |d|f         |z
  dz            z             Ɛn2t                    dk    r|d}
|	rt%          j        d	t(                     dS t           j	        
                    ||
          t           j	        
                    ||
          }}dt          |          |z
  z  |z  g}ng }t          t                              D ]} ||d          }d}
j        dk    rt          j        |d          }||         }|                    dt          |          |z
  z  t          j        ||         |z
  dz            z  g           fd
|D             }fdj        D             t          |          dk    r=t-          |d         j                  _        t3          |d                   _        dS fd|D             _        fd|D             _        dS )z
        Calculate and parameter covariance matrix and standard deviations
        and set `cov_matrix` and `stds` attributes.
        FmaskNr	   )r   .)model_set_axisr^   z_Calculation of fitting uncertainties for 2D models with masked values not currently supported.
c                     g | ]}|z  S r)   r)   )r;   rinv_x_dot_x_primes     r+   rv   z>LinearLSQFitter._add_fitting_uncertainties.<locals>.<listcomp>  s    333!!A%333r*   c                 P    g | ]"}j         |         d u j        |         d u  |#S Fr   tiedr;   r<   r   s     r+   rv   z>LinearLSQFitter._add_fitting_uncertainties.<locals>.<listcomp>  D     
 
 
A%''ejmu.D.D .D.D.Dr*   r   c                 :    g | ]}t          |j                  S r)   )r.   r2   )r;   covr   s     r+   rv   z>LinearLSQFitter._add_fitting_uncertainties.<locals>.<listcomp>  s&    SSSs
30A B BSSSr*   c                 0    g | ]}t          |          S r)   )ro   )r;   r   free_param_namess     r+   rv   z>LinearLSQFitter._add_fitting_uncertainties.<locals>.<listcomp>  s%    TTT,S2BCCTTTr*   )rI   dotThasattrr   r   invr9   r   maarraycountrangeflattenrollaxisr   appendsumwarningswarnr   r   r.   r2   r1   ro   rr   )r4   r   an_coeffr<   r   r   residsx_dot_x_primemaskedr   xxRSSjeval_yr   eval_zcovsr   r   s    `                @@r+   r   z*LinearLSQFitter._add_fitting_uncertainties  s   
 qsA!!V,, ""=11 	LIMM-8895zzQ "6DU[[[..RXXZZ'12f<=5zzA~~s5zz** 	 	AD 8 vc1f~5577QT22B"U2e<<<F[1EFFqIFJJbhhjj723rvqay6?QVW>W7X7XX   
 5zzQ M1 +	   FAD1125;;qt;3L3LASWWw./69:s5zz** 
 
A"U1a>>>FD+q00 "$VQ!7!7#AYFJJs1vv/0BFAaD6Ma;O4P4PPQ    4333s333
 
 
 
[
 
 
 t99>>)$q'53DEEE+DG5EFFEJJJSSSSdSSSETTTTtTTTEJJJr*   c                     |&t          j         | j        |g| j        R            }n&t          j         | j        ||g| j        R            }| j        r||         S |d|f         S )N.)rI   r   	fit_deriv
parameterscol_fit_deriv)r   param_indicesr<   r   ds        r+   _deriv_with_constraintsz'LinearLSQFitter._deriv_with_constraints  s}    9>U-=>>>??AAAA0@AAABBA 	)]##S-'((r*   c                    |t          |d          r4|j        -|                                |                                g|_        t          |d          r|j        	ddg|_        t          ||j        |j                  S t          |d          r4|j        -|                                |                                g|_        t          |d          r4|j        -|                                |                                g|_        t          |d          r|j        	d	d
g|_        t          |d          r|j	        	d	d
g|_	        t          ||j        |j                  }t          ||j        |j	                  }||fS )zd
        Maps domain into window for a polynomial model which has these
        attributes.
        Ndomainwindowr   r	   x_domainy_domainx_windowg            ?y_window)
r   r  minrD   r  r   r  r  r  r  )r4   r   r<   r   xnewynews         r+   _map_domain_windowz"LinearLSQFitter._map_domain_window  s^   
 9uh'' 2EL,@ !1uh'' 'EL,@ "Aw"1elELAAAuj)) 4en.D"#%%''15577!3uj)) 4en.D"#%%''15577!3uj)) -en.D"&uj)) -en.D"&"1enenEED"1enenEED:r*   c           	         |j         st          d          |j        st          d          t	          |d          rt          d          t          | j        |           |                                d_        t                    \  }}j
        dk    r|t          d          t          |||t                    j        	          }t          j                                                  }	|t#          j        |t&          
          }|	rMfdt)          t          j                            D             }
t#          j        fd|
D                       }t          |          dk    r|\  }}|:t          |||j        |j        k    rt                    ndj        	          \  }}t	          d          r|                     |          }|	rCt#          j        |                     |                    }|                     |
|          }n%t#          j         j        |gj        R            }                    |          }|}n|\  }}}|<t          ||||j        |j        k    rt                    ndj        	          \  }}}t	          d          r|                     ||          \  }}|	rEt#          j        |                     ||                    }|                     |
||          }n&t#          j         j        ||gj        R            }                    ||          }t                    dk    r߉j        pd}|j        dk    r=t#          j        |||j                  }|                    d|j        d                   }n|dk    r|j         n|}|||j        dk    r=t#          j        |||j                  }|                    d|j        d                   }n_|j        |j        k    r|dk    r|j         n|}n?|!                                }n*|!                                }||!                                }j"        rt#          j        |          j         }t#          j        |          j        dk    r$t          tG                    j$         d          |	r8j"        rt#          j        |          j         }||%                    |          z
  }|,t                    dk    r|dt"          j&        f         }||z
  }||j        dk    rt|j        |j        k    r1|dt"          j&        f         |ddt"          j&        f         z  }||z  }nQ||ddt"          j&        f         z  }||ddt"          j&        f         z  }n||ddt"          j&        f         z  }||z  }||z                      d          }||z  }t#          j'        t"          j(        )                    |                    }|7|s5t#          j'        t#          j*        |                    rt          d          d}|rt                    dk    s|"|j        dk    rt#          j+        |j        dd         |j        dd         z   |j,        
          }|j        dk    rt#          j        |dd          }n*t#          j-        ||j        dd         |j        z             }t]          ||j         |j                   D ]u\  }}}|r|j/         nta          d          }||         }||         dt"          j&        f         }|}t"          j1        2                    |||          \  }}}}|j         |dd<   vnS|r|j/         nta          d          }||         }t"          j1        2                    ||         ||         |          \  }}}}|| j3        d<   || j3        d<   || j3        d<   ||j        |j        k     r|ddt"          j&        f         n|z  }|| j3        d<   ti          |!                                           t	          d          r;t                    dk    r(|j5        |	z
  k     rtm          j7        dtp                     | j9        rKt          |          t          |          k    r+| :                    ||z  t          |          ||||           d_        S )a  
        Fit data to this model.

        Parameters
        ----------
        model : `~astropy.modeling.FittableModel`
            model to fit to x, y, z
        x : array
            Input coordinates
        y : array-like
            Input coordinates
        z : array-like, optional
            Input coordinates.
            If the dependent (``y`` or ``z``) coordinate values are provided
            as a `numpy.ma.MaskedArray`, any masked points are ignored when
            fitting. Note that model set fitting is significantly slower when
            there are masked points (not just an empty mask), as the matrix
            equation has to be solved for each model separately when their
            coordinate grids differ.
        weights : array, optional
            Weights for fitting.
            For data with Gaussian uncertainties, the weights should be
            1/sigma.
        rcond :  float, optional
            Cut-off ratio for small singular values of ``a``.
            Singular values are set to zero if they are smaller than ``rcond``
            times the largest singular value of ``a``.
        equivalencies : list or None, optional, keyword-only
            List of *additional* equivalencies that are should be applied in
            case x, y and/or z have units. Default is None.

        Returns
        -------
        model_copy : `~astropy.modeling.FittableModel`
            a copy of the input model with parameters set by the fitter

        z)Model must be a subclass of FittableModelzIModel is not linear in parameters, linear fit methods should not be used.submodel_namesz"Model must be simple, not compoundFr^   Nz.Expected x, y and z for a 2 dimensional model.)n_modelsr   dtypec                     g | ]}|v|	S r)   r)   )r;   idxfitparam_indicess     r+   rv   z,LinearLSQFitter.__call__.<locals>.<listcomp>\  s/          ... ...r*   c                 P    g | ]"}t          j        |                   j        #S r)   )getattrr2   r   )r;   r#  
model_copys     r+   rv   z,LinearLSQFitter.__call__.<locals>.<listcomp>f  s>        J
(>s(CDDJ  r*   r	   r  )r<   r  r<   r   r   r   z5 gives unsupported >2D derivative matrix for this x/y.zFound NaNs in the coefficient matrix, which should not happen and would crash the lapack routine. Maybe check that weights are not null.r   r   r   rj   _orderz"The fit may be poorly conditioned
T);fittablerf   linearr    r   _validate_constraintsr   copysync_constraintsmodel_to_fit_paramsn_inputs_convert_inputr9   r   r   r   valuesrI   r   floatr   r2   ndimr  r  r  r  sum_of_implicit_terms
asanyarrayr   reshaper   r   r   r  typer%   r   newaxisanyr   getmaskisnanzerosr!  broadcast_tozipr   slicer   lstsqr   r   r)  r   r   r   r   r   ) r4   r   r<   r   r   weightsrcondr   fargn_fixedfixparam_indices	fixparamslhs	fixderivsr5  rhs
model_axissclr  r   lacoef	lhs_stack	model_lhs	model_rhsmodel_lacoefgoodt_coefr  r   svalr$  r'  s                                  @@r+   r   zLinearLSQFitter.__call__  s	   N ~ 	JHIII| 	%9  
 5*++ 	CABBBd8%@@@ZZ\\
&+
#!4Z!@!@Q!##	MNNNq!c*ooj>W
 
 
 j&--//00 j666G 	        Z%;!<!<==      
   /   I t99>>DAq" ,070F0FS___A#-#<	  
7 z8,, ;++J:: Rj00=MQR0SS  !88 0A 9  		 j!5!5a!P*:O!P!P!PQQ$.$D$DQ$G$G!CCGAq!" !/070F0FS___A#-#<! ! !1g z:.. A..z1a@@1 Xj00=MQRVW0XX  !88 0A 9  		 m$8J$8A$V
@U$V$V$VWW$.$D$DQ$J$J!:""'6;!
6A::
 +aQV<<C++b#)B-88CC ",q!##aC&|a''"$+gz7<"P"P")//"gmB6G"H"H ///9Q'))G #*//"3"3iikk&%oo//G # 	$*S//#C ="Q&&
##, 1 1 1    	1' 4Jy113		i000C !, :""(=c2:o(N%--Cx1}}=CI-- c2:o.BJ1GGC-CC7111bj=11C 2: 66CCwqqq"*}--GmSyooa  s
c**++v"&#2G2GB    0	Vs:**GL1$4$4 Xci!nsy~=SYOOOF x!||KR33		OC233#)1KLL	 7:)SUFH6U6U + +2	9l*0A	eDkk%dO	%dOCO<	 .0Y__y%. .*d #)(QQQ+$ !'7CH99E$KKDD	A)+TCIu)U)U&FFD$%+k" $f+/'(38(;(;#aaam$$D"(hz6>>+;+;<<<
 J))	UJ1$$
)G344M?ASTTT # 	1vvF##//CVaAv   '+
#r*   r   NNr0   NNN)r%   r&   r'   r(   r   supports_masked_inputr5   r   r   r   r  r  r   r   r)   r*   r+   r   r     s        	 	 %I 6 6 6 6   \JU JU JU JUX 	) 	) 	) \	)   2      r*   r   c                   .    e Zd ZdZddZd Zd Zd	dZdS )
r   a  
    This class combines an outlier removal technique with a fitting procedure.
    Basically, given a maximum number of iterations ``niter``, outliers are
    removed and fitting is performed for each iteration, until no new outliers
    are found or ``niter`` is reached.

    Parameters
    ----------
    fitter : `Fitter`
        An instance of any Astropy fitter, i.e., LinearLSQFitter,
        LevMarLSQFitter, SLSQPLSQFitter, SimplexLSQFitter, JointFitter. For
        model set fitting, this must understand masked input data (as
        indicated by the fitter class attribute ``supports_masked_input``).
    outlier_func : callable
        A function for outlier removal.
        If this accepts an ``axis`` parameter like the `numpy` functions, the
        appropriate value will be supplied automatically when fitting model
        sets (unless overridden in ``outlier_kwargs``), to find outliers for
        each model separately; otherwise, the same filtering must be performed
        in a loop over models, which is almost an order of magnitude slower.
    niter : int, optional
        Maximum number of iterations.
    outlier_kwargs : dict, optional
        Keyword arguments for outlier_func.

    Attributes
    ----------
    fit_info : dict
        The ``fit_info`` (if any) from the last iteration of the wrapped
        ``fitter`` during the most recent fit. An entry is also added with the
        keyword ``niter`` that records the actual number of fitting iterations
        performed (as opposed to the user-specified maximum).
    rX   c                 P    || _         || _        || _        || _        dd i| _        d S )Nniter)fitteroutlier_funcrZ  outlier_kwargsr   )r4   r[  r\  rZ  r]  s        r+   r5   z"FittingWithOutlierRemoval.__init__y  s/    (
, $r*   c                 d    d| j         j        j         d| j        j         d| j         d| j         S )NzFitter: z
Outlier function: z
Num. of iterations: z
Outlier func. args.: )r[  r   r%   r\  rZ  r]  rZ   s    r+   __str__z!FittingWithOutlierRemoval.__str__  sT    :t{,5 : :!%!2!;: :#':: : %)$7: :	
r*   c           
      ~    | j         j         d| j        j         j         d| j        j         d| j         d| j         d
S )Nz	(fitter: z, outlier_func: z	, niter: z, outlier_kwargs: ))r   r%   r[  r\  rZ  r]  rZ   s    r+   r[   z"FittingWithOutlierRemoval.__repr__  so    ~& L L1F1O L L!.7L LzL L595HL L L	
r*   Nc           	         t          |          dk    rdnSt          | j        d          r| j        j        dur)t	          t          | j                  j         d          |j        ||f}|}n||f}|}Ndk     r
|j        z  d| j	        vr5t          fdt          |j                  D                       | j	        d<   d	}	 | j        ||||fd
|i|}
t          j                            |          }|j        t          j        j        u rd	|_        |}|j                                        }d}t          d| j        dz             D ]} |
|dd	i}|	s	  | j        ||z
  fi | j	        }n# t(          $ r  | j	                            dd           d}	t          j                            |t          j        ||          d          }|j        t          j        j        u rd	|_        t          j        |d          }t          j        |j        d          }Y nw xY w|	r{t          j        |d          }t1          |||          D ]9\  }}} | j        ||z
  fi | j	        }|j        |j        dd<   |j        |dd<   :t5          j        dt8                     ||z  }?|j         ||         } | j        |
gfd|D             |j                 R d
|i|}
n | j        |
g||R d
|i|}
|j                                        }||k    r n|}d|i| _        | j                            t?          | j        di                      |
|j        fS )a  
        Parameters
        ----------
        model : `~astropy.modeling.FittableModel`
            An analytic model which will be fit to the provided data.
            This also contains the initial guess for an optimization
            algorithm.
        x : array-like
            Input coordinates.
        y : array-like
            Data measurements (1D case) or input coordinates (2D case).
        z : array-like, optional
            Data measurements (2D case).
        weights : array-like, optional
            Weights to be passed to the fitter.
        kwargs : dict, optional
            Keyword arguments to be passed to the fitter.

        Returns
        -------
        fitted_model : `~astropy.modeling.FittableModel`
            Fitted model after outlier removal.
        mask : `numpy.ndarray`
            Boolean mask array, identifying which points were used in the final
            fitting iteration (False) and which were found to be outliers or
            were masked in the input (True).
        r	   NrW  Tz) cannot fit model sets with masked valuesr   axisc              3   (   K   | ]}|k    |V  d S r0   r)   )r;   nr   s     r+   r=   z5FittingWithOutlierRemoval.__call__.<locals>.<genexpr>  s6       4 413F3FA3F3F3F3F4 4r*   FrB  r   )r!  r-  zMoutlier_func did not accept axis argument; reverted to slow loop over models.c              3   (   K   | ]}|         V  d S r0   r)   )r;   crR  s     r+   r=   z5FittingWithOutlierRemoval.__call__.<locals>.<genexpr>*  s'      ..!ag......r*   rZ  r   ) r9   r   r[  rW  rf   r8  r%   r   r4  r]  r   r   rI   r   masked_arrayr   nomaskr   rZ  r\  ri   r   result_typer   r?  datar   r   r   r   updater&  )r4   r   r<   r   r   rB  r   coordsrk  loopfitted_modelfiltered_datafiltered_weightslast_n_maskedre  
model_valsdata_Tmask_Tmodel_vals_Trow_datarow_maskrow_mod_valsmasked_residualsthis_n_maskedrR  r   s                           @@r+   r   z"FittingWithOutlierRemoval.__call__  s   L u::??!NN DK)@AA;4D@@ DK((1      #1N 9TFDDTFD %!!$)+T000.3 4 4 4 4$TY//4 4 4 / /#F+  #t{5!QMM7MfMM**400--!&M"%*..00 q$*q.)) X	* X	*A%vDeDDJ  TT$5D$5%
2% %6:6I% %MM
 ! T T T%-+//===# )+(:(:)"$.
"K"K!% ); ) ) )-==16M.
 "$]NA!N!N!#]-?QR!S!S+T.  !{:~qII8;FL9 9 8 84Hh (9t'8 </( (373F( ($ (8'<HM!!!$"2"7HQQQKK 9&   Z'M %%**&'.t}$*t{  ....v...  "&t,      -	 
      +t{    "      -	 
     *.2244M--)MM !WT[*bAABBB]///s    E77B/H)(H))rX   rU  )r%   r&   r'   r(   r5   r_  r[   r   r)   r*   r+   r   r   V  sk           D( ( ( (
 
 

 
 
s0 s0 s0 s0 s0 s0r*   r   c                        e Zd ZdZg dZ	 d fd	Zd Zed             Zedd	            Z		 dd
Z
d ZddZeddeeeddfd            Z xZS )_NonLinearLSQFittera  
    Base class for Non-Linear least-squares fitters.

    Parameters
    ----------
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False
    use_min_max_bounds : bool
        If the set parameter bounds for a model will be enforced each given
        parameter while fitting via a simple min/max condition.
        Default: True
    )r   r   boundsFTc                 r    d | _         || _        || _        t                                                       d S r0   )r   r   _use_min_max_boundsr   r5   r4   r   use_min_max_boundsr   s      r+   r5   z_NonLinearLSQFitter.__init__Y  s6    #5 #5 r*   c                 `   |d         }|d         }t          ||| j                   |d         }|#t          j         ||dd          |z
            }n%t          j        | ||dd          |z
  z            }t          j        t          j        |                    st          d          |S )z
        Function to minimize.

        Parameters
        ----------
        fps : list
            parameters returned by the fitter
        args : list
            [model, [weights], [input coordinates]]

        r   r	   r   Nr^   zObjective function has encountered a non-finite value, this will cause the fit to fail!
Please remove non-finite values from your input data before fitting to avoid this error.)r   r  rI   ravelrg   isfiniter#   )r4   r   r   r   rB  r   r   s          r+   r   z&_NonLinearLSQFitter.objective_function_  s     Qq'uc4+CDDDBx?HUUD2J/$677EEHWtAbDz(:T(ABCCEvbk%(()) 	%/   r*   c                       fd j         D             }t          ||           _        t          ||           _        dS )z
        Set ``cov_matrix`` and ``stds`` attributes on model with parameter
        covariance matrix returned by ``optimize.leastsq``.
        c                 P    g | ]"}j         |         d u j        |         d u  |#S r   r   r   s     r+   rv   zB_NonLinearLSQFitter._add_fitting_uncertainties.<locals>.<listcomp>  r   r*   N)r   r.   r1   ro   rr   )r   r1   r   s   `  r+   r   z._NonLinearLSQFitter._add_fitting_uncertainties  sY    
 
 
 
[
 
 
 &j2BCC'
4DEE


r*   Nc           
      4   |d}t          j                                                  s't          j                                                  rt	          |            |at          j         j        |gj        R            }j	        st          j
        |          |j        z  }nt          j
        |          |z  }nkt          j        d  j        ||gj        R  D                       }j	        st          j
        |          |j        z  }nt          j
        |          |z  }fdj        D             }d |D             }	d |D             }
t          t          j        d |D             d|
                    }
t          j        |	|
          }t          j        |          }j	        s2t          j        |t          j        |                             j        }n|t          j        |                   }d	 |D             S |t          j         j        |g| R            }	 t          j        d
 t          j        |          |z  D                       }|j        |j        k    r8t          j        d t          j        |          j        |z  D                       }|S # t*          $ r\ t          j        d t          j        |          t          j        |dd          z  D                                                       cY S w xY wj	        sJd t          j
        |          t          j         j        ||g| R            j        z  j        D             S d |t          j         j        ||g| R            z  D             S )au  
        Wraps the method calculating the Jacobian of the function to account
        for model constraints.
        `scipy.optimize.leastsq` expects the function derivative to have the
        above signature (parlist, (argtuple)). In order to accommodate model
        constraints, instead of using p directly, we set the parameter list in
        this function.
        Nr  c                 6    g | ]}t          j        |          S r)   rI   r  r;   r   s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s     SSSQRXa[[SSSr*   c                 0    g | ]}t          |          S r)   )r&  )r;   r   r   s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s#    GGGTGE4((GGGr*   c                     g | ]	}|j         
S r)   )r   r;   pars     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s    ///3SY///r*   c                     g | ]	}|j         
S r)   r   r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s    ---CH---r*   c                      g | ]}|j         d uS r   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s    !H!H!HC#(%"7!H!H!Hr*   Tc                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s     222ABHQKK222r*   c                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s     LLL!LLLr*   c                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s     WWWQRXa[[WWWr*   c                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s0        ! HQKK  r*   r   r   c                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s0          r*   c                 6    g | ]}t          j        |          S r)   r  r  s     r+   rv   z3_NonLinearLSQFitter._wrap_deriv.<locals>.<listcomp>  s0        HQKK  r*   )r:  r   r2  r   r   rI   r   r  r  r  r  r   r2   listwhere
logical_orlogical_notr   nonzeror   
atleast_2drf   moveaxis	transpose)rj   r   rB  r<   r   r   full
full_derivparsr   r   fix_and_tieindresiduesr  outputs    `              r+   _wrap_derivz_NonLinearLSQFitter._wrap_deriv  s    ?Gu{!!##$$ >	EJ,=,=,?,?(@(@ >	"5&111yx EE4D E E EFF* :!#'!2!2TV!;JJ!#'!2!2T!9JJxSS/%/!Q*RAQ*R*R*RSSS  * :!#'!2!2TV!;JJ!#'!2!2T!9JGGGGU5FGGGD//$///E-----D!H!H4!H!H!H$PTUUVVD-t44K.--C& 7:jC&ABBD%bjoo6222222yH_U_Q%@%@%@%@AA	"XLLbhw.?.?).KLLL F |y66!#WW"-2H2H2JY2VWWW" " "M! " " "8 %'Xg%6%6YPRTU9V9V%V   
  ikk" " "" *   HW-- huq!'Ef'E'E'EFFHI    $rx10Nv0N0N0N'O'OO   s   5A<J2 2A#LLc                    t          |          t          |          k    ri|g|| j        d<   |Zt          j         | j        |g|R  dz            }t          |          t          |          z
  }	| j        dxx         ||	z  z  cc<   n
d | j        d<   | j        du r0| j        d         %|                     || j        d                    d S d S d S )N	param_covr^   T)r9   r   rI   r   r   r   r   )
r4   r   r   init_valuescov_x	fitparamsrD  rB  sum_sqrsdofs
             r+   _compute_param_covz&_NonLinearLSQFitter._compute_param_cov  s     FFS%%%%5+<).DM+& 6"9$"9)"Kd"K"K"Kq"PQQ!ffs;///k***hn<***)-DM+&#t++}[)5//t}[7QRRRRR ,+55r*   c                     dS )NrV  r)   )r4   r   rD  maxiteraccepsilonestimate_jacobians          r+   _run_fitterz_NonLinearLSQFitter._run_fitter  s    r*   c                 j   d}|t          j        |t                    nt          j        |          }||t          j        |          nt          j        |          z  }t          j        |          st          j        |t                     ||         ||         |dn||         |dn||         fS )z
        Filter out non-finite values in x, y, z.

        Returns
        -------
        x, y, z : ndarrays
            x, y, and z with non-finite values filtered out.
        z5Non-Finite input data has been removed by the fitter.Nr   )rI   	ones_likeboolr  rg   r   r   r   )r4   r<   r   r   rB  MESSAGEr   s          r+   _filter_non_finitez&_NonLinearLSQFitter._filter_non_finite  s     J.5or|AT****2;wCWCW!)AQ?vd|| 	7M'#5666 dGdGIDD1T7ODD	
 	
r*   c           	      (   t          || j                  }d|_        |
r|                     ||||          \  }}}}||ft	          |||          z   }|                     ||||||	          \  }}}|                     |||||||           d|_        |S )av  
        Fit data to this model.

        Parameters
        ----------
        model : `~astropy.modeling.FittableModel`
            model to fit to x, y, z
        x : array
           input coordinates
        y : array
           input coordinates
        z : array, optional
           input coordinates
        weights : array, optional
            Weights for fitting. For data with Gaussian uncertainties, the weights
            should be 1/sigma.

            .. versionchanged:: 5.3
                Calculate parameter covariances while accounting for ``weights``
                as "absolute" inverse uncertainties. To recover the old behavior,
                choose ``weights=None``.

        maxiter : int
            maximum number of iterations
        acc : float
            Relative error desired in the approximate solution
        epsilon : float
            A suitable step length for the forward-difference
            approximation of the Jacobian (if model.fjac=None). If
            epsfcn is less than the machine precision, it is
            assumed that the relative errors in the functions are
            of the order of the machine precision.
        estimate_jacobian : bool
            If False (default) and if the model has a fit_deriv method,
            it will be used. Otherwise the Jacobian will be estimated.
            If True, the Jacobian will be estimated in any case.
        equivalencies : list or None, optional, keyword-only
            List of *additional* equivalencies that are should be applied in
            case x, y and/or z have units. Default is None.
        filter_non_finite : bool, optional
            Whether or not to filter data with non-finite values. Default is False

        Returns
        -------
        model_copy : `~astropy.modeling.FittableModel`
            a copy of the input model with parameters set by the fitter

        FT)_validate_modelr   r.  r  r1  r  r  )r4   r   r<   r   r   rB  r  r  r  r  filter_non_finiter'  rD  r  r  r  s                   r+   r   z_NonLinearLSQFitter.__call__  s    | %UD,FGG
&+
# 	I#66q!QHHAq!W
 1a##$
 )-(8(8gsG5F)
 )
%Y 	;y$	
 	
 	
 "&r*   )FTr0   rU  )r%   r&   r'   r(   r   r5   r   r   r   r  r  r  r  r   r   r
   r   r   r   r   s   @r+   r}  r}  E  s/         877       @ F F \F J J J \JZ FJS S S S0     
 
 
 
0  P P P P P P P Pr*   r}  c                   *     e Zd ZdZd fd	Zd Z xZS )r   a  
    Levenberg-Marquardt algorithm and least squares statistic.

    Parameters
    ----------
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False

    Attributes
    ----------
    fit_info : dict
        The `scipy.optimize.leastsq` result for the most recent fit (see
        notes).

    Notes
    -----
    The ``fit_info`` dictionary contains the values returned by
    `scipy.optimize.leastsq` for the most recent fit, including the values from
    the ``infodict`` dictionary it returns. See the `scipy.optimize.leastsq`
    documentation for details on the meaning of these values. Note that the
    ``x`` return value is *not* included (as it is instead the parameter values
    of the returned model).
    Additionally, one additional element of ``fit_info`` is computed whenever a
    model is fit, with the key 'param_cov'. The corresponding value is the
    covariance matrix of the parameters as a 2D numpy array.  The order of the
    matrix elements matches the order of the parameters in the fitted model
    (i.e., the same order as ``model.param_names``).

    Fc           
      l    t                                          |           d d d d d d d d d d	| _        d S )N)	nfevfvecfjacipvtqtfmessageierr	param_jacr  )r   r5   r   r4   r   r   s     r+   r5   zLevMarLSQFitter.__init__  sI    +,,,

 

r*   c                    ddl m} |j        |rd }n| j        }t	          |          \  }	}
}
|                    | j        |	|||j        |||d	  	        \  }}}}}t          ||           | j	        
                    |           || j	        d<   || j	        d<   || j	        d<   |dvrt          j        d	t                     |	||fS )
Nr   optimizeT)r   Dfun	col_derivmaxfevepsfcnxtolfull_outputr  r  r  )r	   r^   rX      zLThe fit may be unsuccessful; check fit_info['message'] for more information.)scipyr  r  r  r/  leastsqr   r  r   r   rl  r   r   r   )r4   r   rD  r  r  r  r  r  dfuncr  r   r  r  dinfomessr  s                   r+   r  zLevMarLSQFitter._run_fitter  s   """"""?"&7"EE$E/66Q.6.>.>#) /? 
/
 
/
+	5%t 	ui000U###!&g#'i  $f|##M<"   Iu,,r*   r   r%   r&   r'   r(   r5   r  r   r   s   @r+   r   r   b  sV         >
 
 
 
 
 
- - - - - - -r*   r   c                   *     e Zd ZdZd fd	Zd Z xZS )_NLLSQFitteruj  
    Wrapper class for `scipy.optimize.least_squares` method, which provides:
        - Trust Region Reflective
        - dogbox
        - Levenberg-Marqueardt
    algorithms using the least squares statistic.

    Parameters
    ----------
    method : str
        ‘trf’ :  Trust Region Reflective algorithm, particularly suitable
            for large sparse problems with bounds. Generally robust method.
        ‘dogbox’ : dogleg algorithm with rectangular trust regions, typical
            use case is small problems with bounds. Not recommended for
            problems with rank-deficient Jacobian.
        ‘lm’ : Levenberg-Marquardt algorithm as implemented in MINPACK.
            Doesn’t handle bounds and sparse Jacobians. Usually the most
            efficient method for small unconstrained problems.
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False
    use_min_max_bounds: bool
        If the set parameter bounds for a model will be enforced each given
        parameter while fitting via a simple min/max condition. A True setting
        will replicate how LevMarLSQFitter enforces bounds.
        Default: False

    Attributes
    ----------
    fit_info :
        A `scipy.optimize.OptimizeResult` class which contains all of
        the most recent fit information
    Fc                 Z    t                                          ||           || _        d S r0   )r   r5   _method)r4   methodr   r  r   s       r+   r5   z_NLLSQFitter.__init__  s*    +-?@@@r*   c                     ddl m} ddlm} |j        |rd}	nd fd	}
|
}	t          |          \  }}} j        rt          j         t          j        f}|	                     j
        |||	|t          j        |          | j        |	  	         _         | j        j        d          \  }}}t          j        t                     j        t%           j        j        j                  z  |d         z  }|||k             }|d |j                 }t          j        |j        |d	z  z  |          }t/          | j        j        d            j        j        s't5          j        d
 j        j         t:                     | j        j        |fS )Nr   r  )svdz2-pointc           
          |j         r,t          j                            | |||||                    S                     | |||||          S r0   )r  rI   r  r  )rj   r   rB  r<   r   r   r4   s         r+   _dfuncz(_NLLSQFitter._run_fitter.<locals>._dfunc  s^    & M<((AqII    ++FE7Aq!LLLr*   )r   jacmax_nfev	diff_stepr  r  r~  F)full_matricesr^   z)The fit may be unsuccessful; check: 
    r0   )r  r  scipy.linalgr  r  r/  r  rI   infleast_squaresr   ru   r  r   r  finfor3  epsrD   r   sizer   r   r   r<   successr   r   r  r   )r4   r   rD  r  r  r  r  r  r  r  r  r  r   r~  sVT	thresholdr  s   `                 r+   r  z_NLLSQFitter._run_fitter  s   """"""$$$$$$?"&7"EEM M M M M M E!4U!;!;Q # 	'vgrv&F ..#gg&&< / 

 

 3t}(>>>1bHUOO'#dm.?.E*F*FF1M	a)m!&\rtad{B''udmou===}$ 	MTT]=RTT"  
 DMOU22r*   FFr  r   s   @r+   r  r    sW           D     83 83 83 83 83 83 83r*   r  c                   $     e Zd ZdZd fd	Z xZS )r   a  
    Trust Region Reflective algorithm and least squares statistic.

    Parameters
    ----------
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False
    use_min_max_bounds: bool
        If the set parameter bounds for a model will be enforced each given
        parameter while fitting via a simple min/max condition. A True setting
        will replicate how LevMarLSQFitter enforces bounds.
        Default: False

    Attributes
    ----------
    fit_info :
        A `scipy.optimize.OptimizeResult` class which contains all of
        the most recent fit information
    Fc                 N    t                                          d||           d S )Ntrfr   r5   r  s      r+   r5   zTRFLSQFitter.__init__*  s'     24FGGGGGr*   r  r%   r&   r'   r(   r5   r   r   s   @r+   r   r     sQ         *H H H H H H H H H Hr*   r   c                   $     e Zd ZdZd fd	Z xZS )r   a  
    DogBox algorithm and least squares statistic.

    Parameters
    ----------
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False
    use_min_max_bounds: bool
        If the set parameter bounds for a model will be enforced each given
        parameter while fitting via a simple min/max condition. A True setting
        will replicate how LevMarLSQFitter enforces bounds.
        Default: False

    Attributes
    ----------
    fit_info :
        A `scipy.optimize.OptimizeResult` class which contains all of
        the most recent fit information
    Fc                 N    t                                          d||           d S )Ndogboxr  r  s      r+   r5   zDogBoxLSQFitter.__init__D  s'    #57IJJJJJr*   r  r  r   s   @r+   r   r   .  sQ         *K K K K K K K K K Kr*   r   c                   $     e Zd ZdZd fd	Z xZS )r   a  
    `scipy.optimize.least_squares` Levenberg-Marquardt algorithm and least squares statistic.

    Parameters
    ----------
    calc_uncertainties : bool
        If the covarience matrix should be computed and set in the fit_info.
        Default: False

    Attributes
    ----------
    fit_info :
        A `scipy.optimize.OptimizeResult` class which contains all of
        the most recent fit information
    Fc                 N    t                                          d|d           d S )NlmTr  r  s     r+   r5   zLMLSQFitter.__init__Y  s&    1488888r*   r   r  r   s   @r+   r   r   H  sG          9 9 9 9 9 9 9 9 9 9r*   r   c                   H     e Zd ZdZej        Z fdZedd            Z xZ	S )r   a+  
    Sequential Least Squares Programming (SLSQP) optimization algorithm and
    least squares statistic.

    Raises
    ------
    ModelLinearityError
        A linear model is passed to a nonlinear fitter

    Notes
    -----
    See also the `~astropy.modeling.optimizers.SLSQP` optimizer.

    c                 p    t                                          t          t                     i | _        d S N)r   r   )r   r5   r   r   r   r4   r   s    r+   r5   zSLSQPLSQFitter.__init__o  s+    5K@@@r*   Nc                    t          || j        j                  }d|_        t	          |||          }||f|z   }t          |          \  }	}
}
 | j        | j        |	|fi |\  }| _        t          ||           d|_        |S )a  
        Fit data to this model.

        Parameters
        ----------
        model : `~astropy.modeling.FittableModel`
            model to fit to x, y, z
        x : array
            input coordinates
        y : array
            input coordinates
        z : array, optional
            input coordinates
        weights : array, optional
            Weights for fitting.
            For data with Gaussian uncertainties, the weights should be
            1/sigma.
        kwargs : dict
            optional keyword arguments to be passed to the optimizer or the statistic
        verblevel : int
            0-silent
            1-print summary upon completion,
            2-print summary after each iteration
        maxiter : int
            maximum number of iterations
        epsilon : float
            the step size for finite-difference derivative estimates
        acc : float
            Requested accuracy
        equivalencies : list or None, optional, keyword-only
            List of *additional* equivalencies that are should be applied in
            case x, y and/or z have units. Default is None.

        Returns
        -------
        model_copy : `~astropy.modeling.FittableModel`
            a copy of the input model with parameters set by the fitter

        FT	r  r   r   r.  r1  r/  r   r   r   r4   r   r<   r   r   rB  r   r'  rD  r  r   r  s               r+   r   zSLSQPLSQFitter.__call__s  s    R %UD,<,RSS
&+
#aA&&
  0
;;Q#34#3#[$$
 $
:@$
 $
 	4= 	z9555&*
#r*   rU  )
r%   r&   r'   r(   r   r   r5   r   r   r   r   s   @r+   r   r   ]  sn          "7     6 6 6 6 6 6 6 6r*   r   c                   H     e Zd ZdZej        Z fdZedd            Z xZ	S )r   z
    Simplex algorithm and least squares statistic.

    Raises
    ------
    `ModelLinearityError`
        A linear model is passed to a nonlinear fitter

    c                 p    t                                          t          t                     i | _        d S r  )r   r5   r   r   r   r   s    r+   r5   zSimplexLSQFitter.__init__  s+    7kBBBr*   Nc                    t          || j        j                  }d|_        t	          |||          }||f|z   }t          |          \  }	}
}
 | j        | j        |	|fi |\  }| _        t          ||           d|_        |S )a9  
        Fit data to this model.

        Parameters
        ----------
        model : `~astropy.modeling.FittableModel`
            model to fit to x, y, z
        x : array
            input coordinates
        y : array
            input coordinates
        z : array, optional
            input coordinates
        weights : array, optional
            Weights for fitting.
            For data with Gaussian uncertainties, the weights should be
            1/sigma.
        kwargs : dict
            optional keyword arguments to be passed to the optimizer or the statistic
        maxiter : int
            maximum number of iterations
        acc : float
            Relative error in approximate solution
        equivalencies : list or None, optional, keyword-only
            List of *additional* equivalencies that are should be applied in
            case x, y and/or z have units. Default is None.

        Returns
        -------
        model_copy : `~astropy.modeling.FittableModel`
            a copy of the input model with parameters set by the fitter

        FTr  r  s               r+   r   zSimplexLSQFitter.__call__  s    F %UD,<,RSS
&+
#aA&&
 
 0
;;Q#34#3#[$$
 $
:@$
 $
 	4= 	z9555&*
#r*   rU  )
r%   r&   r'   r(   r   r   r5   r   r   r   r   s   @r+   r   r     sn          $9     1 1 1 1 1 1 1 1r*   r   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r   aH  
    Fit models which share a parameter.
    For example, fit two gaussians to two data sets but keep
    the FWHM the same.

    Parameters
    ----------
    models : list
        a list of model instances
    jointparameters : list
        a list of joint parameters
    initvals : list
        a list of initial values

    c                 &   t          |          | _        t          |          | _        || _        |                                  |                                 | _        d | j        D             | _        t          j	        | j                  | _
        d S )Nc                     g | ]	}|j         
S r)   )r0  )r;   r   s     r+   rv   z(JointFitter.__init__.<locals>.<listcomp>  s    :::!*:::r*   )r  modelsinitvalsjointparams_verify_inputr/  r  	modeldimsrI   r   r4  )r4   r
  jointparametersr  s       r+   r5   zJointFitter.__init__  sy    6llX*1133 ;:dk:::F4>**			r*   c                    g }|                     | j                   | j        D ]Z}|j                                        }| j        |         }|j        }|D ]}||         d         }||= |                     |           [|S )Nr@  )extendr  r
  r  tolistr  _param_metrics)r4   fparamsr   rj   joint_paramsparam_metrics
param_nameslice_s           r+   r/  zJointFitter.model_to_fit_params  s    t}%%%[ 	# 	#E%,,..F+E2L!0M* # #
&z27;6NNNN6""""r*   c                    t          |          }g }t          |          }t          | j                  }|d|         }|d|= | j        D ]}| j        |         }	|d|j        dz            }
|d|j        dz   = t          |j                  t          |	          z
  }|d|         }|d|= g }|j        }|j        D ]w}||	v r2|		                    |          }|
                    ||         g           8||         d         }|j        |j        z
  }|
                    |d|                    |d|= x |j        |
dd         g|R  }|
                    ||
d         z
              t          j        |          S )aQ  
        Function to minimize.

        Parameters
        ----------
        fps : list
            the fitted parameters - result of an one iteration of the
            fitting algorithm
        args : dict
            tuple of measured and input coordinates
            args is always passed as a tuple from optimize.leastsq

        Nr	   r@  r   )r  r9   r  r
  r  r0  _parametersr  r2   rh   r  stopstartevaluaterI   r  )r4   r   r   	lstsqargsfittedr  numjpjointfitparamsr   r  margsnumfpmfparamsmparamsr  r  rh   r  plenmodelfits                       r+   r   zJointFitter.objective_function  s    JJ	II	DM"""6E6*fuf[ 	0 	0E+E2L2 223E.ENQ../)**S->->>E %(H&5&!G!0M#/ 
( 
(
--(..z::E NNN5$9#:;;;;*:6w?F!;5DNN8ETE?333 $%u~eCRCj;7;;;HMM(U2Y.////xr*   c                 X   t          | j                  dk    r%t          dt          | j                   d          t          | j                                                  dk     r7t          dt          | j                                                   d          | j                                        D ]q}t          | j        |                   t          | j                  k    r?t          t          | j        |                    dt          | j                   d          rd S )Nr	   zExpected >1 models, z	 is givenr^   z&At least two parameters are expected, z parameter(s) provided but z	 expected)r9   r
  ri   r  keysr  )r4   r  s     r+   r  zJointFitter._verify_inputN  sA   t{q  N3t{3C3CNNNOOOt$$&&''!++;t',,..//; ; ;   !&&(( 	 	A4#A&''3t}+=+===4+A.// B B$'$6$6B B B   >	 	r*   c                 f   ddl m} t          |          t          d | j                  k    r7t          dt          d | j                   dt          |           d          |                    | j        | j        |          \  | j        d	d	<   }| j        d	d	         }t          | j	                  }|d	|         }|d	|= | j
        D ]}| j        |         }t          |j                  t          |          z
  }	|d	|	         }
|d	|	= g }|j        }|j        D ]w}||v r2|                    |          }|                    ||         g           8||         d
         }|j        |j        z
  }|                    |
d	|                    |
d	|= xt'          j        |          |_        d	S )zk
        Fit data to these models keeping some of the parameters common to the
        two models.
        r   r  c                     | dz   |z   dz   S Nr	   r)   r(  s     r+   <lambda>z&JointFitter.__call__.<locals>.<lambda>d  s    AEAIM r*   z	Expected c                     | dz   |z   dz   S r,  r)   r(  s     r+   r-  z&JointFitter.__call__.<locals>.<lambda>f  s    A	A r*   z coordinates in args but z	 providedr   Nr@  )r  r  r9   r   r  rf   r  r   r  r  r
  r  r  r  r2   rh   r  r  r  rI   r   r  )r4   r   r  r   r  r   r!  r   r  r#  r$  r%  r  r  rh   r  r&  s                    r+   r   zJointFitter.__call__]  s   
 	#"""""t99994>JJJJ@F#=#=t~NN @ @+.t99@ @ @  
  (//#T^$  0  
  
qqq1 .#DM"" %FUFO[ 	1 	1E+E2L)**S->->>EvvHG!0M#/ 
( 
(
--(..z::E NNN5$9#:;;;;*:6w?F!;5DNN8ETE?333 $!x00E+	1 	1r*   N)	r%   r&   r'   r(   r5   r/  r   r  r   r)   r*   r+   r   r     sj          
+ 
+ 
+  /  /  / b  ,1 ,1 ,1 ,1 ,1r*   r   c                    t          j        | t                    } t          j        |t                    }|*t          j        |t                    }|j        |j        }}n|j        |j        }}|dk    s|| j        k    r|pd|k    rt          d          ||         |k    rt          d          |+t          j        |||j                  }|j        dd         }n"|j        d|         |j        |dz   d         z   }||| j        k    rt          d          | |f}n-| j        |j        cxk    r|k    sn t          d	          | ||f}|S )
zConvert inputs to float arrays.r   Nr	   r   zmodel_set_axis out of rangezTNumber of data sets (y or z array) is expected to equal the number of parameter setsr   z"x and y should have the same shapez%x, y and z should have the same shape)rI   r6  r3  r4  r   rf   r   )r<   r   r   r  r   	data_ndim
data_shaperD  s           r+   r1  r1    s   
au%%%A
au%%%A}M!5))) !:		 !:	 !||y16))aI--:;;;n%11/   9 A~qv66A"JJ .1AGNQ<N<P<P4QQJy  ABBB1v170000j0000DEEE1ayKr*   Tc                    t          |           \  }}}t          | j                                                  }t          | j                                                  }t          d | j                                        D                       }| j        }|s|s|s	|| _        dS t          |          }d}	| j        }
t          | j
                  D ]\  }}||vr
|
|         d         }|
|         d         }t          t          j        |d          }||	|	|z            }| j        |         dk    r@|r>| j        |         \  }}|t          j        ||          }|t          j        ||          }|||<   |	|z  }	|                                  |rdt          | j
                  D ]Q\  }}| j        |         r= | j        |         |           }|
|         d         }|||<   |                                  PdS dS )a  
    Constructs the full list of model parameters from the fitted and
    constrained parameters.

    Parameters
    ----------
    model :
        The model being fit
    fps :
        The fit parameter values to be assigned
    use_min_max_bounds: bool
        If the set parameter bounds for model will be enforced on each
        parameter with bounds.
        Default: True
    c              3   "   K   | ]
}|d k    V  dS rU  Nr)   r;   bs     r+   r=   z)fitter_to_model_params.<locals>.<genexpr>  s'      EE!A%EEEEEEr*   Nr   r@  r   r	   rU  )r/  r:  r   r2  r   r~  r  r   r  rE   r2   r   operatormulrI   fmaxfmin_array_to_parameters)r   r   r  r   fit_param_indiceshas_tied	has_fixed	has_boundr  offsetr  r#  r   r  r   r  r2  _min_maxr   s                       r+   r   r     sC     2%88A!5:$$&&''HEK&&(())IEEu|/B/B/D/DEEEEEI!J 	 Y -..F(Mu011  	T'''t$W-d#G, hlE1--Vftm+, <--2D-d+JD$....#
6$ 
     	-"5#455 	- 	-ICz$ -(
4(//&t,W5 &+
6"**,,,	- 	-	- 	-r*   z5.1z@private method: _fitter_to_model_params has been made public now)sincer  c                 "    t          | |          S r0   )r   )r   r   s     r+   _fitter_to_model_paramsrE    s    
 "%---r*   c                 J   t          t          t          | j                                      }| j        }t          | j                                                  }t          | j                                                  s&t          | j	                                                  rt          |          }| j
        }t          t          | j                            ddd         D ]6\  }}| j        |         s| j	        |         r||         d         }||= ||= ||= 7t          j        |          }t          |          D ]G\  }}	|	d         t          j         }
n|	d         }
|	d         t          j        }n|	d         }|
|f||<   Ht          t!          |           }|||fS )aO  
    Convert a model instance's parameter array to an array that can be used
    with a fitter that doesn't natively support fixed or tied parameters.
    In particular, it removes fixed/tied parameters from the parameter
    array.
    These may be a subset of the model parameters, if some of them are held
    constant or tied.
    Nr   r@  r   r	   )r  r   r9   r2   r  r~  r2  r:  r   r   r  rE   rI   r   r  r   r?  )r   r$  model_paramsmodel_boundsrj   r  r#  r   r  boundloweruppers               r+   r/  r/    s    E#e&7"8"899::#L++--..L
5;   	(C
(9(9(;(;$<$< 	(l##,i(9::;;DDbDA 	* 	*IC{4  *EJt$4 *&t,W56N ($S)x''-- + +
U8VGEE!HE8FEE!HE"ENSl+,,L)<77r*   z=private method: _model_to_fit_params has been made public nowc                      t          |           S r0   )r/  )r   s    r+   _model_to_fit_paramsrM  ;  s    
 u%%%r*   c                    d}t          |j                                                  r&d| vr"t          |                    d                    t          |j                                                  r&d| vr"t          |                    d                    t          d |j                                        D                       r&d| vr"t          |                    d                    |j        r&d	| vr"t          |                    d
                    |j        r&d| vr$t          |                    d                    dS dS )z@Make sure model constraints are supported by the current fitter.z(Optimizer cannot handle {0} constraints.r   zfixed parameterr   ztied parameterc              3   <   K   | ]}t          |          d k    V  dS r4  )r   r5  s     r+   r=   z(_validate_constraints.<locals>.<genexpr>N  s-      DDE!HH$DDDDDDr*   r~  zbound parametereqconsequalityineqcons
inequalityN)	r:  r   r2  r   rG   r   r~  rP  rR  )r   r   r  s      r+   r,  r,  C  sZ   8G
5;   LW4I%I%I(8I)J)JKKK
5: KF2G$G$G(8H)I)IJJJ 	DDel.A.A.C.CDDDDDL111(8I)J)JKKK| E(===(
)C)CDDD~ G*,AAA()E)EFFFG GAAr*   c                    | j         st          d          | j        rt          j        dt
                     n"t          |           dk    rt          d          t          ||            |                                 }|S )zT
    Check that model and fitter are compatible and return a copy of the model.
    z%Model does not appear to be fittable.zEModel is linear in parameters; consider using linear fitting methods.r	   z7Non-linear fitters can only fit one data set at a time.)	r*  rf   r+  r   r   r   r9   r,  r-  )r   r   r'  s      r+   r  r  Z  s     > B@AAA| TS	
 	
 	
 	
 
UqRSSS/777Jr*   c           
      :   | D ]}|j         }	 |                                }t          j        |          s&t	          j        t          d| d                     Yt          |t                    r3|j	        }|t                      |<   t                              |           t	          j        t          d| d                     # t          $ rD}t	          j        t          t          |          j	         d| d                     Y d}~d}~ww xY wdS )a^  
    This injects entry points into the `astropy.modeling.fitting` namespace.
    This provides a means of inserting a fitting routine without requirement
    of it being merged into astropy's core.

    Parameters
    ----------
    entry_points : list of `~importlib.metadata.EntryPoint`
        entry_points are objects which encapsulate importable objects and
        are defined on the installation of a package.

    Notes
    -----
    An explanation of entry points can be found `here
    <http://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins>`_
    zModeling entry point z expected to be a Class.z+ expected to extend astropy.modeling.Fitterz error occurred in entry point .N)r   loadr   r   r   r   r   
issubclassr   r%   globals__all__r   	Exceptionr8  )r   entry_pointr   es       r+   populate_entry_pointsr^  n  su   " $  	%**,,K ?;// &NNNN     k622 
&/D&1GIIdONN4((((M*6D 6 6 6    )  	 	 	M"Aww'OOOOO        		 s   C


D9DDc                      t                      } t          | d          r%t          |                     d                     d S t          |                     dg                      d S )Nselectzastropy.modeling)group)r   r   r^  r`  get)eps    r+   _populate_eprd    sc    	Br8 >bii.@iAABBBBBbff%7<<=====r*   )Nr	   r   )T)Fr(   r   r   r7  r   	functoolsr   r   importlib.metadatar   numpyrI   astropy.unitsr   astropy.utils.decoratorsr   astropy.utils.exceptionsr   
optimizersr
   r   r   r   r   spliner   r   r   r   r   r   utilsr   r   rZ  
STATISTICS
OPTIMIZERSRuntimeErrorr#   r.   ro   r[  r!   r    rf   r   ABCMetar   r   r   r   r   r}  r   r  r   r   r   r   r   r   r1  r   rE  r/  rM  r,  r  r^  rd  r)   r*   r+   <module>rr     s   , 


    # # # # # # # # + + + + + +     " " " " " " / / / / / / 7 7 7 7 7 7 Q Q Q Q Q Q Q Q Q Q Q Q Q Q            # " " " " " = = = = = = = =  * ]
 u
    ,   (' (' (' (' (' (' (' ('V' ' ' ' ' ' ' 'T+ + + + +) + + +G G G G G+ G G G    j       #+    k k k\IN IN IN IN IN{ IN IN IN IN^R R R R R R R R Rjl0 l0 l0 l0 l0 l0 l0 l0^Z Z Z Z ZK Z Z Z ZzM- M- M- M- M-) M- M- M-`_3 _3 _3 _3 _3& _3 _3 _3DH H H H H< H H H4K K K K Kl K K K49 9 9 9 9, 9 9 9*M M M M MV M M M`C C C C Cv C C CLV1 V1 V1 V1 V1K V1 V1 V1 V1r, , , ,nF- F- F- F-R 
N  . .	 .$8 $8 $8N 
K  & &	 &G G G.  (. . .b> > > r*   