ó
u™xTc           @   só  d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l 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 d  d
 l m Z d  d l Z d  d l 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 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z  d „  Z! d „  Z" d e f d  „  ƒ  YZ# d! e f d" „  ƒ  YZ$ d# „  Z% e& d$ k rïe% e j' d% ƒ n  d S(&   iÿÿÿÿ(   t   division(   t	   joint_lib(   t
   test_utils(   t   center_of_mass_from_sitesN(   t   flex(   t   matrix(   t   approx_equal(   t   null_outt   show_times_at_exit(   t   show_sorted_by_countst
   setT_mixinc           B   s   e  Z d  „  Z RS(   c         C   s@   t  j |  j d f ƒ |  _ t  j |  j j ƒ  d f ƒ |  _ d  S(   Ni    (   i    i    i    (   i    i    i    (   R   t   rtt   Et   Tpst	   transposet   Tsp(   t   O(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   setT   s    (   t   __name__t
   __module__R   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR
      s   t   euler_paramsc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s\   | d  k r t j d ƒ } n  | |  _ | j ƒ  |  _ t j d |  j ƒ |  _ |  j	 ƒ  d  S(   Ni   i    t   q(   i   i    i    i    (
   t   NoneR   t   colt   qEt	   normalizet   unit_quaternionR   t   RBDA_Eq_4_12R   R   (   R   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   __init__   s     	c         C   s@   t  j d |  j ƒ } | d t  j d |  j ƒ } | } | | S(   NR   i   (   R   t   d_unit_quaternion_d_qE_matrixR   t   RBDA_Eq_4_13R   (   R   t   taut   dt   ct   n(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   tau_as_d_pot_d_q   s    (   R   R   R   R#   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR      s   	t   euler_angles_xyzc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sJ   | d  k r t j d ƒ } n  | |  _ t j d | ƒ |  _ |  j ƒ  d  S(   Ni    R   (   i    i    i    (   R   R   R   R   R   t   RBDA_Eq_4_7R   R   (   R   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   &   s
     	c         C   s)   t  j d |  j ƒ j ƒ  } | } | | S(   NR   (   R   t   RBDA_Eq_4_8R   R   (   R   R   R!   R"   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR#   ,   s    (   R   R   R   R#   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR$   $   s   	t   euler_angles_zxzc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sG   | d  k r t j d ƒ } n  | |  _ t d | ƒ |  _ |  j ƒ  d  S(   Ni    R   (   i    i    i    (   R   R   R   R   t   euler_angles_zxz_matrixR   R   (   R   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   3   s
     	c         C   s&   t  d |  j ƒ j ƒ  } | } | | S(   NR   (   t   euler_angles_zxz_SR   R   (   R   R   R!   R"   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR#   9   s    (   R   R   R   R#   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR'   1   s   	t   euler_angles_yxyzc           B   s   e  Z d  „  Z RS(   c         C   sG   | d  k r t j d ƒ } n  | |  _ t d | ƒ |  _ |  j ƒ  d  S(   Ni    R   (   i    i    i    i    (   R   R   R   R   t   euler_angles_yxyz_matrixR   R   (   R   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   @   s
     	(   R   R   R   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR*   >   s   t   euler_angles_xyzyc           B   s   e  Z d  „  Z RS(   c         C   sG   | d  k r t j d ƒ } n  | |  _ t d | ƒ |  _ |  j ƒ  d  S(   Ni    R   (   i    i    i    i    (   R   R   R   R   t   euler_angles_xyzy_matrixR   R   (   R   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   H   s
     	(   R   R   R   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR,   F   s   t   inf_euler_paramsc           B   s   e  Z d  „  Z RS(   c         C   sà   | d  k r t j d ƒ } n  | |  _ t | ƒ } | d k rQ t j d ƒ } nl t j | ƒ } t j | ƒ | | \ } } } t j | | | | f ƒ } t t | ƒ d ƒ d k  s½ t ‚ t	 j
 d | ƒ |  _ |  j ƒ  d  S(   Ni    i   gê-�™—q=R   (   i    i    i    (   i   i    i    i    (   R   R   R   R   t   abst   matht   cost   sint   AssertionErrorR   R   R   R   (   R   R   t   tht   pt   p0t   p1t   p2t   p3(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   P   s     	 "(   R   R   R   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR.   N   s   t   inf_axis_anglec           B   s   e  Z d  „  Z RS(   c      
   C   s™   | d  k r t j d ƒ } n  | |  _ t | ƒ } | d k rT t j d ƒ |  _ n7 | | } t j t j j	 d | d | d t
 ƒ ƒ |  _ |  j ƒ  d  S(   Ni    i   t   axist   anglet   deg(   i    i    i    (	   i   i    i    i    i   i    i    i    i   (   R   R   R   R   R/   t   sqrR   t   scitbxR0   t$   r3_rotation_axis_and_angle_as_matrixt   FalseR   (   R   R   R<   R;   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   `   s     	
(   R   R   R   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR:   ^   s   c      
   C   sÈ   |  \ } } } d „  } | | ƒ \ } } | | ƒ \ } } | | ƒ \ }	 }
 t  j | |	 | | |
 |	 | | | |
 | |
 | |	 | | |
 | | |	 | |
 |	 | | | | | | f	 ƒ S(   Nc         S   s   t  j |  ƒ t  j |  ƒ f S(   N(   R0   R1   R2   (   t   a(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   csn   s    (   R   R>   (   R   t   q1t   q2t   q3RC   t   c1t   s1t   c2t   s2t   c3t   s3(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR(   l   s    		-.c   	   
   C   sm   |  \ } } } d „  } | | ƒ \ } } | | ƒ \ } } t  j | | | d | | | d | d d f	 ƒ S(   Nc         S   s   t  j |  ƒ t  j |  ƒ f S(   N(   R0   R1   R2   (   RB   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRC   �   s    i    i   (   R   R>   (	   R   RD   RE   RF   RC   RI   RJ   RK   RL   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR)      s    	c         C   sS  |  \ } } } } d „  } | | ƒ \ } } | | ƒ \ } }	 | | ƒ \ }
 } | | ƒ \ } } t  j | | | | | | |
 |	 | | | | | | | |
 |	 | | |	 | |
 | |
 | | |	 | | |
 | |	 | | | | | | | |
 | |	 | | | | | | |
 | |	 | | | |
 | |	 | f	 ƒ S(   Nc         S   s   t  j |  ƒ t  j |  ƒ f S(   N(   R0   R1   R2   (   RB   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRC   Œ   s    (   R   R>   (   R   RD   RE   RF   t   q4RC   RG   RH   RI   RJ   RK   RL   t   c4t   s4(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR+   Š   s     		$##$c         C   sR  |  \ } } } } d „  } | | ƒ \ } } | | ƒ \ } }	 | | ƒ \ }
 } | | ƒ \ } } t  j | | |
 | | |
 |	 | |
 | | | |
 | | | | |	 | | | | | |	 | | | |
 | | | |	 | | | |
 | | | | | | |	 | | |	 | | | | |
 | | | | | |	 | f	 ƒ S(   Nc         S   s   t  j |  ƒ t  j |  ƒ f S(   N(   R0   R1   R2   (   RB   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRC   ¦   s    (   R   R>   (   R   RD   RE   RF   RM   RC   RG   RH   RI   RJ   RK   RL   RN   RO   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR-   ¤   s     		$$#t   body_with_spherical_jointc           B   s5   e  Z d  „  Z d d d „ Z d d „ Z d „  Z RS(   c         C   sI   | |  _  t j d |  j  ƒ |  _ | |  _ | |  _ | d d  ƒ |  _ d  S(   Nt   sitesR   (   RQ   R   t   spherical_alignmentt   At   wellst   spherical_typeR   t   J(   R   RQ   RT   RU   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   À   s
    			g�íµ ÷Æ°>c   
      C   s  | d  k r |  j } n  g  } | j } xÐ t t | ƒ ƒ D]¼ } g  } xŒ | | g D]} } t | ƒ } | | c | 7<t j | ƒ } |  j d | ƒ } t	 j
 d |  j d |  j d |  j d | ƒ }	 | j |	 ƒ qT W| j | d | d d | ƒ q: Wt j | ƒ S(	   NR   RQ   RT   RS   RV   i    i   i   (   R   RV   R   t   xranget   lent   listR   R   RU   R   t   potential_energyRQ   RT   RS   t   append(
   R   RV   t   epst   resultR   t   it   fst
   signed_epst   q_epst   e_pot(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt    d_pot_d_q_via_finite_differencesÇ   s      		$%c      	   C   sl   | d  k r |  j } n  t j d |  j d |  j d |  j d | ƒ } t j | j	 d  ƒ } | j
 d | ƒ S(   NRQ   RT   RS   RV   i   R   (   R   RV   R   t   potential_f_ext_bfRQ   RT   RS   R   R   t   elemsR#   (   R   RV   t   f_ext_bfR   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt	   d_pot_d_qØ   s     	$c      	   C   s�   |  j  d | ƒ } t j d |  j d |  j d |  j d | ƒ } |  j d | ƒ } | r� |  j d | ƒ } t | | d d ƒs„ t	 ‚ | } n | } | | f S(   NR   RQ   RT   RS   RV   R\   g-Cëâ6?(
   RU   R   RZ   RQ   RT   RS   Rc   Rg   R   R3   (   R   R   t   use_analytical_gradientsRV   t   ft   fint   anat   g(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt    compute_functional_and_gradientsß   s    	$	N(   R   R   R   R   Rc   Rg   Rm   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRP   ¾   s   	t   refineryc           B   s   e  Z d  „  Z d „  Z RS(   c   	      C   s  | |  _  t d | j d | d | ƒ |  _ t j |  j j j ƒ |  _ t	 j
 j d |  ƒ } |  j ƒ  \ } } t |  j  ƒ } | | Id | Id | j ƒ  Id | j ƒ  Id | j ƒ  IJ| d	 k |  _ |  j rð | d
 I| I| IJn  t j j ƒ  | j ƒ  |  _ d  S(   NRU   RQ   RT   t   target_evaluators   functional: %12.6gs   gradient norm: %12.6gs	   iter: %3ds	   nfun: %3dgü©ñÒMbP?s                  FAILED(   t   spherical_type_infoRP   t   typet   bodyR   t   doubleRV   R   t   xR?   t   lbfgst   runRm   t   strt   normt   itert   nfunt   failedt   syst   stdoutt   flush(	   R   Rp   RQ   RT   t   outt	   minimizerRi   Rl   t   label(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR   î   s"    	

	 c         C   sF   |  j  j d t j |  j ƒ d |  j j ƒ \ } } | t j | ƒ f S(   NR   Rh   (	   Rr   Rm   R   R   Rt   Rp   Rh   R   Rs   (   R   Ri   Rl   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRm      s    (   R   R   R   Rm   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRn   ì   s   	c         C   sf  t  |  ƒ d k s t d ƒ ‚ t  |  ƒ d k rH d \ } } t ƒ  } n. g  |  D] } t | ƒ ^ qO \ } } t j } t ƒ  d t f d „  ƒ  Y} | t t	 ƒ | t t
 ƒ | t t	 ƒ | t t
 ƒ | t t	 ƒ | t t
 ƒ | t t	 ƒ | t t	 ƒ | t t	 ƒ | t t	 ƒ g
 } i  } i  } x4 | D], }	 t j ƒ  | t |	 ƒ <d | t |	 ƒ <q$Wt j d d ƒ }
 xt | ƒ D]ý } g  t j |
 j d | d ƒ d d	 ƒ D] } t j | ƒ ^ q¡} t | ƒ } t j |
 j ƒ  ƒ } g  | D] } | | | | ^ qä} xk | D]c }	 t d
 |	 d | d | d | ƒ } | t |	 ƒ j | j ƒ | j  r	| t |	 ƒ c d	 7<q	q	WqsWg  } g  } x³ | D]« }	 | |	 IJ| t |	 ƒ } | j! ƒ  j" ƒ  } | j# d | d d ƒ | j t |	 ƒ t j$ | ƒ f ƒ | t |	 ƒ d k r| j d  ƒ q‡| j d | t |	 ƒ ƒ q‡W| Jt& d | d t	 d | d | ƒ | Jd GHd  S(   Ni    i   s   n_sites, n_trialsi   t	   type_infoc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   | |  _  | |  _ d  S(   N(   Rq   Rh   (   R   Rq   Rh   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR     s    	c         S   s   d |  j  j t |  j ƒ f S(   Ns   %s(use_analytical_gradients=%s)(   Rq   R   Rw   Rh   (   R   (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   __str__  s    (   R   R   R   Rƒ   (    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyR‚     s   	t   seedt   sizei   Rp   RQ   RT   R   t   prefixs     s
   failed: %dt   label_count_pairst   reverset   annotationst   OK(   i    i   (   i   i   ('   RX   R3   R   t   intR|   R}   R   t   objectR   RA   t   TrueR$   R'   R*   R,   R.   R:   R   t   size_tRw   t   mersenne_twisterRW   t   vec3_doublet   random_doubleR   R   R   R>   t    random_double_r3_rotation_matrixRn   R[   Rz   R{   t	   as_doublet   min_max_meant   showt   sumR   R	   (   t   argst   n_sitest   n_trialsR   t   argR‚   t   spherical_typest	   nfun_accut   n_failedt   tiR�   t   i_trialt   sRQ   R!   t   rRT   t	   nfun_sumsR‰   t   nfunst   stats(    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyRv     sl    %		:%!	!
"t   __main__i   ((   t
   __future__R    t   scitbx.rigid_body.protoR   R   t   scitbx.rigid_body.proto.utilsR   t   scitbx.lbfgsR?   t   scitbx.matht   scitbx.array_familyR   R   t   libtbx.test_utilsR   t   libtbx.utilsR   R   t   libtbx.str_utilsR	   R0   R|   RŒ   R
   R   R$   R'   R*   R,   R.   R:   R(   R)   R+   R-   RP   Rn   Rv   R   t   argv(    (    (    s^   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/tst_spherical_refinement.pyt   <module>   s:   				.	B