Mercurial > hg > drupalISMI
comparison importFromOpenMind/importer/compare_models.py @ 47:378dcb66a27f
new compare_models comparing the existence of nodes and relations in two graphs.
fixed bugs in ismixml2model.
author | casties |
---|---|
date | Mon, 06 Feb 2017 18:44:43 +0100 |
parents | |
children | 6625019a0c96 |
comparison
equal
deleted
inserted
replaced
46:f3945ef1e6a4 | 47:378dcb66a27f |
---|---|
1 import networkx as nx | |
2 import sys | |
3 import csv | |
4 | |
5 ## configure behaviour | |
6 | |
7 # metworkx graph files | |
8 input1_fn = 'ismi_graph1.gpickle' | |
9 input2_fn = 'ismi_graph2.gpickle' | |
10 | |
11 # name of type attribute | |
12 node_type_attribute = '_type' | |
13 rel_type_attribute = '_type' | |
14 | |
15 # active log levels for logging | |
16 logLevels = {'DEBUG', 'INFO', 'WARNING', 'ERROR', 'SYSMSG'} | |
17 #logLevels = {'INFO', 'WARNING', 'ERROR', 'SYSMSG'} | |
18 #logLevels = {'INFO', 'ERROR', 'SYSMSG'} | |
19 | |
20 def log(level, message): | |
21 if level in logLevels: | |
22 print("%s: %s"%(level, message)) | |
23 | |
24 | |
25 def invertRelations(nx_graph): | |
26 """Add inverse relations to each relation""" | |
27 | |
28 print("Adding inverse relations.") | |
29 # copy list of edges because we add edges in the loop | |
30 edges = nx.edges(nx_graph)[:] | |
31 # iterate list | |
32 cnt = 0 | |
33 for nx_edge in edges: | |
34 (nx_src, nx_tar) = nx_edge | |
35 # get attributes of edge | |
36 rel_attrs = nx_graph.edge[nx_src][nx_tar][0][:] | |
37 rel_type = rel_attrs[rel_type_attribute] | |
38 rel_id = rel_attrs['ismi_id'] | |
39 # create new relation | |
40 rel_attrs[rel_type_attribute] = fixName(rel_type, is_tar_rel=True) | |
41 rel_attrs['ismi_id': -rel_id] | |
42 nx_graph.add_edge(nx_tar, nx_src, attr_dict=invrel_atts) | |
43 | |
44 cnt += 1 | |
45 if cnt % 100 == 0: | |
46 print(" %s relations"%cnt) | |
47 | |
48 | |
49 def compare_nodes(nx_graph1, nx_graph2): | |
50 """compare nodes of two graphs""" | |
51 | |
52 log('INFO', "Compare graph nodes: %s vs %s"%(repr(nx_graph1), repr(nx_graph2))) | |
53 cnt = 0 | |
54 missing_nodes1 = [] | |
55 missing_nodes2 = [] | |
56 # iterate all nodes in graph 1 | |
57 for n in nx.nodes_iter(nx_graph1): | |
58 #attrs = nx_graph.node[n] | |
59 | |
60 if not nx_graph2.has_node(n): | |
61 missing_nodes2.append(n) | |
62 | |
63 if len(missing_nodes2) > 0: | |
64 log('WARNING', "%s nodes missing in graph 2"%len(missing_nodes2)) | |
65 log('DEBUG', "nodes: %s"%missing_nodes2) | |
66 #log('DEBUG', "nodes: %s"%([nx_graph1.node[n] for n in missing_nodes2])) | |
67 | |
68 # iterate all nodes in graph 2 | |
69 for n in nx.nodes_iter(nx_graph2): | |
70 #attrs = nx_graph.node[n] | |
71 | |
72 if not nx_graph1.has_node(n): | |
73 missing_nodes1.append(n) | |
74 | |
75 if len(missing_nodes1) > 0: | |
76 log('WARNING', "%s nodes missing in graph 1"%len(missing_nodes1)) | |
77 log('DEBUG', "nodes: %s"%(missing_nodes1)) | |
78 | |
79 | |
80 def compare_relations(nx_graph1, nx_graph2): | |
81 """compare relations of two graphs""" | |
82 | |
83 log('INFO', "Compare graph relations: %s vs %s"%(repr(nx_graph1), repr(nx_graph2))) | |
84 cnt = 0 | |
85 missing_rels1 = [] | |
86 missing_rels2 = [] | |
87 # iterate all edges in graph 1 | |
88 for (s, t) in nx.edges_iter(nx_graph1): | |
89 | |
90 if not nx_graph2.has_edge(s, t): | |
91 missing_rels2.append((s,t)) | |
92 | |
93 if len(missing_rels2) > 0: | |
94 log('WARNING', "%s relations missing in graph 2"%len(missing_rels2)) | |
95 log('DEBUG', "relations: %s"%missing_rels2) | |
96 #log('DEBUG', "nodes: %s"%([nx_graph1.node[n] for n in missing_nodes2])) | |
97 | |
98 # iterate all nodes in graph 2 | |
99 for (s, t) in nx.edges_iter(nx_graph2): | |
100 #attrs = nx_graph.node[n] | |
101 | |
102 if not nx_graph1.has_edge(s, t): | |
103 missing_rels1.append((s,t)) | |
104 | |
105 if len(missing_rels1) > 0: | |
106 log('WARNING', "%s relations missing in graph 1"%len(missing_rels1)) | |
107 log('DEBUG', "relations: %s"%(missing_rels1)) | |
108 | |
109 | |
110 ## main | |
111 | |
112 print("Modify networkx graph") | |
113 | |
114 # read commandline parameters | |
115 if len(sys.argv) > 2: | |
116 input1_fn = sys.argv[1] | |
117 input2_fn = sys.argv[2] | |
118 | |
119 # read networkx graph from pickle | |
120 print("Reading graph 1 from %s"%input1_fn) | |
121 nx_graph1 = nx.read_gpickle(input1_fn) | |
122 print("Graph 1 info: %s"%nx.info(nx_graph1)) | |
123 | |
124 print("Reading graph 2 from %s"%input2_fn) | |
125 nx_graph2 = nx.read_gpickle(input2_fn) | |
126 print("Graph 2 info: %s"%nx.info(nx_graph2)) | |
127 | |
128 # operate | |
129 compare_nodes(nx_graph1, nx_graph2) | |
130 compare_relations(nx_graph1, nx_graph2) | |
131 | |
132 | |
133 print("Done.") |