annotate importFromOpenMind/importer/model2model.py @ 24:97f2da68fb5f

first version of model2model graph manipulation tool. doesn't work yet.
author casties
date Wed, 23 Sep 2015 19:47:02 +0200
parents
children 5bdcb5805d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
1 import networkx as nx
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
2 import sys
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
3
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
4 ## configure behaviour
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
5
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
6 # metworkx graph files
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
7 input_fn = 'ismi_graph.gpickle'
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
8 output_fn = 'ismi_graph_mod.gpickle'
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
9
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
10 # operations
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
11 ops = ['contract', 'inv_rels']
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
12
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
13 # add relations to these objects as attributes with the relations name
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
14 contract_relations_into_attributes = {'PLACE': ['label'],
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
15 'ALIAS': ['label']}
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
16
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
17
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
18
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
19
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
20 def fixName(name, is_src_rel=False, is_tar_rel=False, att_from_rel=False):
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
21 # these are too embarrassing...
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
22 if 'FLORUIT' in name:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
23 name = name.replace('FLORUIT', 'FLOURISH')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
24
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
25 elif 'floruit' in name:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
26 name = name.replace('floruit', 'flourish')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
27
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
28 if is_src_rel:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
29 #name = name + '>'
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
30 pass
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
31
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
32 if is_tar_rel:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
33 name = '<' + name
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
34
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
35 if att_from_rel:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
36 # clean up relations as attribute names
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
37 name = name.replace('is_', '')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
38 name = name.replace('has_', '')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
39 name = name.replace('was_', '')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
40 name = name.replace('_of', '')
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
41
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
42 return name
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
43
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
44
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
45 def contractRelations(nx_graph):
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
46 """contract relations into attributes"""
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
47
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
48
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
49
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
50
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
51 ## main
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
52
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
53 print("Modify networkx graph")
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
54
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
55 # read commandline parameters
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
56 if len(sys.argv) > 2:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
57 input_fn = sys.argv[1]
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
58 output_fn = sys.argv[2]
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
59 #ops = sys.argv[3]
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
60
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
61 # read networkx graph from pickle
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
62 print("Reading graph from %s"%input_fn)
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
63 nx_graph = nx.read_gpickle(input_fn)
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
64 print("Graph info: %s"%nx.info(nx_graph))
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
65
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
66 # operate
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
67 for op in ops:
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
68 if op == 'contract':
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
69 contractRelations(nx_graph)
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
70
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
71 elif op == 'inv_rels':
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
72 invertRelations(nx_graph)
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
73
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
74
97f2da68fb5f first version of model2model graph manipulation tool. doesn't work yet.
casties
parents:
diff changeset
75 print("Done.")