Commit d7179b1f932a5976a5097e85d9f225fd49ba369f

Authored by Brice Colombier
1 parent 937762d7f0
Exists in master

Exclude signals which contain a certain string

Showing 6 changed files with 112 additions and 69 deletions

1 1 *.pyc
2   -parsers/
  2 +*.txt
  3 +*.gml
3 4 test.py
  5 +CC.py
  6 +parsers/
4 7 benchmarks/
heuristic_approximated_current_flow_betweenness.py View file @ d7179b1
  1 +# -*- coding: utf-8 -*-
1 2 import collections as col
2 3 import numpy as np
3 4 import networkx as nx
4 5  
5   -def heuristic_approximated_current_flow_betweenness(g_nx, key_size):
  6 +def heuristic_approximated_current_flow_betweenness(g_nx, key_size, exclude = " "):
6 7 try:
7   - print("Trying option 1 (fastest)")
  8 + # print("Trying option 1 (fastest)")
8 9 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
9 10 normalized = False,
10 11 solver = "full",
11 12 dtype = np.float32,
12   - epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  13 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
13 14 except:
14 15 try:
15   - print("Trying option 1 (fastest)")
  16 + # print("Trying option 1 (fastest)")
16 17 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
17 18 normalized = False,
18 19 solver = "full",
19 20 dtype = np.float32,
20   - epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  21 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
21 22 except:
22 23 try:
23   - print("Trying option 1 (fastest)")
  24 + # print("Trying option 1 (fastest)")
24 25 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
25 26 normalized = False,
26 27 solver = "full",
27 28 dtype = np.float32,
28   - epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  29 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
29 30 except:
30 31 try:
31   - print("Trying option 2 (average)")
  32 + # print("Trying option 2 (average)")
32 33 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
33 34 normalized = False,
34 35 solver = "lu",
35 36 dtype = np.float32,
36   - epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  37 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
37 38 except:
38 39 try:
39   - print("Trying option 2 (average)")
  40 + # print("Trying option 2 (average)")
40 41 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
41 42 normalized = False,
42 43 solver = "lu",
43 44 dtype = np.float32,
44   - epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  45 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
45 46 except:
46 47 try:
47   - print("Trying option 2 (average)")
  48 + # print("Trying option 2 (average)")
48 49 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
49 50 normalized = False,
50 51 solver = "lu",
51 52 dtype = np.float32,
52   - epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  53 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
53 54 except:
54 55 try:
55   - print("Trying option 3 (slowest)")
  56 + # print("Trying option 3 (slowest)")
56 57 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
57 58 normalized = False,
58 59 solver = "cg",
59 60 dtype = np.float32,
60   - epsilon = 1.0).items(), key=lambda t: t[1])))[-key_size:]]
  61 + epsilon = 1.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
61 62 except:
62 63 try:
63   - print("Trying option 3 (slowest)")
  64 + # print("Trying option 3 (slowest)")
64 65 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
65 66 normalized = False,
66 67 solver = "cg",
67 68 dtype = np.float32,
68   - epsilon = 5.0).items(), key=lambda t: t[1])))[-key_size:]]
  69 + epsilon = 5.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
69 70 except:
70 71 try:
71   - print("Trying option 3 (slowest)")
  72 + # print("Trying option 3 (slowest)")
72 73 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.approximate_current_flow_betweenness_centrality(g_nx,
73 74 normalized = False,
74 75 solver = "cg",
75 76 dtype = np.float32,
76   - epsilon = 10.0).items(), key=lambda t: t[1])))[-key_size:]]
  77 + epsilon = 10.0).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
77 78 except:
78 79 print("Failed !")
79 80 return
heuristic_current_flow_betweenness.py View file @ d7179b1
... ... @@ -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):
  5 +def heuristic_current_flow_betweenness(g_nx, key_size, exclude = " "):
6 6  
7 7 try:
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])))[-key_size:]]
  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:]
10 10 except:
11 11 try:
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])))[-key_size:]]
  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:]
14 14 except:
15 15 print("Failed !")
16 16 return
heuristic_current_flow_closeness.py View file @ d7179b1
  1 +# -*- coding: utf-8 -*-
  2 +
1 3 import collections as col
2 4 import networkx as nx
3 5 import numpy as np
4 6  
5   -def heuristic_current_flow_closeness(g_nx, key_size):
  7 +def heuristic_current_flow_closeness(g_nx, key_size, exclude = " "):
6 8 try:
7   - print("Trying option 1 (fastest)")
  9 + # print("Trying option 1 (fastest)")
8 10 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
9 11 solver = "full",
10   - dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
11   - except:
  12 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  13 + except Exception as e:
  14 + print(e)
12 15 try:
13   - print("Trying option 2 (average)")
  16 + # print("Trying option 2 (average)")
14 17 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
15 18 solver = "lu",
16   - dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
17   - except:
  19 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  20 + except Exception as e:
  21 + print(e)
18 22 try:
19   - print("Trying option 3 (slowest)")
  23 + # print("Trying option 3 (slowest)")
20 24 list_nodes_to_mask = [x for x in list(col.OrderedDict(sorted(nx.current_flow_closeness_centrality(g_nx,
21 25 solver = "cg",
22   - dtype = np.float32).items(), key=lambda t: t[1])))[-key_size:]]
23   - except:
  26 + dtype = np.float32).items(), key=lambda t: t[1]))) if exclude not in x][-key_size:]
  27 + except Exception as e:
  28 + print(e)
24 29 print("Failed !")
25 30 return
26 31 return list_nodes_to_mask
isolate_largest_cluster.py View file @ d7179b1
  1 +# -*- coding: utf-8 -*-
  2 +
1 3 import igraph
2 4 import networkx as nx
3 5 import os
4 6  
... ... @@ -9,9 +11,8 @@
9 11 g_nx = nx.read_gml("saved_graph.gml").to_undirected()
10 12 os.remove("saved_graph.gml")
11 13 else :
12   - g_nx = nx.Graph(g.get_edgelist())
13 14 g.simplify().write_gml("saved_graph.gml")
14   - g_nx = nx.read_gml("saved_graph.gml").to_undirected()
15   - os.remove("saved_graph.gml")
  15 + g_nx = nx.read_gml("saved_graph.gml", label = "label").to_undirected()
  16 + # os.remove("saved_graph.gml")
16 17 return g_nx
... ... @@ -27,29 +27,62 @@
27 27  
28 28 import numpy as np
29 29  
30   -def masking(g, name, prim_in, prim_out, nodes, overhead, heuristic, mod=True):
  30 +from parsers.build_bench import build as build_bench
  31 +
  32 +def masking(g, prim_in, prim_out, nodes, overhead, heuristic, exclude, mod=True):
  33 +
  34 + """
  35 + g: input graph
  36 + prim_in: primary inputs of the netlist
  37 + prim_out: primary outputs of the netlist
  38 + nodes: nodes of the netlist
  39 + overhead: requested overhead
  40 + heuristic: selection heuristic to use
  41 + exclude:
  42 + mod=True: modify the graph or just return the list of nodes to modify
  43 + """
  44 +
31 45  
32 46 h = g.copy() #Perform deep copy
33 47 key_size = int(len(nodes)*overhead)
34 48  
35   - if heuristic == "random":
36   - list_nodes_to_mask = random.sample(nodes, key_size)
37   - elif heuristic == "current_flow_closeness":
38   - g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
39   - list_nodes_to_mask = heuristic_current_flow_closeness.heuristic_current_flow_closeness(g_nx, key_size)
40   - elif heuristic == "approximated_current_flow_betweenness":
41   - g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
42   - list_nodes_to_mask = heuristic_approximated_current_flow_betweenness.heuristic_approximated_current_flow_betweenness(g_nx, key_size)
43   - elif heuristic == "closeness":
44   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.evcent(directed = False), nodes))[-key_size:]]
45   - elif heuristic == "betweenness":
46   - list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes))[-key_size:]]
47   - elif heuristic == "current_flow_betweenness":
48   - g_nx = isolate_largest_cluster.isolate_largest_cluster(g)
49   - list_nodes_to_mask = heuristic_current_flow_betweenness.heuristic_current_flow_betweenness(g_nx, key_size)
  49 + if exclude:
  50 + if heuristic == "random":
  51 + list_nodes_to_mask = random.sample([node for node in nodes if exclude not in node], key_size)
  52 + elif heuristic == "current_flow_closeness":
  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)
  55 + elif heuristic == "approximated_current_flow_betweenness":
  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)
  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:]
  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:]
  62 + elif heuristic == "current_flow_betweenness":
  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)
  65 + else:
  66 + raise ValueError("Unknown heuristic")
50 67 else:
51   - raise ValueError("Unknown heuristic")
52   -
  68 + if heuristic == "random":
  69 + list_nodes_to_mask = random.sample(nodes, key_size)
  70 + elif heuristic == "current_flow_closeness":
  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)
  73 + elif heuristic == "approximated_current_flow_betweenness":
  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)
  76 + elif heuristic == "closeness":
  77 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.closeness(mode = "ALL"), nodes))[-key_size:]]
  78 + elif heuristic == "betweenness":
  79 + list_nodes_to_mask = [i[1] for i in sorted(zip(g.betweenness(directed = False), nodes))[-key_size:]]
  80 + elif heuristic == "current_flow_betweenness":
  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)
  83 + else:
  84 + raise ValueError("Unknown heuristic")
  85 +
53 86 if mod:
54 87 masking_key = []
55 88 for i in list_nodes_to_mask:
56 89  
... ... @@ -60,16 +93,16 @@
60 93 return list_nodes_to_mask
61 94  
62 95 if __name__ == "__main__":
63   - for name in ["c3540"]:
64   - overhead = 0.01
65   - for heuristic in ["random",
66   - "current_flow_closeness",
67   - "approximated_current_flow_betweenness",
68   - "closeness",
69   - "betweenness",
70   - "current_flow_betweenness"]:
71   - g, prim_in, prim_out, nodes = build_bench.build(name)
  96 + for name in ["./benchmarks/c432.txt"]:
  97 + overhead = 0.1
  98 + for heuristic in ["current_flow_closeness"]:
  99 + # ,
  100 + # "approximated_current_flow_betweenness",
  101 + # "closeness",
  102 + # "betweenness",
  103 + # "current_flow_betweenness"]:
  104 + g, prim_in, prim_out, nodes = build_bench(name)
72 105 print heuristic
73   - _, b = masking(g, name, prim_in, prim_out, nodes, overhead, heuristic)
74   - print b
  106 + nodes_to_mask = masking(g, prim_in, prim_out, nodes, overhead, heuristic, exclude="", mod=False)
  107 + print nodes_to_mask