ó
ï™xTc           @   s  d  d l  m Z d  d l Z e j j d ƒ Z d  d l Td  d l Z	 d  d l
 m Z d  d l 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  d l m Z d  d l m Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionNt"   smtbx_refinement_least_squares_ext(   t   *(   t   xray(   t   adopt_optional_init_args(   t   linalg(   t   normal_eqns(   t   flex(   t   direct(   t   origin_fixing_restraints(   t   matht   crystallographic_lsc           B   sâ   e  Z e Z d  Z e j Z d Z	 d Z
 d Z d Z d „  Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ e d „ Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d d „ Z d d „ Z d e d „ Z d „  Z RS(   t   defaultc         K   sÕ   t  t |  ƒ j | j ƒ | |  _ | |  _ t |  | ƒ |  j d  k	 rn |  j j	 ƒ  | j
 j	 ƒ  k sn t ‚ n  t j |  j ƒ |  _ |  j d k r¤ |  j ƒ  |  _ n  |  j |  j j ƒ  ƒ |  _ d  |  _ d  |  _ d  S(   NR   (   t   superR   t   __init__t   n_independentst   observationst   reparametrisationR   t   f_maskt   Nonet   sizet   fo_sqt   AssertionErrorR   t   f_calc_modulus_squaredt   xray_structuret   one_h_linearisationt   weighting_schemet   default_weighting_schemet   origin_fixing_restraints_typet   space_groupt   origin_fixing_restraintt
   taken_stept   restraints_normalisation_factor(   t   selfR   R   t   kwds(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR      s    		'	R   c           B   s   e  Z d  „  Z RS(   c         C   s
   |  j  j S(   N(   R   t	   structure(   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   fget1   s    (   t   __name__t
   __module__R$   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR   0   s   t   twin_fractionsc           B   s   e  Z d  „  Z RS(   c         C   s
   |  j  j S(   N(   R   R'   (   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR$   5   s    (   R%   R&   R$   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR'   4   s   c            s“  ˆ j  d  k	 r! ˆ j  j ƒ  ‰ n t j ƒ  ‰ ˆ j j ‰  ˆ  d  k rT t j ƒ  ‰  n  ‡  ‡ ‡ f d †  } ˆ j	 sÚ ˆ j j
 ƒ  ˆ j j ƒ  ˆ j } | d  k ræ t j | d d  d t ƒ  d t ƒ Œ  } ˆ j ƒ  } qæ n ˆ j ƒ  } ˆ j ƒ  t j | | ˆ j | ƒ Œ  } ˆ j j j d | j ƒ  d d  ƒ ˆ _ ˆ j j j d | j ƒ  d d  ƒ j ƒ  ˆ _ | j ƒ  ˆ _ ˆ j ƒ  ˆ _ ˆ j ƒ  ˆ _ ˆ j  d  k	 r[ˆ j! d  k rÎd ˆ j ˆ j" ˆ j# ˆ _! n  ˆ j  j$ ˆ j% ˆ j j& ƒ  ƒ } ˆ j j' ˆ j j( ƒ j) ƒ  } ˆ j* ƒ  j+ | j, | j- | | j ˆ j! ƒ | j. ƒ  ˆ _. ˆ j ƒ  ˆ _/ n  | s�ˆ j0 j1 ˆ j2 ƒ  ˆ j j3 ƒ  ˆ j j4 ƒ n  d  S(   Nc            sG   ˆ ˆ j  ˆ | |  ˆ j ˆ j j ƒ  ˆ  f } | rC | t f 7} n  | S(   N(   R   R   R   t#   jacobian_transpose_matching_grad_fct   True(   t   scale_factorR   t   objective_onlyt   args(   t   extinction_correctionR   R!   (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR,   B   s    	R*   R   R+   t   datat   sigmasi   (5   R   R   R.   R   t   complex_doubleR   t
   extinctionR   t   dummy_extinction_correctiont	   finalisedt	   lineariset   storet   initial_scale_factort   extt   build_normal_equationst   sigma_weightingR)   R*   t   resetR   R   R   t   arrayt   f_calct   observablest#   set_observation_type_xray_intensityt   fc_sqt   weightst	   objectivet   objective_data_onlyt   chi_sqt   chi_sq_data_onlyt   restraints_managerR    t   n_equationst   n_parameterst   build_linearised_eqnsR   t   parameter_mapt   jacobian_transpose_matchingt   mapping_to_grad_fc_allt	   transposet   reduced_problemt   add_equationst   deltast   design_matrixt   n_restraintst   chi_sq_data_and_restraintsR   t   add_tot   step_equationsR(   t   asu_scatterer_parameters(   R!   R+   R,   R*   t   resultt   linearised_eqnst   jacobian(    (   R-   R   R!   sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   build_up8   s\    		
!		
		c         C   s
   |  j  j S(   N(   R   t$   norm_of_independent_parameter_vector(   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   parameter_vector_norm‚   s    c         C   s
   |  j  ƒ  S(   N(   t   optimal_scale_factor(   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR*   …   s    c         C   sI   |  j  j |  j ƒ  ƒ |  j  j ƒ  |  j  j ƒ  |  j ƒ  j ƒ  |  _ d  S(   N(   R   t   apply_shiftst   stepR4   R5   t	   deep_copyR   (   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   step_forward‡   s    c         C   s;   |  j  j |  j ƒ |  j  j ƒ  |  j  j ƒ  d  |  _ d  S(   N(   R   R]   R   R4   R5   R   (   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   step_backward�   s    c         C   s   t  j |  j ƒ S(   N(   R
   t   sqrtRD   (   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   goof“   s    c         C   s)   |  j  d  k r |  j ƒ  St j |  j ƒ S(   N(   RE   R   Rc   R
   Rb   RR   (   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   restrained_goof–   s    
c         C   sê   | d  k r  t j d |  j ƒ S|  j j } | j ƒ  | | j ƒ  k } | j | ƒ } |  j	 j | ƒ } |  j
 j | ƒ } | j ƒ  } | j ƒ  } | |  j ƒ  9} t j | t j | | ƒ ƒ t j | t j | ƒ ƒ } t j | ƒ S(   Ni   (   R   R
   Rb   RB   R   R   R.   R/   t   selectR?   R@   R*   R   t   sumt   pow2(   R!   t   cutoff_factorR   t   strongR?   t   wghtt   wR2(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyRk   ›   s    :c         C   s³   |  j  j } | d  k	 rX | j ƒ  | | j ƒ  k } | j | ƒ } |  j j | ƒ } n	 |  j } | j ƒ  } | j ƒ  } | j | d t	 j
 |  j ƒ  ƒ d t ƒ} | | j ƒ  f S(   NR*   t   assume_index_matching(   R   R   R   R.   R/   Re   R?   t	   f_sq_as_ft	   r1_factorR
   Rb   R*   R)   R   (   R!   Rh   R   Ri   R?   t   f_obsR<   t   R1(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyRn   ©   s    	c         C   s†   |  j  ƒ  j s |  j ƒ  n  t j |  j  ƒ  j ƒ  ƒ } | |  j ƒ  } | d k	 re | j | ƒ } n  | r‚ | |  j	 ƒ  d 9} n  | S(   s   The columns of the jacobian_transpose determine which crystallographic
        parameters appear in the covariance matrix.
        If jacobian_transpose is None, then the covariance matrix returned will
        be that for the independent L.S. parameters.
    i   N(
   RT   t   solvedt   solveR   t   inverse_of_u_transpose_ut   cholesky_factor_packed_ut   sum_w_yo_sqR   t)   self_transpose_times_symmetric_times_selfRd   (   R!   t   jacobian_transposet   normalised_by_gooft   cov(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   covariance_matrix·   s     c         C   s.   |  j  j ƒ  } t |  j d | ƒ |  j  j ƒ S(   NRw   (   R   R(   t!   covariance_matrix_and_annotationsRz   t   component_annotations(   R!   t   jac_tr(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR{   É   s    N(   R%   R&   t   mainstream_shelx_weightingR   R   R	   t   atomic_number_weightingR   R   R   RE   RQ   R6   R   t   libtbxt   propertyR   R'   t   FalseRY   R[   R*   R`   Ra   Rc   Rd   Rk   Rn   R)   Rz   R{   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR      s,   		J						R{   c           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s0   | |  _  | |  _ d t |  j ƒ d |  _ d S(   sq    The covariance matrix is assumed to be a symmetric matrix stored as a
        packed upper diagonal matrix.
    i   i   N(   t   matrixt   annotationst   lent   _2_n_minus_1(   R!   Rz   R„   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR   Ò   s    		c         C   s   |  j  | |  j | d | S(   Ni   (   Rƒ   R†   (   R!   t   it   j(    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   __call__Ú   s    c         C   s   |  j  j | ƒ } |  | | ƒ S(   N(   R„   t   index(   R!   t
   annotationR‡   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   variance_ofÝ   s    c         C   sM   |  j  j | ƒ } |  j  j | ƒ } | | k r@ | | } } n  |  | | ƒ S(   N(   R„   RŠ   (   R!   t   annotation_1t   annotation_2R‡   Rˆ   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   covariance_ofá   s
    c         C   s   |  j  j ƒ  S(   N(   Rƒ   t   matrix_packed_u_diagonal(   R!   (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   diagonalè   s    (   R%   R&   R   R‰   RŒ   R�   R‘   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyR{   Ð   s
   				(   t
   __future__R    t   boost.pythont   boostt   pythont
   import_extR7   R   t"   smtbx.refinement.weighting_schemest   smtbxt   cctbxR   R€   R   t   scitbxR   t   scitbx.lstbxR   t   scitbx.array_familyR   t   smtbx.structure_factorsR   t   smtbx.refinement.restraintsR	   t   stdlibR
   t)   non_linear_ls_with_separable_scale_factorR   t   objectR{   (    (    (    sL   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/least_squares.pyt   <module>   s    
¼