def generate_shoebox(self, bbox, centre, intensity, mask=False): from dials.model.data import Shoebox from dials.algorithms.shoebox import MaskCode shoebox = Shoebox() shoebox.bbox = bbox shoebox.allocate() for i in range(len(shoebox.mask)): shoebox.mask[i] = MaskCode.Valid | MaskCode.Foreground shoebox.data = self.gaussian( shoebox.size(), 1.0, [c - o for c, o in zip(centre[::-1], shoebox.offset())], [s / 8.0 for s in shoebox.size()]) if mask: shoebox.mask = self.create_mask( shoebox.size(), [c - o for c, o in zip(centre[::-1], shoebox.offset())], MaskCode.Valid | MaskCode.Foreground) tot = 0 mask_code = MaskCode.Valid | MaskCode.Foreground for i in range(len(shoebox.data)): if shoebox.mask[i] & mask_code == mask_code: tot += shoebox.data[i] if tot > 0: shoebox.data *= intensity / tot return shoebox
def sequence_to_stills(experiments, reflections, params): assert len(reflections) == 1 reflections = reflections[0] new_experiments = ExperimentList() new_reflections = flex.reflection_table() # This is the subset needed to integrate for key in [ "id", "imageset_id", "shoebox", "bbox", "intensity.sum.value", "intensity.sum.variance", "entering", "flags", "miller_index", "panel", "xyzobs.px.value", "xyzobs.px.variance", ]: if key in reflections: new_reflections[key] = type(reflections[key])() elif key == "imageset_id": assert len(experiments.imagesets()) == 1 reflections["imageset_id"] = flex.int(len(reflections), 0) new_reflections["imageset_id"] = flex.int() elif key == "entering": reflections["entering"] = flex.bool(len(reflections), False) new_reflections["entering"] = flex.bool() else: raise RuntimeError( "Expected key not found in reflection table: %s" % key) for expt_id, experiment in enumerate(experiments): # Get the goniometr setting matrix goniometer_setting_matrix = matrix.sqr( experiment.goniometer.get_setting_rotation()) goniometer_axis = matrix.col(experiment.goniometer.get_rotation_axis()) step = experiment.scan.get_oscillation()[1] refls = reflections.select(reflections["id"] == expt_id) _, _, _, _, z1, z2 = refls["bbox"].parts() # Create an experiment for each scanpoint for i_scan_point in range(*experiment.scan.get_array_range()): if params.max_scan_points and i_scan_point >= params.max_scan_points: break # The A matrix is the goniometer setting matrix for this scan point # times the scan varying A matrix at this scan point. Note, the # goniometer setting matrix for scan point zero will be the identity # matrix and represents the beginning of the oscillation. # For stills, the A matrix needs to be positioned in the midpoint of an # oscillation step. Hence, here the goniometer setting matrixis rotated # by a further half oscillation step. A = (goniometer_axis.axis_and_angle_as_r3_rotation_matrix( angle=experiment.scan.get_angle_from_array_index(i_scan_point) + (step / 2), deg=True, ) * goniometer_setting_matrix * matrix.sqr( experiment.crystal.get_A_at_scan_point(i_scan_point))) crystal = MosaicCrystalSauter2014(experiment.crystal) crystal.set_A(A) # Copy in mosaic parameters if available if params.output.domain_size_ang is None and hasattr( experiment.crystal, "get_domain_size_ang"): crystal.set_domain_size_ang( experiment.crystal.get_domain_size_ang()) elif params.output.domain_size_ang is not None: crystal.set_domain_size_ang(params.output.domain_size_ang) if params.output.half_mosaicity_deg is None and hasattr( experiment.crystal, "get_half_mosaicity_deg"): crystal.set_half_mosaicity_deg( experiment.crystal.get_half_mosaicity_deg()) elif params.output.half_mosaicity_deg is not None: crystal.set_half_mosaicity_deg( params.output.half_mosaicity_deg) new_experiment = Experiment( detector=experiment.detector, beam=experiment.beam, crystal=crystal, imageset=experiment.imageset.as_imageset() [i_scan_point:i_scan_point + 1], ) new_experiments.append(new_experiment) # Each reflection in a 3D shoebox can be found on multiple images. # Slice the reflections such that any reflection on this scan point # is included with this image new_id = len(new_experiments) - 1 subrefls = refls.select((i_scan_point >= z1) & (i_scan_point < z2)) for refl in subrefls.rows(): assert i_scan_point in range(*refl["bbox"][4:6]) new_sb = Shoebox() start = i_scan_point - refl["bbox"][4] # z1 new_sb.data = refl["shoebox"].data[start:start + 1, :, :] new_sb.background = refl["shoebox"].background[start:start + 1, :, :] new_sb.mask = refl["shoebox"].mask[start:start + 1, :, :] intensity = new_sb.summed_intensity() new_sb.bbox = tuple( list(refl["bbox"])[0:4] + [0, 1]) # keep the original shoebox but reset the z values new_sb.panel = refl["panel"] new_refl = {} new_refl["id"] = new_refl["imageset_id"] = new_id new_refl["shoebox"] = new_sb new_refl["bbox"] = new_sb.bbox new_refl["intensity.sum.value"] = intensity.observed.value new_refl[ "intensity.sum.variance"] = intensity.observed.variance for key in ["entering", "flags", "miller_index", "panel"]: new_refl[key] = refl[key] centroid = new_sb.centroid_foreground_minus_background() new_refl["xyzobs.px.value"] = centroid.px.position new_refl["xyzobs.px.variance"] = centroid.px.variance new_reflections.append({}) for key in new_refl: new_reflections[key][-1] = new_refl[key] # Re-predict using the reflection slices and the stills predictors ref_predictor = ExperimentsPredictorFactory.from_experiments( new_experiments, force_stills=new_experiments.all_stills()) new_reflections = ref_predictor(new_reflections) return (new_experiments, new_reflections)