ó
ò™xTc           @   sº   d  Z  d d l m Z d d l Z e j j d ƒ Z d d l m	 Z	 d d l
 Z
 d e j j e j f d „  ƒ  YZ d e j j e j f d „  ƒ  YZ d e j j e j f d	 „  ƒ  YZ d S(
   s5   Weighting schemes.

They all feature:

  o __str__: a string representation of the weighting scheme in a format
             that is appropriate for the CIF item _refine_ls_weighting_details;

  o type: the kind of weighting scheme,
          that is appropriate for the CIF item _refine_ls_weighting_scheme
iÿÿÿÿ(   t   divisionNt"   smtbx_refinement_least_squares_ext(   t   flext   _c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sq   t  |  j d ƒ d
 k r( d |  j } n d |  j } t  |  j d ƒ d k rV d } n d |  j } d	 | | f S(   Ni   gš™™™™™¹?gš™™™™™É?s   %.1fs   %.4fi    t    s   +%.4fPs6   w=1/[\s^2^(Fo^2^)+(%sP)^2^%s] where P=(Fo^2^+2Fc^2^)/3(   gš™™™™™¹?gš™™™™™É?(   t   roundt   at   b(   t   selfR   t   b_part(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyt   __str__   s     	c         C   s   d S(   Nt   calc(    (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyt   type#   s    c   )         sº  | j  ƒ  s t ‚ t j d |  j d |  j ƒ ‰ ‡  ‡ f d †  } | j ƒ  } | j ƒ  j | j ƒ  d k  d ƒ | j ƒ  j ƒ  } | ˆ  } | j	 ƒ  ˆ  } t
 j | ƒ } | j ƒ  }	 | d |	 d }
 t
 j |
 ƒ } t
 j t
 j | |	 ƒ | | | ƒ } t
 j t
 j | ƒ | ƒ } t
 j |
 ƒ } t j t d d | t d	 | ƒ ƒ ƒ } d
 | | d | j ƒ  } d | } d | } | j ƒ  t
 j | j ƒ  ƒ } t
 j | ƒ } | j d | ƒ j | ƒ } | j | ƒ } | j | ƒ } d } d } t
 j d d ƒ } t
 j ƒ  } x_ t | ƒ D]Q } | j t t j | d | j ƒ  | ƒ ƒ ƒ | j | | d | | ƒ q"W| j ƒ  | j ƒ  | } t
 j d d ƒ } xã| d k rˆ| d k rˆt
 j | d ƒ } g  t | ƒ D] } | j ƒ  ^ qÝ} t | d | ƒ d | } t | d | ƒ d | } xÇ t | ƒ D]¹ } t
 j | | | | d ƒ } | j | ƒ }	 | j | ƒ } | }  xn t d ƒ D]` }! | }" |  | 7}  xG t d ƒ D]9 }# |" | 7}" | | |! |# f c | | |	 |" |  ƒ 7<q°Wq�Wq8Wd }$ d \ }% }& xÃ t d ƒ D]µ }! x¬ t d ƒ D]ž }# d }' xe t | ƒ D]W } | | d k r\q@n  t j | | |! |# f | | | ƒ }( |' |( d d 7}' q@Wt |' |$ ƒ }$ |' |$ k r'|! }% |# }& q'q'WqW| |& | 7} | |% | 7} |& d k r| d 9} q¦n |& d k r| d } n  |% d k r:| d 9} q¦n |% d k rS| d } n  | d k rl| d } n  | d k r¦| d } q¦q¦W| d k r¤d } d } n  | ˆ _ | ˆ _ ˆ S(   s¾   Find optimal values of a and b that give a flat analysis of the variance
        when binned by Fc/max(Fc), and a goodness of fit close to 1.

        This is done in a grid search fashion similar to Shelxl.

        self is not modified in place; instead a new instance of the weighting
        scheme is returned.

        It is intended that f_calc should already contain the contribution from
        f_mask (if a solvent mask is used).
    R   R   c            sg   | ˆ _  | ˆ _ ˆ |  j ƒ  |  j ƒ  | j ƒ  ˆ  ƒ } t j | t j |  j ƒ  ˆ  | j ƒ  ƒ ƒ S(   N(   R   R   t   datat   sigmasR   t   sumt   pow2(   t   fo_sqt   fc_sqR   R   t   weights(   t   scale_factort	   weighting(    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyt   compute_chi_sq6   s    		$i    i   i   g-Cëâ6?g{®Gázä?g:Œ0âŽyE>g      à?gš™™™™™É?gš™™™™™Ù?R   i
   i   i	   g{®Gázt?i   g   ÐˆÃ Bi   gü©ñÒMbP?(   i    i    (   t   is_xray_intensity_arrayt   AssertionErrort   extt   mainstream_shelx_weightingR   R   t	   deep_copyR   t   set_selectedR   R   R   R   t   matht   sqrtt   maxt   sizet   sort_permutationt   customized_copyt   selectt   size_tt   ranget   appendt   intt   ceilt   gridt   doublet   size_t_ranget   min()   R   R   R   R   t   n_independent_paramsR   t   fo2R   t	   sigmas_sqt   fc2t   pt   p_sqt   xt   yt   zt   start_at   start_bt   a_stept   b_stept   fc_sq_over_fc_sq_maxt   permutationt   n_binst   bin_maxt
   bin_limitst	   bin_countt   it   nt   griddingt   tmpt   binned_chi_sqt   i_bint   selR   t   jR   t   kt   min_variancet   j_mint   k_mint   variancet   goof(    (   R   R   sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyt   optimise_parameters&   s¬    "
()

	.!%

5 )

  			(   t   __name__t
   __module__R
   R   RN   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR      s   	
	c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d S(   Ns   w=1(    (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR
   ™   s    c         C   s   d S(   Nt   unit(    (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR   œ   s    c         C   s   |  S(   N(    (   R   R   R   R   R-   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyRN   Ÿ   s    (   RO   RP   R
   R   RN   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR   —   s   		c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d S(   Ns   w=1/sigma^2(    (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR
   ¦   s    c         C   s   d S(   Nt   sigma(    (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR   ¨   s    c         C   s   |  S(   N(    (   R   R   R   R   R-   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyRN   ª   s    (   RO   RP   R
   R   RN   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyR   ¤   s   		(   t   __doc__t
   __future__R    t   boost.pythont   boostt   pythont
   import_extR   t   cctbx.array_familyR   R   t   injectorR   R   t   unit_weightingt   sigma_weighting(    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/weighting_schemes.pyt   <module>
   s   "€"