def testOneExtraEdge(self): G1 = nx.Graph() G1.add_node("A", label="A") G1.add_node("B", label="B") G1.add_node("C", label="C") G1.add_node("C", label="C") G1.add_edge("A", "B", label="a-b") G2 = nx.Graph() G2.add_node("A", label="A") G2.add_node("B", label="B") G2.add_node("C", label="C") G2.add_edge("A", "B", label="a-b") G2.add_edge("A", "C", label="a-c") assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 1
def testOneExtraNodeAndEdge(self): G1 = nx.Graph() G1.add_node('A', label='A') G1.add_node('B', label='B') G1.add_edge('A', 'B', label='a-b') G2 = nx.Graph() G2.add_node('A', label='A') G2.add_node('B', label='B') G2.add_node('C', label='C') G2.add_edge('A', 'B', label='a-b') G2.add_edge('A', 'C', label='a-c') assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 2
def testGraph2(self): G1 = getCanonical() G2 = nx.Graph() G2.add_node("A", label="A") G2.add_node("B", label="B") G2.add_node("C", label="C") G2.add_node("D", label="D") G2.add_node("E", label="E") G2.add_edge("A", "B", label="a-b") G2.add_edge("B", "C", label="b-c") G2.add_edge("C", "D", label="c-d") G2.add_edge("C", "E", label="c-e") assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 4
def testGraph2(self): G1 = getCanonical() G2 = nx.Graph() G2.add_node('A', label='A') G2.add_node('B', label='B') G2.add_node('C', label='C') G2.add_node('D', label='D') G2.add_node('E', label='E') G2.add_edge('A', 'B', label='a-b') G2.add_edge('B', 'C', label='b-c') G2.add_edge('C', 'D', label='c-d') G2.add_edge('C', 'E', label='c-e') assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 4
def main(old_graph_path, new_graph_path): old_graph = create_graph(old_graph_path) new_graph = create_graph(new_graph_path) print(list(old_graph.nodes)) print(list(old_graph.edges)) print(old_graph.number_of_edges()) print(list(new_graph.nodes)) print(list(new_graph.edges)) print(new_graph.number_of_edges()) #gen = optimize_graph_edit_distance( # old_graph, # new_graph, # node_match=node_match, # edge_match=edge_match, # # node_subst_cost=node_subst_cost, # node_del_cost=node_del_cost, # node_ins_cost=node_ins_cost, # # edge_subst_cost=edge_subst_cost, # edge_del_cost=edge_del_cost, # edge_ins_cost=edge_ins_cost, # upper_bound=50 #) #ged = None #try: # for _ in range(5): # ged = next(gen) #except StopIteration: # pass # #print(ged) print( graph_edit_distance( old_graph, new_graph, node_match=node_match, edge_match=edge_match, # node_subst_cost=node_subst_cost, node_del_cost=node_del_cost, node_ins_cost=node_ins_cost, # edge_subst_cost=edge_subst_cost, edge_del_cost=edge_del_cost, edge_ins_cost=edge_ins_cost, upper_bound=100))
def testGraph3(self): G1 = getCanonical() G2 = nx.Graph() G2.add_node('A', label='A') G2.add_node('B', label='B') G2.add_node('C', label='C') G2.add_node('D', label='D') G2.add_node('E', label='E') G2.add_node('F', label='F') G2.add_node('G', label='G') G2.add_edge('A', 'C', label='a-c') G2.add_edge('A', 'D', label='a-d') G2.add_edge('D', 'E', label='d-e') G2.add_edge('D', 'F', label='d-f') G2.add_edge('D', 'G', label='d-g') G2.add_edge('E', 'B', label='e-b') assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 12
def testGraph3(self): G1 = getCanonical() G2 = nx.Graph() G2.add_node("A", label="A") G2.add_node("B", label="B") G2.add_node("C", label="C") G2.add_node("D", label="D") G2.add_node("E", label="E") G2.add_node("F", label="F") G2.add_node("G", label="G") G2.add_edge("A", "C", label="a-c") G2.add_edge("A", "D", label="a-d") G2.add_edge("D", "E", label="d-e") G2.add_edge("D", "F", label="d-f") G2.add_edge("D", "G", label="d-g") G2.add_edge("E", "B", label="e-b") assert graph_edit_distance(G1, G2, node_match=nmatch, edge_match=ematch) == 12
def get_topological_distance(self, other): """:returns either NEAT distance of graph edit distance from self to other (depending on Config option)""" if other == self: return 0 num_disjoint = 0 num_excess = 0 self_max_conn_id = max(self._connections.keys()) other_max_conn_id = max(other._connections.keys()) smaller_id = min(self_max_conn_id, other_max_conn_id) for conn_id in self.get_disjoint_excess_genes(other): if Config.ignore_disabled_connections_for_topological_similarity: if conn_id in self._connections: if not self._connections[conn_id].enabled(): continue else: if not other._connections[conn_id].enabled(): continue if conn_id > smaller_id: num_excess += 1 else: num_disjoint += 1 neat_dist = (num_excess * Props.EXCESS_COEFFICIENT + num_disjoint * Props.DISJOINT_COEFFICIENT) / max( len(self._connections), len(other._connections)) if Config.use_graph_edit_distance: match_func = lambda a, b: a['label'] == b['label'] ged = graph_edit_distance(self.get_netx_graph_form(), other.get_netx_graph_form(), node_match=match_func, edge_match=match_func) if neat_dist > 0: pass return ged return neat_dist
def test_graph_edit_distance_node_cost(self): G1 = path_graph(6) G2 = path_graph(6) for n, attr in G1.nodes.items(): attr["color"] = "red" if n % 2 == 0 else "blue" for n, attr in G2.nodes.items(): attr["color"] = "red" if n % 2 == 1 else "blue" def node_subst_cost(uattr, vattr): if uattr["color"] == vattr["color"]: return 1 else: return 10 def node_del_cost(attr): if attr["color"] == "blue": return 20 else: return 50 def node_ins_cost(attr): if attr["color"] == "blue": return 40 else: return 100 assert ( graph_edit_distance( G1, G2, node_subst_cost=node_subst_cost, node_del_cost=node_del_cost, node_ins_cost=node_ins_cost, ) == 6 )
def test_graph_edit_distance_edge_cost(self): G1 = path_graph(6) G2 = path_graph(6) for e, attr in G1.edges.items(): attr["color"] = "red" if min(e) % 2 == 0 else "blue" for e, attr in G2.edges.items(): attr["color"] = "red" if min(e) // 3 == 0 else "blue" def edge_subst_cost(gattr, hattr): if gattr["color"] == hattr["color"]: return 0.01 else: return 0.1 def edge_del_cost(attr): if attr["color"] == "blue": return 0.2 else: return 0.5 def edge_ins_cost(attr): if attr["color"] == "blue": return 0.4 else: return 1.0 assert ( graph_edit_distance( G1, G2, edge_subst_cost=edge_subst_cost, edge_del_cost=edge_del_cost, edge_ins_cost=edge_ins_cost, ) == 0.23 )
def dist(arg): a,target = arg labelmatch = lambda a,b: a['label'] == b['label'] return int(graph_edit_distance(a,target, labelmatch, labelmatch ))
def testCopy(self): G = nx.Graph() G.add_node('A', label='A') G.add_node('B', label='B') G.add_edge('A', 'B', label='a-b') assert graph_edit_distance(G, G.copy(), node_match=nmatch, edge_match=ematch) == 0
def test_graph_edit_distance(self): G0 = nx.Graph() G1 = path_graph(6) G2 = cycle_graph(6) G3 = wheel_graph(7) assert graph_edit_distance(G0, G0) == 0 assert graph_edit_distance(G0, G1) == 11 assert graph_edit_distance(G1, G0) == 11 assert graph_edit_distance(G0, G2) == 12 assert graph_edit_distance(G2, G0) == 12 assert graph_edit_distance(G0, G3) == 19 assert graph_edit_distance(G3, G0) == 19 assert graph_edit_distance(G1, G1) == 0 assert graph_edit_distance(G1, G2) == 1 assert graph_edit_distance(G2, G1) == 1 assert graph_edit_distance(G1, G3) == 8 assert graph_edit_distance(G3, G1) == 8 assert graph_edit_distance(G2, G2) == 0 assert graph_edit_distance(G2, G3) == 7 assert graph_edit_distance(G3, G2) == 7 assert graph_edit_distance(G3, G3) == 0
def test_multigraph(self): G0 = nx.MultiGraph() G1 = nx.MultiGraph() G1.add_edges_from((('A', 'B'), ('B', 'C'), ('A', 'C'))) G2 = nx.MultiGraph() G2.add_edges_from((('A', 'B'), ('B', 'C'), ('B', 'C'), ('A', 'C'))) G3 = nx.MultiGraph() G3.add_edges_from( (('A', 'B'), ('B', 'C'), ('A', 'C'), ('A', 'C'), ('A', 'C'))) assert graph_edit_distance(G0, G0) == 0 assert graph_edit_distance(G0, G1) == 6 assert graph_edit_distance(G1, G0) == 6 assert graph_edit_distance(G0, G2) == 7 assert graph_edit_distance(G2, G0) == 7 assert graph_edit_distance(G0, G3) == 8 assert graph_edit_distance(G3, G0) == 8 assert graph_edit_distance(G1, G1) == 0 assert graph_edit_distance(G1, G2) == 1 assert graph_edit_distance(G2, G1) == 1 assert graph_edit_distance(G1, G3) == 2 assert graph_edit_distance(G3, G1) == 2 assert graph_edit_distance(G2, G2) == 0 assert graph_edit_distance(G2, G3) == 1 assert graph_edit_distance(G3, G2) == 1 assert graph_edit_distance(G3, G3) == 0
def test_digraph(self): G0 = nx.DiGraph() G1 = nx.DiGraph() G1.add_edges_from((('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A'))) G2 = nx.DiGraph() G2.add_edges_from((('A', 'B'), ('B', 'C'), ('C', 'D'), ('A', 'D'))) G3 = nx.DiGraph() G3.add_edges_from((('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'D'))) assert graph_edit_distance(G0, G0) == 0 assert graph_edit_distance(G0, G1) == 8 assert graph_edit_distance(G1, G0) == 8 assert graph_edit_distance(G0, G2) == 8 assert graph_edit_distance(G2, G0) == 8 assert graph_edit_distance(G0, G3) == 8 assert graph_edit_distance(G3, G0) == 8 assert graph_edit_distance(G1, G1) == 0 assert graph_edit_distance(G1, G2) == 2 assert graph_edit_distance(G2, G1) == 2 assert graph_edit_distance(G1, G3) == 4 assert graph_edit_distance(G3, G1) == 4 assert graph_edit_distance(G2, G2) == 0 assert graph_edit_distance(G2, G3) == 2 assert graph_edit_distance(G3, G2) == 2 assert graph_edit_distance(G3, G3) == 0
def test_selfloops(self): G0 = nx.Graph() G1 = nx.Graph() G1.add_edges_from((('A', 'A'), ('A', 'B'))) G2 = nx.Graph() G2.add_edges_from((('A', 'B'), ('B', 'B'))) G3 = nx.Graph() G3.add_edges_from((('A', 'A'), ('A', 'B'), ('B', 'B'))) assert graph_edit_distance(G0, G0) == 0 assert graph_edit_distance(G0, G1) == 4 assert graph_edit_distance(G1, G0) == 4 assert graph_edit_distance(G0, G2) == 4 assert graph_edit_distance(G2, G0) == 4 assert graph_edit_distance(G0, G3) == 5 assert graph_edit_distance(G3, G0) == 5 assert graph_edit_distance(G1, G1) == 0 assert graph_edit_distance(G1, G2) == 0 assert graph_edit_distance(G2, G1) == 0 assert graph_edit_distance(G1, G3) == 1 assert graph_edit_distance(G3, G1) == 1 assert graph_edit_distance(G2, G2) == 0 assert graph_edit_distance(G2, G3) == 1 assert graph_edit_distance(G3, G2) == 1 assert graph_edit_distance(G3, G3) == 0
def test_graph_edit_distance_upper_bound(self): G1 = circular_ladder_graph(2) G2 = circular_ladder_graph(6) assert graph_edit_distance(G1, G2, upper_bound=5) is None assert graph_edit_distance(G1, G2, upper_bound=24) == 22 assert graph_edit_distance(G1, G2) == 22
def ged(ty="Syn"): if ty == "Syn": length = 238 elif ty == "BENIGN": length = 68 else: length = 0 # length = 238 + 68 # Syn, 238 for attack, 68 for benign length = length + 1 duration = nump.zeros(length) node_degree = nump.zeros(length) client_degree = nump.zeros(length) server_degree = nump.zeros(length) timestamps = [] graph_edit_distances = nump.zeros(7) benign_count = 0 with open('../../../data/CSV-01-12/01-12/Syn.csv', newline='') as csvfile: reader = csv.reader(csvfile, delimiter=' ', quotechar='|') c = 0 # c counts for each edge i = 0 j = 0 g = 0 nodes = [] # each ip is regarded as a node clients = [] servers = [] current_edge_client = 0 current_edge_server = 0 current_edge_total = 0 current_timestamp = 0 benigns = nump.zeros(length) previous_graph = nx.MultiDiGraph() current_graph = nx.MultiDiGraph() for row in reader: elements_1 = row[0].split(",") elements_2 = row[1].split(",") type = elements_2[len(elements_2) - 1] previous_timestamp = current_timestamp if 0 <= c: if 1015 < c < 1023 and type == ty and ty == "Syn": client_ip = elements_1[2] server_ip = elements_1[4] client_port = elements_1[3] server_port = elements_1[5] current_graph.add_node(client_ip) current_graph.add_node(server_ip) current_graph.add_edge(client_ip + client_port, server_ip + server_port) print(i) graph_edit_distances[i] = graph_edit_distance( previous_graph, current_graph) previous_graph = current_graph.copy() i += 1 elif ty == "BENIGN" and type == ty and 1 < c < 2500: client_ip = elements_1[2] server_ip = elements_1[4] client_port = elements_1[3] server_port = elements_1[5] current_graph.add_node(client_ip) current_graph.add_node(server_ip) current_graph.add_edge(client_ip + client_port, server_ip + server_port) print(j) g += graph_edit_distance(previous_graph, current_graph) previous_graph = current_graph.copy() j += 1 g = g / j else: break c = c + 1 if ty == "BENIGN": graph_edit_distances = nump.repeat(g, 7) print(graph_edit_distances) print("start:") return graph_edit_distances
def test_digraph(self): G0 = nx.DiGraph() G1 = nx.DiGraph() G1.add_edges_from((("A", "B"), ("B", "C"), ("C", "D"), ("D", "A"))) G2 = nx.DiGraph() G2.add_edges_from((("A", "B"), ("B", "C"), ("C", "D"), ("A", "D"))) G3 = nx.DiGraph() G3.add_edges_from((("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"))) assert graph_edit_distance(G0, G0) == 0 assert graph_edit_distance(G0, G1) == 8 assert graph_edit_distance(G1, G0) == 8 assert graph_edit_distance(G0, G2) == 8 assert graph_edit_distance(G2, G0) == 8 assert graph_edit_distance(G0, G3) == 8 assert graph_edit_distance(G3, G0) == 8 assert graph_edit_distance(G1, G1) == 0 assert graph_edit_distance(G1, G2) == 2 assert graph_edit_distance(G2, G1) == 2 assert graph_edit_distance(G1, G3) == 4 assert graph_edit_distance(G3, G1) == 4 assert graph_edit_distance(G2, G2) == 0 assert graph_edit_distance(G2, G3) == 2 assert graph_edit_distance(G3, G2) == 2 assert graph_edit_distance(G3, G3) == 0