ó
0™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  d l	 m
 Z
 d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e Z d e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿ(   t   division(   t   ext(   t   weighting_schemes(   t   miller(   t   flex(   t   adopt_init_argst   least_squaresc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s&   | d k s t  ‚ t |  t ƒ  ƒ d  S(   Ni    (   t   AssertionErrorR   t   locals(   t   selft   compute_scale_using_all_datat   f_obst   weightst   r_free_flagst   scale_factor(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyt   __init__
   s    c         C   s¦   | j  ƒ  j |  j j  ƒ  ƒ s$ t ‚ | j ƒ  |  j j ƒ  k sE t ‚ t j d |  j d d d |  j j ƒ  d |  j	 d |  j
 j ƒ  d | j ƒ  d t | ƒ d	 |  j ƒ S(
   NR
   t   obs_typet   Ft   obsR   R   t   f_calct   derivatives_depthR   (   t	   unit_cellt   is_similar_toR   R   t   space_groupR   t   targets_least_squaresR
   t   dataR   R   t   intR   (   R	   R   t   compute_gradients(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyt   __call__   s    $!			(   t   __name__t
   __module__R   R   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR      s   		t   max_likec           B   s   e  Z d  d „ Z d „  Z RS(   g      @c         C   s>   t  |  t ƒ  ƒ | j ƒ  j ƒ  |  _ | j ƒ  j ƒ  |  _ d  S(   N(   R   R   t   epsilonsR   t   centric_flags(   R	   R   R   t   experimental_phasest
   alpha_betaR   t   integration_step_size(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   "   s    c         C   sY  | j  ƒ  j |  j j  ƒ  ƒ s$ t ‚ | j ƒ  |  j j ƒ  k sE t ‚ |  j d  k rÏ t j d |  j j	 ƒ  d |  j
 j	 ƒ  d | j	 ƒ  d |  j d j	 ƒ  d |  j d j	 ƒ  d |  j d	 |  j d
 |  j d | ƒ 	St j d |  j j	 ƒ  d |  j
 j	 ƒ  d |  j j	 ƒ  d | j	 ƒ  d |  j d j	 ƒ  d |  j d j	 ƒ  d	 |  j d
 |  j d |  j d | ƒ 
S(   NR   R   R   t   alphai    t   betai   R   R    R!   R   R"   R$   (   R   R   R   R   R   R"   t   NoneR   t$   targets_maximum_likelihood_criterionR   R   R#   R   R    R!   t'   targets_maximum_likelihood_criterion_hlR$   (   R	   R   R   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   -   s0    $!								(   R   R   R   R   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR       s   t   unified_least_squares_residualc           B   sA   e  Z d  Z e d d „ Z d „  Z e Z d „  Z d d „ Z	 RS(   sB    A least-square residual functor for refinement against F or F^2. c         C   s)  | j  ƒ  p | j ƒ  sh t j | d | j ƒ  d | j ƒ  ƒ|  _ | rX |  j j ƒ  qq |  j j ƒ  n	 | |  _ |  j j  ƒ  s• |  j j ƒ  s• t	 ‚ |  j
 ƒ  j  ƒ  rÂ t j |  _ t j ƒ  } n- |  j
 ƒ  j ƒ  rï t j |  _ t j ƒ  } n  | d k r| } n  | |  _ |  j |  j _ d |  _ d S(   s³  
    Construct a least-square residuals

    .. |Sigma|  unicode:: U+003A3 .. GREEK CAPITAL LETTER SIGMA

    |Sigma|:sub:`i`  w[i] ( f_obs.data[i] - k abs(f_calc.data[i]) )^2
    /  |Sigma|:sub:`i` w[i] f_obs.data[i]^2

    or

    |Sigma|:sub:`i` w[i] ( f_obs_square.data[i] - k abs(f_calc.data[i])^2 )^2
    /  |Sigma|:sub:`i` w[i] f_obs_square.data[i]^2

    depending on which of f_obs and f_obs_square is not None.

    Note that
      - the sums are over the indices i of the reflections,
      - f_calc is to be passed to the __call__ method,
      - the weights w and the scale factor k are discussed below.

    :Parameters:

      obs : real miller.array
        the observed reflections, with F and sigma(F) (or F^2 and sigma(F^2))
        respectively in obs.data() and obs.sigmas()

      is_amplitude : bool
        a flag to discriminate the type of data in obs if the latter does not
        spell out whether it contains amplitudes or intensities;
        the default means that amplitudes is the default when data type is
        unknown.

      weighting
        a weighting scheme for the data (c.f. cctbx.xray.weighting for common
        ones) or None, which means no weights
    R   t   sigmasN(   t   is_xray_amplitude_arrayt   is_xray_intensity_arrayR   t   arrayR   R+   t   _obst#   set_observation_type_xray_amplitudet#   set_observation_type_xray_intensityR   R   R   t   targets_least_squares_residualt   _ext_ls_residualR   t   amplitude_unit_weightingt,   targets_least_squares_residual_for_intensityt   intensity_quasi_unit_weightingR'   t
   _weightingt   observedt   _scale_factor(   R	   R   t   is_amplitudet	   weightingt   default_weighting(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   J   s&    (*	 		c         C   s   |  j  S(   s#    The obs passed to the constructor (   R/   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   ‡   s    c         C   s   |  j  S(   s    The weighting scheme (   R7   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR;   �   s    i    c         C   s�  | j  ƒ  j |  j ƒ  j  ƒ  ƒ s' t ‚ | j ƒ  |  j ƒ  j ƒ  k sK t ‚ |  j d k rf d |  _ n	 | |  _ | |  j ƒ  _ | d k r“ d } n  t	 |  j ƒ  t
 j ƒ sÃ t	 |  j ƒ  t
 j ƒ rî |  j ƒ  j | | ƒ |  j ƒ  j |  _ n |  j ƒ  j ƒ  |  j ƒ  j d k	 rO|  j |  j ƒ  j ƒ  |  j ƒ  j | j ƒ  | |  j ƒ } n- |  j |  j ƒ  j ƒ  | j ƒ  | |  j ƒ } | j ƒ  |  _ | S(   sá  
    Compute the least-squares residual value and perhaps its derivatives
    wrt to the calculated structure factor F_c of the i-th reflection

    :Parameters:

      f_calc : complex `cctbx.miller.array`
        f_calc.data()[i] constains F_c for the i-th reflection in f_obs()
      compute_derivatives : bool
        whether to compute the derivatives of the least square residual or not
      scale_factor : number
        the scale factor k if not null,
        otherwise k will be computed as a by-product of calling this method

    :return:
       An object holding the residual value, derivatives and scale factor
    :rtype:
       Boost.Python binding of
       :doxyclass:`cctbx::xray::targets::least_squares_residual`
    i    N(   R   R   R   R   R   R9   R'   R;   t
   calculatedt
   isinstanceR   t   shelx_weightingt   simple_shelx_weightingt   computeR   R   R3   R   (   R	   R   t   compute_derivativesR   t   result(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   “   s6    '$	 			N(
   R   R   t   __doc__t   TrueR'   R   R   R   R;   R   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR*   G   s   ;		t$   least_squares_residual_for_amplitudec           B   s/   e  Z d  Z e j ƒ  Z e d „ Z d „  Z RS(   s3    A least-square residual functor for F refinement. c         C   s8   | r |  j  } n d  } t t |  ƒ j | t | ƒ d  S(   N(   t   statistical_weightingR'   t   supert   least_squares_residualR   RE   (   R	   R   t   use_sigmas_as_weightsR;   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   Ì   s    c         C   s   t  |  j t ƒ S(   N(   R>   R7   RG   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyRJ   Ó   s    (	   R   R   RD   R   t   pure_statistical_weightingRG   t   FalseR   RJ   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyRF   Ç   s   t   intensity_correlationc           B   s;   e  Z d e d  „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   sc   t  |  t ƒ  d t ƒ|  j d  k s5 |  j s5 t ‚ |  j r_ |  j j ƒ  j	 ƒ  j
 ƒ  |  _ n  d  S(   Nt   hide(   R   R   RE   t   _weightsR'   t   _use_multiplicities_as_weightsR   t   _f_obst   multiplicitiesR   t	   as_double(   R	   R   R   t   use_multiplicities_as_weights(    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   Û   s    	c         C   s   |  j  S(   N(   RQ   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   â   s    c         C   s   |  j  S(   N(   RO   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   å   s    c         C   s   |  j  S(   N(   RP   (   R	   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyRT   è   s    c         C   sp   | j  |  j ƒ  ƒ s t ‚ t j d d d t j |  j ƒ  j ƒ  ƒ d |  j ƒ  d d  d | j ƒ  d t
 | ƒ ƒ S(   NR   t   IR   R   R   R   R   (   t   is_similar_symmetryR   R   R   t   targets_correlationR   t   pow2R   R   R'   R   (   R	   R   RB   (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyR   ë   s    	N(	   R   R   R'   RL   R   R   R   RT   R   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyRM   Ù   s   			c           C   s   i t  d 6t d 6t d 6S(   NRI   R*   RM   (   RI   R*   RM   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyt   registryõ   s    N(   t
   __future__R    t
   cctbx.xrayR   R   t   cctbxR   t   cctbx.array_familyR   t   libtbxR   t   objectR   R   R*   RF   RI   RM   RY   (    (    (    sH   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/target_functors.pyt   <module>   s   '€