ó
§™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
 m Z d  d l Z d  d l m Z 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 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   division(   t   miller(   t   scaling(   t   sigmaa_estimation(   t   flex(   t   SorryN(   t   absolute_scalingt   outlier_plots(   t   erf(   t   table_utils(   t   null_out(   t   StringIOt   outlier_managerc           B   s€   e  Z d d  „ Z d e d „ Z d e d „ Z d d e d „ Z d e d d „ Z d d	 „ Z	 d
 „  Z
 d „  Z d d „ Z RS(   c         C   s”  | |  _  |  j  d  k r' t j |  _  n  | d k rB t ƒ  |  _  n  | |  _ |  j j ƒ  d  k ro t d ƒ ‚ n  |  j j ƒ  j	 |  j ƒ |  _
 |  j
 j ƒ  s± |  j
 j ƒ  |  _
 n  |  j j ƒ  sÕ |  j j ƒ  |  _ n  | |  _ t j |  j
 d t ƒ} |  j
 j d | j j ƒ  | j j ƒ  j ƒ  j ƒ  ƒ |  _ t j |  j j ƒ  ƒ d k sTt ‚ |  j j ƒ  j	 |  j ƒ |  _ |  j j ƒ  j	 |  j ƒ |  _ d  S(   Nt   silents   Unknown observation typet   auto_kernelt   datai    (    t   outt   Nonet   syst   stdoutR
   t
   miller_obst   observation_typeR   t	   deep_copyt   set_observation_typet   work_obst   is_xray_intensity_arrayt	   f_as_f_sqt   is_xray_amplitude_arrayt	   f_sq_as_ft   r_free_flagsR   t   kernel_normalisationt   Truet   customized_copyt   normalised_millerR   t   epsilonst	   as_doublet
   norma_workR   t   mint   AssertionErrort   select_centrict   centric_workt   select_acentrict   acentric_work(   t   selfR   R   R   t
   normalizer(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   __init__   s2    			"$g�íµ ÷Æ°>c   
      C   sq  d d t  j |  j j ƒ  ƒ } d t t  j |  j j ƒ  d ƒ ƒ } t  j | | k ƒ } t  j | | k ƒ } |  j j	 d |  j j
 ƒ  j |  j j
 ƒ  ƒ d | j | ƒ ƒ } |  j j	 d |  j j
 ƒ  j |  j j
 ƒ  ƒ d | j | ƒ ƒ } | j |  j ƒ } | j |  j ƒ } d | }	 |  j |	 | | ƒ }	 |  j J|  j |	 IJ|  j J| sW| S|  j j | j ƒ  ƒ Sd  S(   Ng      ð?g       @t   indicesR   s  
Outlier rejection based on basic Wilson statistics.
--------------------------------------------------

See Read, Acta Cryst. (1999). D55, 1759-1764. for details.
Reflections whose normalized intensity have an associated p-value
lower than %s are flagged as possible outliers.
    (   R   t   expR*   R   R   t   sqrtR(   t   boolR   R    R.   t   concatenatet
   common_setR   t   make_log_wilsonR   t   select(
   R+   t   p_basic_wilsont   return_datat   p_acentric_singlet   p_centric_singlet   acentric_selectiont   centric_selectiont	   all_flagst   all_p_valuest
   log_string(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   basic_wilson_outliers=   s,    !&

gš™™™™™¹?c         C   s¿  |  j  j ƒ  j ƒ  } |  j j ƒ  j ƒ  } d t j d t j |  j  j ƒ  ƒ t | ƒ ƒ } d t j t t j	 |  j j ƒ  d ƒ ƒ t | ƒ ƒ } t j
 | | k ƒ } t j
 | | k ƒ } |  j j d |  j  j ƒ  j |  j j ƒ  ƒ d | j | ƒ ƒ }	 |  j j d |  j  j ƒ  j |  j j ƒ  ƒ d | j | ƒ ƒ }
 |	 j |  j ƒ }	 |
 j |  j ƒ }
 d | } |  j | |	 |
 ƒ } |  j J|  j | IJ|  j J| s¥|	 S|  j j |	 j ƒ  ƒ Sd  S(   Ng      ð?g       @R.   R   sF  
Outlier rejection based on extreme value Wilson statistics.
-----------------------------------------------------------

Reflections whose normalized intensity have an associated p-value
lower than %s are flagged as possible outliers.
The p-value is obtained using extreme value distributions of the
Wilson distribution.
    (   R*   R   t   sizeR(   R   t   powR/   t   floatR   R0   R1   R   R    R.   R2   R3   R   R4   R   R5   (   R+   t   p_extreme_wilsonR7   t
   n_acentrict	   n_centrict   extreme_acentrict   extreme_centricR:   R;   R<   R=   R>   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   extreme_wilson_outliersl   s4    05
g{®Gáz„?g      $@c         C   s0  | d k s t  ‚ t j d | ƒ } t j |  j j ƒ  | k  ƒ } | | t j d } t j |  j j ƒ  | k  ƒ } |  j j ƒ  j ƒ  } t j |  j j	 ƒ  j ƒ  | k ƒ }	 g  }
 x— t
 | | |	 | ƒ D]€ \ } } } } | r?| r| r |
 j t ƒ q|
 j t ƒ n  | sL| r,|
 j t ƒ q<|
 j t ƒ qLqÌ |
 j t ƒ qÌ Wt j |
 ƒ }
 |  j j d |
 ƒ } d | | | f } | j ƒ  } |  j j ƒ  |  _ | j |  j ƒ } | j ƒ  j |  j j ƒ  ƒ sÛt  ‚ |  j j | j ƒ  ƒ j |  j ƒ } |  j | | ƒ } |  j | IJ| s(| S| Sd  S(   Ng333333Ó?g      ð?g       @R   sZ  
Possible outliers due to beamstop shadow
----------------------------------------

Reflection with normalized intensities lower than %4.3e (acentric)
or %4.3e (centric) and an associated d-spacing lower than %3.1f
are considered potential outliers.
The rationale is that these reflection could potentially be in
the shadow of the beamstop.
     (   R&   t   matht   logR   R1   R$   R   t   pit   centric_flagst
   d_spacingst   zipt   appendt   FalseR   R    t
   map_to_asuR   R3   R.   t   all_eqR5   R   t   make_log_beam_stopR   (   R+   t   levelt   d_minR7   t   z_lim_act   z_select_act   z_lim_ct
   z_select_ct   centrict   d_selectt	   tmp_finalt   zact   zct   dst   cft   final_selectiont   log_messageR   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   beamstop_shadow_outliers�   sP    		$c         C   s¹  |  j  d  k	 s t ‚ |  j  j ƒ  j t ƒ d k rX |  j  j d |  j  j ƒ  ƒ |  _  n  t j d |  j	 d | d |  j  d d d d	 d
 d ƒ } | j
 d |  j ƒ | j ƒ  } t | j ƒ } | j } t j d | j ƒ  d d  d | j ƒ  d t j | j ƒ  j ƒ  d ƒ d | j ƒ  j ƒ  d | j ƒ  d d | j ƒ  | j ƒ  ƒ }	 |	 j ƒ  }
 |	 j ƒ  } |	 j ƒ  } |	 j ƒ  } |	 j ƒ  } d } t j | | k  ƒ } | } | j | | ƒ } t t j | d ƒ ƒ } d t j | t  | j ƒ  ƒ ƒ } t j | | k ƒ } |  j	 j! d | ƒ } |  j	 j! d | ƒ } |  j	 j! d | ƒ } d } | j" ƒ  | j" ƒ  | j" ƒ  | j# ƒ  j$ |  j	 j# ƒ  ƒ s‹t ‚ | j# ƒ  j$ |  j	 j# ƒ  ƒ s¯t ‚ | j# ƒ  j$ |  j	 j# ƒ  ƒ sÓt ‚ |  j% | | | | | | | | ƒ } t& ƒ  } | | IJ| J| d IJ| d IJ| J| d IJt j' | j ƒ  d ƒ } | j
 d | d d ƒ |  j | j( ƒ  IJ| s{| S| j# ƒ  j$ |  j	 j# ƒ  ƒ sŸt ‚ |  j	 j) | j ƒ  ƒ Sd  S(   Ni    R   R   t   miller_calcR   t   kernel_width_free_reflectionsiÈ   t   n_sampling_pointsi   t   n_chebyshev_termsi   R   t   f_obst	   sigma_obst   f_calct   epsilong      ð?RZ   t   alphat   betag»½×Ùß|Û=g       @s�  

Model based outlier rejection.
------------------------------

Calculated amplitudes and estimated values of alpha and beta
are used to compute the log-likelihood of the observed amplitude.
The method is inspired by Read, Acta Cryst. (1999). D55, 1759-1764.
Outliers are rejected on the basis of the assumption that a scaled
log likelihood differnce 2(log[P(Fobs)]-log[P(Fmode)])/Q" is distributed
according to a Chi-square distribution (Q" is equal to the second
derivative of the log likelihood function of the mode of the
distribution).
The outlier threshold of the p-value relates to the p-value of the
extreme value distribution of the chi-square distribution.

s9   The histoghram of scaled (LL-gain) values is shown below.sA     Note: scaled (LL-gain) is approximately Chi-square distributed.s     scaled(LL-gain)  Frequencyi   t   ft   format_cutoffss   %7.3f(*   R   R   R&   R   t   countR   t   arrayR   t   sigmaa_estimatorR   t   showR   t   sigmaat   abst   normalized_obst   normalized_calcR   t   likelihood_ratio_outlier_testR   t   doubleR@   RL   t   posterior_modet   log_likelihoodt   posterior_mode_log_likelihoodt   posterior_mode_snd_dert   standardized_likelihoodR1   t   set_selectedR   R0   RA   RB   R    RQ   R.   RR   t   make_log_modelR   t	   histogramt   getvalueR5   (   R+   t   f_modelRT   R7   t   plot_outRr   Rt   t   obs_normt	   calc_normt   f_model_outlier_objectt   modest   likt   p_likt   s_dert   ll_gaint   epst   zerost   p_valuest   flagsRb   t   tmp_logt   histo(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   model_based_outliersô   s†    						"


$$$			



$c         C   s\   |  j  j d |  j  j ƒ  | ƒ j |  j  ƒ |  _  | d  k	 rX |  j  j d | ƒ |  _  n  d  S(   NR   RU   (   R   R    R   R   R   t   resolution_filter(   R+   t   scale_factorRU   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   apply_scale_to_original_dataa  s
    c         C   ss  d } | j  |  j ƒ } | j  |  j ƒ } |  j j | j ƒ  ƒ } | j | j ƒ  ƒ } g  } d } x° t | j ƒ  | j ƒ  | j ƒ  j ƒ  | j ƒ  ƒ D]{ \ }	 }
 } } |
 d k ré t |	 ƒ d t j	 |
 ƒ t | ƒ d | g } n t |	 ƒ d t | ƒ d	 g } | j
 | ƒ q™ Wt | ƒ d k rat j | g | d
 d d t d t d d d d ƒ} n  | d | } | S(   sx    produces a 'nice' table of outliers and their reason for
    being an outlier using basic or extreme wilson statistics t   Indext   E_obst   Centrics   p-values   No outliers were found.i    s   %5.3fs   %5.3es    inft   commentst
   has_headert   separate_rowst   prefixs   | t   postfixs    |s   
(   R—   R˜   R™   s   p-values   0.000N(   R3   R$   R5   R   RN   R.   RL   t   strRI   R0   RO   t   lenR	   t   formatR   R   RP   (   R+   Rb   R�   R�   t   headert   p_valst   roguest   rowst   tablet   hklt   et   ct   pt   this_rowt   final(    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyR4   h  s0    	2c         C   s™  |  j  j ƒ  |  _  |  j j ƒ  |  _ | j ƒ  } |  j j | j ƒ  ƒ } |  j  j | j ƒ  ƒ } d } d } g  } | j ƒ  j ƒ  d k rƒ| j ƒ  j ƒ  d k  rzxœ t | j ƒ  | j ƒ  j ƒ  | j ƒ  | j ƒ  | j	 ƒ  j ƒ  ƒ D]X \ } }	 }
 } } t
 | ƒ d	 |	 d
 |
 d	 t j | ƒ t
 | ƒ g } | j | ƒ qç Wt j | g | d d  d t d t d d d d ƒ} qƒd } n  | d | d } | S(   NR—   s	   d-spacingt   F_obss   E-valueRZ   s   No outliers were foundi    iô  s   %4.2fs   %6.1fRš   R›   Rœ   R�   s   | Rž   s    |s"   Over 500 outliers have been found.s   
s   
 
(   s   Indexs	   d-spacingR­   s   E-values   centric(   R$   RQ   R   R5   R   R@   RN   R.   RM   RL   RŸ   RI   R0   RO   R	   R¡   R   R   RP   (   R+   Rb   R�   R   t   evalsR¢   R¦   R¥   R§   t   dt   fobsR¨   R©   R«   R¬   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyRS   ‰  s<    		(		c	         C   s   d$ }	 d	 }
 g  } | j  | j ƒ  ƒ } | j  | j ƒ  ƒ } | j  | j ƒ  ƒ } | j  | j ƒ  ƒ } | j  | j ƒ  ƒ } |  j j ƒ  j  | j ƒ  ƒ } | j ƒ  j ƒ  d
 k rŽ| j ƒ  j ƒ  d k  r…| j ƒ  } | j ƒ  d  k r
| j ƒ  j ƒ  d
 d } n  xAt	 | j ƒ  | j ƒ  j ƒ  | j ƒ  | j ƒ  | j ƒ  | j ƒ  | j ƒ  | j ƒ  | | j
 ƒ  j ƒ  j ƒ  ƒ
 D]Ð \
 } } } } } } } } } } t | ƒ d | d | d | d | d | d | t | ƒ g } | j | ƒ | d  k	 rzt j d | d | d | d | d d | | d | d | d | d t | ƒ ƒ 	qzqzWt j |	 g | d d  d t d t d d d  d! ƒ}
 qŽd" }
 n  | d# |
 } | S(%   NR—   s	   d-spacingR˜   t   E_modelt   Scores   p-valueRt   RZ   s   No outliers were foundi    iô  g      $@s   %4.2fs   %6.3fs   %5.2fs   %5.3es   %4.3fR°   t   sigmat   fcalcRl   Rm   g      ð?Rk   R   t
   plot_titleRš   R›   Rœ   R�   s   | Rž   s    |sQ   More then 500 outliers were found. This is very suspicious. Check data or limits.s   
(   s   Indexs	   d-spacings   E_obsR±   R²   s   p-values   sigmaas   centric(   R5   R   R   RL   R.   R@   t   sigmasR   RM   RN   R"   R#   RŸ   RO   R   t   plotitR	   R¡   R   RP   (   R+   Rb   R�   RŒ   R�   t   e_obst   e_calcRt   R„   R¢   R¦   R¥   R¤   t   p_arrayt   ll_arrayt   ec_arrayt   sa_arrayRL   R¶   R§   R¯   t   eot   ect   llgRª   t   saR©   t   sR¨   R«   R¬   (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyR€   ¶  sl    								=			N(   t   __name__t
   __module__R   R-   RP   R?   RH   Rc   R“   R–   R4   RS   R€   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyR      s   *.0Vi	!	5(   t
   __future__R    t   cctbxR   t   mmtbxR   t   mmtbx.scalingR   t   cctbx.array_familyR   t   libtbx.utilsR   R   R   t   scitbx.mathR   t   libtbxR	   R
   R   RI   t   libtbx.str_utilsR   t   objectR   (    (    (    sM   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/outlier_rejection.pyt   <module>   s   