ó
G™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 Z d  d	 l m Z d  d
 l m Z d  d l m Z d  d l Z d  d l Z d  d l Z d „  Z d „  Z d „  Z d d d d d „ Z d „  Z d „  Z e d k r(e ƒ  n  d S(   iÿÿÿÿ(   t   division(   t   approx_equal(   t   dmtbx(   t   maptbx(   t   miller(   t   fftpack(   t   complex_mathN(   t   flex(   t   random_structure(   t   debug_utilsc         C   sÐ  t  j t  j j |  ƒ j ƒ  ƒ j ƒ  } | d  k rB |  } |  } n |  j | ƒ } |  j | ƒ } t j	 g  | D] } | d d ^ qn ƒ } t
 } t j j d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d | j ƒ  d t j | j ƒ  ƒ d	 | ƒ } | j | j ƒ  ƒ }	 t j |	 ƒ }
 | j |
 ƒ } t } t j j d | j ƒ  d | j ƒ  d
 | d	 | d | ƒ } | d  k r| j ƒ  S|  j ƒ  j ƒ  } | j | | j ƒ  ƒ | j | ƒ j | j ƒ  ƒ sÌt ‚ | S(   Ni   i   t   space_groupt   anomalous_flagt   miller_indicest   structure_factorst   n_realt   map_gridt   conjugate_flagt   complex_mapt    allow_miller_indices_outside_map(   R   t
   index_spant   sett   expand_to_p1t   indicesR   t   Nonet   selectR   t   real_to_complex_3dt   TrueR   R   t   to_mapR
   R   t   dataR   R   t   gridt	   n_complext   backwardR   t   pow2t   forwardt   Falset   from_mapt	   deep_copyt   set_selectedt   all_eqt   AssertionError(   t   startt   selection_fixedt   map_griddingt   fixedt   vart   nt   rfftR   t   structure_factor_mapt   real_mapt   squared_mapt   squared_sf_mapR   R#   t   result(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   direct_space_squaring   sB    !	*		
$c   	   
   C   s:  t  j d |  ƒ } d s | r� xn t |  j ƒ  d  j ƒ  ƒ D]M } xD | j | ƒ D]3 } | j |  j ƒ  | ƒ G| j | ƒ sƒ d Gn  HqQ Wq; Wn  t |  ƒ j	 ƒ  } | d  k	 rÁ | j | d ƒ n  t j |  j	 ƒ  ƒ } | j d | d | d | d | d  k	 ƒ } | d  k	 r6| j | ƒ j | j | ƒ ƒ s6t ‚ n  | S(	   Nt
   miller_seti    i   s   not sigma-2t
   amplitudest
   phases_radR)   t   use_fixed_only(   R   t   triplet_generatort   xrangeR   t   sizet   relations_fort   formatt
   is_sigma_2t   absR   R   R%   R   t   argt   apply_tangent_formulaR   R&   R'   (	   R(   R)   t   verboset   tprst   iht   relationR6   t   input_phasesR3   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   reciprocal_space_squaring4   s*    #	c      
   C   s0  t  j d |  d t ƒ } t  j d |  d |  j ƒ  d d d t ƒ } | j ƒ  j | j ƒ  ƒ sc t ‚ xe d D]] } t  j d |  d |  j ƒ  d | d t ƒ } | j ƒ  | k j | j ƒ  | k ƒ sj t ‚ qj Wd
 } t  j d |  d |  j ƒ  d | d t ƒ } | j ƒ  } | j ƒ  } |  j ƒ  } x
t |  j ƒ  j	 ƒ  ƒ D]ð } | | | | k rXq8n  t
 j ƒ  } x< | j | ƒ D]+ }	 | j | |	 j ƒ  | |	 j ƒ  ƒ qtWt
 j ƒ  }
 x< | j | ƒ D]+ }	 |
 j | |	 j ƒ  | |	 j ƒ  ƒ q¿W| j t
 j d | d t ƒ ƒ } t | |  |
 ƒ s8t ‚ q8Wd  S(   NR5   t   discard_weightsR6   t   max_relations_per_reflectioni    i   i
   id   iè  i   R   t   reverse(   i   i
   id   iè  (   R   R9   R   R   t   n_relationsR&   R'   R:   R   R;   R   t   doubleR<   t   appendt   ikt   ihmkR   t   sort_permutationR   (   t   q_larget	   tprs_fullRC   R-   t
   n_rel_fullt   n_relt   ampRD   t   aa_fullRE   t   aa(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   exercise_truncateK   sH    				!		1		 ))!i   i   i    g      ø?c         C   sÔ  t  j |  d d g | d d d d d t d d	 ƒ} d
 s@ | rS | j ƒ  j ƒ  n  | j d | d t ƒ j ƒ  } t | ƒ } t	 j
 d | d | j ƒ  t j | j ƒ  j ƒ  | ƒ | j ƒ  j ƒ  ƒ } | j d d ƒ } | j d t ƒ | j ƒ  }	 t j | j ƒ  |	 j ƒ  ƒ }
 d
 s | r-|
 j ƒ  n  |
 j ƒ  s?t ‚ t |
 j ƒ  ƒ d k  s]t ‚ t |
 j ƒ  d ƒ d k  st ‚ | j | j ƒ  j ƒ  | k ƒ } d
 s¬| rÄd | | j ƒ  f GHn  t  j |  d d g | d d d d d t d d	 ƒ} | j ƒ  j | j ƒ  ƒ st ‚ | j | d d ƒj ƒ  } | j  | j ƒ  ƒ } xzd  t j" g  t# | j ƒ  ƒ D] } t$ j$ ƒ  ^ qhƒ d k  f D]8} t% | | ƒ } | j  d | ƒ } t& | | | ƒ } | j  d t j' d | ƒ ƒ } | j( | ƒ } d
 sü| rd | G| j) ƒ  GHn  x­ t* | j+ ƒ  ƒ D]™ \ } } t, j- | j ƒ  | d t ƒ\ } } t, j- | j ƒ  | d t ƒ\ } } t. j j/ | | d t ƒ} | d k  s%t | | ƒ d k  s%t ‚ q%WqŠWt0 | ƒ d  S(   Nt   elementst   constt   volume_per_atomiÈ   t   min_distanceg      @t   general_positions_onlyt   u_isog        i    t   d_minR   R5   R   t   by_valueR?   t   auto_binninggš™™™™™¹?i   gš™™™™™É?s   Number of e-values > %.6g: %dt	   algorithmt   directgš™™™™™Ù?t   phase_sources
   mwpe: %.2ft   degg      ð?g�íµ ÷Æ°>(1   R   t   xray_structureR   t   show_summaryt   show_scatterersR   R"   t   f_calcR?   R   t   arrayR   t   matht   sqrtR
   t   order_pt   n_ltrt   sortt   setup_binnert!   quasi_normalize_structure_factorsR   t   linear_regressiont   is_well_definedR'   t   y_interceptt   slopeR   t   quasi_normalized_as_normalizedR;   t	   unit_cellt   is_similar_tot!   structure_factors_from_scattererst   phase_transferR   RL   R:   t   randomR4   RG   t   polart   mean_weighted_phase_errort   space_group_infot	   enumerateR   R   t   abs_argt   scitbxt   phase_errorRX   (   R~   t   n_scatterersR_   RB   t   e_mint	   structureRi   t   f_obst   q_obst   n_obst   rRQ   t   other_structuret   q_calcR(   t   iR)   t   from_map_datat   direct_space_resultt
   new_phasest   reciprocal_space_resultt   mwpet   ht   amp_dt   phi_dt   amp_rt   phi_rt	   phase_err(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   exercisep   sv    			<"	!A		0c         C   s   t  | d |  j ƒd  S(   NRB   (   R˜   t   Verbose(   t   flagsR~   (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   run_call_back°   s    c           C   s   t  j t j d t ƒ d  S(   Ni   (   R	   t   parse_options_loop_space_groupst   syst   argvR›   (    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   run³   s    t   __main__(   t
   __future__R    t   libtbx.test_utilsR   t   cctbxR   R   R   R�   R   t   libtbxR   t   scitbx.matht   cctbx.array_familyR   t   cctbx.developmentR   R	   R{   Rk   R�   R4   RG   RX   R˜   R›   RŸ   t   __name__(    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_triplet_generator.pyt   <module>   s,   	$		%	?		