コード例 #1
0
def test_macro2micro(s):
    # Only blackboxing
    blackbox = macro.Blackbox(((0, 2), (1, )), (1, 2))
    subsys = macro.MacroSubsystem(s.network,
                                  s.state,
                                  s.node_indices,
                                  blackbox=blackbox)
    assert subsys.macro2micro((0, )) == (0, 2)
    assert subsys.macro2micro((1, )) == (1, )
    assert subsys.macro2micro((1, 0)) == (0, 1, 2)

    assert subsys.macro2blackbox_outputs((0, )) == (2, )
    assert subsys.macro2blackbox_outputs((1, )) == (1, )
    assert subsys.macro2blackbox_outputs((1, 0)) == (1, 2)

    # Only coarse-graining
    coarse_grain = macro.CoarseGrain(((0, ), (1, 2)),
                                     (((0, ), (1, )), ((0, ), (1, 2))))
    subsys = macro.MacroSubsystem(s.network,
                                  s.state,
                                  s.node_indices,
                                  coarse_grain=coarse_grain)
    assert subsys.macro2micro((0, )) == (0, )
    assert subsys.macro2micro((1, )) == (1, 2)
    assert subsys.macro2micro((0, 1)) == (0, 1, 2)

    with pytest.raises(ValueError):
        subsys.macro2blackbox_outputs((0, ))

    # Blackboxing and coarse-graining
    blackbox = macro.Blackbox(((0, 2), (1, )), (1, 2))
    coarse_grain = macro.CoarseGrain(((1, 2), ), (((0, ), (1, 2)), ))
    subsys = macro.MacroSubsystem(s.network,
                                  s.state,
                                  s.node_indices,
                                  blackbox=blackbox,
                                  coarse_grain=coarse_grain)
    assert subsys.macro2micro((0, )) == (0, 1, 2)

    assert subsys.macro2blackbox_outputs((0, )) == (1, 2)

    # Pure micro
    subsys = macro.MacroSubsystem(s.network, s.state, s.node_indices)
    assert subsys.macro2micro((1, )) == (1, )
    assert subsys.macro2micro((0, 1)) == (0, 1)

    with pytest.raises(ValueError):
        subsys.macro2blackbox_outputs((1, ))
コード例 #2
0
def test_macro_cut_is_for_micro_indices(s):
    with pytest.raises(ValueError):
        macro.MacroSubsystem(s.network,
                             s.state,
                             s.node_indices,
                             blackbox=macro.Blackbox((2, ), (0, 1)),
                             cut=models.Cut((0, ), (1, )))
コード例 #3
0
def test_blackbox(s):
    ms = macro.MacroSubsystem(s.network, s.state, s.node_indices,
                              blackbox=macro.Blackbox(((0, 1, 2),), (1,)))
    assert np.array_equal(ms.tpm, np.array([[.5], [.5]]))
    assert np.array_equal(ms.cm, np.array([[1]]))
    assert ms.node_indices == (0,)
    assert ms.state == (0,)
コード例 #4
0
def macro_subsystem():

    tpm = np.zeros((16, 4)) + 0.3
    tpm[12:, 0:2] = 1
    tpm[3, 2:4] = 1
    tpm[7, 2:4] = 1
    tpm[11, 2:4] = 1
    tpm[15, 2:4] = 1

    # fmt: off
    cm = np.array([
        [0, 0, 1, 1],
        [0, 0, 1, 1],
        [1, 1, 0, 0],
        [1, 1, 0, 0],
    ])
    # fmt: on

    state = (0, 0, 0, 0)

    network = pyphi.Network(tpm, cm=cm, node_labels="ABCD")

    partition = ((0, 1), (2, 3))
    grouping = (((0, 1), (2, )), ((0, 1), (2, )))
    coarse_grain = macro.CoarseGrain(partition, grouping)

    return macro.MacroSubsystem(network,
                                state,
                                network.node_indices,
                                coarse_grain=coarse_grain)
コード例 #5
0
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]]))
コード例 #6
0
def test_blackbox_and_coarse_grain_external():
    # Larger, with external nodes, blackboxed and coarse-grained
    tpm = np.zeros((2**6, 6))
    network = pyphi.Network(tpm)
    state = (0, 0, 0, 0, 0, 0)

    blackbox = macro.Blackbox((
        (1, 4),
        (2, ),
        (3, ),
        (5, ),
    ), (1, 2, 3, 5))
    partition = ((1, ), (2, ), (3, 5))
    grouping = (((0, ), (1, )), ((1, ), (0, )), ((0, ), (1, 2)))
    coarse_grain = macro.CoarseGrain(partition, grouping)
    ms = macro.MacroSubsystem(network,
                              state, (1, 2, 3, 4, 5),
                              blackbox=blackbox,
                              coarse_grain=coarse_grain)
    answer_tpm = np.array([[[[0, 1, 0], [0, 1, 0]], [[0, 1, 0], [0, 1, 0]]],
                           [[[0, 1, 0], [0, 1, 0]], [[0, 1, 0], [0, 1, 0]]]])
    assert np.array_equal(ms.tpm, answer_tpm)
    assert np.array_equal(ms.cm, np.ones((3, 3)))
    assert ms.node_indices == (0, 1, 2)
    assert ms.size == 3
    assert ms.state == (0, 1, 0)
コード例 #7
0
def test_blackbox_external(s):
    # Which is the same if one of these indices is external
    ms = macro.MacroSubsystem(s.network, s.state, (1, 2),
                              blackbox=macro.Blackbox(((1, 2),), (1,)))
    assert np.array_equal(ms.tpm, np.array([[.5], [.5]]))
    assert np.array_equal(ms.cm, np.array([[1]]))
    assert ms.node_indices == (0,)
    assert ms.state == (0,)
コード例 #8
0
def test_subsystem_equality(s):
    macro_subsys = macro.MacroSubsystem(s.network, s.state, s.node_indices)
    assert s != macro_subsys
    assert hash(s) != hash(macro_subsys)

    blackbox = macro.Blackbox(((0, 1, 2),), (2,))
    macro_subsys_bb = macro.MacroSubsystem(
        s.network, s.state, s.node_indices, blackbox=blackbox, time_scale=2)
    assert macro_subsys != macro_subsys_bb
    assert hash(macro_subsys) != hash(macro_subsys_bb)

    coarse_grain = macro.CoarseGrain(
        ((0, 1), (2,)), (((0, 1), (2,)), ((0,), (1,))))
    macro_subsys_cg = macro.MacroSubsystem(
        s.network, s.state, s.node_indices, coarse_grain=coarse_grain)
    assert macro_subsys != macro_subsys_cg
    assert hash(macro_subsys) != hash(macro_subsys_cg)
コード例 #9
0
def test_blackbox_and_coarse_grain(s):
    blackbox = macro.Blackbox(((0, 1, 2),), (0, 2))
    coarse_grain = macro.CoarseGrain(partition=((0, 2),),
                                     grouping=((((0, 1), (2,)),)))
    ms = macro.MacroSubsystem(s.network, s.state, s.node_indices,
                              blackbox=blackbox, coarse_grain=coarse_grain)
    assert np.array_equal(ms.tpm, np.array([[0], [1]]))
    assert np.array_equal(ms.cm, [[1]])
    assert ms.node_indices == (0,)
    assert ms.size == 1
    assert ms.state == (0,)
コード例 #10
0
def test_xor_propogation_delay():
    # Three interconnected XOR gates, with COPY gates along each connection
    # acting as propagation delays.

    nodes = 9
    tpm = np.zeros((2**nodes, nodes))

    for psi, ps in enumerate(utils.all_states(nodes)):
        cs = [0 for i in range(nodes)]
        if ps[2] ^ ps[7]:
            cs[0] = 1
        if ps[0] == 1:
            cs[1] = 1
            cs[8] = 1
        if ps[1] ^ ps[5]:
            cs[3] = 1
        if ps[3] == 1:
            cs[2] = 1
            cs[4] = 1
        if ps[4] ^ ps[8]:
            cs[6] = 1
        if ps[6] == 1:
            cs[5] = 1
            cs[7] = 1
        tpm[psi, :] = cs

    cm = np.array([[0, 1, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0, 0, 0],
                   [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 1, 0, 0]])

    # The state of the system is all OFF
    state = (0, 0, 0, 0, 0, 0, 0, 0, 0)

    network = Network(tpm, cm=cm)

    partition = ((0, 2, 7), (1, 3, 5), (4, 6, 8))
    output_indices = (0, 3, 6)
    blackbox = macro.Blackbox(partition, output_indices)
    assert blackbox.hidden_indices == (1, 2, 4, 5, 7, 8)

    time = 2
    subsys = macro.MacroSubsystem(network,
                                  state,
                                  network.node_indices,
                                  blackbox=blackbox,
                                  time_scale=time)

    sia = compute.sia(subsys)
    assert sia.phi == 1.874999
    assert sia.cut == models.Cut((0, ), (1, 2, 3, 4, 5, 6, 7, 8))
コード例 #11
0
def test_coarse_grain(s):
    coarse_grain = macro.CoarseGrain(partition=((0, 1), (2, )),
                                     grouping=((((0, 1), (2, )), ((0, ),
                                                                  (1, )))))
    ms = macro.MacroSubsystem(s.network,
                              s.state,
                              s.node_indices,
                              coarse_grain=coarse_grain)
    answer_tpm = np.array([[[0, 0.66666667], [1, 0.66666667]], [[0, 0], [1,
                                                                         0]]])
    assert np.allclose(ms.tpm, answer_tpm)
    assert np.array_equal(ms.cm, np.ones((2, 2)))
    assert ms.node_indices == (0, 1)
    assert ms.state == (0, 0)
コード例 #12
0
def test_coarsegrain_spatial_degenerate():
    # TODO: move to docs?
    # macro-micro examples from Hoel2016
    # Example 2 - Spatial Degenerate
    # The micro system has a full complex, and big_phi = 0.19
    # The optimal coarse-graining groups AB, CD and EF, each with state
    # mapping ((0, 1), (2))

    nodes = 6
    tpm = np.zeros((2**nodes, nodes))

    for psi, ps in enumerate(utils.all_states(nodes)):
        cs = [0 for i in range(nodes)]
        if ps[0] == 1 and ps[1] == 1:
            cs[2] = 1
            cs[3] = 1
        if ps[2] == 1 and ps[3] == 1:
            cs[4] = 1
            cs[5] = 1
        if ps[4] == 1 and ps[5] == 1:
            cs[0] = 1
            cs[1] = 1
        tpm[psi, :] = cs

    cm = np.array([[0, 0, 1, 1, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1],
                   [0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0]])

    state = (0, 0, 0, 0, 0, 0)

    net = Network(tpm, cm)

    mc = compute.major_complex(net, state)
    assert mc.phi == 0.194445

    partition = ((0, 1), (2, 3), (4, 5))
    grouping = (((0, 1), (2, )), ((0, 1), (2, )), ((0, 1), (2, )))
    coarse = macro.CoarseGrain(partition, grouping)

    sub = macro.MacroSubsystem(net,
                               state,
                               range(net.size),
                               coarse_grain=coarse)

    sia = compute.sia(sub)
    assert sia.phi == 0.834183
コード例 #13
0
def degenerate():

    nodes = 6
    tpm = np.zeros((2**nodes, nodes))

    for psi, ps in enumerate(utils.all_states(nodes)):
        cs = [0 for i in range(nodes)]
        if ps[5] == 1:
            cs[0] = 1
            cs[1] = 1
        if ps[0] == 1 and ps[1]:
            cs[2] = 1
        if ps[2] == 1:
            cs[3] = 1
            cs[4] = 1
        if ps[3] == 1 and ps[4] == 1:
            cs[5] = 1
        tpm[psi, :] = cs

    cm = np.array([[0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0],
                   [0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 0]])

    current_state = (0, 0, 0, 0, 0, 0)

    network = Network(tpm, cm)

    partition = ((0, 1, 2), (3, 4, 5))
    output_indices = (2, 5)
    blackbox = macro.Blackbox(partition, output_indices)
    time_scale = 2

    return macro.MacroSubsystem(network,
                                current_state,
                                network.node_indices,
                                blackbox=blackbox,
                                time_scale=time_scale)
コード例 #14
0
def test_basic_nor_or():
    # A system composed of NOR and OR (copy) gates, which mimics the basic
    # pyphi network

    nodes = 12
    tpm = np.zeros((2**nodes, nodes))

    for psi, ps in enumerate(utils.all_states(nodes)):
        cs = [0 for i in range(nodes)]
        if ps[5] == 0 and ps[11] == 0:
            cs[0] = 1
        if ps[0] == 0:
            cs[1] = 1
        if ps[1] == 1:
            cs[2] = 1
        if ps[11] == 0:
            cs[3] = 1
        if ps[3] == 0:
            cs[4] = 1
        if ps[4] == 1:
            cs[5] = 1
        if ps[2] == 0:
            cs[6] = 1
        if ps[5] == 0:
            cs[7] = 1
        if ps[6] == 0 and ps[7] == 0:
            cs[8] = 1
        if ps[2] == 0 and ps[5] == 0:
            cs[9] = 1
        if ps[9] == 1:
            cs[10] = 1
        if ps[8] == 0 and ps[10] == 0:
            cs[11] = 1
        tpm[psi, :] = cs

    cm = np.array([
        [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    ])

    state = (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)

    network = Network(tpm, cm=cm)

    # (0, 1, 2) compose the OR element,
    # (3, 4, 5) the COPY,
    # (6, 7, 8, 9, 10, 11) the XOR
    partition = ((0, 1, 2), (3, 4, 5), (6, 7, 8, 9, 10, 11))
    output = (2, 5, 11)
    blackbox = macro.Blackbox(partition, output)
    assert blackbox.hidden_indices == (0, 1, 3, 4, 6, 7, 8, 9, 10)
    time = 3

    sub = macro.MacroSubsystem(network,
                               state,
                               network.node_indices,
                               blackbox=blackbox,
                               time_scale=time)

    with config.override(CUT_ONE_APPROXIMATION=True):
        sia = compute.sia(sub)

    assert sia.phi == 1.958332
    assert sia.cut == models.Cut((6, ), (0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11))
コード例 #15
0
def test_soup():
    # An first example attempting to capture the "soup" metaphor
    #
    # The system will consist of 6 elements 2 COPY elements (A, B) input to an
    # AND element (C) AND element (C) inputs to two COPY elements (D, E) 2 COPY
    # elements (D, E) input to an AND element (F) AND element (F) inputs to two
    # COPY elements (A, B)
    #
    # For the soup example, element B receives an additional input from D, and
    # implements AND logic instead of COPY

    nodes = 6
    tpm = np.zeros((2**nodes, nodes))

    for psi, ps in enumerate(utils.all_states(nodes)):
        cs = [0 for i in range(nodes)]
        if ps[5] == 1:
            cs[0] = 1
        if ps[3] == 1 and ps[5] == 1:
            cs[1] = 1
        if ps[0] == 1 and ps[1]:
            cs[2] = 1
        if ps[2] == 1:
            cs[3] = 1
            cs[4] = 1
        if ps[3] == 1 and ps[4] == 1:
            cs[5] = 1
        tpm[psi, :] = cs

    cm = np.array([[0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 0],
                   [0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 0]])

    network = Network(tpm, cm)

    # State all OFF
    state = (0, 0, 0, 0, 0, 0)
    assert compute.major_complex(network, state).phi == 0.125

    # With D ON (E must also be ON otherwise the state is unreachable)
    state = (0, 0, 0, 1, 1, 0)
    assert compute.major_complex(network, state).phi == 0.215278

    # Once the connection from D to B is frozen (with D in the ON state), we
    # recover the degeneracy example
    state = (0, 0, 0, 1, 1, 0)
    partition = ((0, 1, 2), (3, 4, 5))
    output_indices = (2, 5)
    blackbox = macro.Blackbox(partition, output_indices)
    time = 2
    sub = macro.MacroSubsystem(network,
                               state, (0, 1, 2, 3, 4, 5),
                               blackbox=blackbox,
                               time_scale=time)
    assert compute.phi(sub) == 0.638888

    # When the connection from D to B is frozen (with D in the OFF state),
    # element B is inactivated and integration is compromised.
    state = (0, 0, 0, 0, 0, 0)
    partition = ((0, 1, 2), (3, 4, 5))
    output_indices = (2, 5)
    blackbox = macro.Blackbox(partition, output_indices)
    time = 2
    sub = macro.MacroSubsystem(network,
                               state, (0, 1, 2, 3, 4, 5),
                               blackbox=blackbox,
                               time_scale=time)
    assert compute.phi(sub) == 0
コード例 #16
0
def test_pass_node_indices_as_a_range(s):
    # Test that node_indices can be a `range`
    macro.MacroSubsystem(s.network, s.state, range(s.size))
コード例 #17
0
def test_cut_indices(macro_subsystem, s):
    assert macro_subsystem.cut_indices == (0, 1, 2, 3)
    micro = macro.MacroSubsystem(s.network, s.state, s.node_indices)
    assert micro.cut_indices == (0, 1, 2)
コード例 #18
0
def test_node_indices_can_be_none(s):
    ms = macro.MacroSubsystem(s.network, s.state)
    assert ms.micro_node_indices == (0, 1, 2)