ó
¬™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 d  d
 l m 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   iround(   t
   group_argssÿ     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   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.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(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt    compute_functional_and_gradients2   s    0" 
(   t   __name__t
   __module__R%   R)   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.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(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt*   sigmaa_estimator_kernel_width_d_star_cubed<   s    )t   sigmaa_estimatorc        	   B   sq   e  Z d
 d
 e e d  d e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d
 d „ Z d
 e d	 „ Z RS(   i   i
   c         C   sC  | | g j  d  ƒ d k s! t ‚ | |  _ t | ƒ |  _ | |  _ | |  _ | |  _ |	 |  _	 | re|  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 ƒ  r8|  j j ƒ  |  _ n  |  j j ƒ  d  k se|  j j ƒ  se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  t ƒ d k r8|  j j d |  j j ƒ  ƒ |  _ n  |  j j |  j j ƒ  ƒ |  _ |  j j |  j j ƒ  ƒ |  _  |  j j ƒ  j! ƒ  d k ržt" d ƒ ‚ n  |  j d  k rÎt# d |  j d |  j ƒ |  _ n  t$ j% d |  j j ƒ  d	 |  j  j ƒ  d
 |  j j& ƒ  j ƒ  d |  j j' ƒ  j ƒ  d |  j ƒ |  _( |  j j' ƒ  j ƒ  } t) j* | ƒ |  _+ t) j, | ƒ |  _- d  |  _. | r¸t) j/ t0 d | d d ƒ ƒ |  j- |  j+ | d |  j+ |  _. n� |  j+ d 9_+ |  j- d 9_- | rt1 j2 d | d |  j+ d |  j- d t ƒ |  _. n; t) j/ t3 | ƒ ƒ |  j- |  j+ t4 | d ƒ |  j+ |  _. |  j. j! ƒ  | k sbt ‚ t) j/ ƒ  |  _5 |  j5 j6 |  j. j! ƒ  ƒ t) j/ ƒ  |  _7 |  j7 j6 |  j. j! ƒ  ƒ xU |  j. D]J } t8 |  j( | ƒ } |  j5 j9 | j: ƒ |  j7 j9 |  j( j7 d | ƒ ƒ q¼Wt) j; d |  j5 d ƒ } |
 r@t) j< |  j7 ƒ } n d  } t1 j1 d |  j	 d |  j. d | d | ƒ } t= |  j	 |  j+ |  j- | j> ƒ } d „  } | | j? |  j. ƒ ƒ |  _@ | | j? | ƒ ƒ |  _A t) j* |  jA ƒ d k sët ‚ t) j, |  jA ƒ d k s	t ‚ |  j j d |  jA ƒ |  _A d  |  _B d  |  _C d  |  _D d  S(   Ni   t   auto_kerneli    R.   s   No free reflections.R1   R2   t   e_obst   e_calct   centrict   d_star_cubedt   widthi   g®Gáz®ï?g)\�Âõ(ð?t   nt   lowt   hight   include_limitsg      ð?t   n_termst   x_obst   y_obst   w_obsc         S   s   d d t  j |  ƒ S(   Ng      ð?(   R   R    (   t   values(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt   reverse_reparamÆ   s    (E   R/   R   R,   t
   miller_obst   abst   miller_calcR1   R2   t   kernel_width_d_star_cubedt   n_chebyshev_termst
   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_calcR.   t   arrayt   selectt   free_norm_obst   free_norm_calcR-   t   RuntimeErrorR:   R   R;   t   centric_flagsR@   t   sigma_target_functorR   R   t   min_hR   t   max_ht   h_arrayR   t   xrangeR   t   chebyshev_nodest   ranget   floatt   sigmaa_arrayt   reservet   sum_weightsR	   t   appendR!   t   logt   sqrtR   t   coefsR   t   sigmaa_fittedt   sigmaa_miller_arrayt   alphat   betat	   fom_array(   R"   RL   RN   R1   R2   RO   t   kernel_in_bin_centerst   kernel_on_chebyshev_nodest   n_sampling_pointsRP   t   use_sampling_sum_weightst   make_checks_and_clean_upt   d_star_cubed_overallR   t   stimatort
   reparam_saRI   t   fit_lsqt   cheb_polRK   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyR%   K   sÀ    !										B			;								c         C   s   |  j  S(   N(   Rw   (   R"   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.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   powRj   Rs   Rv   t   linear_regressiont   slopeR   t   piR   Rt   (   R"   R   t   yt   result(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt   sigmaa_model_errorÔ   s    	c         C   sç   |  j  d  k rà |  j j ƒ  |  j j ƒ  |  j j ƒ  } | d |  j j ƒ  |  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.   (   Rz   R   Rw   R.   R`   R^   R   t   tanhR   R   t   bessel_i1_over_i0Rf   t   set_selectedt   customized_copy(   R"   t   tmp_xt   centric_fomt   acentric_fomt   centricst	   final_fom(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.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_modelRx   Ry   t   space_groupt   miller_indicesR.   (   t
   alpha_betaR   t   fom_and_phase_errorRL   R.   R   RM   RN   R˜   RR   t   phase_errorR�   (   R"   Rx   Ry   RŠ   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt   phase_errorsî   s    	c         C   s´   |  j  d  k r¤ |  j j ƒ  t j |  j j |  j j ƒ |  _  d |  j j ƒ  |  j j ƒ  |  j j |  _	 |  j
 j d |  j  ƒ |  _  |  j
 j d |  j	 ƒ |  _	 n  |  j  |  j	 f S(   Ng      ð?R.   (   Rx   R   Rw   R.   R   Rt   R\   t   normalizer_for_miller_arrayR_   Ry   RL   Ra   (   R"   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyRš   ú   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   stdoutRO   R2   Rj   R-   RP   t   zipRq   Ro   Rv   (   R"   t   outR   t   wt   sat   saft   d(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt   show  s2    



	
	!c         C   s  | d  k r t j } n  | s% | Jn  | s8 | d IJn  | sK | d IJn  | s^ | d IJn  g  } g  } x� t |  j |  j ƒ D]j \ } } | d k r¡ d } n d d | d } | sÐ | d | | f IJn  | j | ƒ | j | ƒ q€ W| sû | Jn  | s| Jn  t d | d | ƒ S(   Ns   SigmaA vs Resolutions   --------------------s#   1/d^3      d    sum weights  sigmaAi    RŸ   i   s   %7.4fg      ð?i   i   s   %s %7.4ft
   resolutionR!   s          gUUUUUUÕ?(   R   R    R¡   R¢   Rj   Ro   Rr   R   (   R"   R£   t   silentR©   R!   R   R¥   R§   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt
   show_short"  s2         " 	   N(   R*   R+   R   t   FalseR   R%   R!   R‹   R•   R�   Rš   R¨   R«   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyR;   J   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   libtbxR   t   philt   parset   sigmaa_estimator_paramst   objectR	   R:   R;   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/sigmaa_estimation.pyt   <module>   s"   			