def exercise_value(f_c_in_p1, f_o, flags, n_sampled): """ where we check against the trusted correlation map implemented in cctbx.translation_search """ crystal_gridding = f_o.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/3) correlation_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_o.space_group(), anomalous_flag=f_o.anomalous_flag(), miller_indices_f_obs=f_o.indices(), f_obs=f_o.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_c_in_p1.indices(), p1_f_calc=f_c_in_p1.data()).target_map() nx, ny, nz = n = crystal_gridding.n_real() def sampled_indices(): from random import randrange yield (0,0,0) for i in xrange(n_sampled - 1): yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2]) f_o_sq = f_o.as_intensity_array() for i,j,k in sampled_indices(): x = (i/nx, j/ny, k/nz) if random.random() > 0.5: f_o_or_f_o_sq = f_o else: f_o_or_f_o_sq = f_o_sq gos = symmetrised_shifted_structure_factors( f_o_or_f_o_sq, f_c_in_p1, x).misfit(f_o_or_f_o_sq) assert approx_equal(gos.correlation, correlation_map[i,j,k]), (i,j,k)
def exercise_value(f_c_in_p1, f_o, flags, n_sampled): """ where we check against the trusted correlation map implemented in cctbx.translation_search """ crystal_gridding = f_o.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1 / 3) correlation_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_o.space_group(), anomalous_flag=f_o.anomalous_flag(), miller_indices_f_obs=f_o.indices(), f_obs=f_o.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_c_in_p1.indices(), p1_f_calc=f_c_in_p1.data()).target_map() nx, ny, nz = n = crystal_gridding.n_real() def sampled_indices(): from random import randrange yield (0, 0, 0) for i in xrange(n_sampled - 1): yield randrange(0, n[0]), randrange(0, n[1]), randrange(0, n[2]) f_o_sq = f_o.as_intensity_array() for i, j, k in sampled_indices(): x = (i / nx, j / ny, k / nz) if random.random() > 0.5: f_o_or_f_o_sq = f_o else: f_o_or_f_o_sq = f_o_sq gos = symmetrised_shifted_structure_factors(f_o_or_f_o_sq, f_c_in_p1, x).misfit(f_o_or_f_o_sq) assert approx_equal(gos.correlation, correlation_map[i, j, k]), (i, j, k)
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C']*6+['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/2 ) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def exercise_symmetry_flags(): for i_flags in xrange(4): is_isotropic_search_model = (i_flags % 2 != 0) have_f_part = ((i_flags // 2) % 2 != 0) f = translation_search.symmetry_flags( is_isotropic_search_model=is_isotropic_search_model, have_f_part=have_f_part) assert f.is_isotropic_search_model() == is_isotropic_search_model assert f.have_f_part() == have_f_part assert f.use_space_group_symmetry() == is_isotropic_search_model assert f.use_normalizer_k2l() \ == (is_isotropic_search_model and (not have_f_part)) assert f.use_seminvariants() == (not have_f_part)
def exercise_symmetry_flags(): for i_flags in xrange(4): is_isotropic_search_model = (i_flags % 2 != 0) have_f_part = ((i_flags//2) % 2 != 0) f = translation_search.symmetry_flags( is_isotropic_search_model=is_isotropic_search_model, have_f_part=have_f_part) assert f.is_isotropic_search_model() == is_isotropic_search_model assert f.have_f_part() == have_f_part assert f.use_space_group_symmetry() == is_isotropic_search_model assert f.use_normalizer_k2l() \ == (is_isotropic_search_model and (not have_f_part)) assert f.use_seminvariants() == (not have_f_part)
def f_calc_symmetrisations(f_obs, f_calc_in_p1, min_cc_peak_height): # The fast correlation map as per cctbx.translation_search.fast_nv1995 # is computed and its peaks studied. # Inspiration from phenix.substructure.hyss for the parameters tuning. if 0: # Display f_calc_in_p1 from crys3d.qttbx import map_viewer map_viewer.display(window_title="f_calc in P1 before fast CC", fft_map=f_calc_in_p1.fft_map(), iso_level_positive_range_fraction=0.8) crystal_gridding = f_obs.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1/3 ) correlation_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc_in_p1.indices(), p1_f_calc=f_calc_in_p1.data()).target_map() if 0: # Display correlation_map from crys3d.qttbx import map_viewer map_viewer.display(window_title="Fast CC map", raw_map=correlation_map, unit_cell=f_calc_in_p1.unit_cell(), positive_iso_level=0.8) search_parameters = maptbx.peak_search_parameters( peak_search_level=1, peak_cutoff=0.5, interpolate=True, min_distance_sym_equiv=1e-6, general_positions_only=False, min_cross_distance=f_obs.d_min()/2) ## The correlation map is not a miller.fft_map, just a 3D flex.double correlation_map_peaks = crystal_gridding.tags().peak_search( map=correlation_map, parameters=search_parameters) # iterate over the strong peak; for each, shift and symmetrised f_calc for peak in correlation_map_peaks: if peak.height < min_cc_peak_height: break sr = symmetry_search.shift_refinement( f_obs, f_calc_in_p1, peak.site) yield sr.symmetrised_shifted_sf.f_x, sr.shift, sr.goos.correlation
def run(): target_structure = random_structure.xray_structure( space_group_info=sgtbx.space_group_info("I432"), elements=['C'] * 6 + ['O'], use_u_iso=False, use_u_aniso=True, ) shift = tuple(flex.random_double(3)) print "shift to be found: (%.3f, %.3f, %.3f)" % shift target_structure_in_p1 = target_structure.expand_to_p1().apply_shift(shift) miller_indices = miller.build_set(crystal_symmetry=target_structure, anomalous_flag=True, d_min=0.8) f_obs = miller_indices.structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc().amplitudes() miller_indices_in_p1 = miller.build_set( crystal_symmetry=target_structure_in_p1, anomalous_flag=True, d_min=0.8) f_calc = miller_indices_in_p1.structure_factors_from_scatterers( xray_structure=target_structure_in_p1, algorithm="direct").f_calc() crystal_gridding = f_calc.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1 / 2) omptbx.env.num_threads = 1 t_fast_tf = show_times() fast_tf_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc.indices(), p1_f_calc=f_calc.data()).target_map() print print "Fast translation function" t_fast_tf() t_cross_corr = show_times() for op in target_structure.space_group(): f, op_times_f = f_calc.original_and_transformed(op) cross_corr_map = miller.fft_map(crystal_gridding, f * op_times_f.conjugate().data()) print print "Traditional cross-correlation" t_cross_corr()
def f_calc_symmetrisations(f_obs, f_calc_in_p1, min_cc_peak_height): # The fast correlation map as per cctbx.translation_search.fast_nv1995 # is computed and its peaks studied. # Inspiration from phenix.substructure.hyss for the parameters tuning. if 0: # Display f_calc_in_p1 from crys3d.qttbx import map_viewer map_viewer.display(window_title="f_calc in P1 before fast CC", fft_map=f_calc_in_p1.fft_map(), iso_level_positive_range_fraction=0.8) crystal_gridding = f_obs.crystal_gridding( symmetry_flags=translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False), resolution_factor=1 / 3) correlation_map = translation_search.fast_nv1995( gridding=crystal_gridding.n_real(), space_group=f_obs.space_group(), anomalous_flag=f_obs.anomalous_flag(), miller_indices_f_obs=f_obs.indices(), f_obs=f_obs.data(), f_part=flex.complex_double(), ## no sub-structure is already fixed miller_indices_p1_f_calc=f_calc_in_p1.indices(), p1_f_calc=f_calc_in_p1.data()).target_map() if 0: # Display correlation_map from crys3d.qttbx import map_viewer map_viewer.display(window_title="Fast CC map", raw_map=correlation_map, unit_cell=f_calc_in_p1.unit_cell(), positive_iso_level=0.8) search_parameters = maptbx.peak_search_parameters( peak_search_level=1, peak_cutoff=0.5, interpolate=True, min_distance_sym_equiv=1e-6, general_positions_only=False, min_cross_distance=f_obs.d_min() / 2) ## The correlation map is not a miller.fft_map, just a 3D flex.double correlation_map_peaks = crystal_gridding.tags().peak_search( map=correlation_map, parameters=search_parameters) # iterate over the strong peak; for each, shift and symmetrised f_calc for peak in correlation_map_peaks: if peak.height < min_cc_peak_height: break sr = symmetry_search.shift_refinement(f_obs, f_calc_in_p1, peak.site) yield sr.symmetrised_shifted_sf.f_x, sr.shift, sr.goos.correlation
def test_shift(space_group_info, d_min=0.8, grid_resolution_factor=0.48, max_prime=5, verbose=0): n = 12 // len(space_group_info.group()) or 1 target_structure = random_structure.xray_structure( space_group_info=space_group_info, elements=['C']*n, use_u_iso=False, use_u_aniso=False, ) f_target = miller.build_set( crystal_symmetry=target_structure, anomalous_flag=False, d_min=d_min ).structure_factors_from_scatterers( xray_structure=target_structure, algorithm="direct").f_calc() f_obs = abs(f_target) indices_in_p1 = miller.set.expand_to_p1(f_target) target_structure_in_p1 = target_structure.expand_to_p1() reference_translation = matrix.col((0.1, 0.2, 0.7)) structure_in_p1 = target_structure_in_p1.apply_shift(reference_translation) f_structure_in_p1 = indices_in_p1.structure_factors_from_scatterers( xray_structure=structure_in_p1, algorithm="direct").f_calc() symmetry_flags = translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=False) gridding = f_target.crystal_gridding( symmetry_flags=symmetry_flags, resolution_factor=grid_resolution_factor, max_prime=max_prime).n_real() grid_tags = maptbx.grid_tags(gridding) for f_calc_in_p1 in (f_structure_in_p1,): peak_list = run_fast_nv1995( f_obs=f_obs, f_calc_fixed=None, f_calc_p1=f_calc_in_p1, symmetry_flags=symmetry_flags, gridding=gridding, grid_tags=grid_tags, verbose=verbose) assert peak_list.heights()[0] > 0.9 shift = matrix.col(peak_list.sites()[0]) assert f_target.space_group_info().is_allowed_origin_shift( shift + reference_translation, tolerance=0.04)
def test_molecule(space_group_info, use_primitive_setting, flag_f_part, d_min=3., grid_resolution_factor=0.48, max_prime=5, verbose=0): if (use_primitive_setting): space_group_info = space_group_info.primitive_setting() elements = ("N", "C", "C", "O", "N", "C", "C", "O") structure = random_structure.xray_structure( space_group_info, elements=elements, volume_per_atom=50, min_distance=1., general_positions_only=True, random_u_iso=True, random_occupancy=True) if (0 or verbose): structure.show_summary().show_scatterers() miller_set_f_obs = miller.build_set( crystal_symmetry=structure, anomalous_flag=(random.random() < 0.5), d_min=d_min) f_obs = abs(miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc()) if (0 or verbose): f_obs.show_summary() if (0 or verbose): f_obs.show_array() miller_set_p1 = miller.set.expand_to_p1(f_obs) special_position_settings_p1 = crystal.special_position_settings( crystal_symmetry=miller_set_p1) structure_p1 = xray.structure( special_position_settings=special_position_settings_p1) structure_fixed = xray.structure(special_position_settings=structure) for scatterer in structure.scatterers(): if (flag_f_part and structure_fixed.scatterers().size() < structure.scatterers().size()//2): structure_fixed.add_scatterer(scatterer) else: structure_p1.add_scatterer(scatterer) if (0 or verbose): if (flag_f_part): structure_fixed.show_summary().show_scatterers() structure_p1.show_summary().show_scatterers() f_calc_fixed = None if (flag_f_part): f_calc_fixed = f_obs.structure_factors_from_scatterers( xray_structure=structure_fixed, algorithm="direct").f_calc() f_calc_p1 = miller_set_p1.structure_factors_from_scatterers( xray_structure=structure_p1, algorithm="direct").f_calc() symmetry_flags = translation_search.symmetry_flags( is_isotropic_search_model=False, have_f_part=flag_f_part) gridding = miller_set_f_obs.crystal_gridding( symmetry_flags=symmetry_flags, resolution_factor=grid_resolution_factor, max_prime=max_prime).n_real() grid_tags = maptbx.grid_tags(gridding) run_fast_terms( structure_fixed, structure_p1, f_obs, f_calc_fixed, f_calc_p1, symmetry_flags, gridding, grid_tags, test_origin=True, verbose=verbose) peak_list = run_fast_nv1995( f_obs, f_calc_fixed, f_calc_p1, symmetry_flags, gridding, grid_tags, verbose) assert peak_list.heights()[0] > 0.99
def test_atom(space_group_info, use_primitive_setting, n_elements=3, d_min=3., grid_resolution_factor=0.48, max_prime=5, verbose=0): if (use_primitive_setting): space_group_info = space_group_info.primitive_setting() structure = random_structure.xray_structure( space_group_info, n_scatterers=n_elements, volume_per_atom=150, min_distance=1., general_positions_only=True) miller_set_f_obs = miller.build_set( crystal_symmetry=structure, anomalous_flag=(random.random() < 0.5), d_min=d_min) symmetry_flags = translation_search.symmetry_flags( is_isotropic_search_model=True, have_f_part=(n_elements>=2)) gridding = miller_set_f_obs.crystal_gridding( symmetry_flags=symmetry_flags, resolution_factor=grid_resolution_factor, max_prime=max_prime).n_real() structure.build_scatterers( elements=["Se"]*n_elements, grid=gridding) if (0 or verbose): structure.show_summary().show_scatterers() f_obs = abs(miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc()) if (0 or verbose): f_obs.show_summary() if (0 or verbose): f_obs.show_array() miller_set_p1 = miller.set.expand_to_p1(f_obs) special_position_settings_p1 = crystal.special_position_settings( crystal_symmetry=miller_set_p1) structure_fixed = xray.structure(special_position_settings=structure) for scatterer in structure.scatterers(): structure_p1 = xray.structure( special_position_settings=special_position_settings_p1) scatterer_at_origin = scatterer.customized_copy(site=(0,0,0)) structure_p1.add_scatterer(scatterer_at_origin) if (0 or verbose): structure_p1.show_summary().show_scatterers() f_calc_p1 = miller_set_p1.structure_factors_from_scatterers( xray_structure=structure_p1, algorithm="direct").f_calc() if (0 or verbose): f_calc_p1.show_array() f_calc_fixed = None if (structure_fixed.scatterers().size() > 0): f_calc_fixed = f_obs.structure_factors_from_scatterers( xray_structure=structure_fixed, algorithm="direct").f_calc() symmetry_flags = translation_search.symmetry_flags( is_isotropic_search_model=True, have_f_part=(f_calc_fixed is not None)) if (structure_fixed.scatterers().size() <= 1): gridding = miller_set_f_obs.crystal_gridding( symmetry_flags=symmetry_flags, resolution_factor=grid_resolution_factor, max_prime=max_prime).n_real() grid_tags = maptbx.grid_tags(gridding) run_fast_terms( structure_fixed, structure_p1, f_obs, f_calc_fixed, f_calc_p1, symmetry_flags, gridding, grid_tags, verbose=verbose) peak_list = run_fast_nv1995( f_obs, f_calc_fixed, f_calc_p1, symmetry_flags, gridding, grid_tags, verbose) structure_fixed.add_scatterer(scatterer) if (0 or verbose): structure_fixed.show_summary().show_scatterers() if (structure_fixed.scatterers().size() < n_elements): assert peak_list.heights()[0] < 1 else: assert peak_list.heights()[0] > 0.99 assert peak_list.heights()[0] > 0.99