ó
î™xTc           @   s6   d  d l  m Z d  d l m Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   division(   t   flexc         C   s²  |  j  ƒ  j ƒ  } t j | ƒ } | | } t j | ƒ } | j | ƒ } t | ƒ } | d d | d } } t j | | ƒ }	 | | | !}
 t j |	 |
 ƒ } | j	 ƒ  r½ | j
 ƒ  d k sÃ t ‚ t j |	 |
 ƒ } | j ƒ  | j ƒ  } } t j | | t j | ƒ | ƒ } | | k } t j | t ƒ } t j | t ƒ } | | | !j t ƒ | | d d k spt ‚ | | d k  r�| | d k s–t ‚ t | | | | ƒ | f S(   sF    Find delta as hinted on fig. 1 of ref. [1] in module charge_flipping i   i   g®Gáz®ï?i   i    (   t   real_map_unpaddedt   as_1dR   t   maxt   sort_permutationt   selectt   lent   double_ranget   linear_correlationt   is_well_definedt   coefficientt   AssertionErrort   linear_regressiont   y_interceptt   slopet   abst   first_indext   Falset
   last_indext   countt   min(   t   rho_mapt   tolt   rhot   max_rhot   sortingt
   sorted_rhot   nt   pt   qt   indexest   valuest   ct   rt   at   bt	   deviationt   non_linear_selt   lowt   high(    (    sg   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/development/electron_density_distribution.pyt
   find_delta   s(    
$$.&c         C   s¦   t  j |  j ƒ  ƒ } | j t  j | ƒ ƒ } | t  j | ƒ } t t j j	 | ƒ d ƒ } | d IJx | D] }  | d |  IJql W| d IJ| d IJ| j
 ƒ  d S(   s7    To obtain fig. 1 in ref [2] in module charge_flipping t   ws   fp1 = {s   %f, s   1 };s   ListPlot[fp1]N(   R   R   t   dataR   R   R   t   opent   ost   patht
   expandusert   close(   t   ft   filenamet   abs_ft   sortedt   mf(    (    sg   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/development/electron_density_distribution.pyt'   write_sorted_moduli_as_mathematica_plot   s    


N(   t
   __future__R    t   scitbx.array_familyR   R)   R6   (    (    (    sg   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/ab_initio/development/electron_density_distribution.pyt   <module>   s   	