ó
Z™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 „  Z	 d d d	 „ Z
 d d
 e d „ Z e d k r¡ e	 ƒ  e
 ƒ  e d ƒ n  d S(   iÿÿÿÿ(   t   division(   t   flex(   t   chebyshev_polynome(   t   chebyshev_lsq_fit(   t   StringIOc          C   s  t  j t d ƒ ƒ d d }  t  j |  d d ƒ t  j |  ƒ } | t  j d |  j ƒ  ƒ d d } t  j |  j ƒ  d ƒ } d	 GHd
 GHHt j |  | | d d d d d d d d ƒ} d G| Gd GHHt j | |  | ƒ } d | j	 GHd | j	 t  j
 | | ƒ GHHt | | j | j | j ƒ } | j	 |  ƒ } t  j t  j | | ƒ ƒ } d GHd | GHt  j t  j | | ƒ ƒ } d GHd | GHHt  j t  j | | ƒ ƒ } d GHd | GHt  j t  j | | ƒ ƒ } d GHd | GHHd GHd GHxI t d ƒ D]; }	 d |  |	 d | |	 d | |	 d | |	 d f GHqðWy d d  l m }
 Wn t k
 rVn¨ Xd! GHd" GH|
 j d# d$ d% d& d' d( d) |  d* | d+ d, d- d. ƒ } | j d* | d+ d/ ƒ | j d* | d+ d0 ƒ t d1 d2 ƒ } t ƒ  } |
 j | | ƒ | j | j ƒ  ƒ d  S(3   Nid   g      ð?g     @Y@g      @goƒÀÊ!	@t   sizeg      à?i   s-   Trying to determine the best number of terms s     via cross validation techniquest	   min_termsi   t	   max_termsi   t   n_goest   n_frees   Fitting witht   termss   Least Squares residual: %7.6fs     R2-value            : %7.6fs5   Maximum deviation between fitted and error free data:s	       %4.3fs2   Mean deviation between fitted and error free data:s3   Maximum deviation between fitted and observed data:s0   Mean deviation between fitted and observed data:s   Showing 10 pointss      x    y_obs y_ideal y_fiti
   s   %6.3f %6.3f %6.3f %6.3fi	   iÿÿÿÿ(   t
   data_plotss.   Preparing output for loggraph in a file calleds      chebyshev.loggrapht
   plot_titles   Chebyshev fittingt   x_labels   x valuest   y_labels   y valuest   x_datat   y_datat   y_legends   Observed y valuest   commentss   Chebyshev fits   Error free y valuess   Fitted chebyshev approximations   chebyshev.loggrapht   w(   R   t   doublet   ranget   sint   expt   random_doubleR   R   t+   cross_validate_to_determine_number_of_termst   ft   sumR   t	   low_limitt
   high_limitt   coefst   maxt   abst   meant   iotbxR   t   ImportErrort	   plot_datat   add_datat   openR   t   plot_data_loggrapht   writet   getvalue(   t   x_obst   y_idealt   y_obst   w_obst   n_termst   fitt	   fit_functt   y_fittedt   abs_deviationt   iiR   t   chebyshev_plott   output_logfileR   (    (    sS   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/chebyshev_lsq_example.pyt   example	   s€    $$				6		i)   i   c         C   sZ  t  j t |  ƒ ƒ |  d } d t  j | | d ƒ } t  j d | d ƒ } | | d } t  j |  ƒ d | } | | } d d t  j | ƒ } t j | | | | ƒ } t | | j | j	 | j
 ƒ } t j | | | ƒ }	 t | |	 j |	 j	 |	 j
 ƒ }
 Hd GHt | j
 ƒ GHd GHt |	 j
 ƒ GHt  j t  j |	 j
 | j
 ƒ ƒ d k sVt ‚ d  S(   Ni   g®Gáz®ï?g      à?g      ð?s   Coefficients from weighted lsqs"   Coefficients from non-weighted lsqi    (   R   R   R   R   t   logR   R   R   R   R   R   t   listR   R    t   AssertionError(   t   npt   ntt   xt   yR   t   dR,   t   fit_wt   fit_w_ft   fit_nwt   fit_nw_f(    (    sS   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/chebyshev_lsq_example.pyt   another_exampleO   s0    
				i#   c         C   s†  d t  j t | ƒ ƒ t | d ƒ d } d d | | d } t j | | | ƒ } t | | j | j | j ƒ } t j	 | d d t
 ƒ } d d | | d } t j | | | ƒ }	 t | |	 j |	 j |	 j ƒ }	 d t  j t d | ƒ ƒ t d | d ƒ d }
 | j |
 ƒ } |	 j |
 ƒ } d d |
 |
 d } | r‚x= t |
 | | | ƒ D]# \ } } } } | G| G| G| GHqXWn  d  S(   Ng       @i   g      à?i   iÿÿÿÿi   (   R   R   t   xranget   floatR   R   R   R   R   t   chebyshev_nodest   TrueR   t   zip(   t   selft   nR;   t   print_itt   x_et   y_et   fit_et   x_ct   y_ct   fit_ct   x_plott   y_plot_et   y_plot_ct   y_idR<   R=   t   yyt   yyy(    (    sS   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/chebyshev_lsq_example.pyt   runge_phenomenonm   s*    +				3(t   __main__i
   N(   t
   __future__R    t   scitbx.array_familyR   t   scitbx.mathR   R   t	   cStringIOR   R6   RC   t   FalseRX   t   __name__(    (    (    sS   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/examples/chebyshev_lsq_example.pyt   <module>   s   	F#