Ejemplo n.º 1
0
    def __init__(
        self,
        reflections,
        experiments,
        nref_per_degree=None,
        max_sample_size=None,
        min_sample_size=0,
        close_to_spindle_cutoff=0.02,
        scan_margin=0.0,
        outlier_detector=None,
        weighting_strategy_override=None,
    ):

        if len(reflections) == 0:
            raise ValueError("Empty reflections table provided to ReflectionManager")

        # keep track of models
        self._experiments = experiments
        goniometers = [e.goniometer for e in self._experiments]
        self._axes = [
            matrix.col(g.get_rotation_axis()) if g else None for g in goniometers
        ]
        self._s0vecs = [matrix.col(e.beam.get_s0()) for e in self._experiments]

        # unset the refinement flags (creates flags field if needed)
        reflections.unset_flags(
            flex.size_t_range(len(reflections)),
            flex.reflection_table.flags.used_in_refinement,
        )

        # check that the observed beam vectors are stored: if not, compute them
        n_s1_set = set_obs_s1(reflections, experiments)
        if n_s1_set > 0:
            logger.debug("Set scattering vectors for %d reflections", n_s1_set)

        # keep track of the original indices of the reflections
        reflections["iobs"] = flex.size_t_range(len(reflections))

        # Check for monotonically increasing value range. If not, ref_table isn't sorted,
        # and proceed to sort by id and panel. This is required for the C++ extension
        # modules to allow for nlogn subselection of values used in refinement.
        l_id = reflections["id"]
        id0 = l_id[0]
        for id_x in l_id[1:]:
            if id0 <= id_x:
                id0 = id_x
            else:
                reflections.sort("id")  # Ensuring the ref_table is sorted by id
                reflections.subsort(
                    "id", "panel"
                )  # Ensuring that within each sorted id block, sorting is next performed by panel
                break

        # set up the reflection inclusion criteria
        self._close_to_spindle_cutoff = close_to_spindle_cutoff  # close to spindle
        self._scan_margin = DEG2RAD * scan_margin  # close to the scan edge
        self._outlier_detector = outlier_detector  # for outlier rejection
        self._nref_per_degree = nref_per_degree  # random subsets
        self._max_sample_size = max_sample_size  # sample size ceiling
        self._min_sample_size = min_sample_size  # sample size floor

        # exclude reflections that fail some inclusion criteria
        refs_to_keep = self._id_refs_to_keep(reflections)
        self._accepted_refs_size = len(refs_to_keep)

        # set entering flags for all reflections
        reflections.calculate_entering_flags(self._experiments)

        # set observed frame numbers for all reflections if not already present
        calculate_frame_numbers(reflections, self._experiments)

        # reset all use flags
        self.reset_accepted_reflections(reflections)

        # put full list of indexed reflections aside and select only the reflections
        # that were not excluded to manage
        self._indexed = reflections
        self._reflections = reflections.select(refs_to_keep)

        # set exclusion flag for reflections that failed the tests
        refs_to_excl = flex.bool(len(self._indexed), True)
        refs_to_excl.set_selected(refs_to_keep, False)
        self._indexed.set_flags(
            refs_to_excl, self._indexed.flags.excluded_for_refinement
        )

        # set weights for all kept reflections
        if weighting_strategy_override is not None:
            self._weighting_strategy = weighting_strategy_override
        self._weighting_strategy.calculate_weights(self._reflections)

        # not known until the manager is finalised
        self._sample_size = None
Ejemplo n.º 2
0
  def __init__(self, reflections,
                     experiments,
                     nref_per_degree=None,
                     max_sample_size=None,
                     min_sample_size=0,
                     close_to_spindle_cutoff=0.02,
                     outlier_detector=None,
                     weighting_strategy_override=None,
                     verbosity=0):

    # set verbosity
    self._verbosity = verbosity

    # keep track of models
    self._experiments = experiments
    goniometers = [e.goniometer for e in self._experiments]
    self._axes = [matrix.col(g.get_rotation_axis()) if g else None for g in goniometers]
    self._s0vecs = [matrix.col(e.beam.get_s0()) for e in self._experiments]

    # unset the refinement flags (creates flags field if needed)
    reflections.unset_flags(flex.size_t_range(len(reflections)),
        flex.reflection_table.flags.used_in_refinement)

    # check that the observed beam vectors are stored: if not, compute them
    n_s1_set = set_obs_s1(reflections, experiments)
    if n_s1_set > 0 and verbosity > 0:
      logger.debug("Set scattering vectors for %d reflections", n_s1_set)

    # keep track of the original indices of the reflections
    reflections['iobs'] = flex.size_t_range(len(reflections))

    # set up the reflection inclusion criteria
    self._close_to_spindle_cutoff = close_to_spindle_cutoff #too close to spindle
    self._outlier_detector = outlier_detector #for outlier rejection
    self._nref_per_degree = nref_per_degree #random subsets
    self._max_sample_size = max_sample_size #sample size ceiling
    self._min_sample_size = min_sample_size #sample size floor

    # exclude reflections that fail some inclusion criteria
    refs_to_keep = self._id_refs_to_keep(reflections)
    self._accepted_refs_size = len(refs_to_keep)

    # set entering flags for all reflections
    reflections['entering'] = calculate_entering_flags(reflections,
      self._experiments)

    # set observed frame numbers for all reflections if not already present
    calculate_frame_numbers(reflections, self._experiments)

    # reset all use flags
    self.reset_accepted_reflections(reflections)

    # put full list of indexed reflections aside and select only the reflections
    # that were not excluded to manage
    self._indexed = reflections
    self._reflections = reflections.select(flex.size_t(refs_to_keep))

    # set weights for all kept reflections
    if weighting_strategy_override is not None:
      self._weighting_strategy = weighting_strategy_override
    self._weighting_strategy.calculate_weights(self._reflections)

    # not known until the manager is finalised
    self._sample_size = None

    return
Ejemplo n.º 3
0
    def create_indexed(self, experiments, filename):

        print("Simulating indexed observations for {0}".format(filename))

        # Extract the experiment
        exp = experiments[0]

        # print "Experiment scan range is {0},{1}".format(*exp.scan.get_oscillation_range(deg=True))

        # Copy essential columns of the original reflections into a new table
        # (imageset_id is required for reciprocal_lattice_viewer)
        obs_refs = flex.reflection_table()
        cols = [
            "id",
            "imageset_id",
            "miller_index",
            "panel",
            "s1",
            "flags",
            "entering",
            "xyzobs.mm.value",
            "xyzobs.mm.variance",
            "xyzcal.mm",
            "xyzobs.px.value",
            "xyzobs.px.variance",
            "xyzcal.px",
        ]
        for k in cols:
            if k in self.original_reflections.keys():
                obs_refs[k] = self.original_reflections[k]

        x_obs, y_obs, phi_obs = obs_refs["xyzobs.mm.value"].parts()

        # print "Original observation scan range is {0},{1}".format(
        #    flex.min(phi_obs) * RAD_TO_DEG,
        #    flex.max(phi_obs) *  RAD_TO_DEG)

        # Reset panel number to zero prior to prediction
        obs_refs["panel"] = obs_refs["panel"] * 0

        # Predict new centroid positions
        from dials.algorithms.refinement.prediction import ExperimentsPredictor

        predictor = ExperimentsPredictor(experiments)
        predictor(obs_refs)

        # Get vectors to add as errors to the predicted centroid positions to form
        # new 'observations'
        shift_px = flex.vec3_double(self.shiftX_px, self.shiftY_px,
                                    self.shiftZ_px)
        px_size_mm = exp.detector[0].get_pixel_size()
        image_width_rad = exp.scan.get_oscillation(deg=False)[1]
        shift = flex.vec3_double(
            px_size_mm[0] * self.shiftX_px,
            px_size_mm[1] * self.shiftY_px,
            image_width_rad * self.shiftZ_px,
        )
        obs_refs["xyzobs.px.value"] = obs_refs["xyzcal.px"] + shift_px
        obs_refs["xyzobs.mm.value"] = obs_refs["xyzcal.mm"] + shift

        x, y, z = obs_refs["xyzobs.mm.value"].parts()

        # print "Simulated observation scan range is {0},{1}".format(
        #    flex.min(z) * RAD_TO_DEG,
        #    flex.max(z) * RAD_TO_DEG)

        # Keep original variance estimates from spot-finding for centroids in
        # pixels/images, but optionally rescale for centroids in mm/rad.
        # Note that an overall scale factor for the variance is irrelevant to
        # refinement. What matters are differences in the relative scale between
        # the detector space and rotation parts.
        if self.params.recalculate_centroid_variances:
            var_x_px, var_y_px, var_z_px = obs_refs[
                "xyzobs.px.variance"].parts()
            var_x_mm = var_x_px * px_size_mm[0]**2
            var_y_mm = var_y_px * px_size_mm[1]**2
            var_z_rd = var_z_px * image_width_rad**2
            obs_refs["xyzobs.mm.variance"] = flex.vec3_double(
                var_x_mm, var_y_mm, var_z_rd)

        # Reset observed s1 vectors
        from dials.algorithms.refinement.refinement_helpers import set_obs_s1

        obs_refs["s1"] = flex.vec3_double(len(obs_refs))
        set_obs_s1(obs_refs, experiments)

        return obs_refs
Ejemplo n.º 4
0
    def __init__(self,
                 reflections,
                 experiments,
                 nref_per_degree=None,
                 max_sample_size=None,
                 min_sample_size=0,
                 close_to_spindle_cutoff=0.02,
                 outlier_detector=None,
                 weighting_strategy_override=None,
                 verbosity=0):

        # set verbosity
        self._verbosity = verbosity

        # keep track of models
        self._experiments = experiments
        goniometers = [e.goniometer for e in self._experiments]
        self._axes = [
            matrix.col(g.get_rotation_axis()) if g else None
            for g in goniometers
        ]
        self._s0vecs = [matrix.col(e.beam.get_s0()) for e in self._experiments]

        # unset the refinement flags (creates flags field if needed)
        reflections.unset_flags(flex.size_t_range(len(reflections)),
                                flex.reflection_table.flags.used_in_refinement)

        # check that the observed beam vectors are stored: if not, compute them
        n_s1_set = set_obs_s1(reflections, experiments)
        if n_s1_set > 0 and verbosity > 0:
            logger.debug("Set scattering vectors for %d reflections", n_s1_set)

        # keep track of the original indices of the reflections
        reflections['iobs'] = flex.size_t_range(len(reflections))

        # set up the reflection inclusion criteria
        self._close_to_spindle_cutoff = close_to_spindle_cutoff  #too close to spindle
        self._outlier_detector = outlier_detector  #for outlier rejection
        self._nref_per_degree = nref_per_degree  #random subsets
        self._max_sample_size = max_sample_size  #sample size ceiling
        self._min_sample_size = min_sample_size  #sample size floor

        # exclude reflections that fail some inclusion criteria
        refs_to_keep = self._id_refs_to_keep(reflections)
        self._accepted_refs_size = len(refs_to_keep)

        # set entering flags for all reflections
        reflections['entering'] = calculate_entering_flags(
            reflections, self._experiments)

        # set observed frame numbers for all reflections if not already present
        calculate_frame_numbers(reflections, self._experiments)

        # reset all use flags
        self.reset_accepted_reflections(reflections)

        # put full list of indexed reflections aside and select only the reflections
        # that were not excluded to manage
        self._indexed = reflections
        self._reflections = reflections.select(flex.size_t(refs_to_keep))

        # set weights for all kept reflections
        if weighting_strategy_override is not None:
            self._weighting_strategy = weighting_strategy_override
        self._weighting_strategy.calculate_weights(self._reflections)

        # not known until the manager is finalised
        self._sample_size = None

        return