def testCohesiveBlocks2(self): # Taken from the Moody-White paper g = Graph.Formula("1-2:3:4:5:6, 2-3:4:5:7, 3-4:6:7, 4-5:6:7, " "5-6:7:21, 6-7, 7-8:11:14:19, 8-9:11:14, 9-10, " "10-12:13, 11-12:14, 12-16, 13-16, 14-15, 15-16, " "17-18:19:20, 18-20:21, 19-20:22:23, 20-21, " "21-22:23, 22-23") cbs = g.cohesive_blocks() self.genericTests(cbs) expected_blocks = [ list(range(7)), list(range(23)), list(range(7)) + list(range(16, 23)), list(range(6, 16)), [6, 7, 10, 13], ] observed_blocks = sorted( sorted(int(x) - 1 for x in g.vs[bl]["name"]) for bl in cbs) self.assertEqual(expected_blocks, observed_blocks) self.assertTrue(cbs.cohesions() == [1, 2, 2, 5, 3]) self.assertTrue(cbs.parents() == [None, 0, 0, 1, 2]) self.assertTrue( sorted(cbs.hierarchy().get_edgelist()) == [(0, 1), (0, 2), (1, 3), (2, 4)])
def characterize_with_patterns(graph, k): create_list_neighbors(graph) global PT global PS size_pt_per_k = {2:1, 3:3, 4:9, 5:30} size_ps_per_k = {2:1, 3:4, 4:15, 5:73} PT = size_pt_per_k[k]*[0] PS = [] for v in graph.vs: PS.append(size_ps_per_k[k]*[0]) v['id_sub'] = -1 for v in graph.vs: graph_sub = Graph.Formula() v['id_sub'] = 0 if not 'name' in v.attributes(): v['name'] = str(v.index) graph_sub.add_vertex(name = v['name'], **{'id_principal' : v.index, 'evol_class' : 1, 'pattern_sub' : 0, 'neighbors_evol_classes' : []}) vext = [] for nei in v['list_neighbors']: if nei > v.index: vext.append(graph.vs[nei]) if len(vext) > 0: extend_subgraph(graph, k, graph_sub, v, vext) v['id_sub'] = -1 return (PT, PS)
def characterize_with_patterns(self): self.create_list_neighbors() self.patterns_tab = 30 * [0] for v in self._graph.vs: self.positions_tab.append(73 * [0]) v['id_sub'] = -1 for v in self._graph.vs: graph_sub = Graph.Formula() v['id_sub'] = 0 if not 'name' in v.attributes(): v['name'] = str(v.index) graph_sub.add_vertex(name=v['name'], **{ 'id_principal': v.index, 'evol_class': 1, 'pattern_sub': 0 }) vext = [] for nei in v['list_neighbors']: if nei > v.index: vext.append(self._graph.vs[nei]) if len(vext) > 0: self.extend_subgraph(graph_sub, v, vext) v['id_sub'] = -1 return (self.patterns_tab, self.positions_tab)
def test_example(): # create a graph consisting of three nodes connected in sequence # every edge will be upregulating graph = Graph.Formula('A->B->C') graph.es['type'] = ['up'] * 2 # the most detrimental node should be B node = most_detrimental(graph, 'A', 'C') assert node == 'B'
def import_graph(folder, ego): home = os.path.expanduser('~') if not os.path.isfile('%s/GALLERY/%s/%s/Graphs/friends.gml' % (home, folder, ego)): return Graph.Formula('') graph = Graph.Read_GML('%s/GALLERY/%s/%s/Graphs/friends.gml' % (home, folder, ego)) graph['folder'] = folder graph['ego'] = ego return graph
def test_example_other_types(): for edge_types in (('up', 'down'), (0, 1), ('activates', 'inactivates'), (100, -200)): # create a graph consisting of three nodes connected in sequence # every edge will be upregulating graph = Graph.Formula('A->B->C') graph.es['type'] = edge_types # the most detrimental node should be B node = most_detrimental(graph, 'A', 'C') assert node == 'B', 'Failed with edge types {} and {}'.format( *edge_types)
from igraph import Graph from signatures.networks import get_st_bdd_from_cutsets, get_component_types_dict from signatures.sig_funs import compute_signatures, print_survival_sig_table if __name__ == "__main__": network = Graph.Formula("s -- A:B, A -- C:D, B -- C:E, C -- D:E, D -- t, E -- t") for v in network.vs: if v["name"] in ["A", "E"]: v["compType"] = 1 elif v["name"] in ["B", "C", "D"]: v["compType"] = 2 bdd, root = get_st_bdd_from_cutsets(network) component_types = get_component_types_dict(network) sig_dim_types, survival_sig, system_sig = compute_signatures(bdd, root, component_types) print_survival_sig_table(survival_sig, sig_dim_types)
def testFormula(self): tests = [ (None, [], []), ("", [""], []), ("A", ["A"], []), ("A-B", ["A", "B"], [(0, 1)]), ("A --- B", ["A", "B"], [(0, 1)]), ( "A--B, C--D, E--F, G--H, I, J, K", ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"], [(0, 1), (2, 3), (4, 5), (6, 7)], ), ( "A:B:C:D -- A:B:C:D", ["A", "B", "C", "D"], [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)], ), ("A -> B -> C", ["A", "B", "C"], [(0, 1), (1, 2)]), ("A <- B -> C", ["A", "B", "C"], [(1, 0), (1, 2)]), ("A <- B -- C", ["A", "B", "C"], [(1, 0)]), ( "A <-> B <---> C <> D", ["A", "B", "C", "D"], [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2)], ), ( "'this is' <- 'a silly' -> 'graph here'", ["this is", "a silly", "graph here"], [(1, 0), (1, 2)], ), ( "Alice-Bob-Cecil-Alice, Daniel-Cecil-Eugene, Cecil-Gordon", ["Alice", "Bob", "Cecil", "Daniel", "Eugene", "Gordon"], [(0, 1), (1, 2), (0, 2), (2, 3), (2, 4), (2, 5)], ), ( "Alice-Bob:Cecil:Daniel, Cecil:Daniel-Eugene:Gordon", ["Alice", "Bob", "Cecil", "Daniel", "Eugene", "Gordon"], [(0, 1), (0, 2), (0, 3), (2, 4), (2, 5), (3, 4), (3, 5)], ), ( "Alice <-> Bob --> Cecil <-- Daniel, Eugene --> Gordon:Helen", [ "Alice", "Bob", "Cecil", "Daniel", "Eugene", "Gordon", "Helen" ], [(0, 1), (1, 0), (1, 2), (3, 2), (4, 5), (4, 6)], ), ( "Alice -- Bob -- Daniel, Cecil:Gordon, Helen", ["Alice", "Bob", "Daniel", "Cecil", "Gordon", "Helen"], [(0, 1), (1, 2)], ), ( '"+" -- "-", "*" -- "/", "%%" -- "%/%"', ["+", "-", "*", "/", "%%", "%/%"], [(0, 1), (2, 3), (4, 5)], ), ("A-B-C\nC-D", ["A", "B", "C", "D"], [(0, 1), (1, 2), (2, 3)]), ("A-B-C\n C-D", ["A", "B", "C", "D"], [(0, 1), (1, 2), (2, 3)]), ] for formula, names, edges in tests: g = Graph.Formula(formula) self.assertEqual(g.vs["name"], names) self.assertEqual(g.get_edgelist(), sorted(edges))
def test_survival_signature_values_correct_for_networks(self): """ Tests that survival signature is calculated correctly for various network problems. """ # Simple series network with single component type. network = Graph.Formula("s -- 1 -- 2 -- t") for v in network.vs: v["compType"] = 1 component_types = get_component_types_dict(network) bdd, root = get_st_bdd_from_cutsets(network) sig_dim_types, survival_sig, system_sig = compute_signatures( bdd, root, component_types) expected_survival_sig = np.array((0, 0, 1)) expected_system_sig = np.array((1, 0)) self.assertTrue(np.array_equal(survival_sig, expected_survival_sig)) self.assertTrue(np.array_equal(system_sig, expected_system_sig)) '''Network from Figure 1 of "Generalizing the signature to systems with multiple types of components" by F. Coolen and Coolen-Maturi, 2012''' network = Graph.Formula( "s -- 1, 1 -- 2:3, 2 -- 4:5, 3 -- 4:6, 4 -- 5:6, 5 -- t, 6 -- t") for v in (v for v in network.vs if v["name"] in ["1", "2", "5"]): v["compType"] = 1 for v in (v for v in network.vs if v["name"] in ["3", "4", "6"]): v["compType"] = 2 bdd, root = get_st_bdd_from_cutsets(network) component_types = get_component_types_dict(network) sig_dim_types, survival_sig, system_sig = compute_signatures( bdd, root, component_types) expected_sig = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.1111, 0.3333], [0.0, 0.0, 0.4444, 0.6667], [1.0, 1.0, 1.0, 1.0]]) self.assertTrue( np.array_equal(np.around(survival_sig, decimals=4), expected_sig)) '''Networks from Figures 2 and Figure 4 of the paper "Bayesian inference for reliability of systems and networks using the survival signature" by L. Aslett et al, 2015''' # Reliability network from Fig. 2 and survival signature result from Table 1. network = Graph.Formula( "s -- 5:6:7, 5 -- 1, 6 -- 2, 7 -- 3, 1 -- 8, 2 --9, 3 -- 10, 8 -- 4, 9 -- 4," "10 -- 4, 4 -- 11, 11 -- t") for v in (v for v in network.vs if v["name"] in ["1", "2", "3", "4"]): v["compType"] = 1 for v in (v for v in network.vs if v["name"] in ["5", "6", "7", "8", "9", "10", "11"]): v["compType"] = 2 bdd, root = get_st_bdd_from_cutsets(network) component_types = get_component_types_dict(network) sig_dim_types, survival_sig, system_sig = compute_signatures( bdd, root, component_types) expected_sig = np.zeros((5, 8)) expected_sig[2, 3] = 0.014 expected_sig[2, 4] = 0.057 expected_sig[2, 5] = 0.143 expected_sig[2, 6] = 0.286 expected_sig[2, 7] = 0.500 expected_sig[3, 3] = 0.043 expected_sig[3, 4] = 0.171 expected_sig[3, 5] = 0.393 expected_sig[3, 6] = 0.643 expected_sig[3, 7] = 0.750 expected_sig[4, 3] = 0.086 expected_sig[4, 4] = 0.343 expected_sig[4, 5] = 0.714 expected_sig[4, 6] = 0.857 expected_sig[4, 7] = 1.000 self.assertTrue( np.array_equal(np.around(survival_sig, decimals=3), expected_sig)) # Reliability network from Fig. 4 and result from Appendix A. network = Graph.Formula( "s -- 1:7:9, 1 -- 2:4:5, 9 -- 10, 7 -- 8:10, 2 -- 3, 4 -- 6, 5 -- 6," "10 -- 8:11, 8 -- t, 3 -- t, 6 -- t, 11 -- t") for v in (v for v in network.vs if v["name"] in ["1", "6", "11"]): v["compType"] = 1 for v in (v for v in network.vs if v["name"] in ["2", "3", "9"]): v["compType"] = 2 for v in (v for v in network.vs if v["name"] in ["4", "5", "10"]): v["compType"] = 3 for v in (v for v in network.vs if v["name"] in ["7", "8"]): v["compType"] = 4 bdd, root = get_st_bdd_from_cutsets(network) component_types = get_component_types_dict(network) sig_dim_types, survival_sig, system_sig = compute_signatures( bdd, root, component_types) # Test a set of selected values from the signature. self.assertAlmostEqual(survival_sig[0, 0, 0, 0], 0) self.assertAlmostEqual(survival_sig[0, 1, 3, 1], 1.0 / 6) self.assertAlmostEqual(survival_sig[1, 1, 2, 0], 2.0 / 27) self.assertAlmostEqual(survival_sig[2, 1, 1, 1], 7.0 / 18) self.assertAlmostEqual(survival_sig[2, 3, 1, 1], 7.0 / 9) self.assertAlmostEqual(survival_sig[3, 2, 0, 1], 1.0 / 3) self.assertAlmostEqual(survival_sig[3, 2, 1, 0], 1.0)