Beispiel #1
0
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
Beispiel #2
0
def test_target_function_restraints_methods(mock_apm_restrained,
                                            mock_apm_unrestrained):
    """Test for the target restraints methods required for the refinement engine."""
    target = ScalingTarget()

    restraints = target.compute_restraints_residuals_and_gradients(
        mock_apm_restrained)
    assert len(restraints) == 3
    assert target.param_restraints is True
    assert list(restraints[0]) == [1.0, 1.1, 1.2]
    assert list(restraints[2]) == [1.0, 2.0, 3.0]

    restraints = target.compute_restraints_functional_gradients(
        mock_apm_restrained)
    assert len(restraints) == 2
    assert restraints[0] == 6.0
    assert list(restraints[1]) == [0.1, 0.2, 0.3]

    achieved = target.achieved()
    assert isinstance(achieved, bool)

    restraints = target.compute_restraints_residuals_and_gradients(
        mock_apm_unrestrained)
    assert restraints is None
    assert target.param_restraints is False
    # Check that if called again, returns None without doing calculations
    restraints = target.compute_restraints_residuals_and_gradients([])
    assert restraints is None

    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(
        mock_apm_unrestrained)
    assert restraints is None
    assert target.param_restraints is False