ó
\™xTc        	   @   sÚ   d  d l  m Z d  d l Z d  d l m Z m Z d  d l m Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d k rÖ e
 d d ƒ Z e j GHn  d S(   iÿÿÿÿ(   t   divisionN(   t   matrixt   lbfgs(   t   flext   fit_peakc           B   s;   e  Z d  Z d d d d d „ Z d „  Z d d d „ Z RS(   s  
  =============================================================================
  fit_peak Class

  This class fits a list of points (x,y,z) and their heights to a certain shape
  using the LBFGS minimizer and the function sum{(p_i - p_i0)^2}.

  -----------------------------------------------------------------------------
  Arguments:
    height_list - the list of heights (list of floats)
    xyz_list - the list of positions (list of tuples)
    shape - flag for shape to be fitted (parabola, quadratic, gaussian)
    max_iterations - the maximum number of minimization steps (int)

  Useful accessible attributes:
    self.x - the final parameters from the fit (list)
    self.vertex - the peak position (tuple)

  Notes:
    The first point should be the point closest to the peak.  If the peak
    is found outside the 26 nearest neighbors, the center point is returned.
    The order of the points is very important because the initial guess for
    minimization is obtained by solving a linear system.  See
    pick_map_neighbors for the required order.
    To use "gaussian," height_list should actually be the ln(height) since the
    least squares function is sum{[ln(p_i) - ln(p_i0)]^2} for that case.
  -----------------------------------------------------------------------------
  t   parabolai   c         C   së  | |  _  | |  _ | d k rSt ƒ  j |  _ t |  _ |  j d | d | ƒ g  } g  } x× t |  j ƒ D]Æ } | j | | ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j | | d ƒ | j | | d ƒ | j | | d ƒ | j d ƒ qh Wt j	 | ƒ } t j
 | ƒ }	 nÔ| d k sk| d	 k rt ƒ  j |  _ | d k r’t |  _ n	 t |  _ |  j d | d | ƒ g  } g  } x:t |  j ƒ D])} | j | | ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j | | d ƒ | j | | d ƒ | j | | d ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j | | d | | d ƒ | j d ƒ qÍWt j	 | ƒ } t j
 | ƒ }	 n d
 GHt ƒ  t j | j ƒ  |	 ƒ |  _ t | ƒ |  j k r‚t j d |  d t j d | ƒ ƒ |  _ n  |  j d |  j ƒ }
 |
 j |  _ | d d } | d d } | d d } | d d } | d d } | d d } | | k s	t ‚ | | k st ‚ | | k s-t ‚ |  j d | k  sS|  j d | k rk| d d |  j d <n  |  j d | k  s‘|  j d | k r©| d d |  j d <n  |  j d | k  sÏ|  j d | k rç| d d |  j d <n  d  S(   NR   t   height_listt   xyz_listi    i   i   g      ð?t	   quadratict   gaussians   fit_peak error: shape not validt   target_evaluatort   termination_paramst   max_iterationst
   parametersi   i   i   i   (   R   R   R   t   nt   fitt   check_lengthst   xranget   appendR   t   sqrt   colR   R	   t   exitR   t   doublet   inverset   xt   lenR   t   runt   termination_parameterst	   minimizert   vertext   AssertionError(   t   selfR   R   t   shapeR   t
   b_elementst
   A_elementst   it   At   bt   answert   x_maxt   x_mint   y_maxt   y_mint   z_maxt   z_min(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt   __init__#   s‚    			!!!	!!!!!!	&&&c         C   só   |  j  d |  j ƒ } d } g  t |  j ƒ D] } d ^ q+ } x  t t |  j ƒ ƒ D]‰ } | j d |  j | ƒ |  j | } | j d |  j | ƒ } | | | } x5 t |  j ƒ D]$ } | | d | | | | | <q´ WqS W| t	 j
 | ƒ f S(   NR   g        t   rg       @(   R   R   R   R   R   R   t
   get_heightR   t   get_gradientR   R   (   R   R&   t   fR#   t   gt   f_it   g_it   j(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt    compute_functional_and_gradientsx   s    "$&c         C   s=   t  | ƒ |  j k s t ‚ t  | ƒ t  | ƒ k s9 t ‚ d  S(   N(   R   R   R   (   R   R   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR   „   s    N(   t   __name__t
   __module__t   __doc__t   NoneR-   R6   R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR      s
   	T	R   c           B   s2   e  Z d  Z d d „ Z d d „ Z d d „ Z RS(   s  
  =============================================================================
  parabola Class

  This class models a parabola according to the equation,

    p = a x^2 + b y^2 + c z^2 + d x + e y + f z + g

  -----------------------------------------------------------------------------
  Arguments:
    parameters - the parameters a-g (list)

  Accesible methods:
    get_height(r) - returns p for a given r (x,y,z) (float)
    get_gradient(r) - returns the gradient of p with respect to a-g for a given
                      r (x,y,z) (tuple)

  Notes:
    This class is used in conjunction with fit_peak.  Since there are 7
    parameters, at least 7 points are needed to fit the parabola.
  -----------------------------------------------------------------------------
  c         C   s  d |  _  d  |  _ | d  k	 rt | ƒ |  j  k s9 t ‚ | |  _ t |  j d d ƒ d k rg d } n d |  j d |  j d } t |  j d d ƒ d k r¨ d } n d |  j d	 |  j d } t |  j d
 d ƒ d k ré d } n d |  j d |  j d
 } | | | g |  _ n  d  S(   Ni   i    i   g        g}Ã”%­I²Tg      à¿i   i   i   i   i   (   R   R:   t   pR   R   t   roundR   (   R   R   t   rx_vt   ry_vt   rz_v(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR-   Ÿ   s    						c         C   s°   |  j  d  k r d GHn• |  j  d | d | d |  j  d | d | d |  j  d | d | d |  j  d | d |  j  d | d |  j  d | d |  j  d Sd  S(	   Ns+   parabaola error: parameters not initializedi    i   i   i   i   i   i   (   R;   R:   (   R   R.   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR/   µ   s    c         C   sI   | d | d | d | d | d | d | d | d | d d f S(   Ni    i   i   g      ð?(    (   R   R.   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR0   ¼   s    N(   R7   R8   R9   R:   R-   R/   R0   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR   ˆ   s   R   c           B   s2   e  Z d  Z d d „ Z d d „ Z d d „ Z RS(   s'  
  =============================================================================
  quadratic Class

  This class models a parabola according to the equation,

    p = a x^2 + b y^2 + c z^2 + d x + e y + f z + g x y + h x z + i y z + j

  -----------------------------------------------------------------------------
  Arguments:
    parameters - the parameters a-j (list)

  Accesible methods:
    get_height(r) - returns p for a given r (x,y,z) (float)
    get_gradient(r) - returns the gradient of p with respect to a-j for a given
                      r (x,y,z) (tuple)

  Notes:
    This class is used in conjunction with fit_peak.  Since there are 10
    parameters, at least 10 points are needed to fit the quadratic.
  -----------------------------------------------------------------------------
  c         C   s  d |  _  d  |  _ | d  k	 rt | ƒ |  j  k s9 t ‚ | |  _ t j d |  j d |  j d |  j d |  j d d |  j d |  j d |  j d |  j d d |  j d g	 ƒ } t j |  j d	 |  j d
 |  j d g ƒ } t | j	 ƒ  | ƒ |  _
 n  d  S(   Ni
   g       @i    i   i   i   i   i   i   i   i   (   R   R:   R;   R   R   R   R   R   t   listR   R   (   R   R   R$   R%   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR-   Ö   s    			("+c         C   s  |  j  d  k r d GHnæ |  j  d | d | d |  j  d | d | d |  j  d | d | d |  j  d | d |  j  d | d |  j  d | d |  j  d | d | d |  j  d	 | d | d |  j  d
 | d | d |  j  d Sd  S(   Ns+   quadratic error: parameters not initializedi    i   i   i   i   i   i   i   i   i	   (   R;   R:   (   R   R.   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR/   æ   s    Úc         C   sv   | d | d | d | d | d | d | d | d | d | d | d | d | d | d | d d f
 S(   Ni    i   i   g      ð?(    (   R   R.   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR0   ï   s    BN(   R7   R8   R9   R:   R-   R/   R0   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR   ¿   s   	R	   c           B   s2   e  Z d  Z d d „ Z d d „ Z d d „ Z RS(   s˜  
  =============================================================================
  gaussian Class

  This class models a parabola according to the equation,

    p = exp(a x^2 + b y^2 + c z^2 + d x + e y + f z + g x y + h x z + i y z + j)

  -----------------------------------------------------------------------------
  Arguments:
    parameters - the parameters a-j (list)

  Accesible methods:
    get_height(r) - returns ln(p) for a given r (x,y,z) (float)
    get_gradient(r) - returns the gradient of ln(p) with respect to a-j for a
                      given r (x,y,z) (tuple)

  Notes:
    This class is used in conjunction with fit_peak.  Since there are 10
    parameters, at least 10 points are needed to fit the quadratic.
    The positions (x,y,z) should be fractional or the exponent may return
    an overflow exception.
  -----------------------------------------------------------------------------
  c         C   s  d |  _  d  |  _ | d  k	 rt | ƒ |  j  k s9 t ‚ | |  _ t j d |  j d |  j d |  j d |  j d d |  j d |  j d |  j d |  j d d |  j d g	 ƒ } t j |  j d	 |  j d
 |  j d g ƒ } t | j	 ƒ  | ƒ |  _
 n  d  S(   Ni
   g       @i    i   i   i   i   i   i   i   i   (   R   R:   R;   R   R   R   R   R   R@   R   R   (   R   R   R$   R%   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR-     s    			("+c         C   s  |  j  d  k r d GHnæ |  j  d | d | d |  j  d | d | d |  j  d | d | d |  j  d | d |  j  d | d |  j  d | d |  j  d | d | d |  j  d	 | d | d |  j  d
 | d | d |  j  d Sd  S(   Ns*   gaussian error: parameters not initializedi    i   i   i   i   i   i   i   i   i	   (   R;   R:   (   R   R.   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR/     s    Úc         C   s½   d t  j |  j d | ƒ ƒ } | d | d | | d | d | | d | d | | d | | d | | d | | d | d | | d | d | | d | d | d | f
 S(   Ng      ð?R.   i    i   i   (   t   matht   expR/   (   R   R.   t   h(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR0   %  s    ZN(   R7   R8   R9   R:   R-   R/   R0   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR	   ó   s   	t   pick_map_neighborsc           B   s8   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z RS(   s  
  =============================================================================
  pick_map_neighbors Class

  This class orders the nearest neighbors in a 3x3x3 box of gridpoints from
  nearest to farthest.

  -----------------------------------------------------------------------------
  Arguments:
    site - the center of the 3x3x3 box (tuple)
    map_in - the map of gridpoints (flex.double)

  Accessible methods:
    get_6_nearest_neighbors - returns site and 6 nearest neighbors (list of
                              floats, list of tuples)
    get_18_nearest_neighbors - returns site and 18 nearest neighbors (list of
                               floats, list of tuples)
    get_26_nearest_neighbors - returns site and 26 nearest neighbors (list of
                               floats, list of tuples)

  Notes:
    This class works in conjunction with fit_peak and supplies points in the
    proper order for that class to function.
  -----------------------------------------------------------------------------
  c         C   s+  | d  k	 s t ‚ | d  k	 s$ t ‚ d } | j ƒ  } g  t | ƒ D] } d ^ qC |  _ g  t | ƒ D] } d ^ qe |  _ | d } | d d } | d d } | d }	 | d d }
 | d d } | d } | d d } | d d } | | |  j d <| d | d | d f |  j d <| | | d |	 | f |  j d <| | | d |	 | f |  j d <| | |
 | d | f |  j d <| | | | d | f |  j d <| | |	 | | d f |  j d <| | |	 | | d f |  j d	 <| |	 | f |  j d <| |	 | f |  j d <| |
 | f |  j d <| | | f |  j d <| |	 | f |  j d <| |	 | f |  j d	 <| | |
 | d | | d f |  j d
 <| | | d |	 | | d f |  j d <| | | d | | d | f |  j d <| | | | d | | d f |  j d <| | | d |	 | | d f |  j d <| | | d | | d | f |  j d <| | |
 | d | | d f |  j d <| | | d |	 | | d f |  j d <| | | d |
 | d | f |  j d <| | | | d | | d f |  j d <| | | d |	 | | d f |  j d <| | | d |
 | d | f |  j d <| |
 | f |  j d
 <| |	 | f |  j d <| | | f |  j d <| | | f |  j d <| |	 | f |  j d <| | | f |  j d <| |
 | f |  j d <| |	 | f |  j d <| |
 | f |  j d <| | | f |  j d <| |	 | f |  j d <| |
 | f |  j d <| | | d |
 | d | | d f |  j d <| | | d |
 | d | | d f |  j d <| | | d | | d | | d f |  j d <| | | d |
 | d | | d f |  j d <| | | d | | d | | d f |  j d <| | | d |
 | d | | d f |  j d <| | | d | | d | | d f |  j d <| | | d | | d | | d f |  j d <| |
 | f |  j d <| |
 | f |  j d <| | | f |  j d <| |
 | f |  j d <| | | f |  j d <| |
 | f |  j d <| | | f |  j d <| | | f |  j d <xu t | ƒ D]g } t |  j | d ƒ | d t |  j | d ƒ | d t |  j | d ƒ | d f |  j | <q¼Wd  S(   Ni   g        i    i   i   i   i   i   i   i   i   i	   i
   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   i   (   i    i    i    (   R:   R   t   allR   R   R   t   float(   R   t   sitet   map_int   n_neighborst   extentsR#   t   u0t   upt   umt   v0t   vpt   vmt   w0t   wpt   wm(    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyR-   D  s’    ""


"""""""************22222222c         C   s   |  j  d d !|  j d d !f S(   Ni    i   (   R   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt   get_6_nearest_neighborsŸ  s    c         C   s   |  j  d d !|  j d d !f S(   Ni    i   (   R   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt   get_18_nearest_neighbors¢  s    c         C   s   |  j  |  j f S(   N(   R   R   (   R   (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt   get_26_nearest_neighbors¥  s    N(   R7   R8   R9   R:   R-   RT   RU   RV   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyRD   *  s
   [		t   __main__R   i   i   (   iÿÿÿÿiÿÿÿÿiÿÿÿÿi   i   i   i   (   t
   __future__R    RA   t   scitbxR   R   t   scitbx.array_familyR   t   objectR   R   R   R	   RD   R7   R;   R   (    (    (    sB   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/fit_peak.pyt   <module>   s   ‚747