Skip to content

Utils

assert_matrix_correlation_above_value(actual, expected, mask=None, min_correlation=0.95, msg='')

Assert that two tensors are close with a root mean squared error (RMSE) relative to the scaled root mean square values for each matrix. This tells you if the RMSE implies that the two matrices are more similar to eachother as-is than would be the case if values were randomly permuted.

Parameters:

Name Type Description Default
actual Tensor

The actual tensor.

required
expected Tensor

The expected tensor.

required
mask Optional[Tensor]

If there are only some values you want to compare, apply this mask and RMSE will be computed on the unmasked items only.

None
min_relative_rmse

The relative tolerance parameter.

required
Source code in bionemo/testing/utils.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def assert_matrix_correlation_above_value(  # noqa: D417
    actual: torch.Tensor,
    expected: torch.Tensor,
    mask: Optional[torch.Tensor] = None,
    min_correlation: float = 0.95,
    msg: str = "",
) -> None:
    """Assert that two tensors are close with a root mean squared error (RMSE)
        relative to the scaled root mean square values for each matrix. This tells
        you if the RMSE implies that the two matrices are more similar to eachother
        as-is than would be the case if values were randomly permuted.

    Args:
        actual: The actual tensor.
        expected: The expected tensor.
        mask: If there are only some values you want to compare,
            apply this mask and RMSE will be computed on the unmasked items only.
        min_relative_rmse: The relative tolerance parameter.
    """  # noqa: D205
    if mask is None:
        mask = torch.ones_like(actual)
    else:
        if len(mask.shape) < len(actual.shape):
            mask = mask[..., None]
    masked_actual = actual[mask.expand_as(actual).to(bool)]
    masked_expected = expected[mask.expand_as(expected).to(bool)]
    corr = torch.corrcoef(torch.stack([masked_actual, masked_expected]))[0, 1]
    if corr < min_correlation:
        raise AssertionError(f"Correlation below threshold: {corr} < {min_correlation}. {msg}")

assert_matrix_mape_below_value(actual, expected, mask=None, max_mape=0.1, eps=0.001, msg='')

Assert that two tensors are close with a root mean squared error (RMSE) relative to the scaled root mean square values for each matrix. This tells you if the RMSE implies that the two matrices are more similar to eachother as-is than would be the case if values were randomly permuted.

Parameters:

Name Type Description Default
actual Tensor

The actual tensor.

required
expected Tensor

The expected tensor.

required
mask Optional[Tensor]

If there are only some values you want to compare, apply this mask and RMSE will be computed on the unmasked items only.

None
min_relative_rmse

The relative tolerance parameter.

required
Source code in bionemo/testing/utils.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def assert_matrix_mape_below_value(  # noqa: D417
    actual: torch.Tensor,
    expected: torch.Tensor,
    mask: Optional[torch.Tensor] = None,
    max_mape: float = 0.1,
    eps: float = 1e-3,
    msg: str = "",
) -> None:
    """Assert that two tensors are close with a root mean squared error (RMSE)
        relative to the scaled root mean square values for each matrix. This tells
        you if the RMSE implies that the two matrices are more similar to eachother
        as-is than would be the case if values were randomly permuted.

    Args:
        actual: The actual tensor.
        expected: The expected tensor.
        mask: If there are only some values you want to compare,
            apply this mask and RMSE will be computed on the unmasked items only.
        min_relative_rmse: The relative tolerance parameter.
    """  # noqa: D205
    if mask is None:
        mask = torch.ones_like(actual)
    else:
        if len(mask.shape) < len(actual.shape):
            mask = mask[..., None]
    masked_actual = actual[mask.expand_as(actual).to(bool)]
    masked_expected = expected[mask.expand_as(expected).to(bool)]
    mape = (
        torch.mean(
            torch.abs(masked_actual - masked_expected)
            / torch.maximum(torch.abs(masked_expected), torch.zeros_like(masked_expected) + eps)
        )
        * 100.0
    )
    if mape > max_mape:
        raise AssertionError(f"MAPE below threshold: {mape} > {max_mape}. {msg}")