Commit fba367e07e7baca672eac64bc5e1cda74d8341d1

Authored by Brice Colombier
1 parent d7179b1f93
Exists in master

Avoid selecting inputs as nodes to mask

Showing 4 changed files with 29 additions and 29 deletions

heuristic_approximated_current_flow_betweenness.py View file @ fba367e
... ... @@ -3,14 +3,14 @@
3 3 import numpy as np
4 4 import networkx as nx
5 5  
6   -def heuristic_approximated_current_flow_betweenness(g_nx, key_size, exclude = " "):
  6 +def heuristic_approximated_current_flow_betweenness(g_nx, key_size, prim_in, exclude = " "):
7 7 try:
8 8 # print("Trying option 1 (fastest)")
9 9 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
10 10 normalized = False,
11 11 solver = "full",
12 12 dtype = np.float32,
13   - epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  13 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
14 14 except:
15 15 try:
16 16 # print("Trying option 1 (fastest)")
... ... @@ -18,7 +18,7 @@
18 18 normalized = False,
19 19 solver = "full",
20 20 dtype = np.float32,
21   - epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  21 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
22 22 except:
23 23 try:
24 24 # print("Trying option 1 (fastest)")
... ... @@ -26,7 +26,7 @@
26 26 normalized = False,
27 27 solver = "full",
28 28 dtype = np.float32,
29   - epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  29 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
30 30 except:
31 31 try:
32 32 # print("Trying option 2 (average)")
... ... @@ -34,7 +34,7 @@
34 34 normalized = False,
35 35 solver = "lu",
36 36 dtype = np.float32,
37   - epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  37 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
38 38 except:
39 39 try:
40 40 # print("Trying option 2 (average)")
... ... @@ -42,7 +42,7 @@
42 42 normalized = False,
43 43 solver = "lu",
44 44 dtype = np.float32,
45   - epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  45 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
46 46 except:
47 47 try:
48 48 # print("Trying option 2 (average)")
... ... @@ -50,7 +50,7 @@
50 50 normalized = False,
51 51 solver = "lu",
52 52 dtype = np.float32,
53   - epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  53 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
54 54 except:
55 55 try:
56 56 # print("Trying option 3 (slowest)")
... ... @@ -58,7 +58,7 @@
58 58 normalized = False,
59 59 solver = "cg",
60 60 dtype = np.float32,
61   - epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  61 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
62 62 except:
63 63 try:
64 64 # print("Trying option 3 (slowest)")
... ... @@ -66,7 +66,7 @@
66 66 normalized = False,
67 67 solver = "cg",
68 68 dtype = np.float32,
69   - epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  69 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
70 70 except:
71 71 try:
72 72 # print("Trying option 3 (slowest)")
... ... @@ -74,7 +74,7 @@
74 74 normalized = False,
75 75 solver = "cg",
76 76 dtype = np.float32,
77   - epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  77 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
78 78 except:
79 79 print("Failed !")
80 80 return
heuristic_current_flow_betweenness.py View file @ fba367e
... ... @@ -2,15 +2,15 @@
2 2 import numpy as np
3 3 import networkx as nx
4 4  
5   -def heuristic_current_flow_betweenness(g_nx, key_size, exclude = " "):
  5 +def heuristic_current_flow_betweenness(g_nx, key_size, prim_in, exclude = " "):
6 6  
7 7 try:
8 8 # print("Trying option 1 (fastest)")
9   - list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "full").items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  9 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "full").items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
10 10 except:
11 11 try:
12 12 # print("Trying option 2 (average)")
13   - list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "cg").items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  13 + list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_betweenness_centrality(g_nx, normalized = False, solver = "cg").items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
14 14 except:
15 15 print("Failed !")
16 16 return
heuristic_current_flow_closeness.py View file @ fba367e
... ... @@ -4,26 +4,26 @@
4 4 import networkx as nx
5 5 import numpy as np
6 6  
7   -def heuristic_current_flow_closeness(g_nx, key_size, exclude = " "):
  7 +def heuristic_current_flow_closeness(g_nx, key_size, prim_in, exclude = " "):
8 8 try:
9 9 # print("Trying option 1 (fastest)")
10 10 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
11 11 solver = "full",
12   - dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  12 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
13 13 except Exception as e:
14 14 print(e)
15 15 try:
16 16 # print("Trying option 2 (average)")
17 17 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
18 18 solver = "lu",
19   - dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  19 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
20 20 except Exception as e:
21 21 print(e)
22 22 try:
23 23 # print("Trying option 3 (slowest)")
24 24 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
25 25 solver = "cg",
26   - dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  26 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x and x not in prim_in][-key_size:]
27 27 except Exception as e:
28 28 print(e)
29 29 print("Failed !")
... ... @@ -48,38 +48,38 @@
48 48  
49 49 if exclude:
50 50 if heuristic == "random":
51   - list_nodes_to_mask = random.sample([node for node in nodes if exclude not in node], key_size)
  51 + list_nodes_to_mask = random.sample([node for node in nodes if exclude not in node and node not in prim_in], key_size)
52 52 elif heuristic == "current_flow_closeness":
53 53 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
54   - list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size, exclude)
  54 + list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size, prim_in, exclude)
55 55 elif heuristic == "approximated_current_flow_betweenness":
56 56 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
57   - list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size, exclude)
  57 + list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size, prim_in, exclude)
58 58 elif heuristic == "closeness":
59   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.closeness(mode = "ALL"), nodes))if exclude not in i[1]][-key_size:]
  59 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.closeness(mode = "ALL"), nodes))if exclude not in i[1] and i[1] not in prim_in][-key_size:]
60 60 elif heuristic == "betweenness":
61   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes)) if exclude not in i[1]][-key_size:]
  61 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes)) if exclude not in i[1] and i[1] not in prim_in][-key_size:]
62 62 elif heuristic == "current_flow_betweenness":
63 63 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
64   - list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size, exclude)
  64 + list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size, prim_in, exclude)
65 65 else:
66 66 raise ValueError("Unknown heuristic")
67 67 else:
68 68 if heuristic == "random":
69   - list_nodes_to_mask = random.sample(nodes, key_size)
  69 + list_nodes_to_mask = random.sample([node for node in nodes if node not in prim_in], key_size)
70 70 elif heuristic == "current_flow_closeness":
71 71 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
72   - list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size)
  72 + list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size, prim_in)
73 73 elif heuristic == "approximated_current_flow_betweenness":
74 74 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
75   - list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size)
  75 + list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size, prim_in)
76 76 elif heuristic == "closeness":
77   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.closeness(mode = "ALL"), nodes))[-key_size:]]
  77 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.closeness(mode = "ALL"), [node for node in nodes if node not in prim_in]))[-key_size:]]
78 78 elif heuristic == "betweenness":
79   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes))[-key_size:]]
  79 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), [node for node in nodes if node not in prim_in]))[-key_size:]]
80 80 elif heuristic == "current_flow_betweenness":
81 81 g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
82   - list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size)
  82 + list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size, prim_in)
83 83 else:
84 84 raise ValueError("Unknown heuristic")
85 85