Exemple #1
0
def test_validate_outcome_length():
    # Is a sequence
    x = ['1', '2', '3']
    assert v.validate_outcome_length(x)
    # Not a sequence
    x = ['1', '2', 3]
    with pytest.raises(dit.exceptions.ditException):
        v.validate_outcome_length(x)
    # Unequal lengths
    x = ['1', '2', '33']
    with pytest.raises(dit.exceptions.ditException):
        v.validate_outcome_length(x)
Exemple #2
0
def test_validate_outcome_length():
    # Is a sequence
    x = ['1', '2', '3']
    assert_true(v.validate_outcome_length(x))
    # Not a sequence
    x = ['1', '2', 3]
    assert_raises(dit.exceptions.ditException, v.validate_outcome_length, x)
    # Unequal lengths
    x = ['1', '2', '33']
    assert_raises(dit.exceptions.ditException, v.validate_outcome_length, x)
Exemple #3
0
def test_validate_outcome_length():
    # Is a sequence
    x = ['1', '2', '3']
    assert_true(v.validate_outcome_length(x))
    # Not a sequence
    x = ['1', '2', 3]
    assert_raises(dit.exceptions.ditException, v.validate_outcome_length, x)
    # Unequal lengths
    x = ['1', '2', '33']
    assert_raises(dit.exceptions.ditException, v.validate_outcome_length, x)
Exemple #4
0
def SDtoD(dist):
    """
    Convert a ScalarDistribution to a Distribution.

    Parameters
    ----------
    dist : ScalarDistribution
        The ScalarDistribution to convert to a Distribution.

    """
    from dit.exceptions import ditException, InvalidDistribution
    import dit.validate as v

    if len(dist.pmf) == 0:
        msg = "Cannot convert from empty ScalarDistribution."
        raise InvalidDistribution(msg)

    # Check if every element of the sample space is a sequence of the same
    # length. If so, this is an easy conversion.  If not, then we make
    # every outcome a 1-tuple and then construct the joint distribution.
    try:
        # Each outcome is of the same class.
        v.validate_outcome_class(dist.outcomes)
        # Each outcome has the same length.
        v.validate_outcome_length(dist.outcomes)
        # Each outcome is a 'sequence'.
        v.validate_sequence(dist.outcomes[0])
    except ditException:
        # Nested translation.
        outcomes = [(o,) for o in dist.outcomes]
    else:
        outcomes = dist.outcomes

    d = dit.Distribution(outcomes, dist.pmf, base=dist.get_base())

    return d
Exemple #5
0
def SDtoD(dist):
    """
    Convert a ScalarDistribution to a Distribution.

    Parameters
    ----------
    dist : ScalarDistribution
        The ScalarDistribution to convert to a Distribution.

    """
    from dit.exceptions import ditException, InvalidDistribution
    import dit.validate as v

    if len(dist.pmf) == 0:
        msg = "Cannot convert from empty ScalarDistribution."
        raise InvalidDistribution(msg)

    # Check if every element of the sample space is a sequence of the same
    # length. If so, this is an easy conversion.  If not, then we make
    # every outcome a 1-tuple and then construct the joint distribution.
    try:
        # Each outcome is of the same class.
        v.validate_outcome_class(dist.outcomes)
        # Each outcome has the same length.
        v.validate_outcome_length(dist.outcomes)
        # Each outcome is a 'sequence'.
        v.validate_sequence(dist.outcomes[0])
    except ditException:
        # Nested translation.
        outcomes = [(o, ) for o in dist.outcomes]
    else:
        outcomes = dist.outcomes

    d = dit.Distribution(outcomes, dist.pmf, base=dist.get_base())

    return d