def test_target_jacobian_calculation_finite_difference(physical_param, single_exp, large_reflection_table): """Test the calculated jacobian against a finite difference calculation.""" test_params, exp, test_refl = physical_param, single_exp, large_reflection_table test_params.parameterisation.decay_term = False test_params.model = "physical" experiments = create_scaling_model(test_params, exp, test_refl) assert experiments[0].scaling_model.id_ == "physical" scaler = create_scaler(test_params, experiments, test_refl) apm = multi_active_parameter_manager([scaler.components], [["scale"]], scaling_active_parameter_manager) target = ScalingTarget() scaler.update_for_minimisation(apm, 0) fd_jacobian = calculate_jacobian_fd(target, scaler, apm) _, jacobian, _ = target.compute_residuals_and_gradients( scaler.Ih_table.blocked_data_list[0]) n_rows = jacobian.n_rows n_cols = jacobian.n_cols print(jacobian) print(fd_jacobian) for i in range(0, n_rows): for j in range(0, n_cols): assert jacobian[i, j] == pytest.approx(fd_jacobian[i, j], abs=1e-4)
def test_target_function( mock_single_Ih_table, mock_multi_apm_withrestraints, mock_multi_apm_withoutrestraints, ): """Test for the ScalingTarget class.""" # Create a scaling target and check gradients target = ScalingTarget() apm_restr = mock_multi_apm_withrestraints apm_norestr = mock_multi_apm_withoutrestraints # Below methods needed for refinement engine calls r, w = target.compute_residuals(mock_single_Ih_table) assert r.size() == w.size() f, g = target.compute_functional_gradients(mock_single_Ih_table) assert isinstance(f, float) assert g.size( ) == 1 # Number of parameters as determined by deriv matrix cols r, j, w = target.compute_residuals_and_gradients(mock_single_Ih_table) assert r.size() == w.size() assert j.n_cols == 1 # Number of parameters as determined by jacob matrix. assert j.n_rows == r.size() with pytest.raises(AssertionError): _ = target.compute_functional_gradients_and_curvatures( mock_single_Ih_table) restraints = target.compute_restraints_residuals_and_gradients(apm_restr) assert len(restraints) == 3 assert target.param_restraints is True restraints = target.compute_restraints_functional_gradients_and_curvatures( apm_restr) assert len(restraints) == 3 achieved = target.achieved() assert isinstance(achieved, bool) restraints = target.compute_restraints_residuals_and_gradients(apm_norestr) assert restraints is None assert target.param_restraints is False target = ScalingTarget( ) # Need to make new instance or won't calc restr as # param_restraints is set to False assert target.param_restraints is True restraints = target.compute_restraints_functional_gradients_and_curvatures( apm_norestr) assert restraints is None assert target.param_restraints is False
def test_target_function_methods(): """Test for the target methods required for the refinement engine.""" target = ScalingTarget() r, w = target.compute_residuals(mock_single_Ih_table()) assert r.size() == w.size() assert r == pytest.approx([-1.0, 0.0, 1.0]) assert w == pytest.approx([1.0, 1.0, 1.0]) f, g = target.compute_functional_gradients(mock_single_Ih_table()) assert f == pytest.approx(2.0) assert g == pytest.approx([-19.04072398]) r2, j, w2 = target.compute_residuals_and_gradients(mock_single_Ih_table()) assert r == r2 assert w == w2 assert j.n_cols == 1 and j.n_rows == 3
def test_target_jacobian_calculation_finite_difference(physical_param, single_exp, large_reflection_table): """Test the calculated jacobian against a finite difference calculation.""" physical_param.physical.decay_correction = False model = PhysicalScalingModel.from_data(physical_param, single_exp, large_reflection_table) # need to 'add_data' model.configure_components(large_reflection_table, single_exp, physical_param) model.components["scale"].update_reflection_data() apm = multi_active_parameter_manager( ScalingTarget(), [model.components], [["scale"]], scaling_active_parameter_manager, ) Ih_table = IhTable([large_reflection_table], single_exp.crystal.get_space_group()) with patch.object(SingleScaler, "__init__", lambda x, y, z, k: None): scaler = SingleScaler(None, None, None) scaler._Ih_table = Ih_table target = ScalingTarget() scaler.update_for_minimisation(apm, 0) fd_jacobian = calculate_jacobian_fd(target, scaler, apm) r, jacobian, w = target.compute_residuals_and_gradients( scaler.Ih_table.blocked_data_list[0]) assert r == pytest.approx( [-50.0 / 3.0, 70.0 / 3.0, -20.0 / 3.0, 12.5, -2.5] + [-25.0, 0.0, -75.0, 0.0, 200.0]) assert w == pytest.approx( [0.1, 0.1, 0.1, 0.02, 0.1, 0.02, 0.01, 0.02, 0.01, 0.01]) n_rows = jacobian.n_rows n_cols = jacobian.n_cols print(jacobian) print(fd_jacobian) for i in range(0, n_rows): for j in range(0, n_cols): assert jacobian[i, j] == pytest.approx(fd_jacobian[i, j], abs=1e-4)