def run(): structure = random_structure.xray_structure( sgtbx.space_group_info("P21/c"), elements=["Si"] * 10, volume_per_atom=18.6, min_distance=1.2, general_positions_only=False) miller_set_f_obs = miller.build_set(crystal_symmetry=structure, anomalous_flag=True, d_min=0.8) f_obs = miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc() fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry) padded = fft_map.real_map() unpadded = fft_map.real_map_unpadded() # copy unpadded_1d = unpadded.as_1d() # 1D view => in-place mmm = flex.min_max_mean_double(unpadded_1d) for delta in ((mmm.min + mmm.mean) / 2, mmm.mean, (mmm.mean + mmm.max) / 2): # in-place charge flipping ab_initio.ext.flip_charges_in_place(padded, delta) # same but on an unpadded copy using the flex tools flipped_selection = unpadded_1d < delta flipped = unpadded_1d.select(flipped_selection) flipped *= -1 unpadded_1d.set_selected(flipped_selection, flipped) assert approx_equal(padded, unpadded, 1e-15) print(format_cpu_times())
def run(): structure = random_structure.xray_structure( sgtbx.space_group_info("P21/c"), elements=["Si"]*10, volume_per_atom=18.6, min_distance=1.2, general_positions_only=False) miller_set_f_obs = miller.build_set( crystal_symmetry=structure, anomalous_flag=True, d_min=0.8) f_obs = miller_set_f_obs.structure_factors_from_scatterers( xray_structure=structure, algorithm="direct").f_calc() fft_map = f_obs.fft_map(symmetry_flags=maptbx.use_space_group_symmetry) padded = fft_map.real_map() unpadded = fft_map.real_map_unpadded() # copy unpadded_1d = unpadded.as_1d() # 1D view => in-place mmm = flex.min_max_mean_double(unpadded_1d) for delta in ((mmm.min + mmm.mean)/2, mmm.mean, (mmm.mean + mmm.max)/2): # in-place charge flipping ab_initio.ext.flip_charges_in_place(padded, delta) # same but on an unpadded copy using the flex tools flipped_selection = unpadded_1d < delta flipped = unpadded_1d.select(flipped_selection) flipped *= -1 unpadded_1d.set_selected(flipped_selection, flipped) assert approx_equal(padded, unpadded, 1e-15) print format_cpu_times()
def plot_uc_vs_detector_distance( uc_params, panel_distances, outliers, steps_per_angstrom=20, filename="uc_vs_distance.png", ): from matplotlib import pyplot as plt plt.style.use("ggplot") f = plt.figure(figsize=(12, 8)) ax1 = plt.subplot2grid((2, 3), (0, 0)) ax2 = plt.subplot2grid((2, 3), (0, 1), sharey=ax1) ax3 = plt.subplot2grid((2, 3), (0, 2), sharey=ax1) ax4 = plt.subplot2grid((2, 3), (1, 0), colspan=3) a, b, c = uc_params[:3] def hist2d(p1, p2, ax): nbins = 100 H, xedges, yedges = np.histogram2d(p1, p2, bins=nbins) H = np.rot90(H) H = np.flipud(H) Hmasked = np.ma.masked_where(H == 0, H) ax.pcolormesh(xedges, yedges, Hmasked) hist2d(a, panel_distances[0], ax1) hist2d(b, panel_distances[0], ax2) hist2d(c, panel_distances[0], ax3) mmm = flex.min_max_mean_double(panel_distances[0]) steps_per_mm = 20 Amin = math.floor(mmm.min * steps_per_mm) / steps_per_mm Amax = math.floor(mmm.max * steps_per_mm) / steps_per_mm n_slots = max(1, int((Amax - Amin) * steps_per_mm)) if Amin == Amax: eps = 0.05 Amin -= eps Amax += eps hist = flex.histogram(panel_distances[0], Amin, Amax, n_slots=n_slots) ax4.bar( hist.slot_centers(), hist.slots(), align="center", width=hist.slot_width(), zorder=10, color="red", edgecolor=None, linewidth=0, ) ax1.set_ylabel("Detector distance (mm)") ax1.set_xlabel(r"a ($\AA$)") ax2.set_xlabel(r"b ($\AA$)") ax3.set_xlabel(r"c ($\AA$)") ax4.set_xlabel("Detector distance (mm)") f.savefig(filename) plt.tight_layout() f.clf()
def plot_uc_histograms( uc_params, outliers, steps_per_angstrom=20, plot_name="uc_histograms.png" ): from matplotlib import pyplot as plt plt.style.use("ggplot") f, ax = plt.subplots(nrows=2, ncols=3, figsize=(12, 8)) a, b, c = uc_params[:3] def uc_param_hist2d(p1, p2, ax): nbins = 100 H, xedges, yedges = np.histogram2d(p1, p2, bins=nbins) H = np.rot90(H) H = np.flipud(H) Hmasked = np.ma.masked_where(H == 0, H) ax.pcolormesh(xedges, yedges, Hmasked) uc_param_hist2d(a, b, ax[0][0]) uc_param_hist2d(b, c, ax[0][1]) uc_param_hist2d(c, a, ax[0][2]) for i in range(3): mmm = flex.min_max_mean_double(uc_params[i]) steps_per_A = steps_per_angstrom Amin = math.floor(mmm.min * steps_per_A) / steps_per_A Amax = math.floor(mmm.max * steps_per_A) / steps_per_A n_slots = max(1, int((Amax - Amin) * steps_per_A)) if Amin == Amax: eps = 0.05 Amin -= eps Amax += eps hist = flex.histogram(uc_params[i], Amin, Amax, n_slots=n_slots) hist_inliers = flex.histogram( uc_params[i].select(~outliers), Amin, Amax, n_slots=n_slots ) ax[1][i].bar( hist.slot_centers(), hist.slots(), align="center", width=hist.slot_width(), zorder=10, color="black", edgecolor=None, linewidth=0, ) ax[1][i].bar( hist_inliers.slot_centers(), hist_inliers.slots(), align="center", width=hist_inliers.slot_width(), zorder=10, color="red", edgecolor=None, linewidth=0, ) ax[0][i].set_xlim(ax[1][i].get_xlim()) ax[0][0].set_ylabel(r"b ($\AA$)") ax[0][1].set_ylabel(r"c ($\AA$)") ax[0][2].set_ylabel(r"a ($\AA$)") ax[1][0].set_xlabel(r"a ($\AA$)") ax[1][1].set_xlabel(r"b ($\AA$)") ax[1][2].set_xlabel(r"c ($\AA$)") f.savefig(plot_name) plt.tight_layout() plt.close(f)