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])
def test_blackbox_partial_noise(s): blackbox = macro.Blackbox(((0, ), (1, 2)), (0, 1)) subsys = macro.MacroSubsystem(s.network, s.state, s.node_indices, blackbox=blackbox) noised = subsys._blackbox_partial_noise(blackbox, macro.SystemAttrs.pack(s)) # Noise connection from 2 -> 0 assert np.array_equal( noised.tpm, convert.to_multidimensional( np.array([ [.5, 0, 0], [.5, 0, 1], [1., 0, 1], [1., 0, 0], [.5, 1, 0], [.5, 1, 1], [1., 1, 1], [1., 1, 0], ]))) # No change assert np.array_equal(noised.cm, np.array([[0, 0, 1], [1, 0, 1], [1, 1, 0]]))
def test_run_tpm(): tpm = sbs2sbn(np.array([ [0, 1], [1, 0], ])) answer = sbs2sbn(np.array([ [1, 0], [0, 1], ])) assert np.array_equal(timescale.run_tpm(tpm, 2), answer) tpm = np.array([ [0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1], [1, 1, 1], [1, 1, 0], ]) answer = convert.to_multidimensional( np.array([ [0, 0, 0], [1, 1, 0], [1, 1, 1], [0, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 0], [1, 0, 0], ])) assert np.array_equal(timescale.run_tpm(tpm, 2), answer)
def test_blackbox_partial_noise(s): blackbox = macro.Blackbox(((0,), (1, 2)), (0, 1)) subsys = macro.MacroSubsystem(s.network, s.state, s.node_indices, blackbox=blackbox) noised = subsys._blackbox_partial_noise(blackbox, macro.SystemAttrs.pack(s)) # Noise connection from 2 -> 0 assert np.array_equal( noised.tpm, convert.to_multidimensional(np.array([ [.5, 0, 0], [.5, 0, 1], [1., 0, 1], [1., 0, 0], [.5, 1, 0], [.5, 1, 1], [1., 1, 1], [1., 1, 0], ]))) # No change assert np.array_equal( noised.cm, np.array([ [0, 0, 1], [1, 0, 1], [1, 1, 0] ]))
def test_run_tpm(): tpm = sbs2sbn(np.array([ [0, 1], [1, 0], ])) answer = sbs2sbn(np.array([ [1, 0], [0, 1], ])) assert np.array_equal(timescale.run_tpm(tpm, 2), answer) tpm = np.array([ [0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1], [1, 1, 1], [1, 1, 0], ]) answer = convert.to_multidimensional(np.array([ [0, 0, 0], [1, 1, 0], [1, 1, 1], [0, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 0], [1, 0, 0], ])) assert np.array_equal(timescale.run_tpm(tpm, 2), answer)
def test_degenerate(degenerate): assert np.array_equal( degenerate.tpm, convert.to_multidimensional(np.array([[0, 0], [0, 1], [1, 0], [1, 1]]))) assert np.array_equal(degenerate.cm, np.array([[0, 1], [1, 0]])) sia = compute.sia(degenerate) assert sia.phi == 0.638888
def test_state_by_state2state_by_node(): result = convert.state_by_state2state_by_node(state_by_state) expected = convert.to_multidimensional(state_by_node) print("Result:") print(result) print("Expected:") print(expected) assert np.array_equal(result, expected)
def test_to_2dimensional(): # Identity assert np.array_equal(convert.to_2dimensional(state_by_node), state_by_node) # Idempotency for tpm in [state_by_node, state_by_node_nondet, twod_state_by_node]: nd = convert.to_multidimensional(tpm) assert np.array_equal(convert.to_2dimensional(nd), tpm)
def test_nondet_state_by_state2state_by_node(): # Test for nondeterministic TPM. result = convert.state_by_state2state_by_node(state_by_state_nondet) expected = convert.to_multidimensional(state_by_node_nondet) print("Result:") print(result) print("Expected:") print(expected) assert np.array_equal(result, expected)
def test_n_d_state_by_node2state_by_state(): # Check with N-D form. sbn = convert.to_multidimensional(state_by_node) result = convert.state_by_node2state_by_state(sbn) expected = state_by_state print("Result:") print(result) print("Expected:") print(expected) assert np.array_equal(result, expected)
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])
def test_degenerate(degenerate): assert np.array_equal(degenerate.tpm, convert.to_multidimensional(np.array([ [0, 0], [0, 1], [1, 0], [1, 1] ]))) assert np.array_equal(degenerate.cm, np.array([ [0, 1], [1, 0] ])) sia = compute.sia(degenerate) assert sia.phi == 0.638888
def test_blackbox_timescale(): # System is an OR gate and a COPY gate; the OR gate is connected with a # self loop. # fmt: off tpm = convert.to_multidimensional( np.array([ [0, 0], [1, 1], [1, 0], [1, 1], ])) cm = np.array([ [1, 1], [1, 0], ]) # fmt: on indices = (0, 1) blackbox = macro.Blackbox(((0, ), (1, )), (0, 1)) steps = 2 state = (1, 0) system = macro.SystemAttrs(tpm, cm, indices, state) result = macro.run_tpm(system, steps, blackbox) # fmt: off answer = convert.state_by_state2state_by_node( np.array([ [1, 3, 1, 3], [0, 4, 0, 4], [1, 3, 1, 3], [0, 4, 0, 4], ]) / 8) # fmt: on np.testing.assert_array_equal(result, answer) result = macro.run_tpm(system, steps, blackbox) # fmt: off answer = convert.state_by_state2state_by_node( np.array([ [1, 1, 1, 1], [0, 2, 0, 2], [1, 1, 1, 1], [0, 2, 0, 2], ]) / 4) # fmt: on np.testing.assert_array_equal(result, answer)
def test_blackbox_timescale(): # System is an OR gate and a COPY gate; the OR gate is connected with a # self loop. tpm = convert.to_multidimensional(np.array([ [0, 0], [1, 1], [1, 0], [1, 1], ])) cm = np.array([ [1, 1], [1, 0], ]) indices = (0, 1) blackbox = macro.Blackbox(((0,), (1,)), (0, 1)) steps = 2 state = (1, 0) system = macro.SystemAttrs(tpm, cm, indices, state) result = macro.run_tpm(system, steps, blackbox) answer = convert.state_by_state2state_by_node(np.array([ [1, 3, 1, 3], [0, 4, 0, 4], [1, 3, 1, 3], [0, 4, 0, 4], ]) / 8) np.testing.assert_array_equal(result, answer) result = macro.run_tpm(system, steps, blackbox) answer = convert.state_by_state2state_by_node(np.array([ [1, 1, 1, 1], [0, 2, 0, 2], [1, 1, 1, 1], [0, 2, 0, 2], ]) / 4) np.testing.assert_array_equal(result, answer)