ó
«™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 m Z d  d l
 m 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 S(   iÿÿÿÿ(   t   division(   t   miller(   t   flex(   t   SorryN(   t   absolute_scalingt   relative_scaling(   t   pair_analysest   combined_scalingc           B   s2   e  Z d d d  „ Z d „  Z d „  Z d „  Z RS(   c         C   sø  | j  ƒ  j ƒ  |  _ | j  ƒ  j ƒ  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 | d  k	 rŸ | j
 |  _ | j |  _ | j |  _ | j |  _ d  |  _	 n  t |  _ | d  k	 rÒ | j d k rÒ t |  _ qÒ n  d |  _ | d  k	 rö | j |  _ n  |  j s|  j d k st ‚ n  | |  _ |  j d  k r>t j |  _ n  |  j j |  j ƒ \ |  _ |  _ |  j j  ƒ  j ƒ  |  _ |  j j  ƒ  j ƒ  |  _ i t d 6t d 6} |  j d k rÉt | d <t | d <n  |  j d k rït | d <t | d <n  |  j d k rt | d <t | d <n  | GH|  j GHt |  _ d } |  j J|  j d	 IJ|  j d
 IJ|  j d	 IJ|  j Jx|  j sç|  j J|  j d IJ|  j d | IJ|  j d IJ| d k rÚd |  _	 | d  k	 rÚ|  j j |  _	 qÚn  | d k rd |  _	 | d  k	 r|  j j |  _	 qn  |  j j |  j ƒ |  _ |  j j |  j ƒ |  _ |  j j |  j ƒ \ |  _ |  _ | d rx|  j ƒ  n  | d r�|  j ƒ  n  |  j  ƒ  } | d k r³t |  _ n  |  j sÚ| |  j k rÚt |  _ qÚn  | d 7} qiW|  ` |  ` d  S(   Nt   autoi
   i    t   lsqt   localt   lst   loct
   ls_and_locs*   ==========================================s*   =             Relative scaling           =s   --------------------------s       Scaling cycle %i   i   i   (!   t	   deep_copyt
   map_to_asut   x1t   x2t   Nonet   lsq_optionst   loc_optionst   out_optionst   overall_protocolt   cut_level_rmst   least_squares_optionst   local_scaling_optionst   outlier_rejection_optionst   targett   Falset
   auto_cyclet
   iterationst   Truet   max_iterationst   AssertionErrort   outt   syst   stdoutt   common_setst   s1t   s2t   convergencet   cut_level_rms_primaryt   cut_level_rms_secondaryt
   common_sett   perform_least_squares_scalingt   perform_local_scalingt   perform_outlier_rejection(   t   selft   miller_array_x1t   miller_array_x2t   optionsR"   t   scaling_taskst   countert
   num_reject(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyt   __init__   s˜    									!


			!

	c      	   C   sÛ   |  j  J|  j  d IJ|  j  d IJ|  j  J|  j j } t } |  j j d k rU t } n  t } |  j j d k rv t } n  t j |  j	 |  j
 d | d | d | ƒ} | j d |  j  ƒ | j j ƒ  |  _	 | j j ƒ  |  _
 d  S(	   Ns   Least squares scalings   ---------------------t
   amplitudest   basict   use_intensitiest   scale_weightt   use_weightsR"   (   R"   R   t   use_experimental_sigmasR   t
   scale_dataR   t   scale_targetR   t   ls_rel_scale_driverR   R   t   showt   nativeR   t
   derivative(   R/   t   use_exp_sigmast   use_intt   use_wtt
   ls_scaling(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR,   ‹   s(    				c         C   s  |  j  J|  j  d IJ|  j  d IJ|  j  J|  j j } t } |  j j d k rU t } n  i t d 6t d 6t d 6} t | |  j j <| GHt j |  j	 |  j
 | d | d | d	 |  j j d
 |  j j d |  j j d |  j  ƒ} | j j ƒ  |  _	 | j j ƒ  |  _
 d  S(   Ns   Local scalings   -------------R7   t   local_momentt	   local_lsqt   local_nikonovR9   R;   t	   max_deptht   target_neighbourst   sphereR"   (   R"   R   R<   R   R=   R   R>   R   t   local_scaling_driverR   R   RJ   RK   t   neighbourhood_sphereRA   R   RB   (   R/   RC   RD   t   local_scaling_target_dictionaryt   local_scaling(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR-   §   s4    	
c         C   sP  |  j  J|  j  d IJ|  j  d IJ|  j  J|  j  d |  j j IJ|  j  d |  j IJ|  j  d I|  j j IJ|  j  Ji t d 6t d 6t d 6} t | |  j j <t j |  j	 |  j
 d	 |  j d
 |  j j ƒ} |  j	 j ƒ  | j j ƒ  } | |  j
 j ƒ  | j j ƒ  7} | j j ƒ  |  _	 | j j ƒ  |  _
 |  j	 j |  j
 ƒ \ |  _	 |  _
 | S(   Ns   Outlier rejectionss   ------------------s    sigma criterion : %4.1f s    rms criterion   : %4.1f s    protocol        :t   solvet   rmst   rms_and_sigmaR   t   cut_level_sigma(   R"   R   RT   R   t   protocolR   R   R   t   outlier_rejectionR   R   t   sizet   natt   derR   R%   (   R/   t   outlier_protocolRV   t   number_of_outliers(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR.   Ë   s*    		 !N(   t   __name__t
   __module__R   R6   R,   R-   R.   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR      s
   z		$t   ano_scalingc           B   s   e  Z d d d  „ Z RS(   c         C   sI  | j  ƒ  s t ‚ | j ƒ  j ƒ  d k s0 t ‚ | |  _ | j ƒ  \ |  _ |  _ |  j j | ƒ |  _ |  j j	 d |  j j ƒ  ƒ |  _ |  j j | ƒ |  _ |  j j ƒ  j ƒ  |  j j ƒ  j ƒ  k sÍ t ‚ |  j j
 ƒ  |  _ |  j j
 ƒ  |  _ |  j d  k	 rEt |  j |  j | | ƒ } | j j
 ƒ  |  _ | j j
 ƒ  |  _ ~ n  d  S(   Ni    t   indices(   t   anomalous_flagR!   R_   RW   R2   t   hemispheres_acentricst   s1pt   s1nt   set_observation_typet   customized_copyR   t   x1pt   x1nR   R   R   R   (   R/   R0   R2   R"   t
   ano_scaler(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6   í   s&    
	"0	N(   R\   R]   R   R6   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR^   ì   s   t   naive_fa_estimationc           B   s   e  Z d d  „ Z RS(   c      	   C   sl  | d  k r t j } n  | |  _ | j ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ |  j j |  j ƒ \ |  _ |  _ t	 j
 |  j d t d | j ƒ} t	 j
 |  j d t d | j ƒ} |  j j d t j |  j j ƒ  |  j j ƒ  | j |  j j ƒ  |  j j ƒ  | j ƒ d t j |  j j ƒ  |  j j ƒ  | j | j |  j j ƒ  |  j j ƒ  | j | j ƒ ƒ |  _ d  S(   Nt   auto_kernelt   n_termt   datat   sigmas(   R   R#   R$   R2   R   R   t   isot   anoR%   R   t   kernel_normalisationR   t&   number_of_terms_in_normalisation_curveRe   R   t   sqrtRl   t   normalizer_for_miller_arrayRm   t   fa(   R/   Ro   Rn   R2   R"   t   normalizer_isot   normalizer_ano(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6     s&    	! 'N(   R\   R]   R   R6   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyRi     s   t   cns_fa_driverc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c      
   C   s  g  |  _  d |  _ xÇ | D]¿ } | j ƒ  s1 t ‚ | j ƒ  r |  j d 7_ | j ƒ  \ } } | j d t j | j	 ƒ  | j	 ƒ  ƒ d t j
 | j ƒ  | j ƒ  | j ƒ  | j ƒ  ƒ ƒ j | ƒ } |  j  j | ƒ q q Wd |  _ xt t | ƒ ƒ D]ÿ } xö t | d t | ƒ ƒ D]Û } |  j d 7_ | | j ƒ  j | | ƒ } | | j ƒ  j | | ƒ }	 | j |	 ƒ \ }
 } |
 j d t j |
 j	 ƒ  | j	 ƒ  ƒ d t j
 |
 j ƒ  |
 j ƒ  | j ƒ  | j ƒ  ƒ ƒ j |
 ƒ }
 |  j  j |
 ƒ qWqø W|  j ƒ  |  j ƒ  d  S(   Ni    i   Rl   Rm   (   t   ano_and_isot   na_anot   is_xray_amplitude_arrayR!   R`   Ra   Re   R   t   absRl   Rr   Rm   Rd   t   appendt   n_isot   ranget   lent   average_bijvoet_matesR%   t   normalise_allt   average_all(   R/   t   lambdast   sett   plust   minust   d_anot   set1t   set2t   t1t   t2t   tmp1t   tmp2(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6   D  s<    					 		
c         C   sH   xA |  j  D]6 } t j | d t ƒ} | j j ƒ  j | j ƒ } q
 Wd  S(   NRj   (   Rx   R   Rp   R   t   normalised_millerR   Rd   (   R/   R„   t   tmp_norm(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR�   j  s    	c         C   s)  |  j  d j ƒ  } |  j  d j ƒ  } |  j  d j ƒ  } xt t d t |  j  ƒ ƒ D]Z } | j |  j  | j ƒ  ƒ } | j |  j  | j ƒ  ƒ } | j |  j  | j ƒ  ƒ } qR W|  j  d j d | d | d | ƒ j |  j  d ƒ } | j	 ƒ  } | j
 ƒ  } | j ƒ  | j ƒ  j | ƒ j ƒ  |  _ d  S(   Ni    i   R_   Rl   Rm   (   Rx   R_   Rl   Rm   R~   R   t   concatenateRe   Rd   t	   f_as_f_sqt   merge_equivalentst   show_summaryt   arrayt	   f_sq_as_fRt   (   R/   t
   mean_indext	   mean_datat   mean_sigmast   set_not   final_millert   merged(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR‚   t  s$    
(   R\   R]   R6   R�   R‚   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyRw   C  s   	&	
t   mum_dadc           B   s   e  Z d  d „ Z RS(   g      ð?c         C   sì  | j  ƒ  |  _ | j  ƒ  |  _ |  j j ƒ  sB |  j j ƒ  |  _ n  |  j j ƒ  sf |  j j ƒ  |  _ n  |  j j |  j ƒ \ |  _ |  _ |  j j ƒ  \ } } | j ƒ  | j ƒ  |  _ | j ƒ  | j ƒ  |  _	 | j
 ƒ  | j
 ƒ  | j
 ƒ  | j
 ƒ  |  _ |  j j ƒ  \ } } | j ƒ  | j ƒ  |  _ | j ƒ  | j ƒ  |  _ | j
 ƒ  | j
 ƒ  | j
 ƒ  | j
 ƒ  |  _ t j |  j	 | |  j d ƒ |  _ t j |  j | | |  j d ƒ |  _ | j d |  j d |  j ƒ j |  j ƒ |  _ d  S(   Ng       @Rl   Rm   (   R   t   w1t   w2Rz   R•   R%   Ra   Rl   t   mean1t   diff1Rm   t   v1t   mean2t   diff2t   v2R   R{   t   new_diffRr   t   new_sigma_meanRe   Rd   t   dad(   R/   t   lambda1t   lambda2t   k1t   l1pt   l1nt   l2pt   l2n(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6   “  s,    !$(		(   R\   R]   R6   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyRœ   ’  s   t   singh_ramasheshan_fa_estimatec           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   su  | j  ƒ  |  _ | j  ƒ  |  _ |  j j ƒ  rB |  j j ƒ  |  _ n  |  j j ƒ  rf |  j j ƒ  |  _ n  |  j j |  j ƒ \ |  _ |  _ |  j j ƒ  \ |  _ |  _ |  j j ƒ  \ |  _	 |  _
 |  j j ƒ  |  j j ƒ  |  _ |  j	 j ƒ  |  j
 j ƒ  |  _ |  j j ƒ  |  j j ƒ  |  j j ƒ  |  j j ƒ  |  _ t j |  j ƒ |  _ |  j	 j ƒ  |  j	 j ƒ  |  j
 j ƒ  |  j
 j ƒ  |  _ t j |  j ƒ |  _ |  j j ƒ  |  j j ƒ  |  _ |  j	 j ƒ  |  j
 j ƒ  |  _ | | |  _ | | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ |  j  ƒ  |  j j! d |  j d |  j ƒ j" |  j ƒ |  _ d  S(   NRl   Rm   (#   R   R�   Rž   Rz   R‘   R%   Ra   t   p1t   n1t   p2t   n2Rl   R    R£   Rm   R&   R   Rr   R'   t   sum1t   sum2t   k1_sqt   k2_sqR   t   determinantRt   t   sigfat   selectort	   iselectort   at   bt   ct   compute_fa_valuesRe   Rd   (   R/   R�   Rž   Rª   t   k2(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6   ¸  sB    !  								
	c         C   sO   |  j  t j |  j d ƒ |  j t j |  j d ƒ } | d } | |  _ d  S(   Ng�íµ ÷Æ°>g       @(   R'   R   R{   R£   R&   R    R¹   (   R/   t   tmp(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyt   set_sigma_ratioï  s    
c         C   sÆ   |  j  |  j  |  j  d |  j |  j d |  j d |  _ |  j  |  j |  j |  j d |  j |  j |  _ d |  j |  j |  j |  j d |  j  |  j |  j |  j |  j |  j |  _ d  S(   Ni   g      Ð?g      ð?g       @g      À?(	   R·   R¶   R¼   R´   Rµ   R½   R£   R    R¾   (   R/   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyt   compute_coefsõ  s    ;c         C   sM   |  j  |  j  d |  j |  j |  _ |  j d k |  _ |  j j ƒ  |  _ d  S(   Ng      @i    (   R½   R¼   R¾   R¸   Rº   t
   iselectionR»   (   R/   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyt   compute_determinant   s    %c         C   s1  |  j  ƒ  |  j ƒ  |  j j ƒ  } |  j j | d ƒ |  _ |  j t j |  j ƒ } | d |  j	 } |  j t j |  j ƒ } | d |  j	 } | | k } | } | j | j ƒ  d ƒ } | j | j ƒ  d ƒ } | | } | d k  j ƒ  } | j | d ƒ } | | |  _
 |  j ƒ  |  j |  j
 |  _ d  S(   Ni    i   (   RÃ   RÅ   Rº   RÄ   R¸   t   set_selectedR½   R   Rr   R¼   Rt   RÂ   R¹   (   R/   t   reset_selectort   choice1t   choice2t   select1t   select2(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR¿     s$    



(   R\   R]   R6   RÂ   RÃ   RÅ   R¿   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR¯   ·  s
   	7			t   twmad_fa_driverc           B   s   e  Z d d  „ Z RS(   c   
      C   sÆ  | |  _  |  j  d  k r' t j |  _  n  | |  _ |  j  d IJ|  j  d IJ| d  k re t d ƒ ‚ n  | d  k r• |  j j d k r• t d ƒ ‚ q• n  | |  _ i t d 6t d 6t d 6} t | |  j j <d  |  _	 | d r|  j  d IJ|  j  Jt
 | | | | ƒ } | j j ƒ  |  _	 n  | d rY|  j  d	 IJ|  j  Jt | | g ƒ } | j |  _	 n  | d r˜|  j  d
 IJ|  j  Jt | | | ƒ } | j |  _	 n  t j |  j	 d t ƒ}	 |	 j j ƒ  |  _	 d  S(   Ns   FA estimations   =============sG   f"(w1)/f"(w2) ratio is not defined. Please provide f" values upon inputt	   algebraics‰   
delta f' f" ratio is not defined.
Either provide f' and f" values upon input,
or chose different Fa estimation protocol.
               t   cnst   combine_anos0    Using algebraic approach to estimate FA values s*    Using CNS approach to estimate FA values s    Combining anomalous data onlyRj   (   R"   R   R#   R$   R2   R   RU   R   R   t	   fa_valuesR¯   Rt   R•   Rw   Rœ   R§   R   Rp   RŽ   (
   R/   R¨   R©   Rª   RÀ   R2   R"   RU   RÁ   t   norma(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyR6   "  sX    			

	
	

			N(   R\   R]   R   R6   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyRÌ   !  s   (   t
   __future__R    t   cctbxR   t   cctbx.array_familyR   t   libtbx.utilsR   t
   iotbx.philt   iotbxt   mmtbx.scalingt   mmtbxR   R   R   R#   t   objectR   R^   Ri   Rw   Rœ   R¯   RÌ   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/fa_estimation.pyt   <module>   s   à,+O%j