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