def exercise(space_group_info, n_scatterers=8, d_min=2, verbose=0, e_min=1.5): structure = random_structure.xray_structure( space_group_info, elements=["const"] * n_scatterers, volume_per_atom=200, min_distance=3.0, general_positions_only=True, u_iso=0.0, ) if 0 or verbose: structure.show_summary().show_scatterers() f_calc = structure.structure_factors(d_min=d_min, anomalous_flag=False).f_calc() f_obs = abs(f_calc) q_obs = miller.array( miller_set=f_obs, data=f_obs.data() / math.sqrt(f_obs.space_group().order_p() * n_scatterers) / f_obs.space_group().n_ltr(), ) q_obs = q_obs.sort(by_value="abs") q_obs.setup_binner(auto_binning=True) n_obs = q_obs.quasi_normalize_structure_factors() r = flex.linear_regression(q_obs.data(), n_obs.data()) if 0 or verbose: r.show_summary() assert r.is_well_defined() assert abs(r.y_intercept()) < 0.1 assert abs(r.slope() - 1) < 0.2 q_large = q_obs.select(q_obs.quasi_normalized_as_normalized().data() > e_min) if 0 or verbose: print "Number of e-values > %.6g: %d" % (e_min, q_large.size()) other_structure = random_structure.xray_structure( space_group_info, elements=["const"] * n_scatterers, volume_per_atom=200, min_distance=3.0, general_positions_only=True, u_iso=0.0, ) assert other_structure.unit_cell().is_similar_to(structure.unit_cell()) q_calc = q_large.structure_factors_from_scatterers(other_structure, algorithm="direct").f_calc() start = q_large.phase_transfer(q_calc.data()) for selection_fixed in (None, flex.double([random.random() for i in xrange(start.size())]) < 0.4): from_map_data = direct_space_squaring(start, selection_fixed) direct_space_result = start.phase_transfer(phase_source=from_map_data) new_phases = reciprocal_space_squaring(start, selection_fixed, verbose) reciprocal_space_result = start.phase_transfer(phase_source=flex.polar(1, new_phases)) mwpe = direct_space_result.mean_weighted_phase_error(reciprocal_space_result) if 0 or verbose: print "mwpe: %.2f" % mwpe, start.space_group_info() for i, h in enumerate(direct_space_result.indices()): amp_d, phi_d = complex_math.abs_arg(direct_space_result.data()[i], deg=True) amp_r, phi_r = complex_math.abs_arg(reciprocal_space_result.data()[i], deg=True) phase_err = scitbx.math.phase_error(phi_d, phi_r, deg=True) assert phase_err < 1.0 or abs(from_map_data[i]) < 1.0e-6 exercise_truncate(q_large)
def run(server_info, inp, status): # check input to avoid XSS # --------------------------------------------------------------------------- try: d_min = float(inp.d_min) except Exception: print('Please provide a number for Minimum d-spacing.') raise print("Minimum d-spacing (highest resolution): %s" % inp.d_min) if (float(inp.d_min) <= 0.): raise ValueError("d-spacing must be greater than zero.") print() if (inp.form_factors not in ['wk1995', 'it1992', 'n_gaussian']): print('Atomic form factors not correct. Using default value.') inp.form_factors = 'wk1995' # --------------------------------------------------------------------------- print("<h2>Structure factor calculation</h2>") print("<pre>") print("Atomic form factors: %s" % inp.form_factors) print() structure = io_utils.structure_from_inp_pdb(inp, status) structure.scattering_type_registry( table=inp.form_factors, d_min=float(inp.d_min)) print("Input model summary:") print() structure.show_summary() print() structure.scattering_type_registry().show() print() algorithm = inp.algorithm if (algorithm == "automatic"): if (structure.scatterers().size() <= 100): algorithm = "direct" else: algorithm = None elif (algorithm not in ["direct", "fft"]): algorithm = None f_calc_manager = structure.structure_factors( anomalous_flag = False, algorithm = algorithm, d_min = float(inp.d_min)) f_calc = f_calc_manager.f_calc() print("Number of Miller indices:", f_calc.indices().size()) print() print("Structure factor algorithm:", f_calc_manager.algorithm(verbose=True)) print() print(" h k l Fcalc Pcalc") for h,f in zip(f_calc.indices(), f_calc.data()): print("%5d %5d %5d %15.6f %8.2f" % (h + complex_math.abs_arg(f, deg=True))) print() print("</pre>")
def add(self, h, f1, f2): a1, p1 = abs_arg(f1, deg=True) a2, p2 = abs_arg(f2, deg=True) if (0 or self.verbose): print h print " ", a1, p1 print " ", a2, p2 print " " * 20, "%.2f" % scitbx.math.phase_error( phi1=p1, phi2=p2, deg=True) self.amp1.append(a1) self.amp2.append(a2) self.sum_amp1_minus_amp2_sq += (a1 - a2)**2 self.sum_amp1_sq += a1**2 self.sum_w_phase_error += (a1 + a2) * scitbx.math.phase_error( phi1=p1, phi2=p2, deg=True) self.sum_w += (a1 + a2)
def run(server_info, inp, status): print("<pre>") special_position_settings = io_utils.special_position_settings_from_inp( inp) special_position_settings.show_summary() print("Minimum distance between symmetrically equivalent sites:", end=' ') print(special_position_settings.min_distance_sym_equiv()) print() d_min = float(inp.d_min) print("Minimum d-spacing:", d_min) if (d_min <= 0.): raise ValueError("d-spacing must be greater than zero.") print() structure = io_utils.structure_from_inp(inp, status, special_position_settings) algorithm = inp.algorithm if (algorithm == "automatic"): if (structure.scatterers().size() <= 100): algorithm = "direct" else: algorithm = None elif (algorithm not in ["direct", "fft"]): algorithm = None f_calc_manager = structure.structure_factors(anomalous_flag=False, d_min=d_min, algorithm=algorithm) f_calc = f_calc_manager.f_calc() structure.scattering_type_registry().show() print() print("Number of Miller indices:", f_calc.indices().size()) print() print("Structure factor algorithm:", f_calc_manager.algorithm(verbose=True)) print() print("</pre><table border=2 cellpadding=2>") status.in_table = True print("<tr>") print("<th>hkl<th>Amplitude<th>Phase") for i, h in enumerate(f_calc.indices()): print("<tr>") print("<td>%3d %3d %3d<td>%.6g<td align=right>%.3f" % (h + complex_math.abs_arg(f_calc.data()[i], deg=True))) print("</table><pre>") status.in_table = False print() print("</pre>")
def run(server_info, inp, status): print "<pre>" special_position_settings = io_utils.special_position_settings_from_inp(inp) special_position_settings.show_summary() print "Minimum distance between symmetrically equivalent sites:", print special_position_settings.min_distance_sym_equiv() print d_min = float(inp.d_min) print "Minimum d-spacing:", d_min if (d_min <= 0.): raise ValueError, "d-spacing must be greater than zero." print structure = io_utils.structure_from_inp( inp, status, special_position_settings) algorithm = inp.algorithm if (algorithm == "automatic"): if (structure.scatterers().size() <= 100): algorithm = "direct" else: algorithm = None elif (algorithm not in ["direct", "fft"]): algorithm = None f_calc_manager = structure.structure_factors( anomalous_flag=False, d_min=d_min, algorithm=algorithm) f_calc = f_calc_manager.f_calc() structure.scattering_type_registry().show() print print "Number of Miller indices:", f_calc.indices().size() print print "Structure factor algorithm:", f_calc_manager.algorithm(verbose=True) print print "</pre><table border=2 cellpadding=2>" status.in_table = True print "<tr>" print "<th>hkl<th>Amplitude<th>Phase" for i,h in enumerate(f_calc.indices()): print "<tr>" print "<td>%3d %3d %3d<td>%.6g<td align=right>%.3f" % ( h + complex_math.abs_arg(f_calc.data()[i], deg=True)) print "</table><pre>" status.in_table = False print print "</pre>"
def run(server_info, inp, status): print "<h2>Structure factor calculation</h2>" print "<pre>" print "Atomic form factors: %s" % inp.form_factors print print "Minimum d-spacing (highest resolution): %s" % inp.d_min if (float(inp.d_min) <= 0.): raise ValueError("d-spacing must be greater than zero.") print structure = io_utils.structure_from_inp_pdb(inp, status) structure.scattering_type_registry( table=inp.form_factors, d_min=float(inp.d_min)) print "Input model summary:" print structure.show_summary() print structure.scattering_type_registry().show() print algorithm = inp.algorithm if (algorithm == "automatic"): if (structure.scatterers().size() <= 100): algorithm = "direct" else: algorithm = None elif (algorithm not in ["direct", "fft"]): algorithm = None f_calc_manager = structure.structure_factors( anomalous_flag = False, algorithm = algorithm, d_min = float(inp.d_min)) f_calc = f_calc_manager.f_calc() print "Number of Miller indices:", f_calc.indices().size() print print "Structure factor algorithm:", f_calc_manager.algorithm(verbose=True) print print " h k l Fcalc Pcalc" for h,f in zip(f_calc.indices(), f_calc.data()): print "%5d %5d %5d %15.6f %8.2f" % (h + complex_math.abs_arg(f, deg=True)) print print "</pre>"
def write_cns_input(fcalc_array, hl, test_merge=False): assert fcalc_array.data().size() == hl.size() cns_input = make_cns_input.xray_unit_cell(fcalc_array.unit_cell()) cns_input += make_cns_input.xray_symmetry(fcalc_array.space_group()) cns_input += make_cns_input.xray_anomalous(fcalc_array.anomalous_flag()) l = cns_input.append l("xray") l(" declare name=fcalc domain=reciprocal type=complex end") l(" declare name=pa domain=reciprocal type=real end") l(" declare name=pb domain=reciprocal type=real end") l(" declare name=pc domain=reciprocal type=real end") l(" declare name=pd domain=reciprocal type=real end") l(" group type hl") l(" object pa") l(" object pb") l(" object pc") l(" object pd") l(" end") l(" reflection") for i, h in enumerate(fcalc_array.indices()): l((" index %d %d %d" % h) + (" fcalc=%.6g %.6g\n" % abs_arg(fcalc_array.data()[i], deg=True)) + (" pa=%.6g pb=%.6g pc=%.6g pd=%.6g" % hl[i])) l(" end") if (not test_merge): l(" declare name=pi domain=reciprocal type=complex end") l(" do (pi=get_fom[phistep=5,CEN360=false](pa,pb,pc,pd)) (all)") l(" write reflections output=\"tmp_sg.hkl\" end") l(" expand") l(" do (pi=get_fom[phistep=5,CEN360=false](pa,pb,pc,pd)) (all)") l(" write reflections output=\"tmp_p1.hkl\" end") else: l(" anomalous=false") l(" write reflections output=\"tmp_merged.hkl\" end") l("end") l("stop") f = open("tmp.cns", "w") for l in cns_input: print(l, file=f) f.close()
def write_cns_input(fcalc_array, hl, test_merge=False): assert fcalc_array.data().size() == hl.size() cns_input = make_cns_input.xray_unit_cell(fcalc_array.unit_cell()) cns_input += make_cns_input.xray_symmetry(fcalc_array.space_group()) cns_input += make_cns_input.xray_anomalous(fcalc_array.anomalous_flag()) l = cns_input.append l("xray") l(" declare name=fcalc domain=reciprocal type=complex end") l(" declare name=pa domain=reciprocal type=real end") l(" declare name=pb domain=reciprocal type=real end") l(" declare name=pc domain=reciprocal type=real end") l(" declare name=pd domain=reciprocal type=real end") l(" group type hl") l(" object pa") l(" object pb") l(" object pc") l(" object pd") l(" end") l(" reflection") for i,h in enumerate(fcalc_array.indices()): l( (" index %d %d %d" % h) + (" fcalc=%.6g %.6g\n" % abs_arg(fcalc_array.data()[i], deg=True)) + (" pa=%.6g pb=%.6g pc=%.6g pd=%.6g" % hl[i])) l(" end") if (not test_merge): l(" declare name=pi domain=reciprocal type=complex end") l(" do (pi=get_fom[phistep=5,CEN360=false](pa,pb,pc,pd)) (all)") l(" write reflections output=\"tmp_sg.hkl\" end") l(" expand") l(" do (pi=get_fom[phistep=5,CEN360=false](pa,pb,pc,pd)) (all)") l(" write reflections output=\"tmp_p1.hkl\" end") else: l(" anomalous=false") l(" write reflections output=\"tmp_merged.hkl\" end") l("end") l("stop") f = open("tmp.cns", "w") for l in cns_input: print >> f, l f.close()
def exercise(space_group_info, n_scatterers=8, d_min=2, verbose=0, e_min=1.5): structure = random_structure.xray_structure(space_group_info, elements=["const"] * n_scatterers, volume_per_atom=200, min_distance=3., general_positions_only=True, u_iso=0.0) if (0 or verbose): structure.show_summary().show_scatterers() f_calc = structure.structure_factors(d_min=d_min, anomalous_flag=False).f_calc() f_obs = abs(f_calc) q_obs = miller.array( miller_set=f_obs, data=f_obs.data() / math.sqrt(f_obs.space_group().order_p() * n_scatterers) / f_obs.space_group().n_ltr()) q_obs = q_obs.sort(by_value="abs") q_obs.setup_binner(auto_binning=True) n_obs = q_obs.quasi_normalize_structure_factors() r = flex.linear_regression(q_obs.data(), n_obs.data()) if (0 or verbose): r.show_summary() assert r.is_well_defined() assert abs(r.y_intercept()) < 0.1 assert abs(r.slope() - 1) < 0.2 q_large = q_obs.select( q_obs.quasi_normalized_as_normalized().data() > e_min) if (0 or verbose): print("Number of e-values > %.6g: %d" % (e_min, q_large.size())) other_structure = random_structure.xray_structure( space_group_info, elements=["const"] * n_scatterers, volume_per_atom=200, min_distance=3., general_positions_only=True, u_iso=0.0) assert other_structure.unit_cell().is_similar_to(structure.unit_cell()) q_calc = q_large.structure_factors_from_scatterers( other_structure, algorithm="direct").f_calc() start = q_large.phase_transfer(q_calc.data()) for selection_fixed in (None, flex.double( [random.random() for i in range(start.size())]) < 0.4): from_map_data = direct_space_squaring(start, selection_fixed) direct_space_result = start.phase_transfer(phase_source=from_map_data) new_phases = reciprocal_space_squaring(start, selection_fixed, verbose) reciprocal_space_result = start.phase_transfer( phase_source=flex.polar(1, new_phases)) mwpe = direct_space_result.mean_weighted_phase_error( reciprocal_space_result) if (0 or verbose): print("mwpe: %.2f" % mwpe, start.space_group_info()) for i, h in enumerate(direct_space_result.indices()): amp_d, phi_d = complex_math.abs_arg(direct_space_result.data()[i], deg=True) amp_r, phi_r = complex_math.abs_arg( reciprocal_space_result.data()[i], deg=True) phase_err = scitbx.math.phase_error(phi_d, phi_r, deg=True) assert phase_err < 1.0 or abs(from_map_data[i]) < 1.e-6 exercise_truncate(q_large)