def test(cmdline_overrides=[]): tc = _Test() tc.create_models(cmdline_overrides) reflections = tc.generate_reflections() # use a ReflectionManager to exclude reflections too close to the spindle, # plus set the frame numbers from dials.algorithms.refinement.reflection_manager import ReflectionManager refman = ReflectionManager(reflections, tc.experiments, outlier_detector=None) refman.finalise() # create prediction parameterisation of the requested type pred_param = ScanVaryingPredictionParameterisation( tc.experiments, [tc.det_param], [tc.s0_param], [tc.xlo_param], [tc.xluc_param], [tc.gon_param], ) # keep only those reflections that pass inclusion criteria and have predictions reflections = refman.get_matches() # get analytical gradients pred_param.compose(reflections) an_grads = pred_param.get_gradients(reflections) # get finite difference gradients p_vals = pred_param.get_param_vals() p_names = pred_param.get_param_names() deltas = [1.0e-7] * len(p_vals) for i, delta in enumerate(deltas): val = p_vals[i] p_vals[i] -= delta / 2.0 pred_param.set_param_vals(p_vals) pred_param.compose(reflections) tc.ref_predictor(reflections) rev_state = reflections["xyzcal.mm"].deep_copy() p_vals[i] += delta pred_param.set_param_vals(p_vals) pred_param.compose(reflections) tc.ref_predictor(reflections) fwd_state = reflections["xyzcal.mm"].deep_copy() p_vals[i] = val fd = fwd_state - rev_state x_grads, y_grads, phi_grads = fd.parts() x_grads /= delta y_grads /= delta phi_grads /= delta try: for (a, b) in zip(x_grads, an_grads[i]["dX_dp"]): assert a == pytest.approx(b, abs=1e-5) for (a, b) in zip(y_grads, an_grads[i]["dY_dp"]): assert a == pytest.approx(b, abs=1e-5) for (a, b) in zip(phi_grads, an_grads[i]["dphi_dp"]): assert a == pytest.approx(b, abs=1e-5) except AssertionError: print("Failure for {}".format(p_names[i])) raise # return to the initial state pred_param.set_param_vals(p_vals) pred_param.compose(reflections)
def __call__(self, cmdline_overrides): self.create_models(cmdline_overrides) reflections = self.generate_reflections() # use a ReflectionManager to exclude reflections too close to the spindle, # plus set the frame numbers from dials.algorithms.refinement.reflection_manager import ReflectionManager refman = ReflectionManager(reflections, self.experiments, outlier_detector=None) # create prediction parameterisation of the requested type pred_param = ScanVaryingPredictionParameterisation(self.experiments, [self.det_param], [self.s0_param], [self.xlo_param], [self.xluc_param], [self.gon_param]) # make a target to ensure reflections are predicted and refman is finalised from dials.algorithms.refinement.target import \ LeastSquaresPositionalResidualWithRmsdCutoff target = LeastSquaresPositionalResidualWithRmsdCutoff(self.experiments, self.ref_predictor, refman, pred_param, restraints_parameterisation=None) # keep only those reflections that pass inclusion criteria and have predictions reflections = refman.get_matches() # get analytical gradients pred_param.compose(reflections) an_grads = pred_param.get_gradients(reflections) # get finite difference gradients p_vals = pred_param.get_param_vals() p_names = pred_param.get_param_names() deltas = [1.e-7] * len(p_vals) for i in range(len(deltas)): val = p_vals[i] p_vals[i] -= deltas[i] / 2. pred_param.set_param_vals(p_vals) pred_param.compose(reflections) self.ref_predictor(reflections) rev_state = reflections['xyzcal.mm'].deep_copy() p_vals[i] += deltas[i] pred_param.set_param_vals(p_vals) pred_param.compose(reflections) self.ref_predictor(reflections) fwd_state = reflections['xyzcal.mm'].deep_copy() p_vals[i] = val fd = (fwd_state - rev_state) x_grads, y_grads, phi_grads = fd.parts() x_grads /= deltas[i] y_grads /= deltas[i] phi_grads /= deltas[i] try: for n, (a,b) in enumerate(zip(x_grads, an_grads[i]["dX_dp"])): assert approx_equal(a, b, eps=1.e-5) for n, (a,b) in enumerate(zip(y_grads, an_grads[i]["dY_dp"])): assert approx_equal(a, b, eps=1.e-5) for n, (a,b) in enumerate(zip(phi_grads, an_grads[i]["dphi_dp"])): assert approx_equal(a, b, eps=1.e-5) except AssertionError: print "Failure for {0}".format(p_names[i]) raise # return to the initial state pred_param.set_param_vals(p_vals) pred_param.compose(reflections) print "OK" return