Example #1
0
def convert_be_tpm_to_le(be_tpm):
    # Assumes state by node format
    states, nodes = be_tpm.shape
    le_tpm = np.zeros([states, nodes])
    for i in range(states):
        le_state = le_index2state(i, nodes)
        be_tpm_row = state2be_index(le_state)
        le_tpm[i, :] = be_tpm[be_tpm_row, :]

    return le_tpm
Example #2
0
def test_to_multidimensional():
    # Identity
    assert np.array_equal(convert.to_multidimensional(nd_state_by_node),
                          nd_state_by_node)

    for tpm in [state_by_node, twod_state_by_node]:
        N = tpm.shape[-1]
        S = tpm.shape[0]
        result = convert.to_multidimensional(tpm)
        for i in range(S):
            state = convert.le_index2state(i, N)
            assert np.array_equal(result[state], tpm[i])
Example #3
0
def pretty_print_tpm(node_tokens, tpm):
    number_of_states, number_of_nodes = tpm.shape
    for state_index in range(number_of_states):
        current_state = le_index2state(state_index, number_of_nodes)
        next_state = tpm[state_index, :]
        pretty_tokens = format_node_tokens_by_state(node_tokens,
                                                    current_state,
                                                    mode='back')
        pretty_tokens = format_node_tokens_by_state(pretty_tokens,
                                                    next_state,
                                                    mode='fore')
        print(':'.join(pretty_tokens))
Example #4
0
def test_to_multidimensional():
    # Identity
    assert np.array_equal(
        convert.to_multidimensional(nd_state_by_node), nd_state_by_node
    )

    for tpm in [
        state_by_node,
        twod_state_by_node,
        nonsquare_deterministic_1,
        nonsquare_deterministic_2,
        nonsquare_nondeterministic_1,
        nonsquare_nondeterministic_2,
    ]:
        S = tpm.shape[0]
        N = int(log2(S))
        result = convert.to_multidimensional(tpm)
        for i in range(S):
            state = convert.le_index2state(i, N)
            assert np.array_equal(result[state], tpm[i])
Example #5
0
def test_le_index2state():
    assert convert.le_index2state(7, 8) == (1, 1, 1, 0, 0, 0, 0, 0)
    assert convert.le_index2state(1, 3) == (1, 0, 0)
    assert convert.le_index2state(8, 4) == (0, 0, 0, 1)