def map_spots_pixel_to_mm_rad(spots, detector, scan): from dials.algorithms.centroid import centroid_px_to_mm_panel ## ideally don't copy, but have separate spot attributes for mm and pixel import copy spots = copy.deepcopy(spots) spots["xyzcal.mm"] = flex.vec3_double(len(spots)) # spots['xyzobs.mm.variance'] = flex.vec3_double(len(spots)) dummy_variance = flex.vec3_double(len(spots)) panel_numbers = flex.size_t(spots["panel"]) for i_panel in range(len(detector)): sel = panel_numbers == i_panel isel = sel.iselection() spots_panel = spots.select(panel_numbers == i_panel) centroid_position, centroid_variance, _ = centroid_px_to_mm_panel( detector[i_panel], scan, spots_panel["xyzcal.px"], dummy_variance, flex.vec3_double(len(spots_panel), (1, 1, 1)), ) spots["xyzcal.mm"].set_selected(sel, centroid_position) # spots['xyzobs.mm.variance'].set_selected(sel, centroid_variance) return spots
def px_to_mm(experiment, table): # reference: dials/algorithms/indexing/indexer.py map_spots_pixel_to_mm_rad() centroid_position, centroid_variance, _ = centroid_px_to_mm_panel( experiment.detector[0], experiment.scan, table['xyzobs.px.value'], table['xyzobs.px.variance'], flex.vec3_double(len(table), (1, 1, 1))) print centroid_position table['xyzobs.mm.value'] = centroid_position table['xyzobs.mm.variance'] = centroid_variance if "xyzcal.px.value" not in table: return centroid_position, centroid_variance, _ = centroid_px_to_mm_panel( experiment.detector[0], experiment.scan, table['xyzcal.px.value'], flex.vec3_double(len(table), (1, 1, 1)), flex.vec3_double(len(table), (1, 1, 1))) table['xyzcal.mm'] = centroid_position
def map_spots_pixel_to_mm_rad(spots, detector, scan): from dials.algorithms.centroid import centroid_px_to_mm_panel ## ideally don't copy, but have separate spot attributes for mm and pixel import copy spots = copy.deepcopy(spots) spots['xyzcal.mm'] = flex.vec3_double(len(spots)) #spots['xyzobs.mm.variance'] = flex.vec3_double(len(spots)) dummy_variance = flex.vec3_double(len(spots)) panel_numbers = flex.size_t(spots['panel']) for i_panel in range(len(detector)): sel = (panel_numbers == i_panel) isel = sel.iselection() spots_panel = spots.select(panel_numbers == i_panel) centroid_position, centroid_variance, _ = centroid_px_to_mm_panel( detector[i_panel], scan, spots_panel['xyzcal.px'], dummy_variance, flex.vec3_double(len(spots_panel), (1,1,1))) spots['xyzcal.mm'].set_selected(sel, centroid_position) #spots['xyzobs.mm.variance'].set_selected(sel, centroid_variance) return spots
def centroid_px_to_mm(self, experiments): """ Map spot centroids from pixel/image number to mm/radian. Used to convert spot centroids coming from e.g. dials.find_spots which are in pixel/image number units to mm/radian units as required for indexing and refinement. Args: experiments (dxtbx.model.ExperimentList): A list of experiments. """ self["xyzobs.mm.value"] = cctbx.array_family.flex.vec3_double(len(self)) self["xyzobs.mm.variance"] = cctbx.array_family.flex.vec3_double(len(self)) # e.g. data imported from XDS; no variance known then; since is used # only for weights assign as 1 => uniform weights if "xyzobs.px.variance" not in self: self["xyzobs.px.variance"] = cctbx.array_family.flex.vec3_double( len(self), (1, 1, 1) ) panel_numbers = cctbx.array_family.flex.size_t(self["panel"]) for i, expt in enumerate(experiments): if "imageset_id" in self: sel_expt = self["imageset_id"] == i else: sel_expt = self["id"] == i for i_panel in range(len(expt.detector)): sel = sel_expt & (panel_numbers == i_panel) centroid_position, centroid_variance, _ = centroid_px_to_mm_panel( expt.detector[i_panel], expt.scan, self["xyzobs.px.value"].select(sel), self["xyzobs.px.variance"].select(sel), cctbx.array_family.flex.vec3_double(sel.count(True), (1, 1, 1)), ) self["xyzobs.mm.value"].set_selected(sel, centroid_position) self["xyzobs.mm.variance"].set_selected(sel, centroid_variance)
if 'xyzobs.px.value' in reflection_list: xyzobs_px = reflection_list['xyzobs.px.value'] if 'xyzcal.mm' in reflection_list: xyzcal_mm = reflection_list['xyzcal.mm'] if 'xyzobs.mm.value' in reflection_list: xyzobs_mm = reflection_list['xyzobs.mm.value'] panel_ids = reflection_list['panel'] if xyzobs_mm is None and xyzobs_px is not None: xyzobs_mm = flex.vec3_double() for i_panel in range(len(detector)): xyzobs_px_panel = xyzobs_px.select(panel_ids == i_panel) from dials.algorithms.centroid import centroid_px_to_mm_panel xyzobs_mm_panel, _, _ = centroid_px_to_mm_panel( detector[i_panel], scan, xyzobs_px_panel, flex.vec3_double(xyzobs_px_panel.size()), flex.vec3_double(xyzobs_px_panel.size())) xyzobs_mm.extend(xyzobs_mm_panel) if xyzobs_mm is not None: observed_xyz.extend(xyzobs_mm) if xyzcal_mm is not None: predicted_xyz.extend(xyzcal_mm) obs_x, obs_y, _ = observed_xyz.parts() pred_x, pred_y, _ = predicted_xyz.parts() try: import matplotlib if not params.output.show_plot:
def run(args): usage = "dials.plot_reflections models.expt observations.refl [options]" from dials.util.options import OptionParser from dials.util.options import flatten_experiments from dials.util.options import flatten_reflections from scitbx.array_family import flex from scitbx import matrix parser = OptionParser( usage=usage, phil=master_phil_scope, read_experiments=True, read_reflections=True, check_format=False, ) params, options = parser.parse_args(show_diff_phil=True) reflections = flatten_reflections(params.input.reflections) experiments = flatten_experiments(params.input.experiments) if len(experiments.imagesets()) > 0: imageset = experiments.imagesets()[0] imageset.set_detector(experiments[0].detector) imageset.set_beam(experiments[0].beam) imageset.set_goniometer(experiments[0].goniometer) else: parser.print_help() return detector = imageset.get_detector() scan = imageset.get_scan() panel_origin_shifts = {0: (0, 0, 0)} try: hierarchy = detector.hierarchy() except AttributeError: hierarchy = None for i_panel in range(1, len(detector)): origin_shift = matrix.col(detector[0].get_origin()) - matrix.col( detector[i_panel].get_origin()) panel_origin_shifts[i_panel] = origin_shift observed_xyz = flex.vec3_double() predicted_xyz = flex.vec3_double() for reflection_list in reflections: if len(params.scan_range): sel = flex.bool(len(reflection_list), False) xyzcal_px = None xyzobs_px = None if "xyzcal.px" in reflection_list: xyzcal_px = reflection_list["xyzcal.px"] if "xyzobs.px.value" in reflection_list: xyzobs_px = reflection_list["xyzobs.px.value"] if xyzcal_px is not None and not xyzcal_px.norms().all_eq(0): centroids_frame = xyzcal_px.parts()[2] elif xyzobs_px is not None and not xyzobs_px.norms().all_eq(0): centroids_frame = xyzobs_px.parts()[2] else: raise Sorry("No pixel coordinates given in input reflections.") for scan_range in params.scan_range: if scan_range is None: continue range_start, range_end = scan_range sel |= (centroids_frame >= range_start) & (centroids_frame < range_end) reflection_list = reflection_list.select(sel) if params.first_n_reflections is not None: centroid_positions = reflection_list.centroid_position() centroids_frame = centroid_positions.parts()[2] perm = flex.sort_permutation(centroids_frame) perm = perm[:min(reflection_list.size(), params.first_n_reflections )] reflection_list = reflection_list.select(perm) if params.crystal_id is not None: reflection_list = reflection_list.select( reflection_list["id"] == params.crystal_id) xyzcal_px = None xyzcal_px = None xyzobs_mm = None xyzcal_mm = None if "xyzcal.px" in reflection_list: xyzcal_px = reflection_list["xyzcal.px"] if "xyzobs.px.value" in reflection_list: xyzobs_px = reflection_list["xyzobs.px.value"] if "xyzcal.mm" in reflection_list: xyzcal_mm = reflection_list["xyzcal.mm"] if "xyzobs.mm.value" in reflection_list: xyzobs_mm = reflection_list["xyzobs.mm.value"] panel_ids = reflection_list["panel"] if xyzobs_mm is None and xyzobs_px is not None: xyzobs_mm = flex.vec3_double() for i_panel in range(len(detector)): xyzobs_px_panel = xyzobs_px.select(panel_ids == i_panel) from dials.algorithms.centroid import centroid_px_to_mm_panel xyzobs_mm_panel, _, _ = centroid_px_to_mm_panel( detector[i_panel], scan, xyzobs_px_panel, flex.vec3_double(xyzobs_px_panel.size()), flex.vec3_double(xyzobs_px_panel.size()), ) xyzobs_mm.extend(xyzobs_mm_panel) if xyzobs_mm is not None: observed_xyz.extend(xyzobs_mm) if xyzcal_mm is not None: predicted_xyz.extend(xyzcal_mm) obs_x, obs_y, _ = observed_xyz.parts() pred_x, pred_y, _ = predicted_xyz.parts() try: import matplotlib if not params.output.show_plot: # http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear matplotlib.use("Agg") # use a non-interactive backend from matplotlib import pyplot except ImportError: raise Sorry("matplotlib must be installed to generate a plot.") fig = pyplot.figure() fig.set_size_inches(params.output.size_inches) fig.set_dpi(params.output.dpi) pyplot.axes().set_aspect("equal") marker_size = params.output.marker_size if obs_x.size(): pyplot.scatter(obs_x, obs_y, marker="o", c="white", s=marker_size, alpha=1) if pred_x.size(): pyplot.scatter(pred_x, pred_y, marker="+", s=marker_size, c="blue") xmax = max([ detector[i_panel].get_image_size_mm()[0] + panel_origin_shifts[i_panel][0] for i_panel in range(len(detector)) ]) ymax = max([ detector[i_panel].get_image_size_mm()[1] + panel_origin_shifts[i_panel][1] for i_panel in range(len(detector)) ]) try: beam_centre = hierarchy.get_beam_centre(imageset.get_beam().get_s0()) except Exception: beam_centre = detector[0].get_beam_centre(imageset.get_beam().get_s0()) pyplot.scatter([beam_centre[0]], [beam_centre[1]], marker="+", c="blue", s=100) pyplot.xlim(0, xmax) pyplot.ylim(0, ymax) pyplot.gca().invert_yaxis() pyplot.title("Centroid x,y-coordinates") pyplot.xlabel("x-coordinate (mm)") pyplot.ylabel("y-coordinate (mm)") if params.output.file_name is not None: pyplot.savefig( params.output.file_name, size_inches=params.output.size_inches, dpi=params.output.dpi, bbox_inches="tight", ) if params.output.show_plot: pyplot.show()
def run(args): import libtbx.load_env usage = """\ %s datablock.json reflections.pickle [options]""" %libtbx.env.dispatcher_name from dials.util.options import OptionParser from dials.util.options import flatten_datablocks from dials.util.options import flatten_experiments from dials.util.options import flatten_reflections from scitbx.array_family import flex from scitbx import matrix from libtbx.utils import Sorry parser = OptionParser( usage=usage, phil=master_phil_scope, read_datablocks=True, read_experiments=True, read_reflections=True, check_format=False) params, options = parser.parse_args(show_diff_phil=True) datablocks = flatten_datablocks(params.input.datablock) reflections = flatten_reflections(params.input.reflections) experiments = flatten_experiments(params.input.experiments) if len(datablocks) == 1: imageset = datablocks[0].extract_imagesets()[0] elif len(datablocks) > 1: raise Sorry("Only one DataBlock can be processed at a time") elif len(experiments.imagesets()) > 0: imageset = experiments.imagesets()[0] imageset.set_detector(experiments[0].detector) imageset.set_beam(experiments[0].beam) imageset.set_goniometer(experiments[0].goniometer) else: parser.print_help() return detector = imageset.get_detector() scan = imageset.get_scan() panel_origin_shifts = {0: (0,0,0)} try: hierarchy = detector.hierarchy() except AttributeError: hierarchy = None for i_panel in range(1, len(detector)): origin_shift = matrix.col(detector[0].get_origin()) \ - matrix.col(detector[i_panel].get_origin()) panel_origin_shifts[i_panel] = origin_shift observed_xyz = flex.vec3_double() predicted_xyz = flex.vec3_double() for reflection_list in reflections: if len(params.scan_range): sel = flex.bool(len(reflection_list), False) xyzcal_px = None xyzcal_px = None if 'xyzcal.px' in reflection_list: xyzcal_px = reflection_list['xyzcal.px'] if 'xyzobs.px.value' in reflection_list: xyzobs_px = reflection_list['xyzobs.px.value'] if xyzcal_px is not None and not xyzcal_px.norms().all_eq(0): centroids_frame = xyzcal_px.parts()[2] elif xyzobs_px is not None and not xyzobs_px.norms().all_eq(0): centroids_frame = xyzobs_px.parts()[2] else: raise Sorry("No pixel coordinates given in input reflections.") reflections_in_range = False for scan_range in params.scan_range: if scan_range is None: continue range_start, range_end = scan_range sel |= ((centroids_frame >= range_start) & (centroids_frame < range_end)) reflection_list = reflection_list.select(sel) if params.first_n_reflections is not None: centroid_positions = reflection_list.centroid_position() centroids_frame = centroid_positions.parts()[2] perm = flex.sort_permutation(centroids_frame) perm = perm[:min(reflection_list.size(), params.first_n_reflections)] reflection_list = reflection_list.select(perm) if params.crystal_id is not None: reflection_list = reflection_list.select( reflection_list['id'] == params.crystal_id) xyzcal_px = None xyzcal_px = None xyzobs_mm = None xyzcal_mm = None if 'xyzcal.px' in reflection_list: xyzcal_px = reflection_list['xyzcal.px'] if 'xyzobs.px.value' in reflection_list: xyzobs_px = reflection_list['xyzobs.px.value'] if 'xyzcal.mm' in reflection_list: xyzcal_mm = reflection_list['xyzcal.mm'] if 'xyzobs.mm.value' in reflection_list: xyzobs_mm = reflection_list['xyzobs.mm.value'] panel_ids = reflection_list['panel'] if xyzobs_mm is None and xyzobs_px is not None: xyzobs_mm = flex.vec3_double() for i_panel in range(len(detector)): xyzobs_px_panel = xyzobs_px.select(panel_ids == i_panel) from dials.algorithms.centroid import centroid_px_to_mm_panel xyzobs_mm_panel, _, _ = centroid_px_to_mm_panel( detector[i_panel], scan, xyzobs_px_panel, flex.vec3_double(xyzobs_px_panel.size()), flex.vec3_double(xyzobs_px_panel.size())) xyzobs_mm.extend(xyzobs_mm_panel) if xyzobs_mm is not None: observed_xyz.extend(xyzobs_mm) if xyzcal_mm is not None: predicted_xyz.extend(xyzcal_mm) obs_x, obs_y, _ = observed_xyz.parts() pred_x, pred_y, _ = predicted_xyz.parts() try: import matplotlib if not params.output.show_plot: # http://matplotlib.org/faq/howto_faq.html#generate-images-without-having-a-window-appear matplotlib.use('Agg') # use a non-interactive backend from matplotlib import pyplot except ImportError: raise Sorry("matplotlib must be installed to generate a plot.") fig = pyplot.figure() fig.set_size_inches(params.output.size_inches) fig.set_dpi(params.output.dpi) pyplot.axes().set_aspect('equal') marker_size = params.output.marker_size if obs_x.size(): pyplot.scatter(obs_x, obs_y, marker='o', c='white', s=marker_size, alpha=1) if pred_x.size(): pyplot.scatter(pred_x, pred_y, marker='+', s=marker_size, c='blue') #assert len(detector) == 1 panel = detector[0] #if len(detector) > 1: xmin = max([detector[i_panel].get_image_size_mm()[0] + panel_origin_shifts[i_panel][0] for i_panel in range(len(detector))]) xmax = max([detector[i_panel].get_image_size_mm()[0] + panel_origin_shifts[i_panel][0] for i_panel in range(len(detector))]) ymax = max([detector[i_panel].get_image_size_mm()[1] + panel_origin_shifts[i_panel][1] for i_panel in range(len(detector))]) ymax = max([detector[i_panel].get_image_size_mm()[1] + panel_origin_shifts[i_panel][1] for i_panel in range(len(detector))]) try: beam_centre = hierarchy.get_beam_centre(imageset.get_beam().get_s0()) except Exception: beam_centre = detector[0].get_beam_centre(imageset.get_beam().get_s0()) pyplot.scatter([beam_centre[0]], [beam_centre[1]], marker='+', c='blue', s=100) pyplot.xlim(0, xmax) pyplot.ylim(0, ymax) pyplot.gca().invert_yaxis() pyplot.title('Centroid x,y-coordinates') pyplot.xlabel('x-coordinate (mm)') pyplot.ylabel('y-coordinate (mm)') if params.output.file_name is not None: pyplot.savefig(params.output.file_name, size_inches=params.output.size_inches, dpi=params.output.dpi, bbox_inches='tight') if params.output.show_plot: pyplot.show()