Exemple #1
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 #2
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 #3
0
def test_validate_sequence():
    x = '101'
    assert_true(v.validate_sequence(x))
    x = 3
    assert_raises(dit.exceptions.ditException, v.validate_sequence, x)
Exemple #4
0
def test_validate_sequence():
    x = '101'
    assert v.validate_sequence(x)
    x = 3
    with pytest.raises(dit.exceptions.ditException):
        v.validate_sequence(x)
Exemple #5
0
def test_validate_sequence():
    x = '101'
    assert_true(v.validate_sequence(x))
    x = 3
    assert_raises(dit.exceptions.ditException, v.validate_sequence, x)