³ò
YÄGJc           @   sG   d  Z  d d k Z d d k Z d e i f d „  ƒ  YZ d d „ Z d S(   s/   
ffit.py
Functions for Fitness

Lev 1 Feb 2009
iÿÿÿÿNt   CONSTc           B   s   e  Z d  Z RS(   sI   
    This class is intended be used to fix a parameter during a fit.
    (   t   __name__t
   __module__t   __doc__(    (    (    s   c:\py\lib\ffit.pyR       s   c   
         s  | d
 j o/ g  } | D] } | t | t ƒ q ~ } n t i | d t ƒ} t i | d t i d t ƒ} | i	 d j o t
 d ƒ ‚ n | i | i j o t
 d ƒ ‚ n t i | ƒ p t
 d ƒ ‚ n | d
 j o@ ‡  f d †  } t i i | | | | |  | | f |  }	 n@ ‡  f d †  } t i i | | | | |  | | | f |  }	 |	 d	 | | <t | g t |	 d ƒ ƒ }	 |	 S(   s‚  
    A frontend to scipy least square fit.
    
    'x' and 'y' are the data feeded to a fit. If 'y' is None, 'func(x, *params)'
    is used as residuals. Otherwise 'sum(abs(y - func(x, *params)))' is minimized, e.g.
    'func(x, *params)' is a model for data rather than residuals.
    
    Only a subset of 'params' is varied as specified by the 'mask'.
    
    'params' is a mixture of initial guesses of the variated parameters and
    values of the fixed ones.

    'mask' is a boolean array of the same size as 'params'. If 'mask[n]' is True,
    the 'params[n]' is variated, otherwise 'params[n]' is fixed. If 'mask' is
    not specified it is deduced from the 'params' itself. A parameter
    is considered fixed if it is of type 'ffit.CONST' derived from numpy.float64.
    
    Any extra keyword arguments are forwarded to the 'scipy.optimize.leastsq'.

    The return value has the same format as for 'scipy.optimize.leastsq',
    except for the array of fit results (first element of the returned
    tuple): here it contains all 'params', both varied and fixed in the same
    order as in the input argument. The rest of the tuple depends on
    'full_output' keyword argument.
    
    Example:
    
    >>> def func(x, A, B): return A*x + B
    ...
    >>> x = numpy.array([1,2,3])
    >>> y = numpy.array([1,2,3])
    >>> res = ffit.leastsq(x, y, func, [1, 1], [True, True])
    >>> print res[0]
    [  1.00000000e+00  -4.06333240e-09]
    >>> res = ffit.leastsq(x, y, func, [1, 0.25], [True, False])
    >>> print res[0]
    [ 0.89285714  0.25      ]
    >>> res = ffit.leastsq(x, y, func, [ffit.CONST(0.25), 1])
    >>> print res[0]
    [ 0.25  1.5 ]

    In the second example the intercept of the linear fit is kept fixed at 1,
    the best fit being y = 0.89*x + 1, rather than y = 1*x + 0.25.
    Similarily in the third one the slope is fixed at 0.25, and the best ift
    is y = 0.25*x + 1.5.
    t   dtypet   copyi   s   'params' must be a 1D arrays)   'params' and 'mask' must be the same sizes=   all 'params' are constrained. no degrees of freedom for a fitc            s7   t  i t | ƒ ƒ } |  | | <| | | <ˆ  | | Œ S(   N(   t   numpyt   zerost   len(   t	   fitparamst   maskt   xt   constst   params(   t   func(    s   c:\py\lib\ffit.pyt	   residualsT   s    
c            sA   t  i t | ƒ ƒ } |  | | <| | | <t | ˆ  | | Œ ƒ S(   N(   R   R   R   t   abs(   R	   R
   R   t   yR   R   (   R   (    s   c:\py\lib\ffit.pyR   ^   s    
i    N(   t   Nonet
   isinstanceR    R   t   asarrayt   boolt   arrayt   float64t   Truet   ndimt
   ValueErrort   shapet   anyt   scipyt   optimizet   leastsqt   tuplet   list(
   R   R   R   R   R
   t   vargst   _[1]t   pR   t   res(    (   R   s   c:\py\lib\ffit.pyR      s$    1/10(   R   t   scipy.optimizeR   R   R   R    R   R   (    (    (    s   c:\py\lib\ffit.pys   <module>   s   