Esempio n. 1
0
def test_normalize_invalid_ratio():
    """Test invalid ratios."""
    cases = [
        1.1,
        [1.1],
        [0.8, 0.3],
        [0.8, 0.1, 0.2],
    ]
    for ratios in cases:
        with pytest.raises(ValueError):
            _ = normalize_ratios(ratios=ratios)
Esempio n. 2
0
def test_normalize_ratios():
    """Test normalize_ratios."""
    for ratios, exp_output in (
        (0.5, (0.5, 0.5)),
        ((0.3, 0.2, 0.4), (0.3, 0.2, 0.4, 0.1)),
        ((0.3, 0.3, 0.4), (0.3, 0.3, 0.4)),
    ):
        output = normalize_ratios(ratios=ratios)
        # check type
        assert isinstance(output, tuple)
        assert all(isinstance(ratio, float) for ratio in output)
        # check values
        assert len(output) >= 2
        assert all(0 <= ratio <= 1 for ratio in output)
        output_np = numpy.asarray(output)
        numpy.testing.assert_almost_equal(output_np.sum(), numpy.ones(1))
        # compare against expected
        numpy.testing.assert_almost_equal(output_np, numpy.asarray(exp_output))
Esempio n. 3
0
def _get_ratios(*triples_factories: TriplesFactory) -> Sequence[float]:
    total = sum(tf.num_triples for tf in triples_factories)
    ratios = normalize_ratios(
        [tf.num_triples / total for tf in triples_factories])
    return ratios