ó
§™xTc           @   sk  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 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  d l m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l  Z  d  d l! Z! d Z" d Z# d Z$ d e f d „  ƒ  YZ% d e& f d „  ƒ  YZ' d e f d „  ƒ  YZ( d e j) f d „  ƒ  YZ* d „  Z+ d e j) f d „  ƒ  YZ, d e j) f d  „  ƒ  YZ- d! e j) f d" „  ƒ  YZ. d# e j) f d$ „  ƒ  YZ/ d% e j) f d& „  ƒ  YZ0 d' e j) f d( „  ƒ  YZ1 d) e j) f d* „  ƒ  YZ2 d+ e& f d, „  ƒ  YZ3 d- e j) f d. „  ƒ  YZ4 d/ „  Z5 d0 e j) f d1 „  ƒ  YZ6 d2 e j) f d3 „  ƒ  YZ7 d4 e j) f d5 „  ƒ  YZ8 d6 e j) f d7 „  ƒ  YZ9 d8 e j) f d9 „  ƒ  YZ: d: e j) f d; „  ƒ  YZ; e< e< d< e= d= „ Z> d> e< d? „ Z? d> e< d@ dA „ Z@ d S(B   iÿÿÿÿ(   t   division(   t   absolute_scaling(   t   scaling(   t
   data_plots(   t   pointgroup_tools(   t   flexN(   t   crystal(   t   maptbx(   t   sgtbx(   t   matrix(   t   slots_getstate_setstate(   t   format_value(   t   Sorryt   null_out(   t   table_utils(   t   StringIOgš™™™™™©?iK   gš™™™™™É?t	   obliquityc           B   s,   e  Z d  d d d d d g Z e d „ Z RS(   t   typet   ut   ht   tt   taut   deltac         C   sÏ   t  j | j ƒ  ƒ } t  j | j ƒ  ƒ } | j ƒ  } | j ƒ  |  _ | j ƒ  j ƒ  |  _ | j ƒ  j	 ƒ  j ƒ  j ƒ  |  _
 | t  j |  j ƒ |  _ t  j |  j
 ƒ | |  _ |  j j |  j d | ƒ|  _ d  S(   Nt   deg(   R	   t   sqrt   orthogonalization_matrixt   fractionalization_matrixt   infoR   t   evR   t	   transposet   inverseR   t   colR   t   rowR   t   accute_angleR   (   t   selft   reduced_cellt   rot_mxR   t   ortht   fract   r_info(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   __init__   s    !(   t   __name__t
   __module__t	   __slots__t   TrueR(   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR      s   t   twin_law_qualityc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sH   
  Various scores for a potential twin law given the crystal lattice.
  c         C   s‘   | |  _  | j | j ƒ  ƒ |  _ | j ƒ  j | ƒ |  _ t j d |  j j ƒ  j	 ƒ  ƒ |  _
 |  j j ƒ  |  _ |  j j |  j
 ƒ j ƒ  |  _ d  S(   Nt   symbol(   t   xst   change_basist!   change_of_basis_op_to_niggli_cellt	   xs_nigglit   applyt   twin_in_nigR   t   change_of_basis_opt   rt   as_xyzt   cb_opt	   unit_cellt   niggli_cellt   new_niggli_cell(   R"   R/   t   twin_law(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   .   s    		c   
      C   sÕ   |  j  j ƒ  } |  j j ƒ  } t j | ƒ } t j | ƒ } t j | | ƒ } d } d } d } xM t | | ƒ D]< \ } } t j | | ƒ }	 | |	 7} | t j | ƒ 7} qq W| d k sÃ t	 ‚ d | | } | S(   Ni    g      Y@(
   R:   t   metrical_matrixR;   R   t   doublet   abst   zipt   matht   fabst   AssertionError(
   R"   t   oldt   newR   t   resultt   topt   bottomt   oit   nit   tmp(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   delta_santoro;   s    
c         C   sP   |  j  j ƒ  j ƒ  j d ƒ } t |  j | ƒ } t | j ƒ d } | j | f S(   Ni   s   -fold(	   R8   t   cR6   t   new_denominatorR   R:   t   strR   R   (   R"   t   rot_mx_currentt   oblt   type_string(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   delta_le_pageN   s
    		c         C   s   |  j  j ƒ  j d |  j ƒ S(   NR#   (   R4   R6   t   lebedev_2005_perturbationR:   (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   delta_lebedevU   s    c      
   C   sŸ  t  j d ƒ } t  j d ƒ } t  j d ƒ } t  j |  j j ƒ  ƒ | } t  j |  j j ƒ  ƒ | } t  j |  j j ƒ  ƒ | } t  j |  j j ƒ  ƒ | } t  j |  j j ƒ  ƒ | } t  j |  j j ƒ  ƒ | }	 t j | j	 | ƒ ƒ d }
 t j | j	 | ƒ ƒ d } t j |	 j	 |	 ƒ ƒ d } |
 d } | d } | d } |
 | d } |
 | d } | | d } t  j | | | | | | | | | g	 ƒ } | S(   sâ   
    this gives a tensor describing the deformation of the unit cell needed
    to obtain a perfect match. the sum of diagonal elements describes
    the change in volume, off diagonal components measure associated shear.
    i   i    g      ð?g       @(   i   i    i    (   i    i   i    (   i    i    i   (
   R	   R   R   R:   R   R;   R   RA   t   sqrtt   dot(   R"   t   x1t   x2t   x3t   f1t   f2t   f3t   xn1t   xn2t   xn3t   u1t   u2t   u3t   e11t   e22t   e33t   e12t   e13t   e23RF   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   strain_tensorY   s,    


	(   R)   R*   t   __doc__R(   RL   RS   RU   Rj   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR-   *   s   				R<   c           B   s8   e  Z d  Z d d d d d d g Z d „  Z d „  Z RS(	   sj   
  Basic container for information about a possible twin law, with scores for
  fit to crystal lattice.
  t   operatort	   twin_typeRL   RS   RU   t	   axis_typec         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   Rl   Rm   RL   RS   RU   Rn   (   R"   t   opt   pseudo_merohedral_flagRn   RL   RS   RU   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(      s    					c         C   s   t  |  j j ƒ  j ƒ  ƒ S(   N(   RO   Rl   R6   t   as_hkl(   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   __str__�   s    (   R)   R*   Rk   R+   R(   Rr   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR<   x   s
   	t	   twin_lawsc           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   sU   
  Container for all possible twin laws given a crystal lattice and space
  group.
  g      @c         C   sÊ  | j  d t d | ƒ } | j ƒ  |  _ | j ƒ  j d |  j ƒ } t j j d | j ƒ  d | ƒ |  _	 | j
 d | j ƒ  ƒ } | j ƒ  j ƒ  j d t d t ƒ |  _ g  |  _ d	 |  _ d	 |  _ |  j j ƒ  } xæt j j d
 |  j	 d | j ƒ  j ƒ  j ƒ  ƒ j d D]­} | d	 j ƒ  j ƒ  d	 k rt }	 t d ƒ }
 |  j d 7_ t j |  j ƒ } y | j | d	 ƒ WnT t k
 r‡‚  nA t  k
 rÇt }	 t d ƒ }
 |  j d 7_ |  j d 8_ n X| j! ƒ  |  j j! ƒ  k rt }	 |	 rt d ƒ }
 |  j d 7_ |  j d 8_ qn  t" d | d | j# | d	 ƒ ƒ } t$ d | j# | d	 ƒ d t |
 ƒ d | j% ƒ  d d | j& ƒ  d | j% ƒ  d	 d | j' ƒ  ƒ } |  j j( | ƒ qqWg  } x` |  j D]U } | j( | j) | j* d | j& d | j% d | j' t | j+ j ƒ  j, ƒ  ƒ g ƒ qÈWt- j. d d d d d d d g d | d  d! ƒ |  _/ t j j |  j	 | j ƒ  j ƒ  j ƒ  ƒ |  _0 t |  _1 y( |  j	 j |  j j ƒ  ƒ } t |  _1 Wn t  k
 rÅn Xd  S("   Nt   integral_onlyt   logR8   R#   t	   max_deltat   anomalous_flagt   flag_k2lt   flag_l2ni    t   gR   i   s     Ms    PMR/   R<   Ro   Rp   Rn   RL   RS   RU   s   %5.3ft   column_headerst   Typet   Axiss   R metric (%)s   delta (le Page)s   delta (Lebedev)s   Twin lawt
   table_rowst   commentss5   M:  Merohedral twin law
PM: Pseudomerohedral twin law(2   t   eliminate_sys_absentR,   R1   t   crystal_symmetryR0   R   t   lattice_symmetryt   groupR9   t   lattice_groupt   reflection_intensity_symmetryRw   t   space_group_infoR   t.   expand_addl_generators_of_euclidean_normalizert   euclidt	   operatorst   mt   pmR   t   cosetst   left_decompositiont   space_groupt   build_derived_acentric_groupt	   make_tidyt
   partitionsR6   t   determinantt   FalseRO   t
   expand_smxt   KeyboardInterruptt	   Exceptiont   order_zR-   R3   R<   RS   RL   RU   t   appendRm   Rn   Rl   Rq   R   t   simple_tablet   tablet   coset_tablet   coset_decomposition(   R"   t   miller_arrayt   lattice_symmetry_max_deltat   outt   inputt   minimum_cell_symmetryt   intensity_symmetryR8   t	   partitiont   is_pseudo_merohedralRm   t   euclid_checkt   tlqt   tlR~   RK   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   •   s–    						 


#			c         C   s{  |  j  |  j t |  j ƒ k s% t ‚ | j d ƒ t |  j ƒ d k rW | j d ƒ ne | j d ƒ | j |  j	 d d ƒ| j
 d i |  j  d 6|  j d	 6t |  j ƒ d
 6ƒ | j d ƒ t rw| j d ƒ | j d ƒ | j
 d ƒ | j d ƒ |  j rg| j d ƒ | j d ƒ t ƒ  } |  j j d | d |  j j ƒ  ƒ | j | j ƒ  ƒ | j d ƒ qw| j d ƒ n  d  S(   Ns   Twin law identificationi    s5   
No twin laws are possible for this crystal lattice.
s   Possible twin laws:t   indenti   sŒ   
%(m)-3.0f merohedral twin operators found
%(pm)-3.0f pseudo-merohedral twin operators found
In total, %(n_op)3.0f twin operators were foundRŠ   R‹   t   n_opsó   
 Please note that the possibility of twin laws only means that the lattice
 symmetry permits twinning; it does not mean that the data are actually
 twinned.  You should only treat the data as twinned if the intensity
 statistics are abnormal.s¤   
 The presence of twin laws indicates that the symmetry of the lattice (unit
 cell) is higher (has more elements) than the point group of the assigned
 space group.sL   
 There are four likely scenarios associated with the presence of twin laws:s=  
    i.  The assigned space group is incorrect (too low).
   ii.  The assigned space group is correct and the data *are not* twinned.
  iii.  The assigned space group is correct and the data *are* twinned.
   iv.  The assigned space group is not correct (too low) and at the same time,
        the data *are* twinned.sÑ   
 Xtriage tries to distinguish between these cases by inspecting the intensity
 statistics.  It never hurts to carefully inspect statistics yourself and make
 sure that the automated interpretation is correct.s(   Details of automated twin law derivations}  
Below, the results of the coset decomposition are given.  Each coset represents
a single twin law, and all symmetry equivalent twin laws are given.  For each
coset, the operator in (x,y,z) and (h,k,l) notation are given.   The direction
of the axis (in fractional coordinates), the type and possible offsets are
given as well.  Furthermore, the result of combining a certain coset with the
input space group is listed.

This table can be usefull when comparing twin laws generated by xtriage with
those listed in lookup tables.  In the table subgroup H denotes the *presumed
intensity symmetry*. Group G is the symmetry of the lattice.
RŸ   R8   sÆ   Note that if group H is centered (C,P,I,F), elements corresponding to centering
operators are omitted.  (This is because internally the calculations are done
with the symmetry of the reduced cell.)
sä   
The present crystal symmetry does not allow to have the its lattice symmetry
expressed in the setting desired.  Becasue of this, a full coset table cannot
be produced. Working with the data in the reduced cell will solve this.
(   RŠ   R‹   t   lenR‰   RC   t   show_sub_headert   showt   show_paragraph_headert
   show_tableRš   t
   show_linesR“   Rœ   R   R›   R1   R   t   show_preformatted_textt   getvalue(   R"   RŸ   t	   coset_out(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt
   _show_implê   s@    %,		
N(   R)   R*   Rk   t   NoneR(   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRs   �   s   Rc         C   s&   g  t  |  ƒ j D] } t | ƒ ^ q S(   sN   
  Convenience method for getting a list of twin law operators (as strings)
  (   Rs   R‰   RO   (   R�   R§   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   get_twin_laws-  s    t   wilson_normalised_intensitiesc           B   s)   e  Z d  Z e d d d „ Z d „  Z RS(   s!    making centric and acentric cut i    c         C   s1  | d  k r t j } n  | j ƒ  j ƒ  s1 t ‚ | j ƒ  sL | j ƒ  } n  | j ƒ  } | r  t	 j
 | d t ƒ} | j j ƒ  } | j | j ƒ  d k ƒ } n0 | j ƒ  j | ƒ } | j | j ƒ  d k ƒ } | |  _ | j ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ |  j j ƒ  j ƒ  d k r-t d ƒ ‚ n  d  S(   Nt   auto_kerneli    s3   No acentric reflections available. Check your input(   R´   t   syst   stdoutRŽ   t
   is_centricRC   t   is_xray_intensity_arrayt	   f_as_f_sqt	   deep_copyR   t   kernel_normalisationR,   t   normalised_millert   selectt   datat   set_observation_typet   allt   select_acentrict   as_intensity_arrayt   acentrict   select_centrict   centrict   indicest   sizeR   (   R"   R�   t	   normaliseRŸ   t   verboset
   work_arrayt
   normalizer(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   5  s$    	c         C   sP   | j  d ƒ | j  d |  j j ƒ  j ƒ  ƒ | j  d |  j j ƒ  j ƒ  ƒ d  S(   Ns(   Splitting data in centrics and acentricss     Number of centrics  : %ds     Number of acentrics : %d(   R¬   RÈ   RÁ   RÊ   RÆ   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   N  s     N(   R)   R*   Rk   R,   R´   R(   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR¶   3  s
   t   detect_pseudo_translationsc           B   sq   e  Z d  Z d d d d d d d d d d d	 d
 „ Z d d d „ Z d „  Z d „  Z d e d „ Z	 d „  Z
 RS(   sy   
  Analyze the Patterson map to identify off-origin peaks that are a significant
  fraction of the origin peak height.
  g      $@g      @id   g        g      .@gš™™™™™©?g      è?g      @i    c         C   s  | d  k r t j } n  | j ƒ  r3 | j ƒ  } n  | j | | ƒ } | j | j ƒ  d k ƒ j | ƒ } | j	 ƒ  |  _	 | j
 ƒ  |  _
 | |  _ | |  _ | j ƒ  |  _ t j d | j
 ƒ  d | j	 ƒ  ƒ |  _ | j ƒ  | k  r-| J| d IJ| d | j ƒ  | | f IJ| d IJ| d IJ| d IJn  | j ƒ  j ƒ  d k rTt d	 ƒ ‚ n  | j ƒ  rx| j ƒ  j | ƒ } n  t } | j ƒ  j ƒ  d k  rá| d
 | | f IJ| d | j ƒ  j ƒ  IJ| d IJ| d IJt } n  | r| j d t j ƒ j ƒ  } t j d d d t d d d t d d  d |	 d |
 ƒ } | j d | ƒ } | j d d ƒ } | j  ƒ  d } t! j" d | j
 ƒ  d | j	 ƒ  d d" ƒ } g  |  _# | | j$ ƒ  j ƒ  k rÌ| j$ ƒ  j ƒ  } n  x­ t% | ƒ D]Ÿ } | j  ƒ  | | d } | j$ ƒ  | } t! j& | | ƒ } | j' ƒ  | k rÙ| | k rx|  j( | ƒ } |  j# j) | j* ƒ  | | | | j' ƒ  g ƒ qxqÙqÙWt+ |  j# ƒ d k rj| J| d IJ| d | IJ| Jd } x° t% | ƒ D]Ÿ } | j  ƒ  | | d } | j$ ƒ  | } t! j& | | ƒ } | j' ƒ  | k rÄ| | k rc|  j( | ƒ } |  j# j) | j* ƒ  | | | | j' ƒ  g ƒ qcqÄqÄWn  | |  _, d  |  _- d  |  _. d  |  _/ d  |  _0 | r|  j# d d |  _1 |  j# d d! |  _2 |  j# d d |  _3 |  j# d d  |  _4 |  j4 |  j, k r|  j5 ƒ  qqn  d  S(#   Ni    R9   RŽ   s    WARNING (twin_analysis):s;     The completeness is only %3.2f between %3.1f and %3.1f A.s4     This might not be enough to obtain a good estimates4     of the presence or absence of pseudo translationals     symmetry.s   No low resolution reflectionss9   The number of reflection between %3.1f and %3.1f Angstroms   is equal to %is:    ##  This is not enough to obtain a reasonable estimate ofs&    ##  the presence of translational NCSt   symmetry_flagst   peak_search_leveli   t   interpolatet   min_distance_sym_equivg-Cëâ6?t   general_positions_onlyt   effective_resolutiont   min_cross_distancet   min_cubicle_edget
   parameterst   max_clustersiè  t   original_siteg      Y@s.   No Patterson vectors with a length larger thans)   %5.2f found. removing distance constraintgü©ñÒMbP?i   i   (   i    i    i    (6   R´   R¸   R¹   R»   t	   f_sq_as_ft   resolution_filterRÀ   RÁ   RÂ   RŽ   R9   t   d_maxt   d_mint   completenesst   completeness_in_rangeR   t   symmetryR/   RÉ   RÊ   R   Rw   t   average_bijvoet_matesR,   R“   t   patterson_mapR   t   use_space_group_symmetryt   apply_sigma_scalingt   peak_search_parameterst   peak_searchRÃ   t   heightsR   t   sym_equiv_sitest   suspected_peakst   sitest   ranget   min_sym_equiv_distance_infot   distt   p_valueR˜   t   sym_opRª   t   p_value_cutt   mod_ht   mod_kt   mod_lt   suggested_space_groups_tablet	   high_peakt   high_peak_distancet   high_peak_xyzt   high_p_valuet   guesstimate_mod_hkl(   R"   R�   t	   low_limitt
   high_limitt	   max_sitest
   height_cutt   distance_cutRñ   t   completeness_cutt
   cut_radiusR×   RŸ   RÌ   RÍ   t   everything_okayRã   Ræ   t   cluster_analysist	   peak_listt
   max_heightt   sym_equiv_origint   i_peakt   heightt   sitet	   dist_infoRï   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   \  sÄ    			





									
					i�   c         C   s  g  } g  } g  } g  } xÀ |  j  D]µ } | d |  j k  r" | d } |  j | d ƒ }	 |  j | d ƒ }
 |  j | d ƒ } d  } |	 |
 | g j d  ƒ d k r× d |	 |
 | f } | d  k	 rÔ | j | ƒ qÔ q× q" q" Wt j d |  j ƒ } y t j t	 | ƒ d | ƒ} Wn$ t
 k
 r%‚  n t k
 r5n Xx4| D],} d  } yŒ | j ƒ  j ƒ  } t j | ƒ } | j | j ƒ  | j ƒ  ƒ } | j | ƒ t	 t j d | d | ƒ ƒ } t j d | d | ƒ j ƒ  } Wn t k
 rèn X| | k r=| d  k	 rI| j | ƒ | j | ƒ | j |  j j | j ƒ  j ƒ  j ƒ  ƒ ƒ qi| j d  ƒ | j |  j ƒ q=q=Wt | ƒ | j d  ƒ } | d k rg  } x^ t | | | ƒ D]J \ } } } | d  k	 r«| j t	 | ƒ t	 | ƒ d | j ƒ  g ƒ q«q«Wt j d | d	 d ƒ |  _ n  d  S(   Ni   i    i   s   x%s, y%s, z%sRƒ   t   space_group_t_dens*   %5.2f, %5.2f, %5.2f,  %5.2f, %5.2f, %5.2f)R~   R{   s   Space groupt   Operators   Unit cell of reference setting(   s   Space groupR  s   Unit cell of reference setting(   Rê   Rñ   t   closest_rationalR´   t   countR˜   R   R†   RŽ   RO   R•   R–   Rƒ   R�   t   rt_mxt   new_denominatorst   r_dent   t_denR”   t'   change_of_basis_op_to_reference_settingR9   R0   t   c_invR6   t	   as_doubleRª   R@   RØ   R   R™   Rõ   (   R"   R  RŸ   t   symopst   sgst   with_operatort   new_cellt   peakt   xyzt   dxt   dyt   dzt   additional_symopt   tmp_space_groupt   sot   sg_strt
   new_tmp_sgt   smxt
   to_ref_sett   number_of_new_sgsR~   t   sgRo   t   uc(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   suggest_new_space_groupsÏ  sr    
  	 "	 c         C   sè   d |  j  d d } d |  j  d d } d |  j  d d } t t j | ƒ d ƒ } | d k rm d } n  t t j | ƒ d ƒ } | d k r› d } n  t t j | ƒ d ƒ } | d k rÉ d } n  | |  _ | |  _ | |  _ d  S(   Ng      ð?i    g�íµ ÷Æ°>i   i   g      à?i   (   Rø   t   intRA   RB   Rò   Ró   Rô   (   R"   t	   tmp_mod_ht	   tmp_mod_kt	   tmp_mod_l(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRú   	  s    					c         C   s`   | d } d  } | d k  rV | d | } d } d } d t j | | | ƒ } n d } | S(   Ng      Y@g      ð?gp¶¹1=a±?gª‚QI�€@g        (   R´   RA   t   exp(   R"   t   peak_heightt   xRF   t   at   b(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRï     s    
 g{®Gáz”?c         C   sJ  t  | ƒ } d } | d k  r' d } n  d  } d  } d d d d d g } g  }	 d	 }
 d  } x� | D]… } d } | d k r‚ d
 } n  xa t | | ƒ D]P } | | | f } | | } t  | | ƒ } | |
 k  r’ | } t | ƒ }
 q’ q’ Wqa Wd  } |
 | k  rF| r=| d
 d k rd } qCd | d | d
 | d f } qF| } n  | S(   Nt   +i    t   -i   i   i   i   i   g      $@i   t    s   %s%s/%s(   R?   R´   t   xranget   float(   R"   t   fractiont   epst   return_textt   tmp_fractiont   signt   numt   dent   den_listt   fraction_trialst   min_delt	   best_fract	   trial_dent	   start_numt	   trial_numRK   t   tmp_fracR   RF   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR  &  s:    		
	"	c         C   s¼  | j  d ƒ | j d ƒ | j d |  j ƒ | j d |  j |  j |  j f ƒ | j d ƒ | j d |  j ƒ |  j d k r‘ | j d ƒ n  |  j |  j	 k r¸g  } x~ t
 t |  j ƒ ƒ D]g } |  j | d	 |  j	 k rã Pn  | j d
 |  j | d d |  j | d d |  j | d	 g ƒ q¿ Wt | ƒ d k r€| j d ƒ t j d | d d d d g ƒ } | j | d d	 ƒn  |  j d  k	 r¸| j d ƒ | j |  j d d	 ƒq¸n  d  S(   Ns   Patterson analysess<    Largest Patterson peak with length larger than 15 Angstrom:s.    Frac. coord.              : %8.3f %8.3f %8.3fsm    Distance to origin        : %8.3f
 Height relative to origin : %8.3f %%
 p_value(height)           : %12.3e
t   ExplanationsË   The p-value, the probability that a peak of the specified height or larger
 is found in a Patterson function of a macromolecule that does not have any
 translational pseudo-symmetry, is equal to %.3e.  p_values smaller than
 0.05 might indicate weak translational pseudo symmetry, or the self vector of
 a large anomalous scatterer such as Hg, whereas values smaller than 1e-3 are
 a very strong indication for the presence of translational pseudo symmetry.
i   sà    Translational pseudo-symmetry is very likely present in these data.  Be
 aware that this will change the intensity statistics and may impact subsequent
 analyses, and in practice may lead to higher R-factors in refinement.
i   s   %6.3f,%6.3f,%6.3fi    s   %8.3fi   s   %9.3es%    The full list of Patterson peaks is:R~   R{   t   XYZR  s   p-value(height)R¨   su    If the observed pseudo translationals are crystallographic, the following
 spacegroups and unit cells are possible:
(   R«   R¬   R°   Rø   R÷   Rö   Rù   R­   t	   show_textRñ   Rì   Rª   Rê   R˜   R   R™   R®   Rõ   R´   (   R"   RŸ   R~   t   iiRš   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   H  s>    	
	N(   R)   R*   Rk   R´   R(   R)  Rú   Rï   R,   R  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRÏ   W  s"   g:		"t   wilson_momentsc           B   sk   e  Z d  d g Z d d g Z d d g Z d d g Z d d g Z d d g Z d	 „  Z d
 „  Z	 d „  Z
 RS(   g      @g       @gü©ñÒMbä?g…ëQ¸é?g`åÐ"Ûùî?gZd;ßO�ç?g      ø?gR¸…ëQì?gZd;ßOá?c         C   s¯   d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ |  j | j ƒ  | j	 ƒ  j ƒ  j
 ƒ  | j ƒ  | j	 ƒ  j ƒ  j
 ƒ  ƒ t |  _ | j ƒ  j ƒ  d k r« t |  _ n  d  S(   Ni    (   R´   t   centric_i_ratiot   centric_f_ratiot   centric_e_sq_minus_onet   acentric_i_ratiot   acentric_f_ratiot   acentric_abs_e_sq_minus_onet   compute_ratiosRÁ   t   epsilonsR  R,   t   centric_presentRÊ   R“   (   R"   t
   acentric_zt	   centric_z(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   ~  s    						#	c         C   s$  | j  ƒ  d k r� t j | ƒ } t j | | ƒ } t j t j | ƒ ƒ } | | | |  _ | | | |  _ t j t j | d ƒ ƒ |  _ n  | j  ƒ  d k r t j | ƒ } t j | | ƒ } t j t j | ƒ ƒ } | | | |  _ | | | |  _	 t j t j | d ƒ ƒ |  _
 n  d  S(   Ni    g      ð?(   RÊ   R   t   meanRV   RO  RP  R?   RQ  RL  RM  t   centric_abs_e_sq_minus_one(   R"   t   acRM   t   mean_zt	   mean_z_sqt   mean_e(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRR  Ž  s    "c         C   sõ   | j  d ƒ | j d ƒ | j d |  j |  j d |  j d |  j |  j d |  j d |  j |  j d |  j d f	 ƒ |  j	 rñ | j d ƒ | j d |  j
 |  j d |  j d |  j |  j d |  j d |  j |  j d |  j d f	 ƒ n  d  S(   Ns   Wilson ratio and momentss   Acentric reflections:
sÇ   
   <I^2>/<I>^2    :%4.3f   (untwinned: %4.3f; perfect twin %4.3f)
   <F>^2/<F^2>    :%4.3f   (untwinned: %4.3f; perfect twin %4.3f)
   <|E^2 - 1|>    :%4.3f   (untwinned: %4.3f; perfect twin %4.3f)
i    i   s   Centric reflections:
(   R«   R¬   R°   RO  t   acentric_i_ratio_libraryRP  t   acentric_f_ratio_libraryRQ  t   acentric_e_sq_minus_one_libraryRT  RL  t   centric_i_ratio_libraryRM  t   centric_f_ratio_libraryRX  t   centric_e_sq_minus_one_library(   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   ž  s0    	




		




(   R)   R*   R`  Ra  Rb  R]  R^  R_  R(   RR  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRK  w  s   		t   n_z_testc           B   s)   e  Z d  „  Z e d „  ƒ Z d „  Z RS(   c         C   s¯  t  } t  } | j ƒ  j ƒ  d k r- t } n  | j ƒ  j ƒ  d k rN t } n  d d d d d d d d d	 d
 d g } t j d d d d d d d d d d d g ƒ } t j d d d d d d d d d d d  g ƒ } t j d! d ƒ } t j d! d ƒ }	 xs t d" ƒ D]e }
 | j ƒ  |
 d d# k  j t  ƒ | |
 d$ <| r| j ƒ  |
 d d# k  j t  ƒ |	 |
 d$ <qqW| r”| t | j ƒ  j ƒ  ƒ } n  | r¹|	 t | j ƒ  j ƒ  ƒ }	 n  t j	 t j
 | | ƒ ƒ } t j	 t j
 |	 | ƒ ƒ } | j ƒ  d k  j t  ƒ } | j ƒ  d k  j t  ƒ } | | d" t j | ƒ } | | d" t j | ƒ } t j t d! ƒ ƒ d# } | |  _ | |  _ | |  _ d |  _ | j ƒ  j ƒ  d k rÎ| | j ƒ  j ƒ  |  _ n  | |  _ | | d" t j | ƒ |  _ |	 |  _ | |  _ d |  _ | j ƒ  j ƒ  d k rD| | j ƒ  j ƒ  |  _ n  | |  _ | | d" t j | ƒ |  _ t j |  j |  j ƒ d% |  _ t j |  j |  j ƒ d% |  _ d  S(&   Ni    gš™™™™™¹?gš™™™™™É?g333333Ó?gš™™™™™Ù?g      à?g333333ã?gffffffæ?gš™™™™™é?gÍÌÌÌÌÌì?g      ð?g        gtF”ö_¸?g÷äa¡Ö4Ç?g‚âÇ˜»–Ð?gÒo_ÎÕ?gÉv¾Ÿ/Ù?gÿ!ýöuàÜ?gÎQÚà?gÈ=›UŸá?géH.ÿ!ýâ?gµ¦yÇ):ä?gàœ¥½ÁÏ?gaÃÓ+eÖ?gësµûËÚ?g46<½RÞ?g-²�ï§à?gI.ÿ!ýöá?gxz¥,Cã?grŠŽäòä?gdÌ]KÈå?gèj+ö—Ýå?i   i
   g      $@i   g      &@(   R,   RÁ   RÊ   R“   R   R>   Rì   R  R7  t   maxR?   RA   RV   t   zt   ac_obst   ac_untwinnedt   frac_ac_lt_1t   max_diff_act   kolmogorov_smirnoff_act   c_obst   c_untwinnedt   frac_c_lt_1t
   max_diff_ct   kolmogorov_smirnoff_ct   sumt   mean_diff_act   mean_diff_c(   R"   t   normalised_acentrict   normalised_centrict   centric_availablet   acentric_availablet   n_zt	   ac_theoryt   c_theoryRf  Rk  RJ  t   max_deviation_act   max_deviation_ct   n_z_less_then_one_act   n_z_less_then_one_ct   d_kolmogorov_smirnov_act   d_kolmogorov_smirnov_cRe  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   À  sb    		'+2

									 c         C   sŽ   t  j d d d d d d d d g d	 d g d
 d g d t t d ƒ ƒ g d t |  j ƒ t |  j ƒ t |  j ƒ t |  j ƒ t |  j ƒ g ƒ S(   Nt   titles   NZ testt   column_labelsRe  s   Acentric observeds   Acentric untwinneds   Centric observeds   Centric untwinnedt   graph_namest   graph_labelst   Zs   P(Z<=z)t   graph_columnsi   RÁ   (   R„  s   P(Z<=z)(	   R   t
   table_datat   listRì   Re  Rf  Rg  Rk  Rl  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRš   ù  s    				$c         C   sá   | j  d ƒ | j d ƒ d |  _ |  j d k  r> d |  _ n  d |  _ |  j d k  rb d |  _ n  | j d i |  j d 6|  j d 6|  j d	 6|  j d
 6t	 j
 |  j ƒ d 6t	 j
 |  j ƒ d 6ƒ | j rÝ | j |  j ƒ n  d  S(   Ns   NZ test for twinning and TNCSsê   
The NZ test is diagnostic for both twinning and translational NCS.  Note
however that if both are present, the effects may cancel each other out,
therefore the results of the Patterson analysis and L-test also need to be
considered.
R3  i    R4  s÷   
  Maximum deviation acentric      :  %(max_diff_ac)4.3f
  Maximum deviation centric       :  %(max_diff_c)4.3f

  <NZ(obs)-NZ(twinned)>_acentric  : %(sign_ac)1s%(mean_diff_ac)4.3f
  <NZ(obs)-NZ(twinned)>_centric   : %(sign_c)1s%(mean_diff_c)4.3f
Ri  Rn  t   sign_act   sign_cRq  Rr  (   R«   R¬   Rˆ  Rq  R‰  Rr  R°   Ri  Rn  RA   RB   t
   gui_outputt	   show_plotRš   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³     s&    		


	(   R)   R*   R(   t   propertyRš   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRc  ¿  s   	9t   l_testc           B   s8   e  Z d  Z d d d d „ Z e d „  ƒ Z d „  Z RS(   s>  
  Implementation of:

  J. Padilla & T. O. Yeates. A statistic for local intensity differences:
  robustness to anisotropy and pseudo-centering and utility for detecting
  twinning. Acta Crystallogr. D59, 1124-30, 2003.

  This is complementary to the NZ test, but is insensitive to translational
  pseuo-symmetry.
  i   c         C   sW  | j  ƒ  j | ƒ } | j ƒ  s0 | j ƒ  } n  | |  _ | |  _ | |  _ t j d | j	 ƒ  d | j
 ƒ  | j ƒ  j
 ƒ  j ƒ  d | j ƒ  d | j ƒ  d | d | d | d d	 ƒ } | j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ t j t |  j j ƒ  ƒ ƒ t |  j j ƒ  ƒ |  _ |  j |  _ |  j d
 |  j |  j d |  _ | j ƒ  |  _ d  S(   Nt   miller_indicest	   intensityRŽ   Rw   t   parity_ht   parity_kt   parity_lt   max_delta_hi   g      @g       @(   RÄ   RÂ   R»   R¼   R�  R‘  R’  R   R�  RÉ   RÁ   RS  R  RŽ   Rw   t   mean_lt   mean_l2t   cumult   l_cumulR   R>   Rì   RÊ   R7  t   l_valuest   l_cumul_untwinnedt   l_cumul_perfect_twint   ml_alpha(   R"   R�   R�  R‘  R’  t   acentric_datat   l_stats(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   2  s2    							"c         C   s   t  j d d d d d d d g d d	 g d
 d g d t t d ƒ ƒ g d t |  j ƒ t |  j ƒ t |  j ƒ t |  j ƒ g ƒ S(   NR€  s   L test, acentric dataR�  s   |l|t   Observeds   Acentric theorys   Acentric theory, perfect twinR‚  s   L testRƒ  s   P(L>=l)R…  i   RÁ   (   s   |l|s   P(L>=l)(   R   R†  R‡  Rì   R˜  R—  R™  Rš  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRš   R  s    					c         C   sÐ   | j  d ƒ | j d ƒ | j d i |  j d 6|  j d 6|  j d 6ƒ | j d ƒ | j d ƒ | j d	 i |  j d
 6|  j d 6ƒ | j d i |  j	 d 6ƒ | j
 r¿ | j |  j ƒ n  | j d ƒ d  S(   Ns   L test for acentric datas0   Using difference vectors (dh,dk,dl) of the form:s4       (%(parity_h)ihp, %(parity_k)ikp, %(parity_l)ilp)R�  R‘  R’  s9   where hp, kp, and lp are random signed integers such thats        2 <= |dh| + |dk| + |dl| <= 8sŠ     Mean |L|   :%(mean_l)4.3f  (untwinned: 0.500; perfect twin: 0.375)
  Mean  L^2  :%(mean_l2)4.3f  (untwinned: 0.333; perfect twin: 0.200)R”  R•  sÁ   
 The distribution of |L| values indicates a twin fraction of
 %(ml_alpha)3.2f. Note that this estimate is not as reliable as obtained
 via a Britton plot or H-test if twin laws are available.
R›  sÒ    Reference:
  J. Padilla & T. O. Yeates. A statistic for local intensity differences:
  robustness to anisotropy and pseudo-centering and utility for detecting
  twinning. Acta Crystallogr. D59, 1124-30, 2003.
(   R«   R¬   R°   R�  R‘  R’  R¯   R”  R•  R›  RŠ  R‹  Rš   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   ^  s&    
	(   R)   R*   Rk   R(   RŒ  Rš   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR�  '  s   
t   britton_testc           B   s8   e  Z d  d d „ Z d „  Z e d „  ƒ Z d „  Z RS(   g×£p=
×ï?i    c         C   s½  | |  _  d |  _ d d d d g } | j | j ƒ  d k ƒ j | ƒ } | j ƒ  sc | j ƒ  } n  g  } t j t	 d ƒ ƒ d } t
 j | j ƒ  | j ƒ  | j ƒ  | j ƒ  | j ƒ  | ƒ } x: t	 d ƒ D], }	 |	 d }
 | j |
 ƒ } | j | ƒ qË Wt j | ƒ } | | d } t j | ƒ t j | ƒ k rEt } d } n d } t } d } x± | r
| d 7} x} t	 d	 ƒ D]o }	 |	 d }
 t j t	 |	 d ƒ ƒ d } | |	 d !} |  j | | ƒ } | d | k rw| d } t } PqwqwW| d
 8} | |  j k rZPqZqZW| d
 7} |	 d |  _ t j d d ƒ } xQ t	 d ƒ D]C }	 |	 d }
 |
 | k  rjd | |	 <qA| d |
 | d | |	 <qAW| |  _ | d |  _ | |  _ | |  _ | |  _ d  S(   Niè  g      à?g      ð?i    i2   g     @Y@g        i   i0   g{®Gázt?i   i   (   R<   t   max_iterRÀ   RÁ   RÂ   R»   R¼   R   R>   Rì   R   t   detwinRÉ   t   sigmasRŽ   Rw   t   detwin_with_alphaR˜   t   minRd  R“   R,   t	   get_alphat	   alpha_cutt   estimated_alphat   correlationt   britton_alphat   britton_obst   britton_fit(   R"   R<   R�   t
   cc_cut_offRÌ   RF   t   britton_plot_arrayt   britton_plot_alphat   detwin_objectRJ  t   alphat   negative_fractiont   not_doneR§  t   icountt   britton_rangeRª  t   britton_plot_fit(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   ƒ  sl    							
		





			c         C   s  | j  ƒ  | j  ƒ  k s t ‚ t j | ƒ } t j | ƒ } t j | | ƒ | | } t j | | ƒ | | } t j | | ƒ | | } t | j  ƒ  ƒ } t j | | ƒ | | | }	 | | d }
 | |
 | } | t j t | | ƒ ƒ d } | |
 d | | |
 g S(   Ng�íµ ÷Æ°>(	   RÊ   RC   R   RW  R7  Rp  RA   RV   R?   (   R"   R0  t   yt   mean_xt   mean_yt   var_xt   var_yt   covar_xyt   NRŠ   R2  R1  R¨  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR¥  Â  s    !c         C   sw   t  j d d t |  j ƒ d d d d g d d g d	 d g d
 d d d g g d |  j g g d |  j |  j |  j g ƒ S(   NR€  s   Britton plot for twin law %sR�  R°  s   percentage negativest   fitR‚  s   Britton plotRƒ  R…  i    i   i   t   reference_marksRÁ   (   s   alphas   percentage negatives(   R   R†  RO   R<   R§  R©  Rª  R«  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRš   Ð  s    			c         C   s@   | j  d ƒ | j d i |  j d 6|  j d 6|  j d 6ƒ d  S(   Ns   Britton analysess”   
  Extrapolation performed on  %(alpha_cut)3.2f < alpha < 0.495
  Estimated twin fraction: %(estimated_alpha)4.3f
  Correlation: %(correlation)5.4f
R¦  R§  R¨  (   R­   R¬   R¦  R§  R¨  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   Û  s
    
(   R)   R*   R(   R¥  RŒ  Rš   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRŸ  ‚  s
   ;	t   h_testc           B   s5   e  Z d  d „ Z e d „  ƒ Z d „  Z d „  Z RS(   g      à?c         C   s—  | |  _  | |  _ | j | j ƒ  d k ƒ j | ƒ } | j ƒ  sQ | j ƒ  } n  | j ƒ  r“| j ƒ  j | ƒ } yX t	 j
 d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | d | ƒ } WnB t k
 r} | j ƒ  d	 k  rt d
 d d ƒ ‚ q| ‚ n X| j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ |  j d d d |  _ | j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ | j ƒ  |  _ n  d  S(   Ni    RŽ  R�  t   sigmaRŽ   Rw   R<   R8  gš™™™™™©?s5   These data are severely incomplete, which breaks the s@   H-test for twinning.  We recommend that you use a full data set s>   in Xtriage, otherwise the statistical analyses may be invalid.g       @g      ð?g       À(   R8  R<   RÀ   RÁ   RÂ   R»   R¼   t   is_real_arrayRÄ   R   R¿  RÉ   R¢  RŽ   Rw   t
   ValueErrorRß   R   t   mean_ht   mean_h2R°  R§  t   alpha_from_mean_ht   h_arrayt   h_valuest   h_cumul_obst	   cumul_obst   h_cumul_fitt	   cumul_fit(   R"   R<   R�   R8  Rœ  t   h_test_objectt   e(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   ç  s@    				
c         C   sw   t  j d d t |  j ƒ d d d d g d d g d	 d g d d d d g g d |  j g g d |  j |  j |  j g ƒ S(   NR€  s   H test for possible twin law %sR�  t   Hs   Observed S(H)s   Fitted S(H)R‚  s   H test for acentric dataRƒ  s   S(H)R…  i    i   i   R¾  RÁ   (   RÎ  s   S(H)(   R   R†  RO   R<   R§  RÆ  RÉ  RË  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRš     s    			c         C   sX   | j  d ƒ | j d i |  j d d 6|  j d 6|  j d 6|  j d 6|  j d 6ƒ d  S(	   Ns   H-test on acentric datasO  Only %(fraction)3.1f %% of the strongest twin pairs were used.

  mean |H| : %(mean_h)4.3f  (0.50: untwinned; 0.0: 50%% twinned)
  mean H^2 : %(mean_h2)4.3f  (0.33: untwinned; 0.0: 50%% twinned)

Estimation of twin fraction via mean |H|: %(alpha_from_mean_h)4.3f
Estimation of twin fraction via cum. dist. of H: %(estimated_alpha)4.3f
g      Y@R8  RÃ  RÄ  RÅ  R§  (   R­   R¯   R8  RÃ  RÄ  RÅ  R§  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³     s    


c         C   s   d |  _ |  j S(   s0   
    Pickling optimization - see note below
    N(   R´   RÇ  t   __dict__(   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   __getstate__(  s    	(   R)   R*   R(   RŒ  Rš   R³   RÐ  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR¿  æ  s   #	t   ml_murray_rust_with_ncsc           B   sV   e  Z d  d d d „ Z d „  Z e j d „ Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   i
   c      
   C   s]  | d  k r t j } n  | |  _ d |  _ d |  _ | j ƒ  sE t ‚ | j ƒ  d  k	 s] t ‚ | j ƒ  d k } | j	 ƒ  j
 | ƒ j | ƒ } | j d | ƒ |  _ | |  _ d  |  _ d |  _ t j | j ƒ  | j ƒ  | j ƒ  |  j j ƒ  | j ƒ  | j ƒ  | | j ƒ  d ƒ	 |  _ d | |  _ t j d g ƒ |  _ | d  k	 rŸ| d k rWd } n  | d k rld	 } n  t j |  j | d
 ƒ }	 t j |	 g ƒ |  _ n  x, t  | ƒ D] }
 |  j j! d |
 d ƒ q¬W|  j" d | ƒ t# j$ j% d d ƒ } t# j$ j& d |  d | ƒ |  _' t# j$ j& d |  ƒ |  j |  _( |  j d t j) |  j d ƒ |  _* g  } x¢ t  |  j j+ ƒ  d ƒ D]‡ }
 |  j |
 d } |  j d
 t j) | ƒ } |  j j, |
 d ƒ d } |  j j, |
 d ƒ d } | j! d | | f d | g ƒ quWt- j. d | d d d g ƒ |  _/ |  j0 | ƒ | d  k	 rY|  j1 d | d | d | ƒ n  d  S(   NgÍÌÌÌÌÌÜ?gffffffî?i    t   n_binsi   i   iýÿÿÿgš™™™™™Ù?gš™™™™™©?g      ð?g      ð¿g      4@RŸ   t   max_iterationsiè  t   target_evaluatort   termination_paramss   %5.4f - %5.4fs   %4.3fR~   R{   t
   Resolutiont   D_ncst   obst   calc(2   R´   R¸   R¹   R<   t   twin_capt   d_capR»   RC   R¢  R½   RÀ   RÂ   t   setup_binnert   binnerRŸ   t   ft   cycleR   t   ml_twin_with_ncsRÁ   RÉ   t   bin_indicesRŽ   Rw   R9   t	   ml_objectt   nR   R>   R0  RA   Ru   R6  R˜   t
   _show_infot   scitbxt   lbfgst   termination_parameterst   runt	   minimizert   log_likelihoodR.  t   twin_fractionRÊ   t   bin_d_rangeR   R™   Rš   t   _show_resultst   calc_correlation(   R"   R�   R<   RŸ   RÒ  t	   calc_datat   start_alphat   non_zero_sigma_selt   tmp_miller_arrayRK   RJ  t   term_parametersRš   t   d_ncst   lowt   high(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   0  st    														% %		c         C   s   t  d | ƒ S(   Ns   %4.3f(   RO   (   R"   R0  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt	   string_itt  s    c      
   C   s_  | j  ƒ  r | j ƒ  } n  | j d | ƒ } | j | ƒ | d IJ| d IJx
| j ƒ  j ƒ  D]ö } | j | j ƒ  j ƒ  | k ƒ } t j	 | j
 ƒ  | j ƒ  | j ƒ  | j ƒ  |  j ƒ } |  j j | ƒ d } |  j j | ƒ d } | j ƒ  }	 |  j | }
 |  j d t j |
 ƒ }
 |
 |
 }
 | d | | | |  j |	 ƒ |  j |
 ƒ f IJqa Wd  S(   Nt   othersØ   
 The correlation of the calculated F^2 should be similar to the estimated
 values.

 Observed correlation between twin related, untwinned calculated F^2
 in resolutiuon ranges, as well as ewstimates D_ncs^2 values:
s-    Bin    d_max     d_min     CC_obs   D_ncs^2 i    i   g      ð?s#   %3i)    %5.4f -- %5.4f :: %6s   %6s(   t   is_xray_amplitude_arrayR¼   t
   common_sett   use_binning_ofRÝ  t
   range_usedRÀ   Rá  R   t   twin_rRÉ   RÁ   RŽ   Rw   R<   Rì  R¨  R0  RÛ  RA   R.  R÷  (   R"   RØ  RÙ  RŸ   t   i_bint	   tmp_arrayt   tmp_r_classRõ  Rö  t   d_theory_sqt   d_ncs_sq(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRî  w  s,    
!			
c         C   sÃ   |  j  j |  j d |  j d ƒ } | d } | d } | |  _ |  j d 7_ |  j d d k ry |  j d |  j In |  j d I|  j d d k r£ |  j Jn  |  j j ƒ  | t j | ƒ f S(   Ni    i   i   s   %3i t   .i   (	   Râ  t   p_tot_given_t_and_coeffR0  RÞ  Rß  RŸ   t   flushR   R>   (   R"   RK   RÞ  Rz   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt    compute_functional_and_gradients–  s    

	
c         C   sB   t  | t j ƒ s$ t j | ƒ } n  | j d ƒ | j d ƒ d  S(   Ns.   Maximum Likelihood twin fraction determinations]  
 Estimation of twin fraction, while taking into account the effects of
 possible NCS parallel to the twin axis.
     (Zwart, Read, Grosse-Kunstleve & Adams, to be published.)

 A parameters D_ncs will be estimated as a function of resolution, together
 with a global twin fraction.  D_ncs is an estimate of the correlation
 coefficient between untwinned, error-free, twin related, normalized
 intensities.  Large values (0.95) could indicate an incorrect point group.
 Value of D_ncs larger than say, 0.5, could indicate the presence of NCS. The
 twin fraction should be smaller or similar to other estimates given elsewhere.

 The refinement can take some time.  For numerical stability issues, D_ncs is
 limited between 0 and 0.95.  The twin fraction is allowed to vary between 0
 and 0.45.  Refinement cycle numbers are printed out to keep you entertained.
(   t
   isinstanceR   t   xtriage_outputt   printed_outputR­   R¬   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRä  ¨  s
    c         C   sK   t  | t j ƒ s$ t j | ƒ } n  | j d ƒ | j |  j d d ƒd  S(   Nsƒ     Cycle : %(cycle)3i
  Log[likelihood]: %(log_likelihood)15.3f
  twin fraction: %(twin_fraction)4.3f
  D_ncs in resolution ranges:
R¨   i   (   R  R   R  R	  R¬   R®   Rš   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRí  ½  s
    c         C   s   |  j  | ƒ |  j | ƒ d  S(   N(   Rä  Rí  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   È  s    N(   R)   R*   R´   R(   R÷  R¸   R¹   Rî  R  Rä  Rí  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRÑ  /  s   >				t   ml_murray_rustc           B   s2   e  Z d  Z d d „ Z e d „  ƒ Z d „  Z RS(   sm   
  Maximum-likelihood twin fraction estimation (Zwart, Read, Grosse-Kunstleve &
  Adams, to be published).
  i   c         C   sæ  | j  ƒ  s t ‚ | j ƒ  d  k	 s* t ‚ | j ƒ  d k } | j | ƒ } t j d | j ƒ  d | j ƒ  d | j ƒ  d | j	 ƒ  d | j
 ƒ  d | d | ƒ } | |  _ g  |  _ g  |  _ xK t d	 d
 ƒ D]: } | d } |  j j | ƒ |  j j | j | ƒ ƒ qÊ Wt j t j |  j ƒ ƒ }	 d  |  _ |	 d	 k rÒ|	 t |  j ƒ d	 k  rÒd d d g }
 d d d g } |  j |	 d	 |
 d <|  j |	 |
 d	 <|  j |	 d	 |
 d <|  j |	 d	 | d <|  j |	 | d	 <|  j |	 d	 | d <|
 d d | d | d	 |
 d	 d | d	 | d |
 d d | d | d } |
 d | d | d	 |
 d	 | d	 | d |
 d | d | d } | d | |  _ |  j |
 d k  s¼|  j |
 d k râ|  j |	 |  _ qân |  j |	 |  _ d  S(   Ni    Re  t   sig_zRÉ   RŽ   Rw   R<   t	   n_hermitei   i   g      G@i   g       @(   R»   RC   R¢  R´   RÀ   R   R
  RÁ   RÉ   RŽ   Rw   R<   Rë  t   nllR6  R˜   t   fast_log_p_given_tR   t	   min_indexR>   R§  Rª   (   R"   R�   R<   t   n_pointsRñ  Rÿ  t   ml_murray_rust_objectRJ  R   t   i_t_maxt   tmp_tt   tmp_nllt   tmp_topt
   tmp_bottom(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   Ñ  sN    					
	%7/c         C   sk   t  j d d t |  j ƒ d d d g d d g d d g d	 d
 d g g d |  j g g d |  j |  j g ƒ S(   NR€  s9   Likelihood based twin fraction estimation for twin law %sR�  R°  s   -Log[Likelihood]R‚  s'   Likelihood-based twin fraction estimateRƒ  R…  i    i   R¾  RÁ   (   s   alphas   -Log[Likelihood](   R   R†  RO   R<   R§  Rë  R  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRš      s    			c         C   s%   | j  d ƒ | j d |  j ƒ d  S(   Ns.   Maximum Likelihood twin fraction determinationsj     Zwart, Read, Grosse-Kunstleve & Adams, to be published.
  The estimated twin fraction is equal to %4.3f
(   R­   R¬   R§  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³     s    (   R)   R*   Rk   R(   RŒ  Rš   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR
  Ì  s   ,c         C   sä   t  j | ƒ } t  j | |  ƒ | } t  j | | ƒ | } t  j | |  |  ƒ | } t  j | | | ƒ | } t  j | |  | ƒ | } | | | }	 t j | | | | | | ƒ }
 t j |
 ƒ d k rÜ |	 |
 Sd Sd S(   s6   
  Utility function for correlation_analyses class.
  i    g        N(   R   Rp  RA   RV   RB   (   R0  R¶  t   wt   twt   meanxt   meanyt   meanxxt   meanyyt   meanxyRG   RH   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   weighted_cc  s    #t   correlation_analysesc           B   s&   e  Z d  d „ Z d „  Z d „  Z RS(   gš™™™™™¹?c   
         sÝ  t  j | ƒ ˆ _ ˆ j j d | j ƒ  j ƒ  d | j ƒ  j ƒ  ƒ ˆ _ | j ƒ  ‰ | j ƒ  ‰  t j	 ˆ  d t
 ƒ} | j j ƒ  ‰  ˆ  j ˆ  j ƒ  d k ƒ ‰  ˆ  j ˆ j ƒ ‰ ˆ  j d ˆ j ƒ  d ˆ j ƒ  ƒ j ƒ  ‰ ˆ  j ˆ ƒ \ ‰  ‰ ˆ j ˆ  ƒ \ ‰ ‰  ˆ j ˆ ƒ \ ‰ ‰ | ˆ _ g  ˆ _ g  ˆ _ ‡  ‡ ‡ ‡ f d †  } ˆ j ƒ  j ƒ  d k rµxM t d ƒ D]< } | d	 } ˆ j j | ƒ | | ƒ }	 ˆ j j |	 ƒ qrWn  ˆ j ƒ  \ ˆ _ ˆ _ ˆ ` ˆ ` d  S(
   NR  R  R·   i    RÉ   RÁ   c            s±   ˆ j  ƒ  } ˆ j ƒ  } ˆ j ƒ  j ƒ  } t j d ˆ j ˆ j | ƒ } d | | } d |  ˆ  j ƒ  |  ˆ j ƒ  } | | | } | | | | } t | | | ƒ } | S(   NgÍÌÌÌÌÌÀg      ð?i   (   R¢  RÁ   t	   d_star_sqR   R.  t   dR  (   R°  t   wxR0  t   ddt   wyR¶  t   w_tott   cc(   RÙ  t
   calc_part2RØ  R"   (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   twin_the_data_and_compute_cc?  s    !"i2   g      Y@(   R   R5   R<   R  RŽ   R  R  R½   R   R¾   R,   R¿   RÀ   RÁ   R0   t   customized_copyRÉ   t
   map_to_asut   common_setsR!  R°  R&  RÊ   R6  R˜   t   find_maximumt	   max_alphat   max_cc(
   R"   t
   miller_obst   miller_calcR<   t   d_weightRÎ   R(  RJ  R°  R&  (    (   RÙ  R'  RØ  R"   sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   %  s:    				
c         C   sM   t  j t  j |  j ƒ ƒ } t  j t  j |  j ƒ ƒ } |  j | } | | f S(   N(   R   Rd  R>   R&  t	   max_indexR°  (   R"   R.  R-  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR,  T  s    c         C   sF   | j  d ƒ | j d ƒ | j d |  j ƒ | j d |  j ƒ d  S(   Ns   Correlation analysessŠ   
  The supplied calculated data are normalized and artificially twinned;
  subsequently a correlation with the observed data is computed.
s   Correlation : %.3fs   Estimated twin fraction : %.3f(   R­   R¬   R.  R-  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   Z  s
    (   R)   R*   R(   R,  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR  $  s   +	t   twin_law_dependent_twin_testsc           B   s\   e  Z d  Z d d d d d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z	 RS(   s   Twin law dependent test resultsi    c	         C   sŠ  | d  k r d } n  | j ƒ  j | ƒ }	 | |  _ t j d |	 j ƒ  d |	 j ƒ  d |	 j ƒ  d |	 j	 ƒ  d |	 j
 ƒ  d | j j ƒ  d d	 !ƒ }
 |
 j ƒ  |  _ t |  _ d  |  _ |  _ d  |  _ |  _ d  |  _ d  |  _ d  |  _ |  _ d  |  _ |  j d k r†t |  _ t d | j j ƒ  d d	 !d
 |	 ƒ |  _ t d | j j ƒ  d d	 !d
 |	 d | ƒ |  _ t d | d | j j ƒ  d d	 !d | ƒ |  _ | d  k	 r÷d } y+ t d | d | d | j d | ƒ |  _ Wq÷t k
 rã‚  q÷t k
 róq÷Xn  | j ƒ  d  k	 r†t d
 | d | j j ƒ  d d	 !ƒ |  _ | rƒt d
 | d | j j ƒ  d d	 !d | d | d | d |  j j ƒ |  _ qƒq†n  d  S(   Ni   RŽ  R�  RÀ  RŽ   Rw   R<   i    i	   R�   RÌ   R/  R0  g      Ð?R1  RŸ   RÒ  Rï  Rð  (    R´   RÄ   RÂ   R<   R   R¡  RÉ   RÁ   R¢  RŽ   Rw   Rl   t   as_double_arrayRß   t   twin_completenessR“   t   results_availableR¿  t   h_test_tableRŸ  t   britton_test_tablet   r_valuesR¨  R
  t   ml_murray_rust_tablet   ml_murry_rust_with_ncsR,   R  R•   R–   RÑ  R§  (   R"   R<   R�   RŸ   RÌ   R0  t   normalized_intensitiest   ncs_testt
   n_ncs_binsRœ  t   tmp_detwin_objectR1  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   h  sr    											  c         C   s   |  j  d  k	 r |  j  j Sd  S(   N(   R¿  R´   R§  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   h_fracª  s    c         C   s   |  j  d  k	 r |  j  j Sd  S(   N(   RŸ  R´   R§  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   britton_frac¯  s    c         C   s   t  j d  k	 r t  j j Sd  S(   N(   R"   R
  R´   R§  (   t   Self(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   ml_frac´  s    c         C   sØ   | j  d |  j ƒ |  j r¼ |  j j | ƒ |  j j | ƒ |  j j | ƒ |  j d  k	 ro |  j j | ƒ n  |  j j	 |  j j	 g } |  j
 d  k	 r¬ | j |  j
 j	 ƒ n  | j | ƒ n | j d d |  j ƒ d  S(   Ns   Analysis of twin law %ss8   The twin completeness is only %3.2f. Twin law dependent s   tests not performed.(   R«   R<   R6  R¿  R¬   RŸ  R9  R¨  R´   Rš   R
  R˜   t   show_plots_rowR5  (   R"   RŸ   t   tables(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   ¹  s    		N(
   R)   R*   Rk   R´   R(   RŒ  R@  RA  RC  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR3  f  s   :t   symmetry_issuesc           B   sJ   e  Z d  d d d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   g      @gš™™™™™©?g      ô?c      	   C   s´  d  |  _ | d  k r! t j } n  | |  _ | |  _ t j | j ƒ  d | j	 ƒ  ƒ|  _
 t j |  j
 j ƒ  |  j
 j	 ƒ  d | ƒ|  _ |  j
 j	 ƒ  j t t ƒ |  _ t t j d |  j ƒ ƒ |  _ |  j j |  _ t t j d |  j j ƒ ƒ |  _ t t j d |  j j ƒ ƒ |  _ |  j j |  j
 j ƒ  ƒ |  _ i  |  _ i  |  _ i  |  _ i  |  _ i  |  _  i  |  _! i  |  _" i  |  _# d  |  _$ g  |  _% d  |  _& g  |  _' d  |  _( |  j) ƒ  |  j* ƒ  xT|  j D]I} |  j } | j+ ƒ  rì| j, ƒ  } n  | j- ƒ  } | j- ƒ  d  k rq| j. ƒ  j/ ƒ  } t0 j1 d ƒ t2 j3 | ƒ }	 d t2 j4 | |	 ƒ } | j/ ƒ  | } d |	 |  _ n' | d k }
 | j5 |
 ƒ } | j- ƒ  } t6 j7 j8 | j9 ƒ  | j/ ƒ  | |  j t j | ƒ j: ƒ  | j; ƒ  | j ƒ  ƒ } | j< ƒ  } |  j# j= i | | 6ƒ qÂWd d	 d
 d d d d g } g  } d } d „  } xµ |  j# D]ª } | | |  j | d ƒ | |  j | d ƒ | |  j | d ƒ | |  j" | d ƒ | |  j# | d ƒ d g } | j> | ƒ |  j# | | k  rI|  j# | } | |  _$ qIqIWx1 |  j' D]& } |  j$ | d k rd | d <qqWt? j@ d | d | ƒ |  _( x4 |  j jA jB jC |  j$ jD D] } |  j% j> | ƒ qcWt j d |  j j ƒ  d |  j$ d t ƒ jE ƒ  |  _F d  S(   NRŽ   Rv   Rƒ   gš™™™™™É?gš™™™™™©?s     ----> WARNING: NO SIGMAS FOUND  <----
          Sigmas now modeled as 0.05*|F|*exp( -Berror/d^2 )
          with Berror=%6.3fi    s   Point groups   mean R_useds
   max R_useds   mean R_unuseds   min R_unusedt   BICt   choiceg    eÍÝAc         S   s   t  | |  d d ƒS(   Nt   replace_none_withR´   (   R   (   t   valt   fmt(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt	   as_string  s    s   %4.3fs   %6.3es       s   <---i   R{   R~   R9   t   assert_is_compatible_unit_cellg      @(G   R´   t   sigma_warningR¸   R¹   t   inflate_sigmaR�   R   Rá   R9   RŽ   t   xs_inputR   t"   space_group_graph_from_cell_and_sgt
   explore_sgt   build_derived_groupR“   t   pg_of_input_sgRO   R   R†   t   pg_input_namet   pg_low_prim_sett   pg_low_prim_set_namet   pg_hight   pg_lattice_nameR0   R1   t   miller_nigglit   ops_and_r_pairst   pg_r_used_tablet   pg_max_r_used_tablet   pg_r_unused_tablet   pg_r_unused_splitt   pg_r_used_splitt   pg_min_r_unused_tablet	   pg_scorest	   pg_choicet   sg_possibilitiest   legendR†  Rš   t   make_r_tablet   make_pg_r_tableRù  R¼   R¢  R   RÁ   RA   Ru   R   Rd  R.  RÀ   t   cctbxt   xrayt   mergerRÉ   Rƒ   Rw   t   bict   updateR˜   R   R™   t   pg_grapht   grapht   node_objectst   allowed_xtal_symst   as_reference_settingt%   xs_with_pg_choice_in_standard_setting(   R"   R�   Rv   t   r_cutt   sigma_inflationRŸ   t   pgRK   R¢  t   Berrort   non_zero_sigmas_selRj  t   final_scoreRe  R†  t	   min_scoreRL  R    R/   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   Î  s°    																	

		
						c   	      C   sh  t  ƒ  } t t j d |  j ƒ ƒ } |  j j j j | j	 ƒ  d } |  j j j j | | } x€ | j
 ƒ  D]r } t d |  j d | j ƒ  d d !ƒ j d | ƒ } | j } | j } |  j j i | | f | j ƒ  j ƒ  6ƒ qk Wx€ | j ƒ  D]r } t d |  j d | j ƒ  d d !ƒ j d | ƒ } | j } | j } |  j j i | | f | j ƒ  j ƒ  6ƒ qî Wd  S(   NRƒ   i    R/  R<   i	   RŸ   (   R   RO   R   R†   RV  RR  Rm  Rn  t   edge_objectst   keyst   return_usedR9  RZ  R4  R¬   t   r_abs_top_obst   r_abs_bottom_obsR[  Rl  R6   Rq   t   return_unused(	   R"   t
   tmp_buffert   startt   tmp_keyt   tmp_edget   symopt   r_valueRG   RH   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRf  <  s(    	 	"		!	"		c   
      C   s  t  t j d |  j ƒ ƒ } t  t j d |  j j ƒ ƒ } xÈ |  j j j j D]´ } |  j	 | | ƒ \ } } } } } }	 |  j
 j i | | 6ƒ |  j j i | | 6ƒ |  j j i | | 6ƒ |  j j i | | 6ƒ |  j j i | | 6ƒ |  j j i |	 | 6ƒ qL Wd  S(   NRƒ   (   RO   R   R†   RV  RR  RX  Rm  Rn  Ro  t   get_r_value_totalR\  Rl  R]  R`  R^  Ra  R_  (
   R"   R�  t   endRu  t   tot_int   max_int   tot_outt   min_outt   r_int   r_out(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRg  T  s    $c         C   sß  t  j | ƒ j ƒ  } t  j | ƒ j ƒ  } t j  j j | | ƒ } g  } x; | j D]0 } x' | D] } | j | j ƒ  j	 ƒ  ƒ q_ WqR Wt
 j ƒ  }	 t
 j ƒ  }
 d d g } d d g } |  j j ƒ  } |  j j ƒ  } |  j j ƒ  } xò | D]ê } | | k rê | j | ƒ } | | } g  } g  } xK t t | ƒ ƒ D]7 } | | k r4| j | | ƒ | j | | ƒ q4q4Wt | ƒ } t | ƒ } |	 j | d t | d d ƒ ƒ | d c | d 7<| d c | d 7<qê qê WxX | D]P } |
 j | d t | d d ƒ ƒ | d c | d 7<| d c | d 7<qßWd  } d  } d  } d  } |	 j ƒ  d k rŠ| d t d | d ƒ } t
 j |	 ƒ } n  |
 j ƒ  d k rÉ| d t d | d ƒ } t
 j |
 ƒ } n  | | | | |	 |
 f S(   Ni    i   g:Œ0âŽyE>(   R   R†   Rƒ   Rh  RŒ   R�   R‘   R˜   R6   Rq   R   R>   R[  R{  t   valuest   copyt   indexR6  Rª   R‡  Rd  R´   RÊ   R¤  (   R"   t   start_pgt   end_pgt   spgt   epgRŒ   t	   coset_opst   csRo   RŒ  R�  t   tot_r_int	   tot_r_outt   tmp_keyst
   tmp_valuesRK   t   iiiR1  t   tmp_tmp_keyst   tmp_tmp_valuesRJ  Rˆ  R‰  RŠ  R‹  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR†  b  sX    !
""c         C   s=   |  j  } | j ƒ  r$ | j ƒ  } n  | |  j |  j |  j g S(   N(   RZ  Rù  R¼   RW  Rc  RY  (   R"   RZ  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   return_point_groups–  s
    	c         C   sQ  | j  d ƒ |  j d  k	 r/ | j |  j ƒ n  | j rH | j d ƒ n  | j d t |  j ƒ t t	 j
 d |  j ƒ ƒ t |  j ƒ f ƒ | j |  j d d ƒt | d ƒ r» | j ƒ  n  | j d ƒ | j d	 ƒ | j d
 |  j ƒ | j d ƒ xG |  j D]< } t ƒ  } | d j d | d d ƒ | j | j ƒ  ƒ q W| j d ƒ d  S(   Ns    Exploring higher metric symmetrys!   Point group and R-factor analysissÏ   
The point group of data as dictated by the space group is %s
The point group in the niggli setting is %s
The point group of the lattice is %s
A summary of R values for various possible point groups follow.
Rƒ   R¨   i   t   add_change_symmetry_buttons­   
R_used: mean and maximum R value for symmetry operators *used* in this point group
R_unused: mean and minimum R value for symmetry operators *not used* in this point group
sg   
An automated point group suggestion is made on the basis of the BIC (Bayesian
information criterion).
s*   The likely point group of the data is: %s
s.   Possible space groups in this point group are:i    RÞ  t   prefixs      s´   
Note that this analysis does not take into account the effects of twinning.
If the data are (almost) perfectly twinned, the symmetry will appear to be
higher than it actually is.
(   t   show_headerRN  R´   RI  RŠ  R«   R¯   RO   RU  R   R†   RV  RY  R®   Rš   t   hasattrRŸ  R¬   Rc  Rd  R   t   show_summaryR°   R±   (   R"   RŸ   R'  t   sg_out(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   ¡  s0    		N(	   R)   R*   R´   R(   Rf  Rg  R†  Rž  R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRF  Í  s   i			4	R9  c           B   sA   e  Z d d  d „ Z d „  Z d „  Z d „  Z e j d „ Z	 RS(   i�  c         C   s  | j  ƒ  |  _ d  |  _ | |  _ | d  k	 r` | j  ƒ  |  _ |  j j |  j ƒ \ |  _ |  _ n  | |  _ d  |  _ d  |  _ d  |  _	 d  |  _
 d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ g  |  _ g  |  _ g  |  _ d  |  _ |  j |  j |  j ƒ |  j ƒ  |  ` |  ` d  S(   N(   R½   RØ  R´   RÙ  t   n_reflectionsR+  R<   t   rvsr_interpretationRš   t	   r_abs_obst
   r_abs_calct   r_sq_obst	   r_sq_calcR}  R~  t   r_sq_top_obst   r_sq_bottom_obsR   t   r_sq_obs_resot   r_sq_calc_resot   guesst   r_vs_rt   r_vs_r_classification(   R"   R/  R<   R0  R¥  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   É  s2    		$															
c         C   sA  |  j  j d |  j ƒ x$|  j  j ƒ  j ƒ  D]} |  j  j ƒ  j | ƒ j ƒ  } |  j  j | ƒ } d  } |  j	 d  k	 rŒ |  j	 j | ƒ } n	 d  |  _
 |  j | | ƒ |  j  j ƒ  j | ƒ d } |  j  j ƒ  j | ƒ d } |  j j d | | d | | ƒ |  j j |  j ƒ |  j	 d  k	 r, |  j
 j |  j ƒ q, q, Wd  S(   Nt   reflections_per_bini    i   g      à?(   RØ  RÜ  R¥  RÝ  Rü  t	   selectiont
   iselectionRÀ   R´   RÙ  R®  R°  Rì  R   R˜   R­  R©  Rª  (   R"   t
   bin_numberR³  t   tmp_obst   tmp_calct   upt   down(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   resolution_dependent_r_valuesé  s    	$c         C   sl  d  } d  } | d  k	 r0 | j | ƒ \ } } n | j ƒ  } | j ƒ  sW | j ƒ  } n  | d  k	 r� | j ƒ  s� | j ƒ  } q� n  t j | j ƒ  | j ƒ  | j	 ƒ  | j
 ƒ  |  j ƒ } | j ƒ  |  _ | j ƒ  |  _ | j ƒ  \ |  _ |  _ | j ƒ  \ |  _ |  _ | d  k	 rht j | j ƒ  | j ƒ  | j	 ƒ  | j
 ƒ  |  j ƒ } | j ƒ  |  _ | j ƒ  |  _ n  d  S(   N(   R´   R+  R½   R»   R¼   R   Rý  RÉ   RÁ   RŽ   Rw   R<   t   r_abs_valueR§  t
   r_sq_valueR©  t	   r_sq_pairR«  R¬  t
   r_abs_pairR}  R~  R¨  Rª  (   R"   t	   input_obst
   input_calcRØ  RÙ  t   obs_objt   calc_obj(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR°  ý  s6    						c      
   C   sV  d d d d  d g d d d d  d g d d d d  d g d d d d  d g d d d	 d  d g d d d
 d  d g g |  _ d  } d } d } |  j d  k	 r)x� |  j D]‚ } t | d ƒ |  j d } t | d ƒ |  j d } t j | | ƒ } d | | d <| | k  r| } | } n  | d 7} q  Wn  | |  _ | d  k	 rRd |  j | d <n  d  S(   Ns   0.1s*   Misspecified (too low) crystal symmetry 
 R5  s   0.3s*   Twin with NCS parallel to twin operator 
 s   0.5s   Close to perfect twinning 
 sF   Data is not twinned, but NCS is parallel 
   to putative twin operators   Partially twinned data 
 s   No twinning or parallel NCSg      ð?i    g       @i   s   %4.3fi   s   <---i   (   R´   R¦  R¨  R7  R§  RA   RV   R¯  (   R"   R¯  R¤  R  t
   test_classR¶  R·  R!  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR±    s,    		c         C   sÆ   | j  d ƒ | j d ƒ | j d ƒ | j d |  j ƒ |  j d  k	 ra | j d |  j ƒ n  | j d ƒ | j d |  j ƒ |  j d  k	 r¨ | j d |  j ƒ n | j d	 ƒ | j d
 ƒ d  S(   Ns   R vs R statisticss"     R_abs_twin = <|I1-I2|>/<|I1+I2|>s?       (Lebedev, Vagin, Murshudov. Acta Cryst. (2006). D62, 83-95)s$     R_abs_twin observed data   : %4.3fs%      R_abs_twin calculated data : %4.3fs%     R_sq_twin = <(I1-I2)^2>/<(I1+I2)^2>s$     R_sq_twin observed data    : %4.3fs$     R_sq_twin calculated data  : %4.3fs     No calculated data available.s,     R_twin for calculated data not determined.(   R­   R¬   R§  R¨  R´   R©  Rª  (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   7  s    N(
   R)   R*   R´   R(   Rº  R°  R±  R¸   R¹   R³   (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR9  È  s   		 	t   twin_results_interpretationc           B   s†   e  Z d d d d  d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z RS(   g      @g{®Gáz„?c
         C   sK  | |  _  | |  _ d  |  _ |  _ | d  k	 rI | j |  _ | j |  _ n  | j |  _ | j	 |  _
 | j |  _ | j |  _ | j |  _ |  j ƒ  t | ƒ |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ t }
 x>| D]6} |  j j | j j j ƒ  j  ƒ  ƒ |  j j t! | j j" ƒ ƒ | j# rÐt$ }
 |  j j | j% j& ƒ |  j j | j% j' ƒ |  j j | j( j) ƒ |  j j | j* j) ƒ | j+ d  k	 r½|  j j | j+ j) ƒ q |  j j d  ƒ qê |  j j d  ƒ |  j j d  ƒ |  j j d  ƒ |  j j d  ƒ |  j j d  ƒ qê W|  j d k r#|
 ržd } d } xJ t, |  j ƒ D]9 \ } } | d  k	 rUt- | ƒ | k rU| } | } qUqUW| |  _. n^ g  } x: |  j D]/ } | d  k rÐ| j d ƒ q®| j | ƒ q®Wt/ j0 t/ j1 | ƒ ƒ |  _. | j2 |  _3 | j4 |  _5 | j6 |  _7 n$ d  |  _7 d  |  _. d  |  _3 d  |  _5 d  S(   Ni    iÿÿÿÿiœÿÿÿ(8   t
   maha_l_cutt   patterson_p_cutR´   t   patterson_heightt   patterson_p_valueRö   Rù   RO  t   i_ratioRP  t   f_ratioRQ  t   e_sq_minus_1R”  t   l_meanR•  t	   l_sq_meant   compute_maha_lRª   t   n_twin_lawsRs   t   twin_law_typet   r_obst   r_calcR©  t   h_alphat   murray_rust_alphaR“   R˜   R<   Rl   R6   Rq   RO   Rm   R6  R,   R9  R§  R¨  RŸ  R§  R¿  R
  t	   enumerateR7  t   most_worrysome_twin_lawR   R2  R>   Rc  t   suspected_point_groupRd  t   possible_sgsRW  t   input_point_group(   R"   t   nz_testt   wilson_ratiosR�  t   translational_pseudo_symmetryt   twin_law_related_testRF  RÅ  RÆ  RŸ   t   have_twin_test_resultst	   twin_itemt   max_tfR2  RJ  Rë  t   tmp_tf(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   K  sz    
		
							"					c   	      C   sz   d } d } d } d } d } |  j  | } |  j | } | | | | | | | | | } t j | ƒ } | |  _ d  S(   Ng    `¾$Ag    àåAg       @g    <Ï!Ág¿6Íqß?g�s„˜4@Ô?g    <Ï1Á(   RÌ  RÍ  RA   RV   t   maha_l(	   R"   Râ  t   maha_l2t   maha_ll2t   maha_mean_lt   mama_mean_l2t   tmp_lt   tmp_l2t   maha_distance_l(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRÎ  �  s    	c         C   s#   |  j  d  k r d  S|  j  |  j k S(   N(   RÈ  R´   RÆ  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt!   has_pseudo_translational_symmetry³  s    c         C   s   |  j  |  j k o |  j d k  S(   Ng      à?(   Râ  RÅ  RÌ  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt!   has_abnormal_intensity_statistics¸  s    c         C   s   |  j  ƒ  o |  j d k S(   Ni    (   Rë  RÏ  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   has_twinning»  s    c         C   s   |  j  |  j k S(   N(   RÙ  R×  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   has_higher_symmetry¾  s    c         C   sj   d } |  j  ƒ  rG d |  j |  j f } |  j d k rf | d 7} qf n |  j d  k	 rf d |  j } n  | S(   NR5  s$  The analyses of the Patterson function reveals a significant off-origin
peak that is %3.2f %% of the origin peak, indicating pseudo-translational
symmetry.  The chance of finding a peak of this or larger height by random
in a structure without pseudo-translational symmetry is equal to %5.4e.i   sœ   
The detected translational NCS is most likely also responsible for the
elevated intensity ratio.  See the relevant section of the logfile for more
details.s�   The largest off-origin peak in the Patterson function is %3.2f%% of the
height of the origin peak. No significant pseudotranslation is detected.(   Rê  RÇ  RÈ  RÉ  R´   (   R"   t   verdict(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   patterson_verdictÁ  s    c         C   s¾  | j  d |  j ƒ  ƒ |  j |  j k rà |  j d k  r¢ | j  d ƒ |  j d k r‰ | j  d ƒ t |  _ |  j ƒ  rŸ | j  d ƒ qŸ qÝ | j  d ƒ d  |  _ qºd  |  _ | j  d ƒ |  j
 ƒ  rº| j  d	 ƒ t |  _ qºnÚ t |  _ | j  d
 ƒ |  j ƒ  r| j  d ƒ n  |  j d k rº|  j ƒ  sŽ|  j d  k	 r·|  j |  j t k r‹| j  d i |  j |  j d 6|  j |  j d 6ƒ q‹q·qº| j  d ƒ |  j
 ƒ  rº| j  d ƒ qºn  d  S(   Ns   
%s
g      à?s–   The results of the L-test indicate that the intensity statistics
are significantly different than is expected from good to reasonable,
untwinned data.i    s  
As there are twin laws possible given the crystal symmetry, twinning could
be the reason for the departure of the intensity statistics from normality.
It might be worthwhile carrying out refinement with a twin specific target
function.

Please note however that R-factors from twinned refinement cannot be directly
compared to R-factors without twinning, as they will always be lower when a
twin law is used.  You should also use caution when interpreting the maps from
refinement, as they will have significantly more model bias.
s  
Note that the symmetry of the intensities suggest that the assumed space group
is too low. As twinning is however suspected, it is not immediately clear if
this is the case.  Careful reprocessing and (twin)refinement for all cases
might resolve this question.sl  
As there are no twin laws possible given the crystal symmetry, there could be
a number of reasons for the departure of the intensity statistics from
normality.  Overmerging pseudo-symmetric or twinned data, intensity to
amplitude conversion problems as well as bad data quality might be possible
reasons.  It could be worthwhile considering reprocessing the data.s€   The results of the L-test indicate that the intensity statistics show more
centric character than is expected for acentric data.sT   This behavior might be explained by the presence of the detected pseudo
translation.sn   The results of the L-test indicate that the intensity statistics behave as
expected. No twinning is suspected.sÂ   The symmetry of the lattice and intensity however suggests that the input
input space group is too low. See the relevant sections of the log file for
more details on your choice of space groups.sõ   The correlation between the intensities related by the twin law
%(twin_law)s with an estimated twin fraction of %(alpha)3.2f is most likely
due to an NCS axis parallel to the twin axis. This can be verified by
supplying calculated data as well.
R<   R°  s\   As the symmetry is suspected to be incorrect, it is advisable to reconsider
data processing.s  Note however that the presence of translational NCS (and possible rotational
pseudo symmetry parallel to the twin axis) can make the detection of twinning
difficult.  Trying various space groups and twinning hypotheses in structure
refinement might provide an answer.
(   R¬   Rï  Râ  RÅ  RÌ  RÏ  R,   t   twinning_shortRí  R´   Rê  R“   RÖ  R©  t   TWIN_FRAC_SIGNIFICANTRs   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   show_verdictÖ  sR    
			
	c      
   C   st  d „  } d  } g  } |  j d d  k r¸ d } x$t t |  j ƒ ƒ D]m } |  j | |  j | | |  j | ƒ | |  j | ƒ | |  j | ƒ | |  j	 | ƒ g } | j
 | ƒ qD Wn� d } x” t t |  j ƒ ƒ D]} } |  j | |  j | | |  j | ƒ | |  j | ƒ | |  j | ƒ | |  j | ƒ | |  j	 | ƒ g } | j
 | ƒ qÔ Wt j d | d | ƒ |  _ d  S(   Nc         S   s   t  d |  ƒ j ƒ  S(   Ns   %4.3f(   R   t   strip(   R0  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRL  0  s    i    R  R   s   R obs.s   Britton alphas   H alphas   ML alphas
   R_abs obs.s   R_abs calc.R{   R~   (   s   Operators   types   R obs.s   Britton alphas   H alphas   ML alpha(   s   Operators   types
   R_abs obs.s   R_abs calc.s   Britton alphas   H alphas   ML alpha(   R´   RÒ  Rì   Rª   Rs   RÐ  RÑ  R©  RÓ  RÔ  R˜   R   R™   Rš   (   R"   RL  Re  R†  t   itemRK   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   make_sym_op_table/  s8    	 

 

	c         C   sç   | j  d ƒ | j d ƒ |  j d | ƒ | j d ƒ | j d i |  j d 6|  j d 6|  j d 6|  j d	 6|  j d
 6|  j	 d 6ƒ | j
 d ƒ t |  j ƒ d k rÖ | j d ƒ |  j ƒ  | j |  j d d ƒn | j
 d ƒ d  S(   Ns)   Twinning and intensity statistics summarys   Final verdictRŸ   s#   Statistics independent of twin lawss—    <I^2>/<I>^2 : %(i_ratio)5.3f  (untwinned: 2.0, perfect twin: 1.5)
  <F>^2/<F^2> : %(f_ratio)5.3f  (untwinned: 0.785, perfect twin: 0.885)
  <|E^2-1|>   : %(e_sq_minus_1)5.3f  (untwinned: 0.736, perfect twin: 0.541)
  <|L|>       : %(l_mean)5.3f  (untwinned: 0.500; perfect twin: 0.375)
  <L^2>       : %(l_sq_mean)5.3f  (untwinned: 0.333; perfect twin: 0.200)
  Multivariate Z score L-test: %(maha_l)5.3f
RÉ  RÊ  RË  RÌ  RÍ  Râ  s¢  
 The multivariate Z score is a quality measure of the given spread in
 intensities. Good to reasonable data are expected to have a Z score lower
 than 3.5.  Large values can indicate twinning, but small values do not
 necessarily exclude it.  Note that the expected values for perfect twinning
 are for merohedrally twinned structures, and deviations from untwinned will
 be larger for perfect higher-order twinning.
i    s!   Statistics depending on twin lawsR¨   i   s-   
No (pseudo)merohedral twin laws were found.
(   R¡  R«   Rò  R¯   RÉ  RÊ  RË  RÌ  RÍ  Râ  R¬   Rª   Rs   Rõ  R®   Rš   (   R"   RŸ   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   O  s$    




c         C   s!   |  j  d  k	 r |  j |  j  Sd S(   Ni    (   RÖ  R´   R©  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   max_twin_fractionr  s    c         C   s{  g  } t  } |  j ƒ  r |  j d k r^ |  j ƒ  t k r^ | j d d d d d d f ƒ q
| j d d	 d
 d d f ƒ n‹ | j d d d d f ƒ |  j ƒ  t k rÐ | j d d d d d d f ƒ n  |  j ƒ  r
| j d d d d t |  j ƒ d f ƒ n  |  j	 d k r:| j d d d d d f ƒ n= |  j	 d k rj| j d d d d d f ƒ n | j d! ƒ | S("   Ni    i   s&   Intensity statistics suggest twinning s;   (intensities are significantly different from expected for s?   normal data) and one or more twin operators show a significant s   twin fraction.s!   Statistics depending on twin lawsi   s+   The intensity statistics look unusual, but s=   twinning is not indicated or not possible in the given space s   group.s   Wilson ratio and momentss1   The intensity statistics look normal, indicating s   that the data are not twinned.s.   One or more twin operators show a significant sA   twin fraction but since the intensity statistics do not indicate s:   twinning, you may have an NCS rotation axis parallel to a s   crystallographic axis.s0   One or more symmetry operators suggest that the s   data has a higher s   crystallographic symmetry (%s).s!   Point group and R-factor analysisiK   s3   Translational NCS is present at a level that might s?   be a result of a missed centering operation (one or more peaks s    greater than 75% of the origin).s   Patterson analysesi   s1   Translational NCS is present at a level that may sA   complicate refinement (one or more peaks greater than 20% of the s   origin)s0   Translational NCS does not appear to be present.(   i    s0   Translational NCS does not appear to be present.s   Patterson analyses(
   R“   Rë  RÏ  Rö  Rñ  R˜   Rí  RO   R×  RÇ  (   R"   t   issuest   bad_data(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   summarize_issuesw  sB    						N(   R)   R*   R´   R(   RÎ  Rê  Rë  Rì  Rí  Rï  Rò  Rõ  R³   Rö  Rù  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRÄ  J  s"   I							Y	 	#	t   twin_analysesc           B   sG   e  Z d  Z d d d e d d d d d d d „
 Z d „  Z d „  Z RS(   s"   Perform various twin related testsi   c   "      C   s$  | j  ƒ  s t ‚ | d  k r* t j } n  d |  _ d d d d g } t } d } d } |
 d  k	 rŠ |
 j j } |
 j	 j
 } |
 j	 j } n  | d  k rÈ t j | j ƒ  j ƒ  ƒ } | d } d | } n  | d  k rt j | j ƒ  j ƒ  ƒ } | d } d | } n  | d  k r$d d d g } n  t j d | ƒ |  _ t j d | ƒ |  _ | j t j d | ƒ t j d | ƒ ƒ } | j ƒ  j ƒ  d k r£t d	 ƒ ‚ n  | j ƒ  d  k rÄt d
 ƒ ‚ n  | } } | j ƒ  r| j ƒ  rõ| j ƒ  } q| j ƒ  } n t d ƒ ‚ |	 } } |	 d  k	 rP|	 j ƒ  rA|	 j ƒ  } qP|	 j ƒ  } n  t  d | d |  j ƒ } | |  _! t" d | d | d | d | ƒ |  _# d  |  _$ y" t% d | d | d | ƒ |  _$ Wn t k
 rÓn Xd  |  _& yV | j' ƒ  d  k	 r2d d l( m) } | j* d |  j# j+ d d d | d | ƒ |  _& n  Wn t k
 rFn X|  j# j, } |  j# j- } t. | | ƒ |  _. t/ d | d | ƒ |  _0 d  |  _1 |  j$ d  k	 rÚt1 d | d |  j$ j2 d |  j$ j3 d |  j$ j4 ƒ |  _1 n$ t1 d | d d d d d d ƒ |  _1 t5 | j6 ƒ |  _7 g  |  _8 g  |  _9 x™ t: |  j7 ƒ D]ˆ } | j6 | j; j< ƒ  j= ƒ  } |  j9 j> | ƒ t? d | j6 | d | d | d | d | d |  j# j- d | d  | ƒ } |  j8 j> | ƒ q2Wd  |  _@ d  |  _A |  j7 d k rÞtB d | d! |  j d | d | ƒ |  _@ |  j@ jC ƒ  \ } } } } tD jE d" | jF ƒ  d# | d$ t ƒ } tD jE d" | jF ƒ  d# | d$ t ƒ }  | | k r¡tG d | d% |  d | d& d' | d( t ƒ n  | | k rÞtG d | d% | d | d& d) | d( t ƒ }! qÞn  tH d* |  j0 d+ |  j. d, |  j1 d- |  j$ d. |  j8 d/ |  j@ ƒ |  _I d  S(0   Ng      @g{®Gáz´?iK   i   g      ô?g       @g      ð?i    s0   No suitable data available after resolution cutss   Observation type unknowns$   Observations should be a real array.R�   Rž   RË   RŸ   RÌ   iÿÿÿÿ(   t   absencest	   thresholdRt  t   original_dataRs  Rt  R�  R‘  R’  i   R<   R0  R<  R=  R>  Rv   R9   t   space_group_symbolRM  R/   t   txtsl   Merging in *highest possible* point group %s.
 ***** THIS MIGHT NOT BE THE BEST POINT GROUP SYMMETRY *****  t   check_absencess&   Merging in *suggested* point group %s RÚ  RÛ  R�  RÜ  RÝ  RF  (J   t   is_unique_set_under_symmetryRC   R´   R¸   R¹   Rv   R“   t   missing_symmetryRt  t   twinning_with_ncst   perform_analysesRÒ  R   R¤  t
   d_spacingsRÁ   Rd  RA   RV   RÝ   RÞ   RÜ   RÉ   RÊ   R   t   observation_typet   RuntimeErrorRÁ  R»   RÛ   R¼   Rù  Rs   t   possible_twin_lawsR¶   t   normalised_intensitiesRÜ  RÏ   t   abs_sg_analR¢  t   mmtbx.scalingRû  t   protein_space_group_choicesRÃ   RÈ   RÆ   RK  Rc  RÚ  R�  Rò   Ró   Rô   Rª   R‰   RÏ  t   twin_law_dependent_analysest   twin_law_namesRì   Rl   R6   Rq   R˜   R3  t   check_sgt   suggested_space_groupRF  Rž  R   Rá   R9   t!   merge_data_and_guess_space_groupsRÄ  t   twin_summary("   R"   R�   t   d_star_sq_low_limitt   d_star_sq_high_limitt   d_hkl_for_l_testRË   RŸ   t	   out_plotsRÌ   R0  t   additional_parametersRý  t   symm_issue_tablet   perform_ncs_analysesR>  Rt  t   f_obst   i_obst   f_calct   i_calcR  Rû  t   centric_cutt   acentric_cutRJ  t   twin_law_namet   tmp_twin_law_stufft   nig_datat   pg_this_oneRc  RX  t	   xs_choicet   xs_highR  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR(   ¡  s    	



		 		 											


					c         C   sV  |  j  r |  j  j | ƒ n  | j d ƒ | j d |  j |  j f ƒ |  j d  k	 rh |  j j | ƒ n  |  j j | ƒ |  j j | ƒ |  j	 d  k	 rª |  j	 j | ƒ n  | j d ƒ |  j
 j d | ƒ |  j d k r?| j d ƒ | j d ƒ x! |  j D] } | j d | ƒ qý W|  j d  k	 r?|  j j d | ƒ q?n  |  j j d | ƒ d  S(   Ns0   Diagnostic tests for twinning and pseudosymmetrys+   Using data between %4.2f to %4.2f Angstrom.s	   Twin lawsRŸ   i    s   Twin law-specific testss|   The following tests analyze the input data with each of the possible twin
 laws applied.  If twinning is present, the most appropriate twin law will
 usually have a low R_abs_twin value and a consistent estimate of the twin
 fraction (significantly above 0) from each test.  The results are also
 compiled in the summary section.

 WARNING: please remember that the possibility of twin laws, and the results
 of the specific tests, does not guarantee that twinning is actually present
 in the data.  Only the presence of abnormal intensity statistics (as judged
 by the Wilson moments, NZ-test, and L-test) is diagnostic for twinning.
(   R
  R¬   R¡  RÝ   RÞ   RÜ  R´   RK  RÚ  R�  R  RÏ  R«   R  R  R  (   R"   RŸ   t
   twin_tests(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR³   Z	  s,    	c         C   sg   |  j  d k	 r- d |  j  _ d |  j  j _ n  |  j d k	 rW d |  j _ d |  j _ n  d |  _ |  j S(   sB   
    Pickling function with storage efficiency optimizations.
    N(   R
  R´   R�   t   absences_tableR  RZ  R	  RÏ  (   R"   (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRÐ  ƒ	  s    	N(   R)   R*   Rk   R´   R,   R(   R³   RÐ  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyRú  Ÿ  s   ®	)g      ð?c      	   C   sŸ  |  j  ƒ  } | d  k r' | j ƒ  } n  |  j d | ƒ } | j | j ƒ  j ƒ  ƒ j ƒ  } | j ƒ  } | j	 ƒ  } t
 j | d t ƒ}	 |	 j j  ƒ  }
 d  } | j ƒ  d  k	 r•| r•| J| J| d t | ƒ IJ| | IJ| d t | ƒ IJ| J| j d | ƒ | J| d IJ| J| Jt } yV |  j ƒ  d  k	 rsd d l m } | j d |
 d	 d
 d t d | ƒ j | ƒ } n  Wq•t k
 r‘| d IJq•Xn  | | f S(   NR�   R·   R4  RŸ   sR   Suggesting various space group choices on the basis of systematic absence analysesiÿÿÿÿ(   Rû  R�   Rü  g      @t	   print_allRt  s"   Systematic absence analyses failed(   R½   R´   R�   R)  R0   R†   R  t   merge_equivalentst   arrayt   r_linearR   R¾   R,   R¿   R¢  Rª   R£  R“   R  Rû  R  R¬   R   (   R�   Rÿ  R/   RŸ   Rt  R   t   tmp_mat	   merge_objt   r_linRÎ   RÍ   R
  t   this_workedRû  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyR  �	  sF    

	g      @c         C   sÖ   | d k r t ƒ  } n  |  j ƒ  j ƒ  r. d S|  j d | ƒ } |  j ƒ  s‚ | j d | j ƒ  d | j ƒ  d d ƒ j	 | ƒ } n  t
 d | d d d	 d d d t ƒ  d t ƒ  d t ƒ } | j } | j d | ƒ | S(   sd   
  Detect translational pseudosymmetry and twinning.  Returns a
  twin_law_interpretation object.
  RÞ   RÉ   RÁ   R¢  R�   R  g      ð?g      Y@R  gü©ñÒMbP?g       @RŸ   R  RÌ   Ng{®Gáz„?g�íµ ÷Æ°>(   R´   R   RŽ   Rº   RÜ   t   sigmas_are_sensibleR)  RÉ   RÁ   RÂ   Rú  R“   R  R¬   (   R"   RÞ   Ru   Rÿ  t   twin_resultst   summary(    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   analyze_intensity_statistics½	  s(     	
				i    c         C   sF   | d k r$ | d k r$ t j } n  |  j d | d | ƒ } | j ƒ  S(   sž  
  A very brief twin analyses and tries to answer the question whether or
  not the data are twinned.
  possible outputs and the meaning:
  - False: data are not twinned
  - True : data do not behave as expected. One possible explanantion
           is twinning
  - None : data do not behave as expected, and might or might not be
           due to twinning.
           Also gives none when something messes up.
  i    RÞ   Ru   N(   R´   R¸   R¹   R3  Rì  (   R�   t   cut_offRŸ   RÌ   R2  (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   twin_analyses_briefÖ	  s
    	(A   t
   __future__R    R  R   t   mmtbxR   t   iotbxR   t   cctbx.sgtbxR   t   cctbx.array_familyR   t   cctbx.sgtbx.lattice_symmetryRh  R   t   cctbx.sgtbx.cosetsR   R   t
   cctbx.xrayt   scitbx.mathRå  R	   t   libtbxR
   t   libtbx.str_utilsR   t   libtbx.utilsR   R   R   t	   cStringIOR   RA   R¸   Rñ  t   PATT_HEIGHT_MISSED_CENTERINGt   PATT_HEIGHT_TNCSR   t   objectR-   R<   t   xtriage_analysisRs   Rµ   R¶   RÏ   RK  Rc  R�  RŸ  R¿  RÑ  R
  R  R  R3  RF  R9  RÄ  Rú  R´   R,   R  R3  R5  (    (    (    sI   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/scaling/twin_analyses.pyt   <module>   sh   N�	$ÿ !Hh[dI�G	Bgû‚ÿ Vñ,