Example #1
0
 def __init__(self,
              u_cart,
              u_iso=None,
              use_u_aniso=None,
              sites_cart=None,
              adp_similarity_proxies=None,
              rigid_bond_proxies=None,
              isotropic_adp_proxies=None,
              compute_gradients=True,
              gradients_aniso_cart=None,
              gradients_iso=None,
              disable_asu_cache=False,
              normalization=False):
     adopt_init_args(self, locals())
     self.number_of_restraints = 0
     self.residual_sum = 0
     self.normalization_factor = None
     if (adp_similarity_proxies is not None):
         assert u_iso is not None and use_u_aniso is not None
     if (rigid_bond_proxies is not None): assert sites_cart is not None
     if (sites_cart is not None): assert sites_cart.size() == u_cart.size()
     if (u_iso is not None): assert u_iso.size() == u_cart.size()
     if (use_u_aniso is not None):
         assert use_u_aniso.size() == u_cart.size()
     if (compute_gradients):
         if (self.gradients_aniso_cart is None):
             self.gradients_aniso_cart = flex.sym_mat3_double(
                 sites_cart.size(), [0, 0, 0, 0, 0, 0])
         else:
             assert self.gradients_aniso_cart.size() == sites_cart.size()
         if (u_iso is not None and self.gradients_iso is None):
             self.gradients_iso = flex.double(sites_cart.size(), 0)
         elif (u_iso is not None):
             assert self.gradients_iso.size() == sites_cart.size()
     if (adp_similarity_proxies is None):
         self.n_adp_similarity_proxies = None
         self.adp_similarity_residual_sum = 0
     else:
         self.n_adp_similarity_proxies = len(adp_similarity_proxies)
         self.adp_similarity_residual_sum = adp_restraints.adp_similarity_residual_sum(
             u_cart=u_cart,
             u_iso=u_iso,
             use_u_aniso=use_u_aniso,
             proxies=adp_similarity_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart,
             gradients_iso=self.gradients_iso)
         self.number_of_restraints += 6 * self.n_adp_similarity_proxies
         self.residual_sum += self.adp_similarity_residual_sum
     if (rigid_bond_proxies is None):
         self.n_rigid_bond_proxies = None
         self.rigid_bond_residual_sum = 0
     else:
         self.n_rigid_bond_proxies = len(rigid_bond_proxies)
         self.rigid_bond_residual_sum = adp_restraints.rigid_bond_residual_sum(
             sites_cart=sites_cart,
             u_cart=u_cart,
             proxies=rigid_bond_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart)
         self.number_of_restraints += self.n_rigid_bond_proxies
         self.residual_sum += self.rigid_bond_residual_sum
     if (isotropic_adp_proxies is None):
         self.n_isotropic_adp_proxies = None
         self.isotropic_adp_residual_sum = 0
     else:
         self.n_isotropic_adp_proxies = len(isotropic_adp_proxies)
         self.isotropic_adp_residual_sum = adp_restraints.isotropic_adp_residual_sum(
             u_cart=u_cart,
             proxies=isotropic_adp_proxies,
             gradients_aniso_cart=self.gradients_aniso_cart)
         self.number_of_restraints += self.n_isotropic_adp_proxies
         self.residual_sum += self.isotropic_adp_residual_sum
     self.finalize_target_and_gradients()
Example #2
0
def exercise_adp_similarity():
  u_cart = ((1,3,2,4,3,6),(2,4,2,6,5,1))
  u_iso = (-1,-1)
  use_u_aniso = (True, True)
  weight = 1
  a = adp_restraints.adp_similarity(
    u_cart=u_cart,
    weight=weight)
  assert approx_equal(a.use_u_aniso, use_u_aniso)
  assert a.weight == weight
  assert approx_equal(a.residual(), 68)
  assert approx_equal(a.gradients2(),
    ((-2.0, -2.0, 0.0, -8.0, -8.0, 20.0), (2.0, 2.0, -0.0, 8.0, 8.0, -20.0)))
  assert approx_equal(a.deltas(), (-1.0, -1.0, 0.0, -2.0, -2.0, 5.0))
  assert approx_equal(a.rms_deltas(), 2.7487370837451071)
  #
  u_cart = ((1,3,2,4,3,6),(-1,-1,-1,-1,-1,-1))
  u_iso = (-1,2)
  use_u_aniso = (True, False)
  a = adp_restraints.adp_similarity(
    u_cart[0], u_iso[1], weight=weight)
  assert approx_equal(a.use_u_aniso, use_u_aniso)
  assert a.weight == weight
  assert approx_equal(a.residual(), 124)
  assert approx_equal(a.gradients2(),
    ((-2, 2, 0, 16, 12, 24), (2, -2, 0, -16, -12, -24)))
  assert approx_equal(a.deltas(), (-1, 1, 0, 4, 3, 6))
  assert approx_equal(a.rms_deltas(), 3.711842908553348)
  #
  i_seqs_aa = (1,2) # () - ()
  i_seqs_ai = (1,0) # () - o
  i_seqs_ia = (3,2) #  o - ()
  i_seqs_ii = (0,3) #  o - o
  p_aa = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_aa,weight=weight)
  p_ai = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ai,weight=weight)
  p_ia = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ia,weight=weight)
  p_ii = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ii,weight=weight)
  assert p_aa.i_seqs == i_seqs_aa
  assert p_aa.weight == weight
  u_cart = flex.sym_mat3_double(((-1,-1,-1,-1,-1,-1),
                                 (1,2,2,4,3,6),
                                 (2,4,2,6,5,1),
                                 (-1,-1,-1,-1,-1,-1)))
  u_iso = flex.double((1,-1,-1,2))
  use_u_aniso = flex.bool((False, True,True,False))
  for p in (p_aa,p_ai,p_ia,p_ii):
    params = adp_restraint_params(u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso)
    a = adp_restraints.adp_similarity(params, proxy=p)
    assert approx_equal(a.weight, weight)
    #
    gradients_aniso_cart = flex.sym_mat3_double(u_cart.size(), (0,0,0,0,0,0))
    gradients_iso = flex.double(u_cart.size(), 0)
    proxies = adp_restraints.shared_adp_similarity_proxy([p,p])
    residuals = adp_restraints.adp_similarity_residuals(params, proxies=proxies)
    assert approx_equal(residuals, (a.residual(),a.residual()))
    deltas_rms = adp_restraints.adp_similarity_deltas_rms(params, proxies=proxies)
    assert approx_equal(deltas_rms, (a.rms_deltas(),a.rms_deltas()))
    residual_sum = adp_restraints.adp_similarity_residual_sum(
      params,
      proxies=proxies,
      gradients_aniso_cart=gradients_aniso_cart,
      gradients_iso=gradients_iso)
    assert approx_equal(residual_sum, 2 * a.residual())
    fd_grads_aniso, fd_grads_iso = finite_difference_gradients(
      restraint_type=adp_restraints.adp_similarity,
      proxy=p,
      u_cart=u_cart,
      u_iso=u_iso,
      use_u_aniso=use_u_aniso)
    for g,e in zip(gradients_aniso_cart, fd_grads_aniso):
      assert approx_equal(g,  matrix.col(e)*2)
    for g,e in zip(gradients_iso, fd_grads_iso):
      assert approx_equal(g, e*2)
  #
  # check frame invariance of residual
  #
  u_cart_1 = matrix.sym(sym_mat3=(0.1,0.2,0.05,0.03,0.02,0.01))
  u_cart_2 = matrix.sym(sym_mat3=(0.21,0.32,0.11,0.02,0.02,0.07))
  u_cart = (u_cart_1.as_sym_mat3(),u_cart_2.as_sym_mat3())
  u_iso = (-1, -1)
  use_u_aniso = (True, True)
  a = adp_restraints.adp_similarity(u_cart, weight=1)
  expected_residual = a.residual()
  gen = flex.mersenne_twister()
  for i in range(20):
    R = matrix.rec(gen.random_double_r3_rotation_matrix(),(3,3))
    u_cart_1_rot = R * u_cart_1 * R.transpose()
    u_cart_2_rot = R * u_cart_2 * R.transpose()
    u_cart = (u_cart_1_rot.as_sym_mat3(),u_cart_2_rot.as_sym_mat3())
    a = adp_restraints.adp_similarity(u_cart, weight=1)
    assert approx_equal(a.residual(), expected_residual)
Example #3
0
def exercise_adp_similarity():
  u_cart = ((1,3,2,4,3,6),(2,4,2,6,5,1))
  u_iso = (-1,-1)
  use_u_aniso = (True, True)
  weight = 1
  a = adp_restraints.adp_similarity(
    u_cart=u_cart,
    weight=weight)
  assert approx_equal(a.use_u_aniso, use_u_aniso)
  assert a.weight == weight
  assert approx_equal(a.residual(), 68)
  assert approx_equal(a.gradients2(),
    ((-2.0, -2.0, 0.0, -8.0, -8.0, 20.0), (2.0, 2.0, -0.0, 8.0, 8.0, -20.0)))
  assert approx_equal(a.deltas(), (-1.0, -1.0, 0.0, -2.0, -2.0, 5.0))
  assert approx_equal(a.rms_deltas(), 2.7487370837451071)
  #
  u_cart = ((1,3,2,4,3,6),(-1,-1,-1,-1,-1,-1))
  u_iso = (-1,2)
  use_u_aniso = (True, False)
  a = adp_restraints.adp_similarity(
    u_cart[0], u_iso[1], weight=weight)
  assert approx_equal(a.use_u_aniso, use_u_aniso)
  assert a.weight == weight
  assert approx_equal(a.residual(), 124)
  assert approx_equal(a.gradients2(),
    ((-2, 2, 0, 16, 12, 24), (2, -2, 0, -16, -12, -24)))
  assert approx_equal(a.deltas(), (-1, 1, 0, 4, 3, 6))
  assert approx_equal(a.rms_deltas(), 3.711842908553348)
  #
  i_seqs_aa = (1,2) # () - ()
  i_seqs_ai = (1,0) # () - o
  i_seqs_ia = (3,2) #  o - ()
  i_seqs_ii = (0,3) #  o - o
  p_aa = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_aa,weight=weight)
  p_ai = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ai,weight=weight)
  p_ia = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ia,weight=weight)
  p_ii = adp_restraints.adp_similarity_proxy(i_seqs=i_seqs_ii,weight=weight)
  assert p_aa.i_seqs == i_seqs_aa
  assert p_aa.weight == weight
  u_cart = flex.sym_mat3_double(((-1,-1,-1,-1,-1,-1),
                                 (1,2,2,4,3,6),
                                 (2,4,2,6,5,1),
                                 (-1,-1,-1,-1,-1,-1)))
  u_iso = flex.double((1,-1,-1,2))
  use_u_aniso = flex.bool((False, True,True,False))
  for p in (p_aa,p_ai,p_ia,p_ii):
    params = adp_restraint_params(u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso)
    a = adp_restraints.adp_similarity(params, proxy=p)
    assert approx_equal(a.weight, weight)
    #
    gradients_aniso_cart = flex.sym_mat3_double(u_cart.size(), (0,0,0,0,0,0))
    gradients_iso = flex.double(u_cart.size(), 0)
    proxies = adp_restraints.shared_adp_similarity_proxy([p,p])
    residuals = adp_restraints.adp_similarity_residuals(params, proxies=proxies)
    assert approx_equal(residuals, (a.residual(),a.residual()))
    deltas_rms = adp_restraints.adp_similarity_deltas_rms(params, proxies=proxies)
    assert approx_equal(deltas_rms, (a.rms_deltas(),a.rms_deltas()))
    residual_sum = adp_restraints.adp_similarity_residual_sum(
      params,
      proxies=proxies,
      gradients_aniso_cart=gradients_aniso_cart,
      gradients_iso=gradients_iso)
    assert approx_equal(residual_sum, 2 * a.residual())
    fd_grads_aniso, fd_grads_iso = finite_difference_gradients(
      restraint_type=adp_restraints.adp_similarity,
      proxy=p,
      u_cart=u_cart,
      u_iso=u_iso,
      use_u_aniso=use_u_aniso)
    for g,e in zip(gradients_aniso_cart, fd_grads_aniso):
      assert approx_equal(g,  matrix.col(e)*2)
    for g,e in zip(gradients_iso, fd_grads_iso):
      assert approx_equal(g, e*2)
  #
  # check frame invariance of residual
  #
  u_cart_1 = matrix.sym(sym_mat3=(0.1,0.2,0.05,0.03,0.02,0.01))
  u_cart_2 = matrix.sym(sym_mat3=(0.21,0.32,0.11,0.02,0.02,0.07))
  u_cart = (u_cart_1.as_sym_mat3(),u_cart_2.as_sym_mat3())
  u_iso = (-1, -1)
  use_u_aniso = (True, True)
  a = adp_restraints.adp_similarity(u_cart, weight=1)
  expected_residual = a.residual()
  gen = flex.mersenne_twister()
  for i in range(20):
    R = matrix.rec(gen.random_double_r3_rotation_matrix(),(3,3))
    u_cart_1_rot = R * u_cart_1 * R.transpose()
    u_cart_2_rot = R * u_cart_2 * R.transpose()
    u_cart = (u_cart_1_rot.as_sym_mat3(),u_cart_2_rot.as_sym_mat3())
    a = adp_restraints.adp_similarity(u_cart, weight=1)
    assert approx_equal(a.residual(), expected_residual)
Example #4
0
 def __init__(self, u_cart,
                    u_iso=None,
                    use_u_aniso=None,
                    sites_cart=None,
                    adp_similarity_proxies=None,
                    rigid_bond_proxies=None,
                    isotropic_adp_proxies=None,
                    compute_gradients=True,
                    gradients_aniso_cart=None,
                    gradients_iso=None,
                    disable_asu_cache=False,
                    normalization=False):
   adopt_init_args(self, locals())
   self.number_of_restraints = 0
   self.residual_sum = 0
   self.normalization_factor = None
   if (adp_similarity_proxies is not None):
     assert u_iso is not None and use_u_aniso is not None
   if (rigid_bond_proxies is not None): assert sites_cart is not None
   if (sites_cart is not None): assert sites_cart.size() == u_cart.size()
   if (u_iso is not None): assert u_iso.size() == u_cart.size()
   if (use_u_aniso is not None): assert use_u_aniso.size() == u_cart.size()
   if (compute_gradients):
     if (self.gradients_aniso_cart is None):
       self.gradients_aniso_cart = flex.sym_mat3_double(
         sites_cart.size(), [0,0,0,0,0,0])
     else:
       assert self.gradients_aniso_cart.size() == sites_cart.size()
     if (u_iso is not None and self.gradients_iso is None):
       self.gradients_iso = flex.double(sites_cart.size(), 0)
     elif (u_iso is not None):
       assert self.gradients_iso.size() == sites_cart.size()
   if (adp_similarity_proxies is None):
     self.n_adp_similarity_proxies = None
     self.adp_similarity_residual_sum = 0
   else:
     self.n_adp_similarity_proxies = len(adp_similarity_proxies)
     self.adp_similarity_residual_sum = adp_restraints.adp_similarity_residual_sum(
       u_cart=u_cart,
       u_iso=u_iso,
       use_u_aniso=use_u_aniso,
       proxies=adp_similarity_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart,
       gradients_iso=self.gradients_iso)
     self.number_of_restraints += 6 * self.n_adp_similarity_proxies
     self.residual_sum += self.adp_similarity_residual_sum
   if (rigid_bond_proxies is None):
     self.n_rigid_bond_proxies = None
     self.rigid_bond_residual_sum = 0
   else:
     self.n_rigid_bond_proxies = len(rigid_bond_proxies)
     self.rigid_bond_residual_sum = adp_restraints.rigid_bond_residual_sum(
       sites_cart=sites_cart,
       u_cart=u_cart,
       proxies=rigid_bond_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart)
     self.number_of_restraints += self.n_rigid_bond_proxies
     self.residual_sum += self.rigid_bond_residual_sum
   if (isotropic_adp_proxies is None):
     self.n_isotropic_adp_proxies = None
     self.isotropic_adp_residual_sum = 0
   else:
     self.n_isotropic_adp_proxies = len(isotropic_adp_proxies)
     self.isotropic_adp_residual_sum = adp_restraints.isotropic_adp_residual_sum(
       u_cart=u_cart,
       proxies=isotropic_adp_proxies,
       gradients_aniso_cart=self.gradients_aniso_cart)
     self.number_of_restraints += self.n_isotropic_adp_proxies
     self.residual_sum += self.isotropic_adp_residual_sum
   self.finalize_target_and_gradients()