ó
¨™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 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 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d d „ Z  d e j! f d „  ƒ  YZ" d e j! f d „  ƒ  YZ# d e f d „  ƒ  YZ$ d
 S(   iÿÿÿÿ(   t   division(   t   scaling(   t   xray_scattering(   t   flex(   t   adptbx(   t   uctbx(   t   chebyshev_polynome(   t   chebyshev_lsq_fit(   t   eigensystemN(   t   Sorry(   t   format_value(   t   table_utilst   gamma_proteinc           B   s   e  Z d  d g Z d „  Z RS(   t   gammat   sigma_gammac   
   -   C   s  d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- g- } t  j | ƒ } d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ g- } t  j | ƒ } d[ } d\ } t d] | | | ƒ } t d] | | | ƒ } | j ƒ  } | | k } | j | | ƒ } | | k }	 | j |	 | ƒ } t  j | ƒ | k sÑt ‚ t  j | ƒ | k sìt ‚ | j | ƒ |  _	 | j | ƒ |  _
 d  S(^   Ng­‚çOþÏ¿g°µ¸@b‘Ã?gdÝ¯@’o±?gGi6ÜÚ2Ä?gjRùaCÿ³¿gM´vÆ¦?gð0Rœx9¦¿gæ-Ú°84™?g|Ö:¼Êl?g—-³j°¿gmŒ/Ä�ïº?gß¶z�zþ¹¿g#‚á-0¡?gìm›J{¿gøòlÅûØw¿gûæŸ`±Û‹?gïi|Fj‹¿g	o‹'—?göUIØñQ”¿g

4=/�’?g%ÛøÓÉ‚¿gIÚ*ø{?gvñ‹ºÐïx¿gC"�7s::?gÄÏ{9†E¿gÜŒzËp¿g¢Òû‚w?gãbÎ:IÓ~¿g_ñžÔ·íX¿gtûö.Îm¿gjÍ8Ø4Uz¿gh6|Ž3x?g5xšÊf‚¿gË8göäm?gÊ¡1¡gs¿gëÝzL—ÑM¿gÌw#ÖÇO?g=õý/tät¿gìÊb`/s?gƒ£r2lôi¿g²Á�/o?gÝ¨@OçY?g’h’«R?gYV^™¿Y?gÆ!ò]ðp¿gÝlf„Ò¤?gÛ-…�¨\�?gè“ÈjÉÃŠ?gA»‘™@;-¿g!ñÃõ¿g;»!‹1o¿g TëÆ{´[¿g0ô*¯ÁÇl¿g)[Ø=ÜêO¿gdÉ¼Ïvn¿g@�|ãýÏ4?gÕ¬ÕU€r¿g{g�YL¿gM½å+g¿g°�ð“³óI?gÐ5Ö¿³^D?g}M¡Œwn^?gT Ã¥TT?gPØƒ%-?gž�z¡G1¿gq�ÇÙªYR¿gâÃ˜;ÝKK¿gV¤ƒÚÔR¿g…I<ÙzK¿g1šX‡
U)¿gvo%2ë9¿g€‘0!#d-¿g/³ÑcYÊN¿g5�ÊæÅD¿g“—V&"ü5¿gÉ@Zrº:?g¸jNsÔ§W?gj�w²L1V?g›ùÍ7k[?gjôi4†?W?gÀ÷¬J@ØU?g¾7¨´ÃL?gñ©VvÌ<?gØ0“ØõâD?g§†«?ÿ¼G?g^^Y6&?P?gÄær=K?g¸Êdf!?g/€ý¢Ï¾.¿gbNÓÒ¼íQ¿gü©ñÒMb€?g®Gázæ?i-   (   R   t   doubleR   t	   deep_copyt   set_selectedt   mint   AssertionErrort   maxt   fR   R   (
   t   selft	   d_star_sqt
   coefs_meant   coefs_sigmat   lowt   hight
   gamma_chebt   gamma_sigma_chebt   lower_then_low_limitt   higher_then_high_limit(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   __init__   s€    		(   t   __name__t
   __module__t	   __slots__R    (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR      s   	t   gamma_nucleicc           B   s   e  Z d  „  Z RS(   c   
   -   C   s  d d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- g- } t  j | ƒ } d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ g- } t  j | ƒ } d[ } d\ } t d] | | | ƒ } t d] | | | ƒ } | j ƒ  } | | k } | j | | ƒ } | | k }	 | j |	 | ƒ } t  j | ƒ | k sÑt ‚ t  j | ƒ | k sìt ‚ | j | ƒ |  _	 | j | ƒ |  _
 d  S(^   NgvŠ¬Ÿ/[Ó¿gqL˜«? Â?gR¦v�e°?gBÝôþÂ?géÁkru³¿g"Z€ÈÃ½º?g9€�¯Á¿g–N““–1 ?g àH¢L¥?gŸŠâ¬¶¿gßc^Û+O°?guù=˜�ï¦¿gÁ¬ C•‚?g³^öC’›?gb•ñù¾ª¿g‹þÓGé“?gÞH­ÁWR¿gÒ"Y?ŒÖ�¿gËz{?g
!Ál “¿g·‰†·V·ƒ?g™§xçî²�?g3‚'s›¿g3Œ› W;•?g“å	g?gª7€Ì%or?gÛ¢|I9?gý6ûÅN �¿g§úÂ³ük?g¥7”‘5_?g3"Š0J‹¿gïŽÏ°·s?g(ë¬sYÈn?gômÍXn‡¿g¿çÊ7šÜ|?gôo8Ä ‡�¿gíîõwê@?gÀœZJ]Ì|¿g�sa¡†±…¿g“dcp„?gŠÆ˜O@ˆq¿g`=GL•]?gVñàa?g¸È¼Q²s?gZ?¦ŠpÊk?gÚ_�Æ¹À?gM�Úš?gqï¨cô.—?gÞ²§†à…?g9•=év?gç/içä„?g#ù0å˜ç{¿gó¦¥eÖCH?g$§’ƒÔ¢q?gv&Të{¿g‡ðy†ùq?g¨Ë×îr¿grd¾©Ûa¿g[ž¦Ä£^?gp�'ö`@u¿gÐ-Ú8¿g�öÓ…°^¿gîå¸kw¿g´×Q"ra¿g-îª¢Øn¿gÁ�žL!%j¿g»Ó"#É\?gÜî‚~Qc¿gA¡¤+"Äq?ggCR3˜l?g‰ÁÚÂ‹i?gøa0óðÒn?gÊGž	N=W¿gÑü¸�˜šj¿gî©ÖÈu¿g»ÈÂ0¹p�¿gi·
¶zw¿g“BŸÚìöt¿g7ùªíLu¿gbÁè ØÉQ¿g^‹/vQ¾G¿gS"oQºP?g R·¦!I?g0óÑÿš[6?gÿ�Ðgka?g>ïôýLñ4¿g©ºœÈgX¿g<ó¡Ú4?gtÍTWÍF¿g†çî½ùH>¿gü©ñÒMb€?g®Gázæ?i-   (   R   R   R   R   R   R   R   R   R   R   R   (
   R   R   R   R   R   R   R   R   R   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    p   s~    		(   R!   R"   R    (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR$   o   s   t   expected_intensityc           B   s    e  Z d  Z d d d d „ Z RS(   sx   
  This class computes the expected intensity for a given d_star_sq_array given
  some basic info about ASU contents.
  g        g       @c         C   sØ   | j  | ƒ | j |  _ |  j d | j |  _ |  j | _ |  j t j | | d ƒ |  _ |  j t j | ƒ 9_ | j |  _ | j |  j |  _ |  j t j | | d ƒ |  _ |  j t j | ƒ 9_ d  S(   Ng      ð?g       @(	   t	   scat_datat   sigma_tot_sqt   mean_intensityt	   gamma_totR   t   expt   matht   gamma_tot_sigmat   sigma_intensity(   R   t   scattering_infot   d_star_sq_arrayt   p_scalet   b_wilsont   magic_fudge_factor(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    Ê   s    (   R!   R"   t   __doc__R    (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR%   Å   s   t   scattering_informationc           B   s,   e  Z d d d d d d  „ Z d d „ Z RS(   c         C   s5  d |  _ d |  _ d |  _ | d k	 rN | d k s9 t ‚ | d k sN t ‚ n  | d k	 sf | d k	 r{ | d k s{ t ‚ n  | d k	 r® | d k	 s™ t ‚ | d k	 s® t ‚ n  | d k rA| d k rÏ d } n  d d d d t | ƒ } | d k r d } n  d d  d" d$ d& t | ƒ } | | } | | } | | } n  | d k	 rY| |  _ n	 d |  _ | d k	 rz| |  _ n	 d |  _ |  j |  j d k sŸt ‚ | d k r(d } | d k r
i d t | ƒ d 6d t | ƒ d 6d t | ƒ d 6d t | ƒ d 6} n  | d k rti t | ƒ d	 d 6t | ƒ d
 d 6t | ƒ d d 6t | ƒ d d 6t | ƒ d d 6} n  | d k r(| d k r%i t | ƒ d	 d t | ƒ d 6t | ƒ d
 d t | ƒ d 6t | ƒ d d t | ƒ d 6t | ƒ d d t | ƒ d 6t | ƒ d d 6} q%q(n  | |  _ d S('   s0    Returns scattering info for specified structureg        g       @g      ð?g      @g      @g      ø?g      @g333333ó?g      0@gffffff#@gffffff@gš™™™™™@g      .@i    t   Ht   Ct   Nt   Ot   PNg       @g       @g      >@g     €f@g      %@g     `R@g333333#@g333333S@g      0@g      0@g™™™™™M@g33333Óu@g™™™™™™:@gfffffFg@gš™™™™™G@gš™™™™™w@g      .@g      l@(	   t   NoneR'   R)   R,   R   t   floatt   fraction_proteint   fraction_nucleict   asu_contents(   R   t
   n_residuest   n_basesR>   R<   R=   t   tot(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    ä   sf    					

		"c         C   s„  | d  k r* d  |  _ d  |  _ d  |  _ n  | d  k	 r€t j | j ƒ  ƒ |  _ i  } x` |  j j ƒ  D]O \ } } t	 j
 | ƒ j ƒ  | | <| | j | ƒ } |  j | | | 7_ qd W| j ƒ  d k r€t | ƒ } | j |  j |  _ t | ƒ } | j |  j |  _ |  j |  j |  j |  j |  _ | j |  j | j |  j | j |  j | j |  j |  _ t j |  j ƒ |  _ q€n  d  S(   Ni    (   R:   R'   R,   R)   R   R   t   sizeR>   t   itemsR   t   wk1995t   fetcht   at_d_star_sqR   R   R<   t
   gamma_protR$   R=   t	   gamma_nucR   t   sqrt(   R   R   t	   gaussianst   chemical_typet   n_atomst   f0RG   RH   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR&   :  s0    		N(   R!   R"   R:   R    R&   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR4   ã   s   Qg        c         C   sÝ  d  } y% |  j j |  j j ƒ  | k ƒ } Wn$ t k
 rA ‚  n t k
 rQ n X| d  k r| |  j |  j ƒ  | k ƒ } n  t } | j ƒ  r£ | j ƒ  } t	 } n  | j ƒ  s¶ t
 ‚ | d  k	 rç t j | j ƒ  | ƒ } | | } n  t j | j ƒ  | j ƒ  | | j ƒ  | ƒ } | j ƒ  d  k	 rVt j | j ƒ  | j ƒ  | | j ƒ  | ƒ }	 n d  }	 | j d | d |	 ƒ j | ƒ } | rÙt j | j ƒ  ƒ }
 |
 t j t j j ƒ k  sÊt d d |
 ƒ ‚ n  | j ƒ  } n  | S(   Nt   datat   sigmass/   Amplitudes will exceed floating point limit if s&   converted to intensities (max F = %e).(   R:   t   inputt   selectRN   t   KeyboardInterruptt	   Exceptiont   Falset   is_xray_intensity_arrayt	   f_sq_as_ft   TrueR   R   t   b_iso_as_u_start	   unit_cellR   t   ml_normalise_anisot   indicesRO   t   customized_copyt   set_observation_typeR   R   R+   RI   t   syst
   float_infot   OverflowErrort	   f_as_f_sq(   t   cache_0R0   t   u_start   b_addt   must_be_greater_thant
   work_arrayt   change_back_to_intensityt
   u_star_addt   corrected_amplitudest   corrected_sigmast   f_max(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   anisotropic_correction]  sP    %  								t   ml_iso_absolute_scalingc           B   sD   e  Z d  Z d d d d d e d „ Z d „  Z d „  Z d „  Z RS(   s  
  Maximum likelihood isotropic wilson scaling.

  :param miller_array: experimental data (will be converted to amplitudes
                       if necessary
  :param n_residues: number of protein residues in ASU
  :param n_bases: number of nucleic acid bases in ASU
  :param asu_contents: a dictionary specifying scattering types and numbers
                       ( i.e. {'Au':1, 'C':2.5, 'O':1', 'H':3 } )
  :param prot_frac: fraction of scattering from protein
  :param nuc_frac: fraction of scattering from nucleic acids
  g      ð?g        c      	   C   s‹  d \ |  _ |  _ | d  k r` | d  k r` | d  k	 s< t ‚ t | ƒ t i  ƒ k s] t ‚ q` n  | d  k r� | d  k	 s� | d  k	 s� t ‚ n  | | d k s£ t ‚ | j ƒ  r¾ | j ƒ  } n  | j ƒ  sÐ t ‚ d  |  _ | rñ | j ƒ  |  _ n  | j	 d d t
 j t j ƒ  ƒ d d t
 j t j ƒ  ƒ ƒ } | j ƒ  j ƒ  d k r‡| j | j ƒ  d k ƒ } | j ƒ  j ƒ  |  _ d  |  _ | d  k r³t d | d | d | d | ƒ |  _ n t d	 | d | d | ƒ |  _ | j ƒ  d k r‡|  j j |  j ƒ | j ƒ  |  _ | j ƒ  d  k	 r)| j ƒ  |  _ n t j |  j j ƒ  d
 ƒ |  _ t j |  j ƒ d k  rr|  j d
 |  _ n  | j ƒ  j ƒ  j ƒ  |  _ t j | j  ƒ  j ƒ  ƒ |  _! |  j j" |  _# |  j j$ |  _% t j d d
 ƒ |  _& d
 |  j& d <d |  j& d <d |  _' t( j) j* d d ƒ }	 t( j) j+ d |  d |	 ƒ }
 |  j& d |  _ |  j& d |  _ |  `& |  ` |  ` |  ` |  `# |  `% |  ` |  `! q‡n  d  S(   Ng      ð?t   d_maxt   d_mini    R?   R@   R<   R=   R>   g        i   g      I@i   t   max_iterationsg    €„.At   target_evaluatort   termination_params(   NN(,   R:   R0   R1   R   t   typeRU   RV   t   is_real_arrayt   infot   resolution_filterR+   RI   R   t   get_d_star_sq_low_limitt   get_d_star_sq_high_limitRN   RB   RQ   R   t	   scat_infoR4   R&   t   f_obsRO   t   sigma_f_obsR   R   R   t   epsilonst	   as_doublet   epsilont   boolt   centric_flagst   centricR)   RG   R'   t   sigma_prot_sqt   xR   t   scitbxt   lbfgst   termination_parameterst   run(   R   t   miller_arrayR?   R@   R>   t	   prot_fract   nuc_fract   include_array_infoRf   t   term_parameterst	   minimizer(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    ©  sx    $!					c         C   sæ   t  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j	 d	 d
 |  j	 d ƒ 	} t
 j t  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j d |  j	 d	 d
 |  j	 d ƒ 	ƒ } | |  _ | | f S(   NR   Rz   R{   R~   t   sigma_sqRG   R�   R0   i    t
   p_B_wilsoni   (   R   t   wilson_total_nllR   Rz   R{   R~   R‚   RG   R�   Rƒ   R   R   t   wilson_total_nll_gradientR   (   R   R   t   g(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt    compute_functional_and_gradientsù  s,    																c         C   sØ   d } |  j  d  k	 r+ d t |  j  ƒ } n  | j d ƒ | j d | ƒ | j d t d |  j ƒ ƒ | j d | ƒ | j d t d |  j ƒ ƒ | j d	 ƒ |  j d
 k  rÔ | j	 d d d d d d ƒ n  d  S(   Nt    s    of %ss+   Maximum likelihood isotropic Wilson scalings"    ML estimate of overall B value%s:s      %s A**(-2)s   %5.2fs"    Estimated -log of scale factor%s:s      %ss¶   The overall B value ("Wilson B-factor", derived from the Wilson plot) gives
 an isotropic approximation for the falloff of intensity as a function of
 resolution.  Note that this approximation may be misleading for anisotropic
 data (where the crystal is poorly ordered along an axis).  The Wilson B is
 strongly correlated with refined atomic B-factors but these may differ by
 a significant amount, especially if anisotropy is present.i    s6   The Wilson B-factor is negative!  This indicates that s9   the data are either unusually pathological, or have been s>   artifically manipulated (for instance by applying anisotropic s;   scaling directly).  Please inspect the raw data and/or use s@   unmodified reflections for phasing and refinement, as any model s7   generated from the current dataset will be unrealistic.(
   Ru   R:   t   strt   show_sub_headert   showt   show_preformatted_textR
   R1   R0   t   warn(   R   t   outt   label_suffix(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt
   _show_impl  s     		c         C   s0   |  j  d k  r d g S|  j  d k r, d	 g Sg  S(
   Ni    i   s/   The Wilson plot isotropic B-factor is negative.s+   Maximum likelihood isotropic Wilson scalingiÈ   i   s7   The Wilson plot isotropic B-factor is greater than 200.(   i   s/   The Wilson plot isotropic B-factor is negative.s+   Maximum likelihood isotropic Wilson scaling(   i   s7   The Wilson plot isotropic B-factor is greater than 200.s+   Maximum likelihood isotropic Wilson scaling(   R1   (   R   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   summarize_issues,  s      N(	   R!   R"   R3   R:   RW   R    R“   Rœ   R�   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyRm   œ  s   I		t   ml_aniso_absolute_scalingc           B   s}   e  Z d  Z d d d d d d „ Z d „  Z d „  Z d „  Z d d „ Z d	 „  Z	 d
 d d d d „ Z
 d „  Z d „  Z RS(   s  
  Maximum likelihood anisotropic wilson scaling.

  :param miller_array: experimental data (will be converted to amplitudes
                       if necessary
  :param n_residues: number of protein residues in ASU
  :param n_bases: number of nucleic acid bases in ASU
  :param asu_contents: a dictionary specifying scattering types and numbers
                       ( i.e. {'Au':1, 'C':2.5, 'O':1', 'H':3 } )
  :param prot_frac: fraction of scattering from protein
  :param nuc_frac: fraction of scattering from nucleic acids
  g      ð?g        c         C   sÛ  | d k rN | d k rN | d k	 s* t ‚ t | ƒ t i  ƒ k sK t ‚ qN n  | d k r{ | d k	 s{ | d k	 s{ t ‚ n  | | d k s‘ t ‚ | j ƒ  s£ t ‚ | j ƒ  |  _ | j ƒ  rÍ | j ƒ  } n  | j d d t j	 t
 j ƒ  ƒ d d t j	 t
 j ƒ  ƒ ƒ } | j | j ƒ  d k ƒ } | j ƒ  j ƒ  |  _ d |  _ | d k rkt d | d | ƒ |  _ n t d | d | d	 | ƒ |  _ |  j j |  j ƒ d |  _ | j ƒ  d k r×| j ƒ  |  _ | j ƒ  |  _ t j | j ƒ  j ƒ  ƒ |  _ | j ƒ  d k	 r| j ƒ  |  _ n t j |  j j ƒ  d
 ƒ |  _ t j |  j ƒ d k  r`|  j d
 |  _ n  | j  ƒ  j ƒ  j! ƒ  |  _" |  j j# |  _$ |  j j% |  _& t j' | j( ƒ  j ƒ  ƒ |  _) | j* ƒ  |  _+ |  j+ j, ƒ  |  _, |  j, j- ƒ  |  _. |  j. d k sÿt ‚ t j |  j. d d
 ƒ |  _/ t0 j1 j2 d t3 d t3 d t3 ƒ } t0 j1 j4 d d ƒ }	 t0 j1 j5 d |  d |	 d | ƒ }
 t j6 |  j j7 ƒ  d ƒ } |  j/ d |  _8 |  j9 ƒ  |  _: t; t j |  j: ƒ | ƒ |  _: t< j= t< j> |  j |  j: ƒ ƒ |  _ t< j? |  j |  j: ƒ |  _@ tA jB |  j ƒ } | jC ƒ  |  _D | jE ƒ  |  _F | |  _G |  jH ƒ  |  jD d d k r�tI |  jD d |  jD d ƒ |  jD d |  _J n	 d |  _J |  `/ |  ` |  ` |  `" |  `$ |  `& |  `) |  ` |  ` |  `, n  d S(   s.    Maximum likelihood anisotropic wilson scalingg      ð?Rn   Ro   i    R?   R@   R>   R<   R=   g        i   i   t-   ignore_line_search_failed_step_at_lower_boundt-   ignore_line_search_failed_step_at_upper_boundt    ignore_line_search_failed_maxfevRp   i2   Rq   Rr   t   exception_handling_paramsg       @g      @i   NgUUUUUUå?(K   R:   R   Rs   Rt   Ru   RU   RV   Rv   R+   RI   R   Rw   Rx   RQ   RN   R   Ry   R4   R&   t   b_cartRB   R[   t   hklRz   R   RY   t
   parametersRO   R{   R   R   R   R|   R}   R~   R)   RG   R'   R‚   R   R€   R�   t   space_groupt   sgt   adp_constraintst   n_independent_paramst   dim_uRƒ   R„   R…   t   exception_handling_parametersRT   R†   R‡   t   powt   volumeR0   t   unpackRc   t   listR   t   u_as_bt   u_star_as_u_cartt   u_star_as_u_cift   u_cifR   t   real_symmetrict   valuest   eigen_valuest   vectorst   eigen_vectorsRf   t   analyze_aniso_correctiont   abst   anirat(   R   Rˆ   R?   R@   R>   R‰   RŠ   Rf   R¢   RŒ   R�   t   Vrwgkt   eigen(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    B  s¤    	$!							
	c         C   s@   | d g } | t  |  j j t  | d ƒ ƒ ƒ } t j | ƒ S(   Ni    i   (   R¯   R¨   t   independent_gradientsR   R   (   R   R’   t   g_independent(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   pack²  s
     c         C   s#   |  j  j t |  j d ƒ ƒ } | S(   Ni   (   R¨   t
   all_paramsR¯   Rƒ   (   R   t   u_star_full(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR®   »  s    c         C   sÉ   |  j  ƒ  } t j |  j |  j |  j |  j |  j |  j |  j	 |  j
 d |  j | ƒ
 } | |  _ t j t j |  j |  j |  j |  j |  j |  j |  j	 |  j
 d |  j | ƒ
 ƒ } |  j | ƒ } | | f S(   Ni    (   R®   R   t   wilson_total_nll_anisoR¤   Rz   R{   R~   R‚   RG   R�   Rƒ   RY   R   R   R   t   wilson_total_nll_aniso_gradientRÀ   (   R   t   uR   t   g_full_exactR’   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR“   Á  s2    
		
s   %3.2fc         C   s'   | | } | d k r# d | } n  | S(   Ni    t    (    (   R   Rƒ   t   formatt   xx(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt	   format_itÝ  s    
c         C   sö   d St  j d d d d d d d d	 d
 d d g ƒ } t j j d d d | ƒ } t  j t d ƒ ƒ d } t | d ƒ } t  j t  j | j	 | ƒ ƒ ƒ | d } | d | | } t  j t  j | j	 | ƒ ƒ ƒ | d | d } t j
 | | ƒ S(   Niýÿÿÿg÷ŽâH<ü¿gsÂŸ·¦ŠÀgwškí�ñ¿gùSzó%Æ?gu`þ‡ó×?g¯À½q}÷°?gÔ�çˆ°¿g;¦:ê½Ðy¿gREà×2³?g‹ØÁšàòÂ?g·[ßë—¿Ã?i   i    g      ð?iè  g     8�@i   (   R   R   R„   R+   R   t   ranget   intt   sumR*   R   t   log(   R   t   ratt   coefst   fit_eRƒ   t   startt   normat   norma2(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   aniso_ratio_p_valueã  s    		)1iÐ  g      Ð?i   i	   c         C   sž  d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  |  _ d  |  _ |  j j ƒ  d  k rŽ d S|  j j d |  j j ƒ  d d d d  ƒ } t | d |  j ƒ j ƒ  } |  j j ƒ  |  _ |  j j ƒ  |  j j ƒ  t d t j |  j j ƒ  ƒ ƒ } |  j j ƒ  j ƒ  j ƒ  } | j ƒ  | k rb| j ƒ  d } n  t j | ƒ } | d | !}	 | |	 d }
 | |	 | d	 } | j |	 ƒ } t j | ƒ } t j | | k ƒ j ƒ  } t j | ƒ } | j |	 ƒ } t j | ƒ } t j | d
 t ƒ} t  | j ƒ  | ƒ } | d | !} | | } t j | j | ƒ ƒ } t j | j | ƒ ƒ } t j | j | ƒ ƒ } t j | j | ƒ ƒ } t j | ƒ } t! j" | d | | ƒ } t# | | ƒ t d | ƒ } t# | | ƒ t d | ƒ } t! j" | | | | ƒ } |
 |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ | |  _ | |  _ d  S(   NsJ   No further analysis of anisotropy carried out because of absence of sigmasRN   g        g      ð?RO   g:Œ0âŽyE>i   i    i   t   reverse($   R:   t   min_dt   max_dt   levelt   z_levelt   z_lowt   z_hight   z_tott
   mean_isigit
   mean_countt    mean_isigi_low_correction_factort   frac_below_low_correctiont!   mean_isigi_high_correction_factort   frac_below_high_correctionRf   RO   R\   RN   Rl   Rc   Ra   R   R   R   t
   d_spacingsR}   RB   t   sort_permutationRQ   t   meanR   RW   RÌ   R+   RI   Rº   (   R   t   n_checkt   p_checkRÙ   RÚ   t   correction_factorst   isigiRä   t   d_sortt   d_selectR×   RØ   RÞ   t   observed_countRß   t
   isigi_rankt   correction_rankt   n_againt   sel_hct   sel_lcRà   Râ   Rá   Rã   t   mut   varRÛ   RÜ   RÝ   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR¹   ñ  sz    													 /
												c      
   C   sj  |  j  d  k	 s t ‚ | j d ƒ | j d ƒ | j d |  j  d |  j  d |  j  d |  j  d |  j  d |  j  d	 f ƒ | j d
 ƒ | j d |  j d |  j d |  j d |  j d |  j d |  j d	 f ƒ | j d ƒ d d „ } d | |  j d d ƒ d | |  j d ƒ | |  j d ƒ | |  j d	 ƒ f g d | |  j d d ƒ d | |  j d ƒ | |  j d ƒ | |  j d ƒ f g d | |  j d	 d ƒ d | |  j d ƒ | |  j d ƒ | |  j d ƒ f g g } t	 j
 d d d d g d | ƒ } | j | ƒ | j d ƒ | j d |  j ƒ | j d ƒ |  j d d k rot d d d  d! ƒ ‚ n  d" } |  j d  k	 rº|  j |  j k rº|  j |  j k  r±d# } qºd$ } n  |  j d  k	 rf| j d% |  j |  j |  j d& |  j f ƒ | j d' ƒ | j d( |  j |  j |  j f ƒ | j d) ƒ | j d( |  j |  j |  j f ƒ | j d* ƒ n  d  S(+   Ns-   Maximum likelihood anisotropic Wilson scalings$   ML estimate of overall B_cart value:s/     %5.2f, %5.2f, %5.2f
  %12.2f, %5.2f
  %19.2f
i    i   i   i   i   i   s#   Equivalent representation as U_cif:s   Eigen analyses of B-cart:s   %3.2fc         S   s'   | |  } |  d k r# d | } n  | S(   Ni    RÇ   (    (   Rƒ   RÈ   RÉ   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyRÊ   G  s    
t   1s   %5.3fs   (%s, %s, %s)t   2t   3i   i   i   t   column_headerst   Eigenvectort   Valuet   Vectort
   table_rowss%   ML estimate of  -log of scale factor:s     %5.2fs   Anisotropy analysess6   Fatal error: eigenvector 1 of the overall anisotropic s   B-factor B_cart is zero.  This s?   may indicate severe problems with the input data, for instance s;   if only a single plane through reciprocal space is present.sP   indicates that there probably is no
 significant systematic noise amplification.s�   indicates that there probably is significant
 systematic noise amplification that could possibly lead to artefacts in the
 maps or difficulties in refinements  indicates that there probably is some
 systematic dependence between the anisotropy and not-so-well-defined
 intensities. Because the signal to noise for the most affected intensities
 is relatively good, the affect on maps or refinement behavior is most likely
 not very serious.sx  
 For the resolution shell spanning between %4.2f - %4.2f Angstrom,
 the mean I/sigI is equal to %5.2f. %4.1f %% of these intensities have
 an I/sigI > 3. When sorting these intensities by their anisotropic
 correction factor and analysing the I/sigI behavior for this ordered
 list, we can gauge the presence of 'anisotropy induced noise amplification'
 in reciprocal space.
g      Y@sO    The quarter of Intensities *least* affected by the anisotropy correction show
s_       <I/sigI>                 :   %5.2e
    Fraction of I/sigI > 3   :   %5.2e     ( Z = %8.2f )sO     The quarter of Intensities *most* affected by the anisotropy correction show
s    Z-scores are computed on the basis of a Bernoulli model assuming independence
 of weak reflections with respect to anisotropy.(   R£   R:   R   R–   R—   R˜   R³   R¶   R¸   R   t   simple_tablet
   show_tableR0   R	   RÝ   RÚ   Râ   RÙ   Rß   RØ   R×   RÞ   Rà   Rá   RÛ   Rã   RÜ   (   R   Rš   RÊ   t   rowst   tablet   message(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyRœ   3  sz    !!		!		'c         C   s—   t  |  j d d !ƒ d } t |  j d d !ƒ t |  j d d !ƒ } | | } | d k rm | d k rm d g S| d k rŒ | d k rŒ d g Sd g Sd  S(   Ni    i   i   i   i   s    The data show severe anisotropy.s-   Maximum likelihood anisotropic Wilson scalingg      à?s$   The data are moderately anisotropic.s+   The data are not significantly anisotropic.(   i   s    The data show severe anisotropy.s-   Maximum likelihood anisotropic Wilson scaling(   i   s$   The data are moderately anisotropic.s-   Maximum likelihood anisotropic Wilson scaling(   i    s+   The data are not significantly anisotropic.s-   Maximum likelihood anisotropic Wilson scaling(   RÍ   R£   R   R   (   R   t   b_cart_meant   b_cart_ranget   aniso_ratio(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR�      s    *
   N(   R!   R"   R3   R:   R    RÀ   R®   R“   RÊ   RÕ   R¹   Rœ   R�   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyRž   5  s   j						A	mt   kernel_normalisationc           B   s&   e  Z d d  d d d e d d „ Z RS(   i   i   i2   c	         C   s   | d  k r! | t k	 s! t ‚ n  | t k	 rB | d  k sB t ‚ n  | j ƒ  d k sZ t ‚ d  }	 | j ƒ  s{ t d ƒ ‚ n  | j ƒ  r– | j ƒ  }	 n  | j ƒ  rÀ | j	 ƒ  }	 |	 j
 | ƒ }	 n  | j ƒ  sê | j ƒ  sê t d ƒ ‚ qê n  |	 j ƒ  }
 |	 j ƒ  j ƒ  j ƒ  } |	 j ƒ  j ƒ  } d | | } | d  k rLt j | ƒ } n  | d  k rjt j | ƒ } n  | |  _ | t k	 rt j | ƒ } | t k r£| } n t | ƒ } | | j ƒ  k rÔ| j ƒ  d } n  | | | | |  _ |  j d k st ‚ n  | d k st ‚ t j d | d | d | d	 t ƒ |  _ t j d
 | d |
 d | d |  j d |  j ƒ |  _ t j d
 | d |
 |
 d | | d |  j d |  j ƒ |  _ |  j |  j |  j |  _ t j d
 | d |
 d d d | d d d |  j d |  j ƒ |  _ |  _ d } |  j | k |  _ |  j j ƒ  } | j ƒ  d k sKt ‚ | j ƒ  |  j j ƒ  d k  r…t  d d d d ƒ ‚ n  |  j j! | ƒ |  _ |  j j! | ƒ |  _ t j" |  j j! | ƒ ƒ |  _ |  j j! | ƒ |  _ t j" |  j ƒ |  _ t j | |  j |  j ƒ } t# | | | | j$ ƒ |  _% t j | |  j |  j ƒ } t# | | | | j$ ƒ |  _& t j | |  j |  j ƒ } t# | | | | j$ ƒ |  _ t j' |  j ƒ |  _ t j' |  j% j( | ƒ ƒ |  _) t j' |  j ƒ |  _ t j' |  j& j( | ƒ ƒ |  _& t j' |  j j( | ƒ ƒ |  _ d  |  _* d  |  _+ |	 j, ƒ  d  k	 rÃ|	 j- d |	 j ƒ  |  j) d |	 j, ƒ  |  j) ƒ j
 |	 ƒ |  _* |  j* j- d |  j* j ƒ  | d |  j* j, ƒ  | ƒ j
 |	 ƒ |  _+ nY |	 j- d |	 j ƒ  |  j) ƒ j
 |	 ƒ |  _* |  j* j- d |  j* j ƒ  | ƒ j
 |	 ƒ |  _+ d  S(   Ni    s   Please provide real arrays onlys   Observation type unknowng      ð?i   t   nR   R   t   include_limitst   d_star_sq_hklt   I_hklR~   R/   t   kernel_widthg        g¼‰Ø—²Òœ<i   s7   Analysis could not be continued because more than half sD   of the data have values below 1e-16.  This usually indicates either sC   an inappropriately high resolution cutoff, or an error in the data s:   file which artificially creates a higher resolution limit.RN   RO   (.   R:   RT   R   RB   Rt   t   RuntimeErrort   is_xray_amplitude_arrayRa   RU   R   R]   RN   R|   R}   Rä   R   R   R   R
  Rå   RW   RÌ   R   t   chebyshev_nodesR/   R   R  t   mean_I_arrayt   var_I_arrayt
   weight_sumt   bin_selectiont
   iselectionR	   RQ   RÎ   R   RÐ   t
   normalizert   var_normR*   R   t   normalizer_for_miller_arrayt   normalised_millert   normalised_miller_dev_epsRO   R\   (   R   Rˆ   R
  t   n_binst   n_termt   d_star_sq_lowt   d_star_sq_hight   auto_kernelt,   number_of_sorted_reflections_for_auto_kernelRf   t   I_obsR|   R  t   sort_permutt   numbert   epst   sel_post   normalizer_fit_lsqt   var_lsq_fitt   ws_fit(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR    °  sò    
						

							N(   R!   R"   R:   RT   R    (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyR  ¯  s   (%   t
   __future__R    t   mmtbxR   t   cctbx.eltbxR   t   cctbx.array_familyR   t   cctbxR   R   t   scitbx.mathR   R   t   scitbx.linalgR   t   scitbx.lbfgsR„   t   libtbx.utilsR	   t   libtbx.str_utilsR
   t   libtbxR   R+   R^   t   objectR   R$   R%   R4   R:   Rl   t   xtriage_analysisRm   Rž   R  (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/absolute_scaling.pyt   <module>   s0   [V};™ÿ {