Exemple #1
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
Exemple #2
0
def test_rule152_complexes_no_caching(rule152):
    net = rule152
    # Mapping from index of a PyPhi subsystem in network.subsystems to the
    # index of the corresponding subsystem in the Matlab list of subsets
    perm = {0: 0, 1: 1, 2: 3, 3: 7, 4: 15, 5: 2, 6: 4, 7: 8, 8: 16, 9: 5, 10:
            9, 11: 17, 12: 11, 13: 19, 14: 23, 15: 6, 16: 10, 17: 18, 18: 12,
            19: 20, 20: 24, 21: 13, 22: 21, 23: 25, 24: 27, 25: 14, 26: 22, 27:
            26, 28: 28, 29: 29, 30: 30}
    with open('test/data/rule152_results.pkl', 'rb') as f:
        results = pickle.load(f)

    # Don't use concept caching for this test.
    constants.CACHE_CONCEPTS = False

    for state, result in results.items():
        # Empty the DB.
        _flushdb()
        # Unpack the state from the results key.
        # Generate the network with the state we're testing.
        net = Network(rule152.tpm, state, cm=rule152.cm)
        # Comptue all the complexes, leaving out the first (empty) subsystem
        # since Matlab doesn't include it in results.
        complexes = list(compute.complexes(net))[1:]
        # Check the phi values of all complexes.
        zz = [(sia.phi, result['subsystem_phis'][perm[i]]) for i, sia in
            list(enumerate(complexes))]
        diff = [utils.eq(sia.phi, result['subsystem_phis'][perm[i]]) for
                i, sia in list(enumerate(complexes))]
        assert all(utils.eq(sia.phi, result['subsystem_phis'][perm[i]])
                   for i, sia in list(enumerate(complexes))[:])
        # Check the major complex in particular.
        major = compute.major_complex(net)
        # Check the phi value of the major complex.
        assert utils.eq(major.phi, result['phi'])
        # Check that the nodes are the same.
        assert (major.subsystem.node_indices ==
                complexes[result['major_complex'] - 1].subsystem.node_indices)
        # Check that the concept's phi values are the same.
        result_concepts = [c for c in result['concepts']
                           if c['is_irreducible']]
        z = list(zip([c.phi for c in major.ces],
                     [c['phi'] for c in result_concepts]))
        diff = [i for i in range(len(z)) if not utils.eq(z[i][0], z[i][1])]
        assert all(list(utils.eq(c.phi, result_concepts[i]['phi']) for i, c
                        in enumerate(major.ces)))
        # Check that the minimal cut is the same.
        assert major.cut == result['cut']
Exemple #3
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
Exemple #4
0
 def time_L1_approximation(self, distance):
     compute.major_complex(self.network, self.state)
Exemple #5
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
Exemple #6
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
Exemple #7
0
 def time_major_complex(self, mode, network, cache):
     # Do it!
     compute.major_complex(self.network, self.state)