ó
î™xTc           @   s/  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	 d  d l
 m Z d  d l m Z d  d	 l m Z d  d l j Z d  d
 l m Z d  d l m Z d e d d „ Z d e d d „ Z e d d „ Z d „  Z d „  Z d „  Z e d k r+e ƒ  n  d S(   iÿÿÿÿ(   t   divisionN(   t   miller(   t   maptbx(   t   random_structure(   t   debug_utils(   t   flex(   t   euclidean_model_matching(   t
   group_args(   t   StringIO(   t   charge_flippingg      ð?i   t   Fc   	      C   sY   t  j d | d | d t d t d d d t ƒ } t | |  | | d | d	 | d
 | ƒd  S(   Nt   space_group_infot   elementst	   use_u_isot   random_u_isot   random_u_iso_scaleg{®Gáz¤?t   use_u_anisot   grid_resolution_factort   verboset   amplitude_type(   R   t   xray_structuret   Truet   Falset   exercise_one_structure(	   t   flipping_typeR   R   t   anomalous_flagt   d_minR   R   R   t   target_structure(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   randomly_exercise   s    			c      	      sô  | d+ k s t  ‚ t j d ˆ d ˆ j ƒ  j ƒ  d k d | ƒ j d ˆ d	 d
 ƒ j ƒ  ‰  ˆ  j ƒ  j ƒ  j	 ƒ  j
 ƒ  } ˆ  j ƒ  } | d d  ƒ }	 t ƒ  }
 | d k r½ ‡ f d †  |
 _ n | d k rØ t j |
 _ n  t j |	 | d t d d |
 j �} t ƒ  } t j | d d d | ƒ| r2| j ƒ  GHn  | j sAt  ‚ | j }	 | j j } ˆ j ˆ j ƒ  d k d t ƒ‰ ˆ j ƒ  } t j d t d d d t | j ƒ  j ƒ  d ƒ ƒ } |	 j j  | ƒ } t! j" | j# ƒ  j$ ƒ  d g  t% | j& ƒ  j' ƒ  ƒ D]" \ } } t! j( d | | ƒ ^ qúƒ} t! j) | j* ƒ  | d d d t+ ƒj, } | d } | j- d k  stt  | j- ƒ ‚ | j. j/ t0 j1 d  ƒ t0 j2 d  ƒ f k s¤t  ‚ | d j. j3 } ‡  f d! †  } d  } x„ t% | j4 ƒ D]s \ } \ } } } | | | ƒ s| | | ƒ r| } PqÚ| d" k rÚd# t5 | ƒ | f GHd$ t5 | ƒ GHqÚqÚW| d  k	 sct  ‚ | r‡| d k r‡d% | | f GHn  t j d t d d d t d& ˆ j ƒ  j ƒ  ƒ ƒ } | j6 d' t j7 ƒ } | j  | d( t+ ƒ} t! j" ˆ j# ƒ  j$ ƒ  d g  t% | j& ƒ  j' ƒ  ƒ D]" \ } } t! j( d | | ƒ ^ qƒ} t! j) ˆ j* ƒ  | d t+ ƒj, } | snt  ‚ | d } | j8 s”t  | j9 ƒ  ƒ ‚ | j- d) k  s²t  | j- ƒ ‚ | j. j/ t0 j1 d  ƒ t0 j2 d  ƒ f k sât  ‚ | rðd* GHn  d  S(,   NR
   t   Es   quasi-Et   crystal_symmetryR   i    R   R   t	   algorithmt   directt   deltac            s   |  j  ˆ  j d d ƒ ƒ S(   Nt   omitt   Ht   D(   R#   R$   (   t   amplitude_normalisationst   unit_cell_content(   t   f(   R   (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   <lambda>I   s   t   yield_during_delta_guessingt   yield_solving_intervali   R   t   highlyt   outR#   t   negatet   interpolatet   min_distance_sym_equivg      ð?t   max_clustersg333333ó?t	   positionss   Q%it	   toleranceg      à?t   break_if_match_with_no_singlesgš™™™™™É?i   c            s   ˆ  j  ƒ  j |  d d ƒS(   NR2   gš™™™™™¹?(   R   t   is_allowed_origin_shift(   t   x(   t   f_target(    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyR(   {   s   t   mores8   ++ Incorrect peak: shift=(%.3f, %.3f, %.3f), height=%.2fs%      Reference shift=(%.3f, %.3f, %.3f)s0   ** First correct correlation peak: #%i (%.3f) **g      ø?t   symmetry_flagst   verify_symmetryg333333Ã?s   @@ Success @@(   R
   R   s   quasi-E(:   t   AssertionErrorR   t	   build_sett
   scattererst   count_anomaloust!   structure_factors_from_scattererst   f_calct   expand_to_p1t   as_non_anomalous_arrayt   merge_equivalentst   arrayt   as_amplitude_arrayt   NoneR   t   normalisations_forR	   t   amplitude_quasi_normalisationst   solving_iteratorR   t   __dict__R   t   loopt   getvaluet   had_phase_transitiont   flipping_iteratort   selectt   scattering_typesR   t   peak_search_parameterst   intt   sizet   rho_mapt   peak_searcht   emmat   modelR   t   special_position_settingst	   enumeratet   allt   sitest   positiont   model_matchest   as_emma_modelR   t   refined_matchest   rmst   rtt   rt   matt   identityt	   inversiont   tt   f_calc_solutionst   tuplet   fft_mapt   use_space_group_symmetryt   singles1t   show(   R   R   R   R   R   R   R   t   f_target_in_p1t   f_obst   flippingt   extrat   solvingt   st   f_result_in_p1t   target_structure_in_p1t   search_parameterst   peak_search_outcomet   iR5   t   peak_structureR^   t   mt   reference_shiftt
   is_allowedt   first_correct_correlation_peakR?   t   shiftt   cc_peak_heightt   solution_fft_mapt   solution_peakst   solution_peak_structure(    (   R6   R   sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyR   .   s°    					A	
0			A	
0s   quasi-Ec         C   sJ   d d l  m } | j ƒ  } d GHt | |  | | d d	 d | d | ƒd  S(
   Niÿÿÿÿ(   t   developmentt   SucroseR   i   i   R   R   g      à?(   t   smtbxR�   t   sucroseR   (   R   R   R   R   R   R�   R   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   exercise_sucroseª   s    c         C   sÁ  |  j  s d |  _  n  |  j s* d |  _ n  |  j s? d |  _ n  |  j re t j d ƒ t j d ƒ n  t | j	 ƒ  ƒ } | j
 ƒ  j ƒ  G|  j r³ | d k r³ d GH|  j r¯ Hn  d  SHd | pÁ d } d | } d | } |  j r)d	 | | | | | | f GHd
 | | | f GHd |  j |  j f GHn  t d |  j ƒ } xq t t |  j  ƒ ƒ D]Z } t d | d | d d g | d g | d g | d t d d d |  j d |  j ƒ qRW|  j r½Hn  d  S(   Ni   t   weak_reflection_improvedR   i   s     [ skipped ]i   i   i   s   unit cell content: C%i O%i N%is   asu content: C%i O%i N%is   on %s's with %ss   charge_flipping.%s_iteratorR   R   R   t   Ct   Ot   NR   R   gš™™™™™é?R   R   (   t   repeatst   algot   ont   fix_seedt   randomt   seedR   t   set_random_seedt   lent   groupt   typet   hall_symbolt   high_symmetryt   Verboset   evalt   xrangeRQ   R   R   (   t   flagsR   t   nt   n_Ct   n_Ot   n_NR   Rv   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   exercise¼   sH    	 	 	 		 

	#		 c       	   C   s3   d d  l  }  t j |  j d t d d
 d	 t ƒd  S(   Niÿÿÿÿi   t   keywordsRŠ   RŒ   R‹   R�   R•   t   symbols_to_stderr(   s   repeatss   ons   algos   fix_seeds   high_symmetry(   t   sysR   t   parse_options_loop_space_groupst   argvRž   R   (   R¡   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   exercise_charge_flippingà   s    
c           C   s   t  ƒ  d  S(   N(   R¤   (    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   runì   s    t   __main__g      à?g      à?(   t
   __future__R    R¡   RŽ   t   cctbxR   R   t   cctbx.developmentR   R   t   cctbx.array_familyR   R   RU   t   libtbxR   t   scitbx.matrixt   matrixRb   t	   cStringIOR   t   smtbx.ab_initioR	   R   R   R   R…   Rž   R¤   R¥   t   __name__(    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/tests/tst_charge_flipping.pyt   <module>   s2   z	$		