In [1]:
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import os
from glob import glob
pd.set_option('display.mpl_style', 'default')
pd.set_option('display.width', 5000)
pd.set_option('display.max_columns', 60)
#gml_files = glob('../output/network/*/*.gml')
pos_files = glob('../output/network/positive/*.gml')
neg_files = glob('../output/network/negative/*.gml')
neu_files = glob('../output/network/neutral/*.gml')
In [2]:
def calculate_graph_inf(graph):
graph.name = filename
info = nx.info(graph)
print info
def plot_graph(graph):
info = nx.info(graph)
print info
plt.figure(figsize=(10,10))
nx.draw_spring(graph, with_labels = True)
In [3]:
#
pos_graphs = []
pos_ugraphs = []
neg_graphs = []
neg_ugraphs = []
neu_graphs = []
neu_ugraphs = []
for graph_num, gml_graph in enumerate(pos_files):
graph = nx.read_gml(gml_graph)
ugraph = graph.to_undirected()
U = graph.to_undirected(reciprocal=True)
e = U.edges()
ugraph.add_edges_from(e)
(filepath, filename) = os.path.split(gml_graph)
print('-' * 40)
print(gml_graph)
print(nx.info(graph))
pos_graphs.append(graph)
pos_ugraphs.append(ugraph)
for graph_num, gml_graph in enumerate(neg_files):
graph = nx.read_gml(gml_graph)
ugraph = graph.to_undirected()
U = graph.to_undirected(reciprocal=True)
e = U.edges()
ugraph.add_edges_from(e)
(filepath, filename) = os.path.split(gml_graph)
print('-' * 40)
print(gml_graph)
print(nx.info(graph))
neg_graphs.append(graph)
neg_ugraphs.append(ugraph)
for graph_num, gml_graph in enumerate(neu_files):
graph = nx.read_gml(gml_graph)
ugraph = graph.to_undirected()
U = graph.to_undirected(reciprocal=True)
e = U.edges()
ugraph.add_edges_from(e)
(filepath, filename) = os.path.split(gml_graph)
print('-' * 40)
print(gml_graph)
print(nx.info(graph))
neu_graphs.append(graph)
neu_ugraphs.append(ugraph)
In [4]:
# compose(G1, G2): combine graphs identifying nodes common to both
# C = nx.compose(pos_graphs[0], pos_graphs[1])
# compose_all
pos_all = nx.compose_all(pos_graphs)
pos_uall = nx.compose_all(pos_ugraphs)
neg_all = nx.compose_all(neg_graphs)
neg_uall = nx.compose_all(neg_ugraphs)
neu_all = nx.compose_all(neu_graphs)
neu_uall = nx.compose_all(neu_ugraphs)
In [5]:
print nx.info(pos_all)
print nx.info(pos_uall)
print nx.info(neg_all)
print nx.info(neg_uall)
print nx.info(neu_all)
print nx.info(neu_uall)
In [6]:
# write to gml
nx.write_gml(pos_all, "positive_all.gml")
nx.write_gml(pos_uall, "positive_uall.gml")
nx.write_gml(neg_all, "negative_all.gml")
nx.write_gml(neg_uall, "negative_uall.gml")
nx.write_gml(neu_all, "neutral_all.gml")
nx.write_gml(neu_uall, "neutral_uall.gml")
In [9]:
# plotting
positive_all = nx.read_gml("positive_all.gml")
# nx.draw_spring(pos_all, with_labels = True)
# plt.savefig("path.png")
In [ ]:
In [ ]: