def test_subsystem_validation(s): # Wrong state length. with pytest.raises(ValueError): s = Subsystem(s.network, (0, 0), s.node_indices) # Wrong state values. with pytest.raises(ValueError): s = Subsystem(s.network, (2, 0, 0), s.node_indices) # Disallow impossible states at subsystem level (we don't want to return a # phi-value associated with an impossible state). net = example_networks.simple() with pytest.raises(exceptions.StateUnreachableError): s = Subsystem(net, (0, 1, 0), s.node_indices)
def test_validate_cut_nodes_equal_subsystem_nodes(s): assert s.node_indices == (0, 1, 2) cut = Cut((0, ), (1, 2)) # A-ok Subsystem(s.network, s.state, s.node_indices, cut=cut) cut = Cut((0, ), (1, )) # missing node 2 in cut with pytest.raises(ValueError): Subsystem(s.network, s.state, s.node_indices, cut=cut) cut = Cut((0, ), (1, 2)) # missing node 2 in subsystem with pytest.raises(ValueError): Subsystem(s.network, s.state, (0, 1), cut=cut)
def test_find_cut_matrix(s, big_subsys_0_thru_3): cut = Cut((0, ), (1, 2)) cut_s = Subsystem(s.node_indices, s.network, cut=cut, mice_cache=s._mice_cache) answer_s = np.array([[0, 1, 1], [0, 0, 0], [0, 0, 0]]) cut = Cut((0, 1), (2, 3)) cut_big = Subsystem(big_subsys_0_thru_3.node_indices, big_subsys_0_thru_3.network, cut=cut, mice_cache=big_subsys_0_thru_3._mice_cache) answer_big = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]) assert np.array_equal(cut_s.cut_matrix, answer_s) assert np.array_equal(cut_big.cut_matrix, answer_big)
def trivial(): """Single-node network with a self-loop.""" tpm = np.array([[1], [1]]) cm = np.array([[1]]) net = Network(tpm, connectivity_matrix=cm) state = (1, ) return Subsystem(net, state, range(net.size))
def trivial(): """Single-node network with a self-loop.""" trivial = Network(np.array([[1], [1]]), (1, ), (1, ), connectivity_matrix=np.array([[1]])) return Subsystem(range(trivial.size), trivial)
def reducible(cm=False): tpm = np.zeros([2] * 2 + [2]) if cm is False: cm = np.array([[1, 0], [0, 1]]) r = Network(tpm, connectivity_matrix=cm) state = (0, 0) # Return the full subsystem return Subsystem(r, state, range(r.size))
def test_find_cut_matrix(s, big_subsys_0_thru_3): cut = Cut((0, ), (1, 2)) cut_s = Subsystem(s.network, s.state, s.node_indices, cut=cut) answer_s = np.array([[0, 1, 1], [0, 0, 0], [0, 0, 0]]) assert np.array_equal(cut_s.cut_matrix, answer_s) cut = Cut((0, 1), (2, 3)) cut_big = Subsystem(big_subsys_0_thru_3.network, big_subsys_0_thru_3.state, big_subsys_0_thru_3.node_indices, cut=cut) answer_big = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]]) assert np.array_equal(cut_big.cut_matrix, answer_big) null_cut_matrix = np.array([ [0, 0, 0], [0, 0, 0], [0, 0, 0], ]) assert np.array_equal(s.cut_matrix, null_cut_matrix)
def rule30(cm=False): # fmt: off tpm = np.array([ [0, 0, 0, 0, 0], [1, 1, 0, 0, 1], [1, 1, 1, 0, 0], [1, 0, 1, 0, 1], [0, 1, 1, 1, 0], [1, 0, 1, 1, 1], [1, 1, 0, 1, 0], [1, 0, 0, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 0, 1, 1], [1, 0, 0, 1, 0], [0, 1, 1, 0, 1], [1, 0, 1, 0, 0], [1, 1, 0, 0, 1], [1, 0, 0, 0, 0], [1, 0, 0, 1, 1], [0, 1, 0, 1, 1], [0, 1, 1, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 0, 1], [0, 0, 1, 0, 1], [0, 1, 0, 0, 1], [0, 0, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 0, 0, 1, 0], [1, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], ]) if cm is False: cm = np.array([ [1, 1, 0, 0, 1], [1, 1, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1], [1, 0, 0, 1, 1], ]) # fmt: on rule30 = Network(tpm, cm=cm) all_off = (0, 0, 0, 0, 0) return Subsystem(rule30, all_off, range(rule30.size))
def rule30(cm=False): tpm = np.array([[0, 0, 0, 0, 0], [1, 1, 0, 0, 1], [1, 1, 1, 0, 0], [1, 0, 1, 0, 1], [0, 1, 1, 1, 0], [1, 0, 1, 1, 1], [1, 1, 0, 1, 0], [1, 0, 0, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 0, 1, 1], [1, 0, 0, 1, 0], [0, 1, 1, 0, 1], [1, 0, 1, 0, 0], [1, 1, 0, 0, 1], [1, 0, 0, 0, 0], [1, 0, 0, 1, 1], [0, 1, 0, 1, 1], [0, 1, 1, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 0, 1], [0, 0, 1, 0, 1], [0, 1, 0, 0, 1], [0, 0, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 0, 0, 1, 0], [1, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0]]) if cm is False: cm = np.array([[1, 1, 0, 0, 1], [1, 1, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1], [1, 0, 0, 1, 1]]) rule30 = Network(tpm, connectivity_matrix=cm) all_off = (0, 0, 0, 0, 0) return Subsystem(rule30, all_off, range(rule30.size))
def rule152_s_complete(): net = rule152(cm=None) state = [0] * 5 return Subsystem(net, state, range(net.size))
def subsys_n1n2(): net = standard() return Subsystem(net, s_state, (1, 2))
def rule152_s(): net = rule152() state = [0] * 5 return Subsystem(net, state, range(net.size))
def eights(): net = eight_node() state = [0] * 8 return Subsystem(net, state, range(net.size))
def eights_complete(): net = eight_node(cm=None) state = [0] * 8 return Subsystem(net, state, range(net.size))
def test_node_neq_by_context(s): other_network = Network(s.network.tpm, (0, 0, 0), (0, 0, 0)) other_s = Subsystem(s.node_indices, other_network) assert s.nodes[0] != Node(other_network, 0, other_s)
def s_single(): net = standard() return Subsystem(net, s_state, (1, ))
def micro_s(): net = micro() state = [1] * 4 return Subsystem(net, state, range(net.size))
def s_noised(): n = noised() state = (1, 0, 0) return Subsystem(n, state, range(n.size))
def big_subsys_0_thru_3(): """Return a subsystem consisting of the first 4 nodes of ``big``.""" net = big() state = (1, ) * 5 return Subsystem(net, state, range(net.size)[:-1])
def test_empty_init(s): # Empty mechanism s = Subsystem(s.network, s.state, ()) assert s.nodes == ()
def big_subsys_all(): """Return the subsystem associated with ``big``.""" net = big() state = (1, ) * 5 return Subsystem(net, state, range(net.size))
def simple_subsys_all_a_just_on(): net = simple() a_just_turned_on = (1, 0, 0) return Subsystem(net, a_just_turned_on, range(net.size))
def simple_subsys_all_off(): net = simple() return Subsystem(net, s_all_off, range(net.size))
def macro_s(): net = macro() state = [0] * 2 return Subsystem(net, state, range(net.size))
def test_is_cut(s): assert s.is_cut() is False s = Subsystem(s.network, s.state, s.node_indices, cut=Cut((0,), (1, 2))) assert s.is_cut() is True
def s_complete(): net = standard(cm=None) return Subsystem(net, s_state, range(net.size))
def noisy_selfloop_single(): net = pyphi.examples.basic_noisy_selfloop_network() state = (1, 0, 0) return Subsystem(net, state, (1, ))
def micro_s_all_off(): net = micro() state = [0] * 4 return Subsystem(net, state, range(net.size))
def s_empty(): net = standard() return Subsystem(net, s_state, ())
def s_noised_complete(): n = noised(cm=None) state = (1, 0, 0) return Subsystem(n, state, range(n.size))
def test_specify_elements_with_labels(standard): network = Network(standard.tpm, node_labels=('A', 'B', 'C')) subsystem = Subsystem(network, (0, 0, 0), ('B', 'C')) assert subsystem.node_indices == (1, 2) assert tuple(node.label for node in subsystem.nodes) == ('B', 'C') assert str(subsystem) == 'Subsystem(B, C)'