ó
©™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 m Z d  d l	 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 Z e j j d
 ƒ Z d e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   absolute_scaling(   t   ext(   t   flex(   t   max_likN(   t   chebyshev_polynome(   t   chebyshev_lsq_fit(   t   iroundsÿ     kernel_width_free_reflections = 100
    .type = int
  kernel_on_chebyshev_nodes = True
    .type = bool
  number_of_sampling_points = 20
    .type = int
  number_of_chebyshev_terms = 10
    .type = int
  use_sampling_sum_weights = True
    .type = bool
t   sigmaa_point_estimatorc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sÍ   | |  _  | |  _ d  |  _ t j d g ƒ |  _ d |  _ d |  _ t	 j
 j d d ƒ } t	 j
 j d t d t ƒ } t	 j
 j d |  d	 | d
 | ƒ |  _ |  j |  j |  j d t j |  j d ƒ |  _ d  S(   Ng      ð¿g®Gáz®ï?g{®Gáz„?t   max_iterationsid   t-   ignore_line_search_failed_step_at_lower_boundt-   ignore_line_search_failed_step_at_upper_boundt   target_evaluatort   termination_paramst   exception_handling_paramsg      ð?i    (   t   functort   ht   Nonet   fR   t   doublet   xt   maxt   mint   scitbxt   lbfgst   termination_parameterst   exception_handling_parameterst   Truet   runt	   minimizert   matht   expt   sigmaa(   t   selft   target_functorR   t   term_parametersR   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   __init__   s    							c         C   s¶   |  j  |  j |  j  d t j |  j d ƒ } |  j |  j  t j |  j d ƒ d t j |  j d ƒ d } |  j j |  j | ƒ \ } } | |  _ | t	 j
 | | g ƒ f S(   Ng      ð?i    g       @(   R   R   R   R   R   R   t   target_and_gradientR   R   R   R   (   R!   R    t   dsdxR   t   g(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt    compute_functional_and_gradients1   s    0" 
(   t   __name__t
   __module__R$   R(   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyR      s   	c   	      C   s¼   | d k s t  ‚ |  j ƒ  } |  j ƒ  j t ƒ } | } | d k rV | | | 9} n  t | t | ƒ ƒ } t d | t d | ƒ ƒ } g  |  j ƒ  D] } | d ^ q” \ } } | | | S(   Ni    i   i   i   g      ø?(	   t   AssertionErrort   sizet   datat   countR   R   R   R   t   min_max_d_star_sq(	   t   r_free_flagst   kernel_width_free_reflectionst   n_reflt   n_freet   n_refl_per_bint   n_binst   dsst   dsc_mint   dsc_max(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt*   sigmaa_estimator_kernel_width_d_star_cubed;   s    )t   ta_alpha_beta_calcc        	   B   sw   e  Z d d e e d  d e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d d	 „ Z d d
 „ Z RS(   i   i
   c         C   sž  | | g j  d  ƒ d k s! t ‚ | |  _ t | ƒ |  _ | |  _ | |  _ | |  _ |
 |  _	 | |  _
 | rn|  j j ƒ  |  _ |  j j ƒ  |  _ |  j j ƒ  |  _ |  j j ƒ  j |  j j ƒ  ƒ sÉ t ‚ |  j j |  j ƒ |  _ |  j j ƒ  j |  j j ƒ  ƒ st ‚ |  j j ƒ  st ‚ |  j j ƒ  rA|  j j ƒ  |  _ n  |  j j ƒ  d  k sn|  j j ƒ  snt ‚ n  |  j j ƒ  d  k rž|  j j |  j ƒ |  _ n  t j |  j d t ƒ|  _ |  j j j ƒ  |  _ t j |  j d t ƒ|  _ |  j j j ƒ  |  _ |  j j |  j j ƒ  ƒ |  _ |  j j |  j j ƒ  ƒ |  _  |  j j ƒ  j! ƒ  d k rdt" d ƒ ‚ n  d  |  _# d  |  _$ d  |  _% |  j j& d |  j
 ƒ |  _' d  S(   Ni   t   auto_kerneli    s   No free reflections.R-   ((   R.   R   R+   t
   miller_obst   abst   miller_calcR0   R1   t   kernel_width_d_star_cubedt   n_chebyshev_termst   ta_dt
   map_to_asut   indicest   all_eqt
   common_sett   is_real_arrayt   is_xray_intensity_arrayt	   f_sq_as_ft   observation_typet   is_xray_amplitude_arrayt   set_observation_typeR   t   kernel_normalisationR   t   normalized_obs_ft   normalised_miller_dev_epst   normalized_obst   normalized_calc_ft   normalized_calct   selectR-   t   free_norm_obst   free_norm_calcR,   t   RuntimeErrort   alphat   betat	   fom_arrayt   arrayt   ta_d_miller(   R!   R<   R>   R0   RA   R1   R?   t   kernel_in_bin_centerst   kernel_on_chebyshev_nodest   n_sampling_pointsR@   t   use_sampling_sum_weightst   make_checks_and_clean_up(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyR$   J   sP    !								F			c         C   s   |  j  S(   N(   t   sigmaa_miller_array(   R!   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyR    Ñ   s    c         C   s}   d t  j |  j d ƒ } t  j |  j ƒ } t  j | | ƒ } | j ƒ  t j d } | d k  rj d  } n t j
 | ƒ } | S(   Ng      Ð?g       @g      @i   i    gUUUUUUå?(   R   t   powt   h_arrayt   logt   sigmaa_fittedt   linear_regressiont   slopeR   t   piR   t   sqrt(   R!   R   t   yt   result(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   sigmaa_model_errorÔ   s    	c         C   sÕ   |  j  d  k rÎ |  j |  j j ƒ  |  j j ƒ  } | d |  j |  j } t j | ƒ } t j	 j
 d | ƒ } |  j j ƒ  j ƒ  } | j | d ƒ } | j | d ƒ } | | } |  j j d | ƒ |  _  n  |  j  S(   Ng      ð?g       @i    R-   (   RX   R   RA   RQ   R-   RO   R   t   tanhR   R   t   bessel_i1_over_i0RZ   t   centric_flagst   set_selectedt   customized_copy(   R!   t   tmp_xt   centric_fomt   acentric_fomt   centricst	   final_fom(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   fomà   s    #
c         C   sš   |  j  ƒ  \ } } t j d |  j j ƒ  d t j |  j j ƒ  ƒ d | j ƒ  d | j ƒ  d |  j j ƒ  d |  j j	 ƒ  ƒ j
 ƒ  } |  j j d | ƒ } | S(   Nt   f_obst   f_modelRV   RW   t   space_groupt   miller_indicesR-   (   t
   alpha_betaR   t   fom_and_phase_errorR<   R-   R   R=   R>   Ry   RC   t   phase_errorRp   (   R!   RV   RW   Rj   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   phase_errorsï   s    	c         C   s§   |  j  d  k r— d GH|  j t j |  j j |  j j ƒ |  _  d |  j |  j |  j j |  _ |  j	 j
 d |  j  ƒ |  _  |  j	 j
 d |  j ƒ |  _ n  |  j  |  j f S(   Ns   re calc a/bg      ð?R-   (   RV   R   RA   R   Rh   RM   t   normalizer_for_miller_arrayRP   RW   R<   RY   (   R!   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyR{   û   s    	c         C   s   |  j  j |  j j f S(   N(   RM   R   RP   (   R!   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt'   eobs_and_ecalc_miller_array_normalizers  s    c         C   s  | d  k r t j } n  | J| d IJ| d IJ| d |  j IJ| d I|  j IJ| d I|  j j ƒ  IJ| d I|  j IJ| J| d IJx€ t |  j |  j	 |  j
 |  j ƒ D]] \ } } } } | d k r× d } n d d | d } | d | | | | | | | f IJq° W| J| Jd  S(   Ns   SigmaA estimation summarys   -------------------------s!   Kernel width d* cubed     :  %.6gs   Kernel width free refl.   : s   Number of sampling points : s   Number of Chebyshev terms : s4   1/d^3      d    sum weights  sigmaA   fitted    diffi    t    i   s   %7.4fg      ð?i   i   s&   %5.4f  %s  %8.2f   %7.4f  %7.4f  %7.4fs          gUUUUUUÕ?(   R   t   syst   stdoutR?   R1   Rb   R,   R@   t   zipt   sum_weightst   sigmaa_arrayRd   (   R!   t   outR   t   wt   sat   saft   d(    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   show
  s2    



	
	!c         C   s§   | d  k r t j } n  | J| d IJ| d IJ| d IJx^ t |  j |  j ƒ D]G \ } } | d k rq d } n d d | d } | d | | f IJqP W| J| Jd  S(   Ns   SigmaA vs Resolutions   --------------------s#   1/d^3      d    sum weights  sigmaAi    R�   i   s   %7.4fg      ð?i   i   s   %s %7.4fs          gUUUUUUÕ?(   R   R‚   Rƒ   R„   Rb   R†   (   R!   R‡   R   R‰   R‹   (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt
   show_short&  s     


" 	N(   R)   R*   R   t   FalseR   R$   R    Rk   Rv   R~   R{   R€   RŒ   R�   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyR:   I   s    {						(   t
   __future__R    t   mmtbx.scalingR   R   t   cctbx.array_familyR   t   mmtbxR   t   scitbx.lbfgsR   t   scitbx.mathR   R   t   libtbx.math_utilsR   R   R‚   t
   iotbx.philt   iotbxt   philt   parset   sigmaa_estimator_paramst   objectR   R9   R:   (    (    (    sN   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/ta_alpha_beta_calc.pyt   <module>   s    			