Exemplo n.º 1
0
    def predict_reflections(self, experiment, dmin=None, dmax=None, **kwargs):
        """Predict the reflections."""
        from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
        from dials.array_family import flex

        # Create the scan varying reflection predictor
        predictor = ScanVaryingReflectionPredictor(experiment,
                                                   dmin=dmin,
                                                   margin=1)

        # Get length of scan
        scan_len = experiment.scan.get_num_images()
        crystal = experiment.crystal

        # Get the A matrices
        if crystal.num_scan_points == 0:
            A = [crystal.get_A() for i in range(scan_len + 1)]
        else:
            assert (crystal.num_scan_points) == scan_len + 1
            A = [crystal.get_A_at_scan_point(i) for i in range(scan_len + 1)]

        # Do the prediction
        result = predictor.for_ub(flex.mat3_double(A))
        if dmax is not None:
            assert dmax > 0
            result.compute_d_single(experiment)
            mask = result["d"] > dmax
            result.del_selected(mask)

        # Return the reflections
        return result
Exemplo n.º 2
0
def test_for_reflection_table(data):
    from dials.algorithms.spot_prediction import (
        ScanStaticReflectionPredictor,
        ScanVaryingReflectionPredictor,
    )
    from dials.array_family import flex

    predict = ScanStaticReflectionPredictor(data.experiments[0])
    preds = predict.for_ub(data.experiments[0].crystal.get_A())

    preds["ub_matrix"] = flex.mat3_double(len(preds),
                                          data.experiments[0].crystal.get_A())
    preds["s0"] = flex.vec3_double(len(preds),
                                   data.experiments[0].beam.get_s0())
    preds["d_matrix"] = flex.mat3_double(len(preds))
    preds["S_matrix"] = flex.mat3_double(
        len(preds), data.experiments[0].goniometer.get_setting_rotation())
    for ipanel, panel in enumerate(data.experiments[0].detector):
        sel = preds["panel"] == ipanel
        D = panel.get_d_matrix()
        preds["d_matrix"].set_selected(sel, D)
    predict = ScanVaryingReflectionPredictor(data.experiments[0])
    old_preds = copy.deepcopy(preds)
    predict.for_reflection_table(preds, preds["ub_matrix"], preds["s0"],
                                 preds["d_matrix"], preds["S_matrix"])

    # Because UB, s0, d and S values are the same for all reflections, the new
    # reflections should be approx equal to those produced by the scan static
    # predictor
    old_x, old_y, old_z = old_preds["xyzcal.px"].parts()
    new_x, new_y, new_z = preds["xyzcal.px"].parts()
    assert old_x.all_approx_equal(new_x)
    assert old_y.all_approx_equal(new_y)
    assert old_z.all_approx_equal(new_z)
 def _predict_new(self, hkl=None, frame=None, panel=None):
     from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
     from dials.array_family import flex
     predict = ScanVaryingReflectionPredictor(self.experiments[0])
     #if hkl is None:
     A = [
         self.experiments[0].crystal.get_A_at_scan_point(i)
         for i in range(self.experiments[0].crystal.num_scan_points)
     ]
     result = predict.for_ub(flex.mat3_double(A))
     #else:
     #if panel is None:
     #result = predict(hkl, frame)
     #else:
     #result = predict(hkl, frame, panel)
     return result
  def predict_new(self, hkl=None, frame=None, panel=None):
    from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
    from time import time
    from dials.array_family import flex
    st = time()
    predict = ScanVaryingReflectionPredictor(self.experiments[0])
    #if hkl is None:
    A = [self.experiments[0].crystal.get_A_at_scan_point(i) for i in
           range(self.experiments[0].crystal.num_scan_points)]
    result = predict.for_ub(flex.mat3_double(A))
    #else:
      #if panel is None:
        #result = predict(hkl, frame)
      #else:
        #result = predict(hkl, frame, panel)

    #print "New Time: ", time() - st
    return result
Exemplo n.º 5
0
def ref_gen_varying(experiments):
    """Generate some reflections using the scan varying predictor"""

    beam = experiments[0].beam
    crystal = experiments[0].crystal
    detector = experiments[0].detector
    scan = experiments[0].scan

    # We need a UB matrix at the beginning of every image, and at the end of the
    # last image. These are all the same - we want to compare the scan-varying
    # predictor with the scan-static one for a flat scan.
    ar_range = scan.get_array_range()
    UBlist = [crystal.get_A() for t in range(ar_range[0], ar_range[1] + 1)]
    dmin = detector.get_max_resolution(beam.get_s0())

    from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor

    sv_predictor = ScanVaryingReflectionPredictor(experiments[0], dmin=dmin)
    refs = sv_predictor.for_ub(flex.mat3_double(UBlist))

    return refs
Exemplo n.º 6
0
def test_scan_varying_results_are_close_to_static_prediction_when_model_is_static(
        static_test,  # noqa: F811, not a redefinition
):
    """Test that various modes of scan-varying prediction produce results
    close to static prediction when the supplied models are indeed static"""

    # Get static predictor results

    scan = static_test.experiments[0].scan
    crystal = static_test.experiments[0].crystal
    beam = static_test.experiments[0].beam
    goniometer = static_test.experiments[0].goniometer
    n_scan_points = scan.get_num_images() + 1
    static_preds = static_test.predict_new()
    static_preds.sort("miller_index")

    # Set up scan-varying predictor
    from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
    from dials.array_family import flex

    predict = ScanVaryingReflectionPredictor(static_test.experiments[0])

    def compare(refs1, refs2):
        assert len(refs1) == len(refs2)
        for r1, r2 in zip(refs1.rows(), refs2.rows()):
            assert r1["miller_index"] == r2["miller_index"]
            # differences less than one hundredth of a pixel/image
            for e1, e2 in zip(r1["xyzcal.px"], r2["xyzcal.px"]):
                assert e1 == pytest.approx(e2, abs=0.01)

    # Prediction for UB matrix expressed as array of static UB
    A = [crystal.get_A() for i in range(n_scan_points)]
    result1 = predict.for_ub(flex.mat3_double(A))
    result1.sort("miller_index")
    compare(static_preds, result1)

    # Prediction for UB matrix, s0 vectors and goniometer setting rotation
    # matrices expressed as arrays of static model states
    s0 = [beam.get_s0() for i in range(n_scan_points)]
    S = [goniometer.get_setting_rotation() for i in range(n_scan_points)]
    result2 = predict.for_varying_models(flex.mat3_double(A),
                                         flex.vec3_double(s0),
                                         flex.mat3_double(S))
    result2.sort("miller_index")
    compare(static_preds, result2)

    # First frame only, start and end UB
    _, _, z = static_preds["xyzcal.px"].parts()
    static_preds_frame0 = static_preds.select((z >= 0) & (z < 1))
    A = crystal.get_A()
    result3 = predict.for_ub_on_single_image(0, A, A)
    result3.sort("miller_index")
    compare(static_preds_frame0, result3)

    # First frame only, start and end UB, s0 and S
    s0 = beam.get_s0()
    S = goniometer.get_setting_rotation()
    result4 = predict.for_varying_models_on_single_image(0, A, A, s0, s0, S, S)
    result4.sort("miller_index")
    compare(static_preds_frame0, result4)
Exemplo n.º 7
0
def ref_gen_varying(experiments):
  """Generate some reflections using the scan varying predictor"""

  beam = experiments[0].beam
  crystal = experiments[0].crystal
  goniometer = experiments[0].goniometer
  detector = experiments[0].detector
  scan = experiments[0].scan

  # We need a UB matrix at the beginning of every image, and at the end of the
  # last image. These are all the same - we want to compare the scan-varying
  # predictor with the scan-static one for a flat scan.
  ar_range = scan.get_array_range()
  UBlist = [crystal.get_A() for t in range(ar_range[0], ar_range[1]+1)]
  dmin = detector.get_max_resolution(beam.get_s0())

  from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
  sv_predictor = ScanVaryingReflectionPredictor(experiments[0],
                                                dmin=dmin)
  refs = sv_predictor.for_ub(flex.mat3_double(UBlist))

  return refs
Exemplo n.º 8
0
  def tst_for_reflection_table(self):
    from libtbx.test_utils import approx_equal
    from dials.algorithms.spot_prediction import \
      ScanVaryingReflectionPredictor, ScanStaticReflectionPredictor
    from dials.array_family import flex

    predict = ScanStaticReflectionPredictor(self.experiments[0])
    preds = predict.for_ub(self.experiments[0].crystal.get_A())

    preds['ub_matrix'] = flex.mat3_double(len(preds), self.experiments[0].crystal.get_A())
    preds['s0'] = flex.vec3_double(len(preds), self.experiments[0].beam.get_s0())
    preds['d_matrix'] = flex.mat3_double(len(preds))
    preds['S_matrix'] = flex.mat3_double(len(preds),
        self.experiments[0].goniometer.get_setting_rotation())
    for ipanel, panel in enumerate(self.experiments[0].detector):
      sel = preds['panel'] == ipanel
      D = panel.get_d_matrix()
      preds['d_matrix'].set_selected(sel, D)
    predict = ScanVaryingReflectionPredictor(self.experiments[0])
    from copy import deepcopy
    old_preds = deepcopy(preds)
    predict.for_reflection_table(preds,
                                 preds['ub_matrix'],
                                 preds['s0'],
                                 preds['d_matrix'],
                                 preds['S_matrix'])

    # Because UB, s0, d and S values are the same for all reflections, the new
    # reflections should be approx equal to those produced by the scan static
    # predictor
    old_x, old_y, old_z = old_preds['xyzcal.px'].parts()
    new_x, new_y, new_z = preds['xyzcal.px'].parts()
    assert old_x.all_approx_equal(new_x)
    assert old_y.all_approx_equal(new_y)
    assert old_z.all_approx_equal(new_z)

    print "OK"

    return
Exemplo n.º 9
0
    def __init__(
        self, experiment, dmin=None, dmax=None, margin=1, force_static=False, padding=0
    ):
        """
        Initialise a predictor for each experiment.

        :param experiment: The experiment to predict for
        :param dmin: The maximum resolution
        :param dmax: The minimum resolution
        :param margin: The margin of hkl to predict
        :param force_static: force scan varying prediction to be static

        """
        from dials.algorithms.spot_prediction import ScanStaticReflectionPredictor
        from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
        from dials.algorithms.spot_prediction import StillsReflectionPredictor
        from dxtbx.imageset import ImageSweep
        from dials.array_family import flex

        class Predictor(object):
            def __init__(self, name, func):
                self.name = name
                self.func = func

            def __call__(self):
                result = self.func()
                if dmax is not None:
                    assert dmax > 0
                    result.compute_d_single(experiment)
                    mask = result["d"] > dmax
                    result.del_selected(mask)
                return result

        # Check prediction to maximum resolution is possible
        wl = experiment.beam.get_wavelength()
        if dmin is not None and dmin < 0.5 * wl:
            raise Sorry(
                "Prediction at d_min of {0} is not possible "
                "with wavelength {1}".format(dmin, wl)
            )

        # Select the predictor class
        if isinstance(experiment.imageset, ImageSweep):
            xl_nsp = experiment.crystal.num_scan_points
            bm_nsp = experiment.beam.num_scan_points
            gn_nsp = experiment.goniometer.num_scan_points
            nim = experiment.scan.get_num_images()

            sv_compatible = (xl_nsp == nim + 1) or (bm_nsp == nim + 1)
            if not force_static and sv_compatible:
                predictor = ScanVaryingReflectionPredictor(
                    experiment, dmin=dmin, margin=margin, padding=padding
                )

                if bm_nsp == 0 and gn_nsp == 0:
                    # Only varying crystal
                    A = [
                        experiment.crystal.get_A_at_scan_point(i)
                        for i in range(experiment.crystal.num_scan_points)
                    ]
                    predict = Predictor(
                        "scan varying crystal prediction",
                        lambda: predictor.for_ub(flex.mat3_double(A)),
                    )

                else:
                    # Any allowed model may vary
                    if xl_nsp == nim + 1:
                        A = [
                            experiment.crystal.get_A_at_scan_point(i)
                            for i in range(experiment.crystal.num_scan_points)
                        ]
                    else:
                        A = [experiment.crystal.get_A() for i in range(nim + 1)]
                    if bm_nsp == nim + 1:
                        s0 = [
                            experiment.beam.get_s0_at_scan_point(i)
                            for i in range(experiment.beam.num_scan_points)
                        ]
                    else:
                        s0 = [experiment.beam.get_s0() for i in range(nim + 1)]
                    if gn_nsp == nim + 1:
                        S = [
                            experiment.goniometer.get_setting_rotation_at_scan_point(i)
                            for i in range(experiment.goniometer.num_scan_points)
                        ]
                    else:
                        S = [
                            experiment.goniometer.get_setting_rotation()
                            for i in range(nim + 1)
                        ]
                    predict = Predictor(
                        "scan varying model prediction",
                        lambda: predictor.for_varying_models(
                            flex.mat3_double(A),
                            flex.vec3_double(s0),
                            flex.mat3_double(S),
                        ),
                    )
            else:
                predictor = ScanStaticReflectionPredictor(
                    experiment, dmin=dmin, padding=padding
                )

                # Choose index generation method based on number of images
                # https://github.com/dials/dials/issues/585
                if experiment.scan.get_num_images() > 50:
                    predict_method = predictor.for_ub_old_index_generator
                else:
                    predict_method = predictor.for_ub

                predict = Predictor(
                    "scan static prediction",
                    lambda: predict_method(experiment.crystal.get_A()),
                )
        else:
            predictor = StillsReflectionPredictor(experiment, dmin=dmin)

            predict = Predictor(
                "stills prediction",
                lambda: predictor.for_ub(experiment.crystal.get_A()),
            )

        # Create and add the predictor class
        self._predict = predict
Exemplo n.º 10
0
  def __init__(self,
               experiment,
               dmin=None,
               dmax=None,
               margin=1,
               force_static=False):
    '''
    Initialise a predictor for each experiment.

    :param experiment: The experiment to predict for
    :param dmin: The maximum resolution
    :param dmax: The minimum resolution
    :param margin: The margin of hkl to predict
    :param force_static: force scan varying prediction to be static

    '''
    from dials.algorithms.spot_prediction import ScanStaticReflectionPredictor
    from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
    from dials.algorithms.spot_prediction import StillsReflectionPredictor
    from dxtbx.imageset import ImageSweep
    from dials.array_family import flex

    class Predictor(object):
      def __init__(self, name, func):
        self.name = name
        self.func = func
      def __call__(self):
        result = self.func()
        if dmax is not None:
          assert(dmax > 0)
          result.compute_d_single(experiment)
          mask = result['d'] > dmax
          result.del_selected(mask)
        return result

    # Select the predictor class
    if isinstance(experiment.imageset, ImageSweep):
      nsp = experiment.crystal.num_scan_points
      nim = experiment.scan.get_num_images()
      if not force_static and nsp == nim + 1:
        predictor = ScanVaryingReflectionPredictor(
          experiment,
          dmin=dmin,
          margin=margin)
        A = [experiment.crystal.get_A_at_scan_point(i) for i in
               range(experiment.crystal.num_scan_points)]
        predict = Predictor(
          "scan varying prediction",
          lambda: predictor.for_ub(flex.mat3_double(A)))
      else:
        predictor = ScanStaticReflectionPredictor(
          experiment,
          dmin=dmin)
        predict = Predictor(
          "scan static prediction",
          lambda: predictor.for_ub(experiment.crystal.get_A()))
    else:
      predictor = StillsReflectionPredictor(
        experiment,
        dmin=dmin)

      predict = Predictor(
        "stills prediction",
        lambda: predictor.for_ub(experiment.crystal.get_A()))

    # Create and add the predictor class
    self._predict = predict
Exemplo n.º 11
0
    def __init__(self,
                 experiment,
                 dmin=None,
                 dmax=None,
                 margin=1,
                 force_static=False,
                 padding=0):
        '''
    Initialise a predictor for each experiment.

    :param experiment: The experiment to predict for
    :param dmin: The maximum resolution
    :param dmax: The minimum resolution
    :param margin: The margin of hkl to predict
    :param force_static: force scan varying prediction to be static

    '''
        from dials.algorithms.spot_prediction import ScanStaticReflectionPredictor
        from dials.algorithms.spot_prediction import ScanVaryingReflectionPredictor
        from dials.algorithms.spot_prediction import StillsReflectionPredictor
        from dxtbx.imageset import ImageSweep
        from dials.array_family import flex

        class Predictor(object):
            def __init__(self, name, func):
                self.name = name
                self.func = func

            def __call__(self):
                result = self.func()
                if dmax is not None:
                    assert (dmax > 0)
                    result.compute_d_single(experiment)
                    mask = result['d'] > dmax
                    result.del_selected(mask)
                return result

        # Check prediction to maximum resolution is possible
        wl = experiment.beam.get_wavelength()
        if dmin is not None and dmin < 0.5 * wl:
            raise Sorry("Prediction at d_min of {0} is not possible "
                        "with wavelength {1}".format(dmin, wl))

        # Select the predictor class
        if isinstance(experiment.imageset, ImageSweep):
            nsp = experiment.crystal.num_scan_points
            nim = experiment.scan.get_num_images()

            if not force_static and nsp == nim + 1:
                predictor = ScanVaryingReflectionPredictor(experiment,
                                                           dmin=dmin,
                                                           margin=margin,
                                                           padding=padding)
                A = [
                    experiment.crystal.get_A_at_scan_point(i)
                    for i in range(experiment.crystal.num_scan_points)
                ]
                predict = Predictor(
                    "scan varying prediction",
                    lambda: predictor.for_ub(flex.mat3_double(A)))
            else:
                predictor = ScanStaticReflectionPredictor(experiment,
                                                          dmin=dmin,
                                                          padding=padding)
                predict = Predictor(
                    "scan static prediction",
                    lambda: predictor.for_ub(experiment.crystal.get_A()))
        else:
            predictor = StillsReflectionPredictor(experiment, dmin=dmin)

            predict = Predictor(
                "stills prediction",
                lambda: predictor.for_ub(experiment.crystal.get_A()))

        # Create and add the predictor class
        self._predict = predict