def residual(restraint_type, proxy, sites_cart=None, u_cart=None, u_iso=None, use_u_aniso=None): if sites_cart is not None: return restraint_type( adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart), proxy=proxy).residual() elif u_iso is None: return restraint_type( adp_restraint_params(u_cart=u_cart), proxy=proxy).residual() else: assert use_u_aniso is not None return restraint_type( adp_restraint_params(u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso), proxy=proxy).residual()
def adp_volume_similarity_as_cif_loops(xray_structure, proxies): site_labels = xray_structure.scatterers().extract_labels() fmt = "%.4e" loop = model.loop(header=( "_restr_U_volume_similar_atom_site_label_1", "_restr_U_volume_similar_diff", "_restr_U_volume_similar_class_id", )) class_loop = model.loop(header=( "_restr_U_volume_similar_class_class_id", "_restr_U_volume_similar_class_target_weight_param", "_restr_U_volume_similar_class_average", "_restr_U_volume_similar_class_esd", "_restr_U_volume_similar_class_diff_max", )) unit_cell = xray_structure.unit_cell() params = adp_restraints.adp_restraint_params( u_cart=xray_structure.scatterers().extract_u_cart(unit_cell), u_iso=xray_structure.scatterers().extract_u_iso(), use_u_aniso=xray_structure.use_u_aniso()) class_id = 0 for proxy in proxies: restraint = adp_restraints.adp_volume_similarity(params=params, proxy=proxy) class_id += 1 class_loop.add_row( (class_id, fmt % math.sqrt(1 / proxy.weight), fmt % restraint.mean_u_volume, fmt % restraint.rms_deltas(), fmt % flex.max_absolute(restraint.deltas()))) for i, i_seq in enumerate(proxy.i_seqs): loop.add_row( (site_labels[i_seq], fmt % restraint.deltas()[i], class_id)) return class_loop, loop
def rigu_as_cif_loop(xray_structure, proxies): unit_cell = xray_structure.unit_cell() sites_cart = xray_structure.sites_cart() u_cart = xray_structure.scatterers().extract_u_cart(unit_cell) site_labels = xray_structure.scatterers().extract_labels() fmt = "%.6f" loop = model.loop(header=( "_restr_RIGU_atom_site_label_1", "_restr_RIGU_atom_site_label_2", "_restr_RIGU_target_weight_param", "_restr_RIGU_U13_diff", "_restr_RIGU_U23_diff", "_restr_RIGU_U33_diff" )) for proxy in proxies: restraint = adp_restraints.rigu( adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart), proxy=proxy) loop.add_row((site_labels[proxy.i_seqs[0]], site_labels[proxy.i_seqs[1]], fmt % math.sqrt(1/proxy.weight), fmt % restraint.delta_13(), fmt % restraint.delta_23(), fmt % restraint.delta_33() )) return loop
def restraint(self, proxy, u_iso=None, u_cart=None): if u_cart is None: u_cart=self.xray_structure.scatterers().extract_u_cart( self.xray_structure.unit_cell()) return adp.fixed_u_eq_adp( adp_restraint_params(u_cart=u_cart), proxy)
def fixed_u_eq_adp_as_cif_loop(xray_structure, proxies): site_labels = xray_structure.scatterers().extract_labels() fmt = "%.4f" loop = model.loop(header=( "_restr_U_Ueq_atom_site_label_1", "_restr_U_Ueq_weight_param", "_restr_U_Ueq_target", "_restr_U_Ueq_diff" )) unit_cell = xray_structure.unit_cell() params = adp_restraints.adp_restraint_params( u_cart=xray_structure.scatterers().extract_u_cart(unit_cell), u_iso=xray_structure.scatterers().extract_u_iso(), use_u_aniso=xray_structure.use_u_aniso() ) for proxy in proxies: restraint = adp_restraints.fixed_u_eq_adp( params=params, proxy=proxy) for i, i_seq in enumerate(proxy.i_seqs): loop.add_row((site_labels[i_seq], fmt % math.sqrt(1/proxy.weight), fmt % proxy.u_eq_ideal, fmt % restraint.delta())) return loop
def restraint(self, proxy, u_iso=None, u_cart=None): if u_cart is None: u_cart = self.xray_structure.scatterers().extract_u_cart( self.xray_structure.unit_cell()) sites_cart = self.xray_structure.sites_cart() return adp.rigid_bond( adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart), proxy)
def exercise_isotropic_adp(): i_seqs = (0,) weight = 2 u_cart = ((1,2,3,5,2,8),) u_iso = (0,) use_u_aniso = (True,) p = adp_restraints.isotropic_adp_proxy( i_seqs=i_seqs, weight=weight) assert p.i_seqs == i_seqs assert approx_equal(p.weight, weight) i = adp_restraints.isotropic_adp(u_cart=u_cart[0], weight=weight) expected_deltas = (-1, 0, 1, 5, 2, 8) expected_gradients = (-4, 0, 4, 40, 16, 64) assert approx_equal(i.weight, weight) assert approx_equal(i.deltas(), expected_deltas) assert approx_equal(i.rms_deltas(), 4.5704364002673632) assert approx_equal(i.residual(), 376.0) assert approx_equal(i.gradients(), expected_gradients) gradients_aniso_cart = flex.sym_mat3_double(1, (0,0,0,0,0,0)) gradients_iso = flex.double(1,0) proxies = adp_restraints.shared_isotropic_adp_proxy([p,p]) u_cart = flex.sym_mat3_double(u_cart) u_iso = flex.double(u_iso) use_u_aniso = flex.bool(use_u_aniso) params = adp_restraint_params(u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso) residuals = adp_restraints.isotropic_adp_residuals(params, proxies=proxies) assert approx_equal(residuals, (i.residual(),i.residual())) deltas_rms = adp_restraints.isotropic_adp_deltas_rms(params, proxies=proxies) assert approx_equal(deltas_rms, (i.rms_deltas(),i.rms_deltas())) residual_sum = adp_restraints.isotropic_adp_residual_sum( params, proxies=proxies, gradients_aniso_cart=gradients_aniso_cart ) assert approx_equal(residual_sum, 752.0) fd_grads_aniso, fd_grads_iso = finite_difference_gradients( restraint_type=adp_restraints.isotropic_adp, 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) # # check frame invariance of residual # u_cart = matrix.sym(sym_mat3=(0.1,0.2,0.05,0.03,0.02,0.01)) a = adp_restraints.isotropic_adp( u_cart=u_cart.as_sym_mat3(), 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_rot = R * u_cart * R.transpose() a = adp_restraints.isotropic_adp( u_cart=u_cart_rot.as_sym_mat3(), weight=1) assert approx_equal(a.residual(), expected_residual)
def build_linearised_eqns(self, xray_structure, parameter_map): n_restraints = 0 n_params = parameter_map.n_parameters geometry_proxies = [ proxies for proxies in (self.bond_proxies, self.angle_proxies, self.dihedral_proxies) if proxies is not None ] # count restraints, i.e. number of rows for restraint matrix n_restraints = sum([proxies.size() for proxies in geometry_proxies]) if self.bond_similarity_proxies is not None: for proxy in self.bond_similarity_proxies: n_restraints += proxy.i_seqs.size() geometry_proxies.append(self.bond_similarity_proxies) if self.chirality_proxies is not None: for proxy in self.chirality_proxies: n_restraints += 1 geometry_proxies.append(self.chirality_proxies) adp_proxies = [] if self.adp_similarity_proxies is not None: adp_proxies.append(self.adp_similarity_proxies) n_restraints += 6 * self.adp_similarity_proxies.size() if self.adp_u_eq_similarity_proxies is not None: adp_proxies.append(self.adp_u_eq_similarity_proxies) for p in self.adp_u_eq_similarity_proxies: n_restraints += len(p.i_seqs) if self.adp_volume_similarity_proxies is not None: adp_proxies.append(self.adp_volume_similarity_proxies) for p in self.adp_volume_similarity_proxies: n_restraints += len(p.i_seqs) if self.isotropic_adp_proxies is not None: adp_proxies.append(self.isotropic_adp_proxies) n_restraints += 6 * self.isotropic_adp_proxies.size() if self.fixed_u_eq_adp_proxies is not None: adp_proxies.append(self.fixed_u_eq_adp_proxies) n_restraints += self.fixed_u_eq_adp_proxies.size() if self.rigid_bond_proxies is not None: adp_proxies.append(self.rigid_bond_proxies) n_restraints += self.rigid_bond_proxies.size() if self.rigu_proxies is not None: adp_proxies.append(self.rigu_proxies) n_restraints += 3 * self.rigu_proxies.size() # construct restraints matrix linearised_eqns = linearised_eqns_of_restraint(n_restraints, n_params) for proxies in geometry_proxies: linearise_restraints(xray_structure.unit_cell(), xray_structure.sites_cart(), parameter_map, proxies, linearised_eqns) u_cart = xray_structure.scatterers().extract_u_cart( xray_structure.unit_cell()) params = adp_restraint_params( sites_cart=xray_structure.sites_cart(), u_cart=u_cart, u_iso=xray_structure.scatterers().extract_u_iso(), use_u_aniso=xray_structure.use_u_aniso()) for proxies in adp_proxies: linearise_restraints(xray_structure.unit_cell(), params, parameter_map, proxies, linearised_eqns) return linearised_eqns
def restraint(self, proxy, u_iso=None, u_cart=None): if u_cart is None: u_cart=self.xray_structure.scatterers().extract_u_cart( self.xray_structure.unit_cell()) if u_iso is None: u_iso=self.xray_structure.scatterers().extract_u_iso() use_u_aniso=self.xray_structure.use_u_aniso() return adp.adp_volume_similarity( adp_restraint_params(u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso), proxy)
def exercise_rigu(): ins = """ CELL 0.71073 7.772741 8.721603 10.863736 90 102.9832 90 ZERR 2 0.000944 0.001056 0.001068 0 0.0107 0 LATT -1 SYMM -X,0.5+Y,-Z SFAC C H O UNIT 24 44 22 RIGU 0.0001 0.0001 O4 C C12 C12 1 -0.12812 0.06329 -0.17592 11.00000 0.01467 0.02689 0.02780 = -0.00379 0.00441 -0.00377 O4 3 0.08910 0.02721 0.02186 11.00000 0.02001 0.03168 0.03125 = -0.00504 0.00144 -0.00274 C 1 -0.05545 -0.04221 -0.06528 11.00000 0.01560 0.02699 0.02581 = -0.00481 0.00597 -0.00068 HKLF 4 END """ sio = StringIO(ins) import iotbx.shelx as shelx model = shelx.parse_smtbx_refinement_model(file=sio) sites_cart = model.structure.sites_cart() u_cart = model.structure.scatterers().extract_u_cart( model.structure.unit_cell()) arp = adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart) for rp in model._proxies['rigu']: rr = adp_restraints.rigu(arp, rp) U1 = matrix.sym(sym_mat3=u_cart[rp.i_seqs[0]]) U2 = matrix.sym(sym_mat3=u_cart[rp.i_seqs[1]]) vz = matrix.col(sites_cart[rp.i_seqs[1]]) - matrix.col( sites_cart[rp.i_seqs[0]]) vy = vz.ortho() vx = vy.cross(vz) R = matrix.rec( vx.normalize().elems + vy.normalize().elems + vz.normalize().elems, (3, 3)) # with this matrix we can only test Z component as X and Y will differ dU = ((R * U1) * R.transpose() - (R * U2) * R.transpose()).as_sym_mat3() assert approx_equal(dU[2], rr.delta_33()) #with the original matrix all components should match R1 = matrix.rec(rr.RM(), (3, 3)) dU = ((R1 * U1) * R1.transpose() - (R1 * U2) * R1.transpose()).as_sym_mat3() assert approx_equal(dU[2], rr.delta_33()) assert approx_equal(dU[4], rr.delta_13()) assert approx_equal(dU[5], rr.delta_23()) # check the raw gradients against the reference implementation for x, y in zip(rr.reference_gradients(R1), rr.raw_gradients()): for idx in range(0, 6): assert approx_equal(x[idx], y[idx]) for x, y in zip(rigu_finite_diff(R1, U1), rr.raw_gradients()): for idx in range(0, 6): assert approx_equal(x[idx], y[idx])
def build_linearised_eqns(self, xray_structure, parameter_map): n_restraints = 0 n_params = parameter_map.n_parameters geometry_proxies = [proxies for proxies in ( self.bond_proxies, self.angle_proxies, self.dihedral_proxies) if proxies is not None] # count restraints, i.e. number of rows for restraint matrix n_restraints = sum([proxies.size() for proxies in geometry_proxies]) if self.bond_similarity_proxies is not None: for proxy in self.bond_similarity_proxies: n_restraints += proxy.i_seqs.size() geometry_proxies.append(self.bond_similarity_proxies) adp_proxies = [] if self.adp_similarity_proxies is not None: adp_proxies.append(self.adp_similarity_proxies) n_restraints += 6 * self.adp_similarity_proxies.size() if self.adp_u_eq_similarity_proxies is not None: adp_proxies.append(self.adp_u_eq_similarity_proxies) for p in self.adp_u_eq_similarity_proxies: n_restraints += len(p.i_seqs) if self.adp_volume_similarity_proxies is not None: adp_proxies.append(self.adp_volume_similarity_proxies) for p in self.adp_volume_similarity_proxies: n_restraints += len(p.i_seqs) if self.isotropic_adp_proxies is not None: adp_proxies.append(self.isotropic_adp_proxies) n_restraints += 6 * self.isotropic_adp_proxies.size() if self.fixed_u_eq_adp_proxies is not None: adp_proxies.append(self.fixed_u_eq_adp_proxies) n_restraints += self.fixed_u_eq_adp_proxies.size() if self.rigid_bond_proxies is not None: adp_proxies.append(self.rigid_bond_proxies) n_restraints += self.rigid_bond_proxies.size() # construct restraints matrix linearised_eqns = linearised_eqns_of_restraint( n_restraints, n_params) for proxies in geometry_proxies: linearise_restraints( xray_structure.unit_cell(), xray_structure.sites_cart(), parameter_map, proxies, linearised_eqns) u_cart = xray_structure.scatterers().extract_u_cart( xray_structure.unit_cell()) params = adp_restraint_params( sites_cart=xray_structure.sites_cart(), u_cart=u_cart, u_iso=xray_structure.scatterers().extract_u_iso(), use_u_aniso=xray_structure.use_u_aniso()) for proxies in adp_proxies: linearise_restraints( xray_structure.unit_cell(), params, parameter_map, proxies, linearised_eqns) return linearised_eqns
def rigid_bond_as_cif_loop(xray_structure, proxies): unit_cell = xray_structure.unit_cell() sites_cart = xray_structure.sites_cart() u_cart = xray_structure.scatterers().extract_u_cart(unit_cell) site_labels = xray_structure.scatterers().extract_labels() fmt = "%.4f" loop = model.loop(header=( "_restr_U_rigid_atom_site_label_1", "_restr_U_rigid_atom_site_label_2", "_restr_U_rigid_target_weight_param", "_restr_U_rigid_U_parallel", "_restr_U_rigid_diff", )) for proxy in proxies: restraint = adp_restraints.rigid_bond( adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart), proxy=proxy) loop.add_row((site_labels[proxy.i_seqs[0]], site_labels[proxy.i_seqs[1]], fmt % math.sqrt(1/proxy.weight), fmt % (0.5*(restraint.z_12()+restraint.z_21())), fmt % restraint.delta_z())) return loop
def rigid_bond_as_cif_loop(xray_structure, proxies): unit_cell = xray_structure.unit_cell() sites_cart = xray_structure.sites_cart() u_cart = xray_structure.scatterers().extract_u_cart(unit_cell) site_labels = xray_structure.scatterers().extract_labels() fmt = "%.4f" loop = model.loop(header=( "_restr_U_rigid_atom_site_label_1", "_restr_U_rigid_atom_site_label_2", "_restr_U_rigid_target_weight_param", "_restr_U_rigid_U_parallel", "_restr_U_rigid_diff", )) for proxy in proxies: restraint = adp_restraints.rigid_bond(adp_restraint_params( sites_cart=sites_cart, u_cart=u_cart), proxy=proxy) loop.add_row( (site_labels[proxy.i_seqs[0]], site_labels[proxy.i_seqs[1]], fmt % math.sqrt(1 / proxy.weight), fmt % (0.5 * (restraint.z_12() + restraint.z_21())), fmt % restraint.delta_z())) return loop
def adp_volume_similarity_as_cif_loops(xray_structure, proxies): site_labels = xray_structure.scatterers().extract_labels() fmt = "%.4e" loop = model.loop(header=( "_restr_U_volume_similar_atom_site_label_1", "_restr_U_volume_similar_diff", "_restr_U_volume_similar_class_id", )) class_loop = model.loop(header=( "_restr_U_volume_similar_class_class_id", "_restr_U_volume_similar_class_target_weight_param", "_restr_U_volume_similar_class_average", "_restr_U_volume_similar_class_esd", "_restr_U_volume_similar_class_diff_max", )) unit_cell = xray_structure.unit_cell() params = adp_restraints.adp_restraint_params( u_cart=xray_structure.scatterers().extract_u_cart(unit_cell), u_iso=xray_structure.scatterers().extract_u_iso(), use_u_aniso=xray_structure.use_u_aniso() ) class_id = 0 for proxy in proxies: restraint = adp_restraints.adp_volume_similarity( params=params, proxy=proxy) class_id += 1 class_loop.add_row((class_id, fmt % math.sqrt(1/proxy.weight), fmt % restraint.mean_u_volume, fmt % restraint.rms_deltas(), fmt % flex.max_absolute(restraint.deltas()))) for i, i_seq in enumerate(proxy.i_seqs): loop.add_row((site_labels[i_seq], fmt % restraint.deltas()[i], class_id)) return class_loop, loop
def _show_sorted_impl(self, proxy_type, proxy_label, item_label, by_value, u_cart, u_iso=None, use_u_aniso=None, sites_cart=None, site_labels=None, f=None, prefix="", max_items=None): assert by_value in ["residual", "rms_deltas", "delta"] assert site_labels is None or len(site_labels) == u_cart.size() assert sites_cart is None or len(sites_cart) == u_cart.size() assert [u_iso, use_u_aniso].count(None) in (0,2) if (f is None): f = sys.stdout print >> f, "%s%s restraints: %d" % (prefix, proxy_label, self.size()) if (self.size() == 0): return if (max_items is not None and max_items <= 0): return from cctbx.adp_restraints import adp_restraint_params if use_u_aniso is None: use_u_aniso = [] if sites_cart is None: sites_cart = [] if u_iso is None: u_iso = [] params = adp_restraint_params( sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso) if (by_value == "residual"): if proxy_type in (adp_similarity, adp_u_eq_similarity, adp_volume_similarity,\ isotropic_adp, fixed_u_eq_adp, rigid_bond): data_to_sort = self.residuals(params=params) else: raise AssertionError elif (by_value == "rms_deltas"): if proxy_type in (adp_similarity, adp_u_eq_similarity, adp_volume_similarity,\ isotropic_adp, fixed_u_eq_adp): data_to_sort = self.deltas_rms(params=params) else: raise AssertionError elif (by_value == "delta"): if proxy_type is rigid_bond: data_to_sort = flex.abs(self.deltas(params=params)) else: raise AssertionError else: raise AssertionError i_proxies_sorted = flex.sort_permutation(data=data_to_sort, reverse=True) if (max_items is not None): i_proxies_sorted = i_proxies_sorted[:max_items] item_label_blank = " " * len(item_label) print >> f, "%sSorted by %s:" % (prefix, by_value) dynamic_proxy_types = ( adp_u_eq_similarity, adp_volume_similarity, ) for i_proxy in i_proxies_sorted: proxy = self[i_proxy] s = item_label if proxy_type in (isotropic_adp, fixed_u_eq_adp): if (site_labels is None): l = str(proxy.i_seqs[0]) else: l = site_labels[proxy.i_seqs[0]] print >> f, "%s%s %s" % (prefix, s, l) s = item_label_blank elif proxy_type in dynamic_proxy_types: restraint = proxy_type(params=params, proxy=proxy) restraint._show_sorted_item(f=f, prefix=prefix) print >> f, \ "%s delta" % (prefix) for n, i_seq in enumerate(proxy.i_seqs): if (site_labels is None): l = str(i_seq) else: l = site_labels[i_seq] print >> f, "%s %s %9.2e" % (prefix, l, restraint.deltas()[n]) else: for n, i_seq in enumerate(proxy.i_seqs): if (site_labels is None): l = str(i_seq) else: l = site_labels[i_seq] print >> f, "%s%s %s" % (prefix, s, l) s = item_label_blank if proxy_type in (adp_similarity, isotropic_adp, fixed_u_eq_adp,\ rigid_bond): restraint = proxy_type(params=params, proxy=proxy) restraint._show_sorted_item(f=f, prefix=prefix) elif proxy_type in dynamic_proxy_types: pass else: raise AssertionError n_not_shown = self.size() - i_proxies_sorted.size() if (n_not_shown != 0): print >> f, prefix + "... (remaining %d not shown)" % n_not_shown
def exercise_rigid_bond(): i_seqs = (1,2) weight = 1 p = adp_restraints.rigid_bond_proxy(i_seqs=i_seqs,weight=weight) assert p.i_seqs == i_seqs assert p.weight == weight sites = ((1,2,3),(2,3,4)) u_cart = ((1,2,3,4,5,6), (3,4,5,6,7,8)) expected_gradients = ((-4, -4, -4, -8, -8, -8), (4, 4, 4, 8, 8, 8)) r = adp_restraints.rigid_bond(sites=sites, u_cart=u_cart, weight=weight) assert r.weight == weight assert approx_equal(r.delta_z(), -6) assert approx_equal(r.residual(), 36) assert approx_equal(r.gradients(), expected_gradients) sites_cart = flex.vec3_double(((1,2,3),(2,5,4),(3,4,5))) u_cart = flex.sym_mat3_double(((1,2,3,4,5,6), (2,3,3,5,7,7), (3,4,5,3,7,8))) r = adp_restraints.rigid_bond( adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart), proxy=p) assert approx_equal(r.weight, weight) unit_cell = uctbx.unit_cell([15,25,30,90,90,90]) sites_frac = unit_cell.fractionalize(sites_cart=sites_cart) u_star = flex.sym_mat3_double([ adptbx.u_cart_as_u_star(unit_cell, u_cart_i) for u_cart_i in u_cart]) pair = adp_restraints.rigid_bond_pair(sites_frac[1], sites_frac[2], u_star[1], u_star[2], unit_cell) assert approx_equal(pair.delta_z(), abs(r.delta_z())) assert approx_equal(pair.z_12(), r.z_12()) assert approx_equal(pair.z_21(), r.z_21()) # gradients_aniso_cart = flex.sym_mat3_double(sites_cart.size(), (0,0,0,0,0,0)) gradients_iso = flex.double(sites_cart.size(), 0) proxies = adp_restraints.shared_rigid_bond_proxy([p,p]) params = adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart) residuals = adp_restraints.rigid_bond_residuals(params, proxies=proxies) assert approx_equal(residuals, (r.residual(),r.residual())) deltas = adp_restraints.rigid_bond_deltas(params, proxies=proxies) assert approx_equal(deltas, (r.delta_z(),r.delta_z())) residual_sum = adp_restraints.rigid_bond_residual_sum( params=params, proxies=proxies, gradients_aniso_cart=gradients_aniso_cart) assert approx_equal(residual_sum, 2 * r.residual()) for g,e in zip(gradients_aniso_cart[1:3], r.gradients()): assert approx_equal(g, matrix.col(e)*2) fd_grads_aniso, fd_grads_iso = finite_difference_gradients( restraint_type=adp_restraints.rigid_bond, proxy=p, sites_cart=sites_cart, u_cart=u_cart) for g,e in zip(gradients_aniso_cart, fd_grads_aniso): assert approx_equal(g, matrix.col(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()) site_cart_1 = matrix.col((1,2,3)) site_cart_2 = matrix.col((3,1,4.2)) sites = (tuple(site_cart_1),tuple(site_cart_2)) a = adp_restraints.rigid_bond(sites=sites, u_cart=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()) site_cart_1_rot = R * site_cart_1 site_cart_2_rot = R * site_cart_2 sites = (tuple(site_cart_1_rot),tuple(site_cart_2_rot)) a = adp_restraints.rigid_bond( sites=sites, u_cart=u_cart, weight=1) assert approx_equal(a.residual(), expected_residual)
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)
def _show_sorted_impl(self, proxy_type, proxy_label, item_label, by_value, u_cart, u_iso=None, use_u_aniso=None, sites_cart=None, site_labels=None, f=None, prefix="", max_items=None): assert by_value in ["residual", "rms_deltas", "delta"] assert site_labels is None or len(site_labels) == u_cart.size() assert sites_cart is None or len(sites_cart) == u_cart.size() assert [u_iso, use_u_aniso].count(None) in (0, 2) if (f is None): f = sys.stdout print >> f, "%s%s restraints: %d" % (prefix, proxy_label, self.size()) if (self.size() == 0): return if (max_items is not None and max_items <= 0): return from cctbx.adp_restraints import adp_restraint_params if use_u_aniso is None: use_u_aniso = [] if sites_cart is None: sites_cart = [] if u_iso is None: u_iso = [] params = adp_restraint_params(sites_cart=sites_cart, u_cart=u_cart, u_iso=u_iso, use_u_aniso=use_u_aniso) if (by_value == "residual"): if proxy_type in (adp_similarity, adp_u_eq_similarity, adp_volume_similarity,\ isotropic_adp, fixed_u_eq_adp, rigid_bond): data_to_sort = self.residuals(params=params) else: raise AssertionError elif (by_value == "rms_deltas"): if proxy_type in (adp_similarity, adp_u_eq_similarity, adp_volume_similarity,\ isotropic_adp, fixed_u_eq_adp): data_to_sort = self.deltas_rms(params=params) else: raise AssertionError elif (by_value == "delta"): if proxy_type is rigid_bond: data_to_sort = flex.abs(self.deltas(params=params)) else: raise AssertionError else: raise AssertionError i_proxies_sorted = flex.sort_permutation(data=data_to_sort, reverse=True) if (max_items is not None): i_proxies_sorted = i_proxies_sorted[:max_items] item_label_blank = " " * len(item_label) print >> f, "%sSorted by %s:" % (prefix, by_value) dynamic_proxy_types = ( adp_u_eq_similarity, adp_volume_similarity, ) for i_proxy in i_proxies_sorted: proxy = self[i_proxy] s = item_label if proxy_type in (isotropic_adp, fixed_u_eq_adp): if (site_labels is None): l = str(proxy.i_seqs[0]) else: l = site_labels[proxy.i_seqs[0]] print >> f, "%s%s %s" % (prefix, s, l) s = item_label_blank elif proxy_type in dynamic_proxy_types: restraint = proxy_type(params=params, proxy=proxy) restraint._show_sorted_item(f=f, prefix=prefix) print >> f, \ "%s delta" % (prefix) for n, i_seq in enumerate(proxy.i_seqs): if (site_labels is None): l = str(i_seq) else: l = site_labels[i_seq] print >> f, "%s %s %9.2e" % (prefix, l, restraint.deltas()[n]) else: for n, i_seq in enumerate(proxy.i_seqs): if (site_labels is None): l = str(i_seq) else: l = site_labels[i_seq] print >> f, "%s%s %s" % (prefix, s, l) s = item_label_blank if proxy_type in (adp_similarity, isotropic_adp, fixed_u_eq_adp,\ rigid_bond): restraint = proxy_type(params=params, proxy=proxy) restraint._show_sorted_item(f=f, prefix=prefix) elif proxy_type in dynamic_proxy_types: pass else: raise AssertionError n_not_shown = self.size() - i_proxies_sorted.size() if (n_not_shown != 0): print >> f, prefix + "... (remaining %d not shown)" % n_not_shown