
[c           @` s  d  Z  d d l m Z m Z m Z m 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 Z d d l m Z m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z m Z d d l m Z y d d l m  Z  e! Z" Wn e# k
 r)e$ Z" n Xd d d d d d d g Z% e g Z& e e g Z' d d l m( Z( m) Z) m* Z* d e+ f d     YZ, d e, f d     YZ- d e, e. f d     YZ/ d e j0 f d     YZ1 d   Z2 e j3 e1  d  e4 f d!     Y Z5 e j3 e1  d" e4 f d#     Y Z6 d$ e4 f d%     YZ7 e j3 e1  d& e4 f d'     Y Z8 d( e5 f d)     YZ9 d* e5 f d+     YZ: e j3 e1  d, e4 f d-     Y Z; d d d d.  Z= d/   Z> d0   Z? d1   Z@ d2   ZA d3   ZB e" reB e  d4 d5 d6 d   n  d S(7   u  
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.
i    (   t   absolute_importt   unicode_literalst   divisiont   print_functionN(   t   reducet   wrapsi   (   t   poly_map_domaint   _combine_equivalency_dicti   (   t   Quantity(   t   AstropyUserWarning(   t   six(   t   range(   t   SLSQPt   Simplex(   t   leastsquare(   t   iter_entry_pointsu   LinearLSQFitteru   LevMarLSQFitteru   FittingWithOutlierRemovalu   SLSQPLSQFitteru   SimplexLSQFitteru   JointFitteru   Fitter(   t   DEFAULT_MAXITERt   DEFAULT_EPSt   DEFAULT_ACCt   ModelsErrorc           B` s   e  Z d  Z RS(   u   Base class for model exceptions(   t   __name__t
   __module__t   __doc__(    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   A   s   t   ModelLinearityErrorc           B` s   e  Z d  Z RS(   u=    Raised when a non-linear model is passed to a linear fitter.(   R   R   R   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   E   s   t   UnsupportedConstraintErrorc           B` s   e  Z d  Z RS(   uE   
    Raised when a fitter does not support a type of constraint.
    (   R   R   R   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   I   s   t   _FitterMetac           B` s    e  Z d  Z e   Z d   Z RS(   uD   
    Currently just provides a registry for all Fitter classes.
    c         C` sX   t  t |   j |  | | |  } t j |  rT | j d  rT |  j j |  n  | S(   Nu   _(   t   superR   t   __new__t   inspectt
   isabstractt
   startswitht   registryt   add(   t   mclst   namet   basest   memberst   cls(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   V   s    ! (   R   R   R   t   setR   R   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   O   s   	c         ` s"   t     d   f d   } | S(   u&  
    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.
    c         ` sV  | j  d d   } t | t  p< t | t  p< t | t  } | j } | sT | r6| j r't | j | | j  }	 | j	 d  k	 r t | t  r | j
 | j	 d d |	 d } n  t | t  r | d  k	 r | j
 | j	 d d |	 d } q n  | j d | d | d |  } t }
 t | t  r<t }
 | j } n t j |  } t | t  rlt }
 | j } n t j |  } | d  k	 rt | t  rt }
 | j } qt j |  } n  | d  k r  |  | | | |  } n   |  | | | | |  } |
 r#| j d | d | d |  } n  | St d   n   |  | | | d | | Sd  S(	   Nu   equivalenciesu   xt   equivalenciesu   yt   xt   yt   zu8   This model does not support being fit to data with units(   t   popt   Nonet
   isinstanceR   t
   _has_unitst   _supports_unit_fittingR   t   inputst   input_units_equivalenciest   input_unitst   tot   without_units_for_datat   Falset   Truet   valuet   npt   asarrayt   with_units_from_datat   NotImplementedError(   t   selft   modelR(   R)   R*   t   kwargsR'   t   data_has_unitst   model_has_unitsR1   t   add_back_unitst   xdatat   ydatat   zdatat	   model_new(   t   func(    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   wrapperf   sJ    		#&!N(   R   R,   (   RF   RG   (    (   RF   s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   fitter_unit_support_   s    	Qt   Fitterc           B` s2   e  Z d  Z d   Z d   Z e j d    Z RS(   u   
    Base class for all fitters.

    Parameters
    ----------
    optimizer : callable
        A callable implementing an optimization algorithm
    statistic : callable
        Statistic function
    c         C` s   | d  k r t d   n  | d  k r6 t d   n  t j |  rT |   |  _ n' t j |  ro | |  _ n t d   t j |  r |   |  _ n	 | |  _ d  S(   Nu   Expected an optimizer.u   Expected a statistic function.u8   Expected optimizer to be a callable class or a function.(   R,   t
   ValueErrorR   t   isclasst   _opt_methodt
   isfunctiont   _stat_method(   R<   t	   optimizert	   statistic(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   __init__   s    c         G` sA   | d } | d } t  | |  |  j | | | d d ! } | S(   u  
        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.

        i    ii   (   t   _fitter_to_model_paramsRN   (   R<   t   fpst   argsR=   t   meast   res(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   objective_function   s
    

c         C` s   t  d   d S(   u   
        This method performs the actual fitting and modifies the parameter list
        of a model.

        Fitter subclasses should implement this method.
        u(   Subclasses should implement this method.N(   R;   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   __call__   s    	(   R   R   R   RQ   RW   t   abct   abstractmethodRX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRI      s   		t   LinearLSQFitterc           B` sY   e  Z d  Z d g Z d   Z e d d d   Z d d  Z e	 d d d d   Z
 RS(   u  
    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.
    u   fixedc         C` s)   i d  d 6d  d 6d  d 6d  d 6|  _ d  S(   Nu	   residualsu   ranku   singular_valuesu   params(   R,   t   fit_info(   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ     s    
c         C` sq   | d  k r- t j |  j | |  j   } n! t j |  j | | |  j   } |  j r_ | | S| d | f Sd  S(   N.(   R,   R8   t   arrayt	   fit_derivt
   parameterst   col_fit_deriv(   R=   t   param_indicesR(   R)   t   d(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   _deriv_with_constraints  s    !!	c         C` s  | d k r t | d  rH | j d k rH | j   | j   g | _ n  t | d  rx | j d k rx d d g | _ n  t | | j | j  St | d  r | j d k r | j   | j   g | _ n  t | d  r| j d k r| j   | j   g | _ n  t | d  r6| j	 d k r6d d	 g | _	 n  t | d
  rf| j
 d k rfd d	 g | _
 n  t | | j | j	  } t | | j | j
  } | | f Sd S(   ud   
        Maps domain into window for a polynomial model which has these
        attributes.
        u   domainu   windowii   u   x_domainu   y_domainu   x_windowg      g      ?u   y_windowN(   R,   t   hasattrt   domaint   mint   maxt   windowR   t   x_domaint   y_domaint   x_windowt   y_window(   R<   R=   R(   R)   t   xnewt   ynew(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   _map_domain_window!  s"    c         C` s?  | j  s t d   n  | j s0 t d   n  t |  j |  | j   } t |  \ } }	 | j d k r | d k r t d   n  t
 | | | d t |  d | j }
 t | j j    } | r6g  t t | j   D] } | |	 k r | ^ q } t j g  | D] } t | | j |  j ^ q } n  t |
  d k r|
 \ } } t | d  rx|  j | |  } n  | r|  j | |	 d | } |  j | | d | } n | j | | j  } | j |  } | } n|
 \ } } } t | d	  r|  j | | |  \ } } n  | r_|  j | |	 d | d
 | } |  j | | d | d
 | } n | j | | | j  } | j | |  } t |  d k r| j d k rt j g  | D] } | j   ^ q j } q| j } n | j   } | j  rt j |  j } n  | rH| j  r2t j |  j } n  | | j! |  } n  | d k	 rt |  d k r|| d t j" f } n  | | } n  | d k	 rJt j | d t j# } t |  t |  k rt d   n  | j d k r | | d d  t j" f 9} | | d d  t j" f } qJ| | d d  t j" f 9} | | } n  | d k rxt |  t j$ | j%  j& } n  | | j' d  } t j( j) | | | |  \ } } } } | |  j* d <| |  j* d <| |  j* d <| j | j } | |  j* d <t | d  r(| | j+ k r(t, j- d t.  n  t/ | | j    | S(   u9  
        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.
        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 and keyword-only argument
            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
        u)   Model must be a subclass of FittableModeluI   Model is not linear in parameters, linear fit methods should not be used.i   u.   Expected x, y and z for a 2 dimensional model.t   n_modelst   model_set_axisu   domainR(   u   x_domainR)   i   .t   dtypeu)   x and weights should have the same lengthNi    u	   residualsu   ranku   singular_valuesu   paramsu   _orderu"   The fit may be poorly conditioned
(0   t   fittableRJ   t   linearR   t   _validate_constraintst   supported_constraintst   copyt   _model_to_fit_paramst   n_inputsR,   t   _convert_inputt   lenRq   t   anyt   fixedt   valuesR   t   param_namesR8   R9   t   getattrR7   Rd   Ro   Rc   R^   R_   t   sum_of_implicit_termst   ndimR]   t   flattent   TR`   t   dott   newaxist   floatt   finfoRr   t   epst   sumt   linalgt   lstsqR\   t   _ordert   warningst   warnR	   RR   (   R<   R=   R(   R)   R*   t   weightst   rcondt
   model_copyt   _t   fitparam_indicest   fargt	   has_fixedt   idxt   fixparam_indicest	   fixparamst   lhst	   fixderivsR   t   rhst   it   sclt   lacoeft   residst   rankt   sval(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX   ;  s    !			/						.		 "(	
N(   R   R   R   Rv   RQ   t   staticmethodR,   Rc   Ro   RH   RX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR[     s   		t   FittingWithOutlierRemovalc           B` s;   e  Z d  Z d d  Z d   Z d   Z d d d  Z RS(   ug  
    This class combines an outlier removal technique with a fitting procedure.
    Basically, given a number of iterations ``niter``, outliers are removed
    and fitting is performed for each iteration.

    Parameters
    ----------
    fitter : An Astropy fitter
        An instance of any Astropy fitter, i.e., LinearLSQFitter,
        LevMarLSQFitter, SLSQPLSQFitter, SimplexLSQFitter, JointFitter.
    outlier_func : function
        A function for outlier removal.
    niter : int (optional)
        Number of iterations.
    outlier_kwargs : dict (optional)
        Keyword arguments for outlier_func.
    i   c         K` s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   fittert   outlier_funct   nitert   outlier_kwargs(   R<   R   R   R   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ     s    			c         C` s,   d d j  |  j j |  j j |  j |  j  S(   Nu9   Fitter: {0}
Outlier function: {1}
Num. of iterations: {2}u   
Outlier func. args.: {3}(   t   formatt   fitter__class__R   R   R   R   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   __str__  s
    	c         C` s8   d d j  |  j j |  j j j |  j j |  j |  j  S(   Nu#   {0}(fitter: {1}, outlier_func: {2},u!    niter: {3}, outlier_kwargs: {4})(   R   t	   __class__R   R   R   R   R   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   __repr__
  s    	c      	   K` s  |  j  | | | | d | | } | } | d k r | }	 xEt |  j  D] }
 |  j |	 | |  |  j  }	 |	 | |  7}	 | d k	 r | |	 j } n  |  j  | | |	 j |	 j |	 j d | | } qI Wn | }	 x t |  j  D] }
 |  j |	 | | |  |  j  }	 |	 | | |  7}	 | d k	 rA| |	 j } n  |  j  | | |	 j | |	 j |	 j |	 j d | | } q W|	 | f S(   u  
        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
        -------
        filtered_data : numpy.ma.core.MaskedArray
            Data used to perform the fitting after outlier removal.
        fitted_model : `~astropy.modeling.FittableModel`
            Fitted model after outlier removal.
        R   N(   R   R,   R   R   R   R   t   maskt   data(   R<   R=   R(   R)   R*   R   R>   t   fitted_modelt   filtered_weightst   filtered_datat   n(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX     s:    !		N(   R   R   R   RQ   R   R   R,   RX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s
   		t   LevMarLSQFitterc           B` sb   e  Z d  Z d d d g Z d   Z d   Z e d d e e	 e
 e d   Z e d d   Z RS(	   u  
    Levenberg-Marquardt algorithm and least squares statistic.

    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``).
    u   fixedu   tiedu   boundsc         C` s_   i	 d  d 6d  d 6d  d 6d  d 6d  d 6d  d 6d  d 6d  d 6d  d	 6|  _ t t |   j   d  S(
   Nu   nfevu   fvecu   fjacu   ipvtu   qtfu   messageu   ierru	   param_jacu	   param_cov(   R,   R\   R   R   RQ   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ   m  s    
c         G` s{   | d } | d } t  | |  | d } | d k rU t j | | d d !  |  St j | | | d d !  |  Sd S(   u   
        Function to minimize.

        Parameters
        ----------
        fps : list
            parameters returned by the fitter
        args : list
            [model, [weights], [input coordinates]]
        i    i   ii   N(   RR   R,   R8   t   ravel(   R<   RS   RT   R=   R   RU   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRW   z  s    


c
         C` s  d d l  m }
 t | |  j  } | | f t | | |  } | j d k sS |	 r\ d } n	 |  j } t |  \ } } |
 j	 |  j
 | d | d | d | j d | d | d | d	 t \ } } } } } t | |  |  j j |  | |  j d
 <| |  j d <| |  j d <| d k r+t j d t  n  t |  t |  k r| d k	 rt j |  j
 | |  d  } t |  t |  } | | | |  j d <n d |  j d <| S(   u  
        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.
        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 and keyword-only argument
            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
        i    (   t   optimizeRT   t   Dfunt	   col_derivt   maxfevt   epsfcnt   xtolt   full_outputu   cov_xu   messageu   ierri   i   i   i   uL   The fit may be unsuccessful; check fit_info['message'] for more information.u	   param_covN(   i   i   i   i   (   t   scipyR   t   _validate_modelRv   Rz   R^   R,   t   _wrap_derivRx   t   leastsqRW   R`   R6   RR   R\   t   updateR   R   R	   R{   R8   R   (   R<   R=   R(   R)   R*   R   t   maxitert   acct   epsilont   estimate_jacobianR   R   R   t   dfunct   init_valuesR   t	   fitparamst   cov_xt   dinfot   messt   ierrt   sum_sqrst   dof(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX     s2    -			
$c         C` s  | d k r d } n  t | j j    s? t | j j    r3t | |   | d k r t j | j | | j	   } | j
 s t j |  | j } q t j |  | } nr t j g  | j | | | j	  D] } t j |  ^ q  } | j
 st j |  | j } n t j |  | } g  | j D] }	 t | |	  ^ q*}
 g  |
 D] } | j ^ qL} g  |
 D] } | j ^ qh} t t j g  |
 D] } | j t k	 ^ qt |   } t j | |  } t j |  } | j
 s t j | t j |   j } n | t j |  } g  | D] } t j |  ^ qS| d k rg  t j |  t j | j | |    D] } t j |  ^ qhS| j
 sg  t j |  t j | j | | |    j j D] } t j |  ^ qSg  | t j | j | | |    D] } t j |  ^ qSd S(   uv  
        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.
        g      ?N(   R,   R|   R}   R~   t   tiedRR   R8   R]   R^   R_   R`   R   R   R   R   t   listt   whereR5   R6   t
   logical_ort   logical_notR9   t   nonzero(   t   paramsR=   R   R(   R)   R*   t   fullt
   full_derivR   R"   t   parst   parR}   R   t   fix_and_tiet   indt   residues(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s<    	*	=	%(	" B	HN(   R   R   R   Rv   RQ   RW   RH   R,   R   R   R   R5   RX   R   R   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   M  s   		Lt   SLSQPLSQFitterc           B` s5   e  Z d  Z e j Z d   Z e d d d   Z RS(   u   
    SLSQP optimization algorithm and least squares statistic.


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

    c         C` s,   t  t |   j d t d t  i  |  _ d  S(   NRO   RP   (   R   R   RQ   R   R   R\   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ   $  s    c         K` s~   t  | |  j j  } t | | |  } | | f | } t |  \ }	 }
 |  j |  j |	 | |  \ } |  _ t | |  | S(   u  
        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 and keyword-only argument
            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
        (   R   RL   Rv   Rz   Rx   RW   R\   RR   (   R<   R=   R(   R)   R*   R   R>   R   R   t   p0R   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX   (  s    *N(	   R   R   R   R   Rv   RQ   RH   R,   RX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s
   
		t   SimplexLSQFitterc           B` s5   e  Z d  Z e j Z d   Z e d d d   Z RS(   u   

    Simplex algorithm and least squares statistic.

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

    c         C` s,   t  t |   j d t d t  i  |  _ d  S(   NRO   RP   (   R   R   RQ   R   R   R\   (   R<   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ   k  s    c         K` s~   t  | |  j j  } t | | |  } | | f | } t |  \ }	 }
 |  j |  j |	 | |  \ } |  _ t | |  | S(   uG  
        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 and keyword-only argument
            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
        (   R   RL   Rv   Rz   Rx   RW   R\   RR   (   R<   R=   R(   R)   R*   R   R>   R   R   R   R   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX   p  s    $N(	   R   R   R   R   Rv   RQ   RH   R,   RX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR   ]  s
   
		t   JointFitterc           B` s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   uH  
    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         C` s{   t  |  |  _ t  |  |  _ | |  _ |  j   |  j   |  _ g  |  j D] } | j ^ qJ |  _ t	 j
 |  j  |  _ d  S(   N(   R   t   modelst   initvalst   jointparamst   _verify_inputRx   R   Ry   t	   modeldimsR8   R   R   (   R<   R   t   jointparametersR   t   m(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRQ     s    	
"c   	      C` s   g  } | j  |  j  x| |  j D]q } g  | j D] } | j   ^ q0 } |  j | } | j } x# | D] } | | d } | | =qe W| j  |  q  W| S(   Nu   slice(   t   extendR   R   R_   R   R   t   _param_metrics(	   R<   t   fparamsR=   t   pR   t   joint_paramst   param_metricst
   param_namet   slice_(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRx     s    "	c         G` sk  t  |  } g  } t  |  } t |  j  } | |  } | | 4x|  j D]} |  j | }	 | | j d  }
 | | j d 4t | j  t |	  } | |  } | | 4g  } | j } xy | j D]n } | |	 k r |	 j	 |  } | j
 | | g  q | | d } | j | j } | j
 | |   | | 4q W| j |
 d  |  } | j
 | |
 d  qH Wt j |  S(   uP  
        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
        i   u   slicei(   R   R{   R   R   R   Ry   t   _parametersR   R   t   indexR   t   stopt   startt   evaluateR8   R   (   R<   RS   RT   t	   lstsqargst   fittedR   t   numjpt   jointfitparamsR=   R   t   margst   numfpt   mfparamst   mparamsR   R   R   R   t   plent   modelfit(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRW     s4    

	c         C` s   t  |  j  d k r6 t d j t  |  j     n  t  |  j j    d k  rx t d j t  |  j j       n  xj |  j j   D]Y } t  |  j |  t  |  j  k r t d j t  |  j |  t  |  j     q q Wd  S(   Ni   u   Expected >1 models, {} is giveni   u1   At least two parameters are expected, {} is givenu(   {} parameter(s) provided but {} expected(   R{   R   t	   TypeErrorR   R   t   keysR   (   R<   t   j(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s    		"	c         G` s  d d l  m } t |  t d   |  j  k ra t d j t d   |  j  t |     n  | j |  j |  j	 d | \ |  j	 (} |  j	 } t |  j
  } | |  } | | 4x |  j D] } |  j | } t | j  t |  }	 | |	  }
 | |	 4g  } | j } xy | j D]n } | | k rK| j |  } | j | | g  q| | d } | j | j } | j |
 |   |
 | 4qWt j |  | _ q Wd S(	   uk   
        Fit data to these models keeping some of the parameters common to the
        two models.
        i    (   R   c         S` s   |  d | d S(   Ni   (    (   R(   R)   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   <lambda>  s    u/   Expected {} coordinates in args but {} providedc         S` s   |  d | d S(   Ni   (    (   R(   R)   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR    s    RT   u   sliceN(   R   R   R{   R   R   RJ   R   R   RW   R   R   R   R   R   R   R   R   R   R   R   R8   R]   R_   (   R<   RT   R   R   R   R   R   R=   R   R   R   R   R   R   R   R   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRX   	  s6    !		


	(   R   R   R   RQ   Rx   RW   R   RX   (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s   			1	c         C` s*  t  j |  d t  j }  t  j | d t  j } | d k	 rW t  j | d t  j } n  | d k r | d k r | j | | k r t d   n  t  j | | | j  } q | j |  | j | d } |  j | j k o | k n s t d   q n  | d k r|  | f } n |  | | f } | S(   u   Convert inputs to float arrays.Rr   i   uO   Number of data sets (y array is expected to equal the number of parameter sets)u%   x, y and z should have the same shapeN(   R8   R9   R   R,   t   shapeRJ   t   rollaxisR   (   R(   R)   R*   Rp   Rq   t   z_shapeR   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRz   7  s"    
"c         C` s  t  |   \ } } t |  j j    } t |  j j    } t d   |  j j   D  } | pj | pj | sz | |  _ d St |  } d } |  j } x t	 |  j
  D] \ }	 }
 |	 | k r q n  | |
 d } | |
 d } t t j | d  } | | | | !} |  j |
 d k rp|  j |
 \ } } | d k	 rLt j | |  } n  | d k	 rpt j | |  } qpn  | |  j | <| | 7} q W| rx^ t	 |  j
  D]J \ }	 }
 |  j |
 r|  j |
 |   } | |
 d } | |  j | <qqWn  d S(   uf   
    Constructs the full list of model parameters from the fitted and
    constrained parameters.
    c         s` s   |  ] } | d k Vq d  S(   N(   NN(   R,   (   t   .0t   b(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pys	   <genexpr>q  s    Ni    u   sliceu   shapei   (   NN(   Rx   R|   R   R~   R}   t   boundsR_   R&   R   t	   enumerateR   R   t   operatort   mulR,   R8   t   fmaxt   fmin(   R=   RS   R   t   fit_param_indicest   has_tiedR   t	   has_boundt   offsetR   R   R"   R   R  t   sizeR~   t   _mint   _maxR7   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRR   g  s>    		c         C` s   t  t t |  j    } t |  j j    sE t |  j j    r t  |  j  } |  j	 } xi t  t
 |  j   d d d  D]E \ } } |  j | s |  j | r | | d } | | =| | =q q Wt j |  | f S|  j | f Sd S(   uP  
    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.
    Niu   slice(   R   R   R{   R   R|   R}   R~   R   R_   R   R	  R8   R]   (   R=   R   R   R   R   R"   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRx     s    *	/c         C` s"  d } t  t j | j   rB d |  k rB t | j d    n  t  t j | j   r~ d |  k r~ t | j d    n  t  d   t j | j  D  r d |  k r t | j d    n  | j r d	 |  k r t | j d
    n  | j	 rd |  k rt | j d    n  d S(   u@   Make sure model constraints are supported by the current fitter.u(   Optimizer cannot handle {0} constraints.u   fixedu   fixed parameteru   tiedu   tied parameterc         s` s!   |  ] } t  |  d k Vq d  S(   N(   NN(   t   tupleR,   (   R  R  (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pys	   <genexpr>  s    u   boundsu   bound parameteru   eqconsu   equalityu   ineqconsu
   inequalityN(
   R|   R
   t
   itervaluesR}   R   R   R   R  t   eqconst   ineqcons(   Rv   R=   t   message(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyRu     s     $"c         C` sr   |  j  s t d   n  |  j r4 t j d t  n! t |   d k rU t d   n  t | |   |  j   } | S(   uT   
    Check that model and fitter are compatible and return a copy of the model.
    u%   Model does not appear to be fittable.uE   Model is linear in parameters; consider using linear fitting methods.i   u7   Non-linear fitters can only fit one data set at a time.(	   Rs   RJ   Rt   R   R   R	   R{   Ru   Rw   (   R=   Rv   R   (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyR     s    			
c         C` s   x |  D] } | j  } y | j   } WnA t k
 ri } t j t d j d t |  j d |    q Xt	 j
 |  s t j t d j |    q t | t  r | j } | t   | <t j |  q t j t d j |    q Wd S(   u}  
    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 : a list of `~pkg_resources.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>`

    u,   {type} error occurred in entry point {name}.t   typeR"   u0   Modeling entry point {0} expected to be a Class.uC   Modeling entry point {0} expected to extend astropy.modeling.FitterN(   R"   t   loadt	   ExceptionR   R   R	   R   R  R   R   RK   t
   issubclassRI   t   globalst   __all__t   append(   t   entry_pointst   entry_pointR"   t   e(    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   populate_entry_points  s$    	 			t   groupu   astropy.modelingR"   (C   R   t
   __future__R    R   R   R   RY   R   R
  R   t	   functoolsR   R   t   numpyR8   t   utilsR   R   t   unitsR   t   utils.exceptionsR	   t   externR
   t   extern.six.movesR   t
   optimizersR   R   RP   R   t   pkg_resourcesR   R6   t   HAS_PKGt   ImportErrorR5   R  t
   STATISTICSt
   OPTIMIZERSR   R   R   R  R   R   RJ   R   t   ABCMetaR   RH   t   add_metaclasst   objectRI   R[   R   R   R   R   R   R,   Rz   RR   Rx   Ru   R   R$  (    (    (    s7   lib/python2.7/site-packages/astropy/modeling/fitting.pyt   <module>   s`   "

			\FcGD0	7				,