def test_clear_subsystem_caches_after_computing_sia_config_option(s): with config.override(CLEAR_SUBSYSTEM_CACHES_AFTER_COMPUTING_SIA=False, PARALLEL_CONCEPT_EVALUATION=False, PARALLEL_CUT_EVALUATION=False, CACHE_REPERTOIRES=True): sia = compute.sia(s) assert s._repertoire_cache.cache with config.override(CLEAR_SUBSYSTEM_CACHES_AFTER_COMPUTING_SIA=True, PARALLEL_CONCEPT_EVALUATION=False, PARALLEL_CUT_EVALUATION=False, CACHE_REPERTOIRES=True): sia = compute.sia(s) assert not s._repertoire_cache.cache
def test_sia_disconnected_network(reducible): assert (compute.sia(reducible) == models.SystemIrreducibilityAnalysis(subsystem=reducible, cut_subsystem=reducible, phi=0.0, ces=[], partitioned_ces=[]))
def test_sia_disconnected_network(reducible): assert compute.sia(reducible) == models.SystemIrreducibilityAnalysis( subsystem=reducible, cut_subsystem=reducible, phi=0.0, ces=[], partitioned_ces=[], )
def test_sia_empty_subsystem(s_empty): assert (compute.sia(s_empty) == models.SystemIrreducibilityAnalysis( phi=0.0, ces=(), partitioned_ces=(), subsystem=s_empty, cut_subsystem=s_empty))
def test_sia_wrappers(reducible): assert (compute.sia(reducible) == models.SystemIrreducibilityAnalysis(subsystem=reducible, cut_subsystem=reducible, phi=0.0, ces=[], partitioned_ces=[])) assert compute.phi(reducible) == 0.0
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_sia_disconnected_network(reducible, flushcache, restore_fs_cache): flushcache() assert (compute.sia(reducible) == models.SystemIrreducibilityAnalysis( subsystem=reducible, cut_subsystem=reducible, phi=0.0, ces=[], partitioned_ces=[]))
def test_sia_empty_subsystem(s_empty, flushcache, restore_fs_cache): flushcache() assert (compute.sia(s_empty) == models.SystemIrreducibilityAnalysis( phi=0.0, ces=(), partitioned_ces=(), subsystem=s_empty, cut_subsystem=s_empty))
def test_sia_wrappers(reducible): assert (compute.sia(reducible) == models.SystemIrreducibilityAnalysis(subsystem=reducible, cut_subsystem=reducible, phi=0.0, ces=[], partitioned_ces=[])) assert compute.phi(reducible) == 0.0
def test_sia_empty_subsystem(s_empty): assert (compute.sia(s_empty) == models.SystemIrreducibilityAnalysis( phi=0.0, ces=(), partitioned_ces=(), subsystem=s_empty, cut_subsystem=s_empty))
def test_ces_distance_switches_to_small_phi_difference(s): sia = compute.sia(s) ce_structures = (sia.ces, sia.partitioned_ces) with config.override(USE_SMALL_PHI_DIFFERENCE_FOR_CES_DISTANCE=False): assert compute.ces_distance(*ce_structures) == 2.3125 with config.override(USE_SMALL_PHI_DIFFERENCE_FOR_CES_DISTANCE=True): assert compute.ces_distance(*ce_structures) == 1.083333
def test_clear_subsystem_caches_after_computing_sia_config_option(s): with config.override( CLEAR_SUBSYSTEM_CACHES_AFTER_COMPUTING_SIA=False, PARALLEL_CONCEPT_EVALUATION=False, PARALLEL_CUT_EVALUATION=False, CACHE_REPERTOIRES=True, ): sia = compute.sia(s) assert s._repertoire_cache.cache with config.override( CLEAR_SUBSYSTEM_CACHES_AFTER_COMPUTING_SIA=True, PARALLEL_CONCEPT_EVALUATION=False, PARALLEL_CUT_EVALUATION=False, CACHE_REPERTOIRES=True, ): sia = compute.sia(s) assert not s._repertoire_cache.cache
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))
def test_deserialization_memoizes_duplicate_objects(s): with config.override(PARALLEL_CUT_EVALUATION=True): sia = compute.sia(s) loaded = jsonify.loads(jsonify.dumps(sia)) l1 = loaded.subsystem l2 = loaded.ces.subsystem assert l1 == l2 assert hash(l1) == hash(l2) assert l1 is l2
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))
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_expand_cause_repertoire(micro_s_all_off): sia = compute.sia(micro_s_all_off) A = sia.ces[0] cause = A.cause_repertoire assert np.all(abs(A.expand_cause_repertoire(CD) - cause) < EPSILON) assert np.all(abs( A.expand_cause_repertoire(BCD).flatten(order='F') - np.array([1 / 6 if i < 6 else 0 for i in range(8)])) < EPSILON) assert np.all(abs( A.expand_cause_repertoire(ABCD).flatten(order='F') - np.array([1 / 12 if i < 12 else 0 for i in range(16)])) < EPSILON) assert np.all(abs(A.expand_cause_repertoire(ABCD) - A.expand_cause_repertoire()) < EPSILON)
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
def test_expand_cause_repertoire(micro_s_all_off): sia = compute.sia(micro_s_all_off) A = sia.ces[0] cause = A.cause_repertoire assert np.all(abs(A.expand_cause_repertoire(CD) - cause) < EPSILON) assert np.all( abs( A.expand_cause_repertoire(BCD).flatten(order='F') - np.array([1 / 6 if i < 6 else 0 for i in range(8)])) < EPSILON) assert np.all( abs( A.expand_cause_repertoire(ABCD).flatten(order='F') - np.array([1 / 12 if i < 12 else 0 for i in range(16)])) < EPSILON) assert np.all( abs(A.expand_cause_repertoire(ABCD) - A.expand_cause_repertoire()) < EPSILON)
def test_expand_effect_repertoire(micro_s_all_off): sia = compute.sia(micro_s_all_off) A = sia.ces[0] effect = A.effect_repertoire assert np.all(abs(A.expand_effect_repertoire(CD) - effect) < EPSILON) assert np.all(abs(A.expand_effect_repertoire(BCD).flatten(order='F') - np.array([.25725, .23275, .11025, .09975, .11025, .09975, .04725, .04275])) < EPSILON) assert np.all(abs( A.expand_effect_repertoire(ABCD).flatten(order='F') - np.array([.13505625, .12219375, .12219375, .11055625, .05788125, .05236875, .05236875, .04738125, .05788125, .05236875, .05236875, .04738125, .02480625, .02244375, .02244375, .02030625])) < EPSILON) assert np.all(abs(A.expand_effect_repertoire(ABCD) - A.expand_effect_repertoire()) < EPSILON)
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
def test_deserialization_memoizes_duplicate_objects(s): with config.override(PARALLEL_CUT_EVALUATION=True): sia = compute.sia(s) s1 = sia.subsystem # Computed in a parallel process, so has a different id s2 = sia.ces[0].subsystem assert s1 is not s2 assert s1 == s2 assert hash(s1) == hash(s2) loaded = jsonify.loads(jsonify.dumps(sia)) l1 = loaded.subsystem l2 = loaded.ces[0].subsystem assert l1 == l2 assert hash(l1) == hash(l2) assert l1 is l2
def test_expand_effect_repertoire(micro_s_all_off): sia = compute.sia(micro_s_all_off) A = sia.ces[0] effect = A.effect_repertoire assert np.all(abs(A.expand_effect_repertoire(CD) - effect) < EPSILON) assert np.all( abs( A.expand_effect_repertoire(BCD).flatten(order='F') - np.array([ .25725, .23275, .11025, .09975, .11025, .09975, .04725, .04275 ])) < EPSILON) assert np.all( abs( A.expand_effect_repertoire(ABCD).flatten(order='F') - np.array([ .13505625, .12219375, .12219375, .11055625, .05788125, .05236875, .05236875, .04738125, .05788125, .05236875, .05236875, .04738125, .02480625, .02244375, .02244375, .02030625 ])) < EPSILON) assert np.all( abs(A.expand_effect_repertoire(ABCD) - A.expand_effect_repertoire()) < EPSILON)
def test_sia_macro(macro_s): sia = compute.sia(macro_s) check_sia(sia, macro_answer)
def test_sia_rule152(rule152_s): sia = compute.sia(rule152_s) check_sia(sia, rule152_answer)
def test_sia_big_network_0_thru_3(big_subsys_0_thru_3): sia = compute.sia(big_subsys_0_thru_3) check_sia(sia, big_subsys_0_thru_3_answer)
def test_sia_big_network(big_subsys_all): sia = compute.sia(big_subsys_all) check_sia(sia, big_answer)
def test_sia_complete_graph_rule152_s(rule152_s_complete): sia = compute.sia(rule152_s_complete) check_sia(sia, rule152_answer)
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))
def test_sia_complete_graph_standard_example(s_complete): sia = compute.sia(s_complete) check_sia(sia, standard_answer)
def test_sia_single_micro_nodes_without_selfloops_dont_have_phi(s_single): assert compute.sia(s_single).phi == 0.0
def test_sia_single_micro_node_selfloops_dont_have_phi(noisy_selfloop_single): assert compute.sia(noisy_selfloop_single).phi == 0.0
def test_sia_big_network_0_thru_3(big_subsys_0_thru_3, flushcache, restore_fs_cache): flushcache() sia = compute.sia(big_subsys_0_thru_3) check_sia(sia, big_subsys_0_thru_3_answer)
def test_sia_rule152(rule152_s, flushcache, restore_fs_cache): flushcache() sia = compute.sia(rule152_s) check_sia(sia, rule152_answer)
def test_sia_macro(macro_s, flushcache, restore_fs_cache): flushcache() sia = compute.sia(macro_s) check_sia(sia, macro_answer)
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))
def test_unpickle(s): bm = compute.sia(s) pickle.loads(pickle.dumps(bm))
def test_sia_big_network(big_subsys_all, flushcache, restore_fs_cache): flushcache() sia = compute.sia(big_subsys_all) check_sia(sia, big_answer)
def test_sia_complete_graph_s_noised(s_noised_complete): sia = compute.sia(s_noised_complete) check_sia(sia, noised_answer)
def test_unpickle(s): bm = compute.sia(s) pickle.loads(pickle.dumps(bm))
def test_sia_complete_graph_big_subsys_all(big_subsys_all_complete): sia = compute.sia(big_subsys_all_complete) check_sia(sia, big_answer)