view 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
line wrap: on
line source

import networkx as nx
import sys

## configure behaviour

# metworkx graph files
input_fn = 'ismi_graph.gpickle'
output_fn = 'ismi_graph_mod.gpickle'

# operations
ops = ['contract', 'inv_rels']

# add relations to these objects as attributes with the relations name
contract_relations_into_attributes = {'PLACE': ['label'],
                                      'ALIAS': ['label']}




def fixName(name, is_src_rel=False, is_tar_rel=False, att_from_rel=False):
    # these are too embarrassing...
    if 'FLORUIT' in name:
        name = name.replace('FLORUIT', 'FLOURISH')
        
    elif 'floruit' in name:
        name = name.replace('floruit', 'flourish')
        
    if is_src_rel:
        #name = name + '>'
        pass
        
    if is_tar_rel:
        name = '<' + name
        
    if att_from_rel:
        # clean up relations as attribute names
        name = name.replace('is_', '')
        name = name.replace('has_', '')
        name = name.replace('was_', '')
        name = name.replace('_of', '')

    return name


def contractRelations(nx_graph):
    """contract relations into attributes"""
    
    
    
    
## main

print("Modify networkx graph")

# read commandline parameters
if len(sys.argv) > 2:
    input_fn = sys.argv[1]
    output_fn = sys.argv[2]
    #ops = sys.argv[3]

# read networkx graph from pickle
print("Reading graph from %s"%input_fn)
nx_graph = nx.read_gpickle(input_fn)
print("Graph info: %s"%nx.info(nx_graph))

# operate    
for op in ops:
    if op == 'contract':
        contractRelations(nx_graph)
        
    elif op == 'inv_rels':
        invertRelations(nx_graph)


print("Done.")