ó
\™xTc           @   sÚ   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 e
 f d „  ƒ  YZ d e
 f d	 „  ƒ  YZ d
 d e e d „ Z d
 d e e d „ Z d „  Z d „  Z e d k rÖ e ƒ  e ƒ  d GHn  d S(   iÿÿÿÿ(   t   division(   t   differential_evolution(   t   flexN(   t   approx_equalt   curve_interpolatorc           B   s/   e  Z d  d „ Z d „  Z d „  Z d „  Z RS(   id   c         C   s�   | |  _  | |  _ | |  _ t j t | ƒ ƒ t | d ƒ |  _ |  j |  j |  j  |  j  |  _ |  j d |  j d |  _ d  S(   Ni   i    (	   t   startt   stopt   n_pointsR   t   doublet   ranget   floatt   target_xt   delta(   t   selfR   R   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   __init__   s    			&!c         C   sS  g  } g  } d  } d  } d  } d  } t j | ƒ }	 t j | ƒ }
 xºt |  j ƒ D]©\ } } d  } t } xˆ t | ƒ D]z \ } } | | d k rw | |	 k rñ | |
 k rî | } | d  k rÆ | } n  | d  k rÛ | } n  | } | } Pqî qñ qw qw W| j | ƒ d  } | d  k	 rR | d k rV|  j | | | | | | | d | | d ƒ } n• | t	 | ƒ d k r¢|  j | | | d | | d | | | | ƒ } nI |  j
 | | | d | | d | | | | | | d | | d ƒ } | j | ƒ qR qR Wt	 | ƒ } t j |  j | | d !ƒ } t j | ƒ } | | | | f | | f f S(   Ni    i   (   t   NoneR   t   mint   maxt	   enumerateR   t   Falset   appendt   two_point_interpolatet   lent   parabolic_interpolateR   (   R   t   x_arrayt   y_arrayt   index_arrayt   result_arrayt   start_index_usert   end_index_usert   start_index_targett   end_index_targett   user_mint   user_maxt   jjt   xt
   this_indext   break_againt   indext   this_xt   yt   n(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   interpolate   sV    					c   
      C   sM   | | } | | } | } | d k r3 | | } n  d | | | | }	 |	 S(   Ni    i   (    (
   R   R#   t   xot   fxot   xpt   fxpt   pht   ht   pt   result(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyR   F   s    

c   	      C   sr   | | | | | | | | | | | | | | | | | | | | | | | | | | | } | S(   N(    (	   R   R#   t   xmt   fxmR+   R,   R-   R.   R2   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyR   O   s    G'(   t   __name__t
   __module__R   R*   R   R   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyR      s   	6		t   linear_scalerc           B   sk   e  Z d  Z d d d d d d d e d e d „
 Z d „  Z d	 „  Z d
 „  Z	 d „  Z
 d „  Z d „  Z RS(   s\   This class scales together varios curves. means and varainces should be lists of flex arraysi    g      ð?gš™™™™™¹?i2   gffffffæ?gê-�™—q=c         C   sø   d  |  _ |  j d  k r' t j |  _ n  | |  _ | |  _ | |  _ | |  _ t |  j ƒ |  _	 |  j
 ƒ  |  _ t |  j ƒ d d |  _ d  |  _ | | | | f g |  j |  _ t j |  d |  j | d |
 d | d | d d d d	 d
 | ƒ|  _ d  S(   Ni   i   t   population_sizet   show_progresst   epst   ft   n_crosst   crgš™™™™™é?t   insert_solution_vector(   R   t   outt   syst   stdoutt   addt   meanst   varst   refR   t   n_setst   setup_coeff_mapt   mapR)   R#   t   domaint   det    differential_evolution_optimizert	   optimizer(   R   RC   t	   variancest   reference_idt	   init_meant   spreadt   factorR;   R:   R?   R9   R>   RB   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyR   _   s    						"c         C   s`   g  } d } xM t  |  j ƒ D]< } | |  j k rK | j | ƒ | d 7} q | j d ƒ q W| S(   s>    ii: datset number ; result[ii]=associated scale_factor index i    i   N(   t   xrangeRF   RE   R   R   (   R   R2   t   countt   ii(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyRG   q   s    c         C   sÆ   |  j  d k r´ |  j |  j d } |  j |  j d } xl t |  j |  j | | ƒ D]O \ } } } } | | | }	 d t j | ƒ d }
 | |
 7} | |	 |
 } qS W| | } | S|  j |  j Sd  S(   Ni   i    i   g:Œ0âŽyE>(   RF   RC   RE   t   zipRD   R   t   sqrt(   R   t   scalest   offsetsR2   t   weightst   mt   vt   st   ot   mmt   w(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   get_mean}   s    .

c         C   s­   g  } g  } x” t  |  j ƒ D]ƒ } d } d } | |  j k r… t | |  j | ƒ } | |  j | |  j d } |  j s… d } q… n  | j | ƒ | j | ƒ q W| | f S(   Ng      ð?g        i   (   RR   RF   RE   t   absRH   RB   R   (   R   t   vectorRW   RX   t   scalet	   tmp_scalet
   tmp_offset(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   get_scales_offsets‹   s    	c         C   s  |  j  | ƒ \ } } |  j | | ƒ } d } xå t |  j ƒ D]Ô } | | |  j | | | } |  j | | | | | } t j | | d ƒ d | }	 |  j d k rÀ | t j |	 ƒ 7} q= | |  j	 k r= |  j |  j	 }
 | t j t j | | d ƒ d | |
 ƒ 7} q= q= W| S(   Ni    i   g‚vIhÂ%<=(
   Rf   R`   RR   RF   RC   RD   R   t   powt   sumRE   (   R   Rb   RW   RX   t   drR2   R"   t   djt   vjt   tt   vr(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   targetš   s    6c         C   s`   |  j  | ƒ \ } } |  j d IJ|  j | I| I| IJ|  j | IJ|  j | IJ|  j Jd  S(   Nt   PROGRESS(   Rf   R?   (   R   t   bestt   meanRb   RS   RW   RX   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   print_statusª   s    c         C   s(   |  j  |  j ƒ \ } } | | |  j f S(   N(   Rf   R#   RH   (   R   RW   RX   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   retrieve_results³   s    N(   R5   R6   t   __doc__R   R   t   TrueR   RG   R`   Rf   Rn   Rr   Rs   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyR7   ]   s   '						i    i
   c   	   
   C   sC   t  |  | | d | d | d | ƒ} | j ƒ  \ } } }  | | f S(   NRQ   R9   RB   (   R7   Rs   (	   RZ   R[   t   ref_idRQ   R9   RB   t   scaler_objectR\   R]   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   scale_it_global·   s    $c         C   så   t  |  ƒ } g  } g  } xÀ t | ƒ D]² }	 |	 | k r½ |  | |  |	 g }
 |  | |  |	 g } t |
 | d d | d | d | ƒ} | j ƒ  \ } } } | j | d ƒ | j | d ƒ q% | j d ƒ | j d ƒ q% W| | f S(   Ni    RQ   R9   RB   i   g      ð?g        (   R   RR   R7   Rs   R   (   RZ   R[   Rv   RQ   R9   RB   R)   RW   t   ofsetsRT   t   mst   vsRw   R\   R]   RH   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   scale_it_pairwise¼   s    $c          C   së  t  j d ƒ t  j t d ƒ ƒ d }  t  j |  ƒ } | } | d d } | d d } | d d	 } | d d
 } | } t | | | g | | | g d d d d t ƒ} | j ƒ  \ }	 }
 } t |	 d	 d d d ƒsÞ t	 ‚ t |	 d d d d ƒsý t	 ‚ t |
 d	 d d d ƒst	 ‚ t |
 d d d d ƒs;t	 ‚ t
 | | | g | | | g d d t ƒ\ }	 }
 t |	 d	 d d d ƒsŠt	 ‚ t |	 d d d d ƒs©t	 ‚ t |
 d	 d d d ƒsÈt	 ‚ t |
 d d d d ƒsçt	 ‚ d  S(   Ni90  i
   g      $@id   i2   iè  iô  i    i   g      ð?RQ   i   R9   g{®Gáz„?R:   gü©ñÒMbP?i   iÎÿÿÿiþÿÿ(   R   t   set_random_seedR   R	   t   expR7   R   Rs   R   t   AssertionErrorR|   (   R#   R(   t   y0t   y1t   y2t   v0t   v1t   v2Rw   R\   R]   RZ   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   test_curve_scalerÍ   s(    00c          C   s'  t  j t d ƒ ƒ d }  |  |  } t d d d ƒ } | j } | | } | j |  | ƒ \ } } } } d }	 x0 | D]( }
 t  j |  ƒ |
 k rs |	 d 7}	 qs qs W|	 t | ƒ k s· t ‚ x8 t	 | | ƒ D]' \ } } t
 | | d d ƒsÇ t ‚ qÇ W| d d k st ‚ | d d	 k st ‚ | d d k s4t ‚ | d d k sJt ‚ t  j t d d ƒ ƒ d }  |  |  } t d d d ƒ } | j |  | ƒ \ } } } } | d t  j |  ƒ k s¿t ‚ | d t  j |  ƒ k sÞt ‚ | | } x8 t	 | | ƒ D]' \ } } t
 | | d d ƒsøt ‚ qøWd  S(   Ni   g      8@i    g       @iÈ   i   R:   gü©ñÒMbP?i   ic   id   i   i   iÿÿÿÿ(   ic   id   (   R   R   R	   R   R   R*   R   R   R   RU   R   R   (   R#   R(   t   ipt   x_targett   y_reft   nxt   nyt   at   bRS   t   xxt   yyt   yyy(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   tst_curve_interpolatorç   s4    
	


t   __main__t   OK(   t
   __future__R    t   scitbxR   RJ   t   scitbx.array_familyR   R@   t   libtbx.test_utilsR   t   objectR   R7   R   Ru   Rx   R|   R†   R‘   R5   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/scale_curves.pyt   <module>   s   VZ		!