B
    §d$  ã               @   s°   d dl mZmZ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 G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZeZG dd„ deƒZdd„ ZdS )é    )Úabsolute_importÚdivisionÚprint_function)Úext)Úweighting_schemes)Úmiller)Úflex)Úadopt_init_argsc               @   s   e Zd Zdd„ Zdd„ ZdS )Úleast_squaresc             C   s   |dkst ‚t| tƒ ƒ d S )Nr   )ÚAssertionErrorr	   Úlocals)ÚselfÚcompute_scale_using_all_dataÚf_obsÚweightsÚr_free_flagsÚscale_factor© r   úy/mnt/filia/a/genomebrowser/www/genomebrowser/fleming/tools/molprobity/modules/cctbx_project/cctbx/xray/target_functors.pyÚ__init__
   s    zleast_squares.__init__c          
   C   sb   |  ¡  | j  ¡ ¡st‚| ¡ | j ¡ ks.t‚tj| jd| j ¡ | j	| j
 ¡ | ¡ t|ƒ| jdS )NÚF)r   Úobs_typeÚobsr   r   Úf_calcÚderivatives_depthr   )Ú	unit_cellÚis_similar_tor   r   Úspace_groupr   Útargets_least_squaresr   Údatar   r   Úintr   )r   r   Úcompute_gradientsr   r   r   Ú__call__   s    zleast_squares.__call__N)Ú__name__Ú
__module__Ú__qualname__r   r"   r   r   r   r   r
      s   	r
   c               @   s   e Zd Zddd„Zdd„ ZdS )Úmax_likeç      @c	       	      C   s   t | tƒ ƒ | ¡  ¡ | _d S )N)r	   r   Úcentric_flagsr   )	r   r   r   Úexperimental_phasesÚ
alpha_betar   ÚepsilonsÚspacializationÚintegration_step_sizer   r   r   r   "   s    	zmax_like.__init__c             C   s.  |  ¡  | j  ¡ ¡st‚| ¡ | j ¡ ks.t‚| jd krà| jdkrˆtj| j 	¡ | j
 	¡ | 	¡ | jd  	¡ | jd  	¡ | j| j| j|d	S | jdkrØtj| j 	¡ | j
 	¡ | 	¡ | jd  	¡ | jd  	¡ | j| j| j|d	S dsàt‚tj| j 	¡ | j
 	¡ | j 	¡ | 	¡ | jd  	¡ | jd  	¡ | j| j| j|d
S )NÚfr   é   )	r   r   r   ÚalphaÚbetar   r+   r(   r!   Úi)
r   r   r)   r   r0   r1   r+   r(   r-   r!   )r   r   r   r   r   r)   r,   r   Úmlf_target_and_gradientsr   r   r*   r   r+   r(   Úmli_target_and_gradientsÚmlhl_target_and_gradientsr-   )r   r   r!   r   r   r   r"   .   sJ    


zmax_like.__call__N)r'   )r#   r$   r%   r   r"   r   r   r   r   r&       s   

r&   c               @   s8   e Zd ZdZddd„Zdd„ ZeZdd	„ Zddd„ZdS )Úunified_least_squares_residualzB A least-square residual functor for refinement against F or F^2. TNc             C   sÄ   |  ¡ sF| ¡ sFtj|| ¡ | ¡ d| _|r:| j ¡  qL| j ¡  n|| _| j  ¡ sd| j ¡ sdt	‚|  
¡   ¡ r‚tj| _t ¡ }n|  
¡  ¡ ržtj| _t ¡ }|dkrª|}|| _| j| j_d| _dS )a³  
    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   ÚsigmasN)Úis_xray_amplitude_arrayÚis_xray_intensity_arrayr   Úarrayr   r7   Ú_obsÚ#set_observation_type_xray_amplitudeÚ#set_observation_type_xray_intensityr   r   r   Útargets_least_squares_residualÚ_ext_ls_residualr   Zamplitude_unit_weightingÚ,targets_least_squares_residual_for_intensityZintensity_quasi_unit_weightingÚ
_weightingZobservedÚ_scale_factor)r   r   Zis_amplitudeÚ	weightingZdefault_weightingr   r   r   r   X   s&    (

 
z'unified_least_squares_residual.__init__c             C   s   | j S )z# The obs passed to the constructor )r;   )r   r   r   r   r   •   s    z"unified_least_squares_residual.obsc             C   s   | j S )z The weighting scheme )rA   )r   r   r   r   rC      s    z(unified_least_squares_residual.weightingr   c             C   s  |  ¡  |  ¡   ¡ ¡st‚| ¡ |  ¡  ¡ ks2t‚| jdkrDd| _n|| _||  ¡ _|dkr`d}t|  ¡ t	j
ƒs€t|  ¡ t	jƒrž|  ¡  ||¡ |  ¡ j| _n|  ¡  ¡  |  ¡ jdk	rà|  |  ¡  ¡ |  ¡ j| ¡ || j¡}n|  |  ¡  ¡ | ¡ || j¡}| ¡ | _|S )aá  
    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`
    Nr   )r   r   r   r   r   rB   rC   Z
calculatedÚ
isinstancer   Zshelx_weightingZsimple_shelx_weightingÚcomputer   r   r?   r   )r   r   Úcompute_derivativesr   Úresultr   r   r   r"   ¡   s6    

 



z'unified_least_squares_residual.__call__)TN)r   )	r#   r$   r%   Ú__doc__r   r   r   rC   r"   r   r   r   r   r6   U   s    
;r6   c                   s2   e Zd ZdZe ¡ Zd‡ fdd„	Zdd„ Z‡  Z	S )Ú$least_squares_residual_for_amplitudez3 A least-square residual functor for F refinement. Fc                s(   |r| j }nd }tt| ƒ |d|¡ d S )NT)Ústatistical_weightingÚsuperÚleast_squares_residualr   )r   r   Úuse_sigmas_as_weightsrC   )Ú	__class__r   r   r   Ú   s    z-least_squares_residual_for_amplitude.__init__c             C   s   t | jtƒS )N)rD   rA   rJ   )r   r   r   r   rM   á   s    z:least_squares_residual_for_amplitude.use_sigmas_as_weights)F)
r#   r$   r%   rH   r   Zpure_statistical_weightingrJ   r   rM   Ú__classcell__r   r   )rN   r   rI   Õ   s   rI   c               @   s6   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Úintensity_correlationNFc             C   sB   t | tƒ dd | jd ks$| jr$t‚| jr>| j ¡  ¡  ¡ | _d S )NT)Úhide)	r	   r   Ú_weightsÚ_use_multiplicities_as_weightsr   Ú_f_obsÚmultiplicitiesr   Ú	as_double)r   r   r   Úuse_multiplicities_as_weightsr   r   r   r   é   s    zintensity_correlation.__init__c             C   s   | j S )N)rT   )r   r   r   r   r   ð   s    zintensity_correlation.f_obsc             C   s   | j S )N)rR   )r   r   r   r   r   ó   s    zintensity_correlation.weightsc             C   s   | j S )N)rS   )r   r   r   r   rW   ö   s    z3intensity_correlation.use_multiplicities_as_weightsc             C   sB   |  |  ¡ ¡st‚tjdt |  ¡  ¡ ¡|  ¡ d | ¡ t	|ƒdS )NÚI)r   r   r   r   r   r   )
Úis_similar_symmetryr   r   r   Útargets_correlationr   Úpow2r   r   r    )r   r   rF   r   r   r   r"   ù   s    zintensity_correlation.__call__)NF)r#   r$   r%   r   r   r   rW   r"   r   r   r   r   rP   ç   s    
rP   c               C   s   t ttdœS )N)rL   r6   rP   )rL   r6   rP   r   r   r   r   Úregistry  s    r\   N)Ú
__future__r   r   r   Ú
cctbx.xrayr   r   Úcctbxr   Úcctbx.array_familyr   Úlibtbxr	   Úobjectr
   r&   r6   rI   rL   rP   r\   r   r   r   r   Ú<module>   s   5 