def generate_dep_graphs(topicListFilePath): topicListFile = open(topicListFilePath, 'r', encoding="utf8") for topicIndex, topic in enumerate(topicListFile): topic = topic.replace('\n', '') print('Processing topic -> [{}]'.format(topic)) outputTopicDepGraphFileDir = '../data/train_data/{}/dep_graphs'.format( topic) topicDataFilePath = '../data/train_data/{}/corpus.txt'.format(topic) corpusDataFile = open(topicDataFilePath, 'r', encoding="utf8") for docIndex, doc in enumerate(corpusDataFile): graphOutputFile = '{}/{}.gexf'.format(outputTopicDepGraphFileDir, docIndex) if os.path.exists(graphOutputFile): print( 'Graph`s file for document index -> [{}] is existed, skipping !' .format(docIndex)) continue else: print('Proceed document id: {}'.format(docIndex + 1)) depGraph = depGraphParser.proceed(doc) if depGraph is not None: nx.write_gexf(depGraph, graphOutputFile)
def plot_graph(self, name, score): #Import matplotlib, a library used to produce graphic in python. import matplotlib.pyplot as plt from networkx import nx plt.figure(figsize=(7, 7)) # Adjust the window size plt.margins(.2, .2) # Adjust margins #We determine the node position: https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.layout.spring_layout.html position = nx.spring_layout(self.graph) nx.draw_networkx(self.graph, with_labels=True, pos=position) plt.axis('off') # Removing axis #Weights on edges : solution found on stackoverflow #https://stackoverflow.com/questions/28372127/add-edge-weights-to-plot-output-in-networkx labels = nx.get_edge_attributes(self.graph, 'weight') nx.draw_networkx_edge_labels(self.graph, pos=position, edge_labels=labels) plt.title( "fasta alignment graph according to a minimum alignment score of " + str(score)) plt.savefig( name + ".png") # Produce a png file with the graph annotated by default plt.show() nx.write_gexf( self.graph, name + ".gexf") # Produce a gexf file that can be annotated with gephi. return None
def generate_dep_graphs(doc_file_path): outputDepGraphs = [] outputDocDepGraphFileDir = 'data/test/dep_graphs' if not os.path.exists(outputDocDepGraphFileDir): os.mkdir(outputDocDepGraphFileDir) corpusDataFile = open(doc_file_path, 'r', encoding="utf8") for docIndex, docContent in enumerate(corpusDataFile): graphOutputFile = '{}/{}.gexf'.format(outputDocDepGraphFileDir, docIndex) depGraph = depGraphParser.proceed(docContent) if depGraph is not None: outputDepGraphs.append(depGraph) nx.write_gexf(depGraph, graphOutputFile) return outputDepGraphs
print len(comms), "communities found" print "calculating positions via fruchterman reingold" pos = g.layout_grid_fruchterman_reingold() print "setting colors and positions on each node" for i in range(len(comms)): for n in comms[i]: color = rgbs[i] if i < len(rgbs) else { "r": 211, "g": 211, "b": 211 } if len(comms[i]) < 5: color = { "r": 211, "g": 211, "b": 211 } g.vs[n]['viz'] = str({'color': color, 'position':{'x':pos[n][0], 'y':pos[n][1]}}) g.vs[n]['uid'] = g.vs[n]['name'] g.vs[n]['label'] = mapping[g.vs[n]['uid']] print "saving to gml" g.write_gml(open("graph.gml", "w")) print "read gml file in networkx" from networkx import nx G = nx.read_gml('graph.gml') for node in G.nodes(data=True): node[1]['viz'] = eval(node[1]['viz']) print "write to gefx from networkx" nx.write_gexf(G, './network/data/%s.gexf' % KEYWORD) config = json.load(open("./network/sample.json")) config['data'] = 'data/%s.gexf' % KEYWORD config['logo']['text'] = KEYWORD.title() config['text']['title'] = ALG json.dump(config, open('./network/%s.json' % KEYWORD, 'w')) print "done!"
def map_concept_to_onto_by_topic_name(topic_name): topicConceptOutputFolder = '../data/cdo_ontology/concepts/{}'.format( topic_name) outputTopicConceptFilePath = '../data/train_data/{}/freq_graph.txt'.format( topic_name) if not os.path.exists(topicConceptOutputFolder): os.makedirs(topicConceptOutputFolder) else: clear_dir(topicConceptOutputFolder) conceptDataFile = open(outputTopicConceptFilePath, 'r', encoding="utf8") vocFile = open(vocFilePath, 'r', encoding="utf8") relLabelFile = open(relLabelFilePath, 'r', encoding="utf8") vocList = [] relLabelList = [] for index, line in enumerate(vocFile): splits = line.split('\t') vocList.append(splits[1]) for index, line in enumerate(relLabelFile): splits = line.split('\t') relLabelList.append(splits[1]) graphBasedConcepts = [] graphCount = 0 verticeList = [] graphBasedConcept = nx.DiGraph() for index, line in enumerate(conceptDataFile): splits = line.split(' ') if splits[0] == 't': if graphCount > 0: verticeList = [] graphCount = graphCount + 1 graphBasedConcepts.append(graphBasedConcept) graphBasedConcept = nx.DiGraph() else: graphCount += 1 continue else: if splits[0] == 'v': termLabel = vocList[int(splits[2])].replace('\n', '') #print(termLabel) #print(termLabel) graphBasedConcept.add_node(termLabel) verticeList.insert(int(splits[1]), termLabel) elif splits[0] == 'e': graphBasedConcept.add_edge( verticeList[int(splits[1])].replace('\n', ''), verticeList[int(splits[2])].replace('\n', ''), semantic_label=relLabelList[int(splits[3])].replace( '\n', '')) #write concept to file conceptFileCount = 0 for conceptIndex, concept in enumerate(graphBasedConcepts): if conceptFileCount < maxNumConceptForEachTopic: if len(concept.nodes()) > 0: outputConceptFilePath = '{}/{}.gexf'.format( topicConceptOutputFolder, conceptFileCount) nx.write_gexf(concept, outputConceptFilePath) conceptFileCount += 1 else: continue else: break print( 'Mapping to topic [{}] of CDO -> total [{}] concepts, get top [{}] concepts.' .format(topic_name, len(graphBasedConcepts), conceptFileCount))
import sys, json from networkx import nx filename = sys.argv[1].split("/")[-1] G = nx.read_gml(sys.argv[1]) for node in G.nodes(data=True): node[1]['viz'] = eval(node[1]['viz'].replace("-Inf", "float(\"-inf\")")) print "write to gefx from networkx" nx.write_gexf(G, './network/data/%s.gexf' % filename) config = json.load(open("./network/sample.json")) config['data'] = 'data/%s.gexf' % filename config['logo']['text'] = filename.title() config['text']['title'] = filename.title() json.dump(config, open('./network/%s.json' % filename, 'w'))