make_union

  1. (network data copy)
  2. import positive, negative, neutral gml files
  3. write to directed and undirected gml files
  4. out = pos_all.gml, pos_uall.gml, neg_all.gml, neg_uall.gml, neu_all.gml, neu_uall.gml

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)


----------------------------------------
../output/network/positive/article2.gml
Name: 
Type: MultiDiGraph
Number of nodes: 28
Number of edges: 23
Average in degree:   0.8214
Average out degree:   0.8214
----------------------------------------
../output/network/positive/article1804.gml
Name: 
Type: MultiDiGraph
Number of nodes: 42
Number of edges: 50
Average in degree:   1.1905
Average out degree:   1.1905
----------------------------------------
../output/network/positive/article6247.gml
Name: 
Type: MultiDiGraph
Number of nodes: 109
Number of edges: 96
Average in degree:   0.8807
Average out degree:   0.8807
----------------------------------------
../output/network/positive/article4150.gml
Name: 
Type: MultiDiGraph
Number of nodes: 24
Number of edges: 18
Average in degree:   0.7500
Average out degree:   0.7500
----------------------------------------
../output/network/positive/article1105.gml
Name: 
Type: MultiDiGraph
Number of nodes: 38
Number of edges: 31
Average in degree:   0.8158
Average out degree:   0.8158
----------------------------------------
../output/network/positive/article5077.gml
Name: 
Type: MultiDiGraph
Number of nodes: 45
Number of edges: 49
Average in degree:   1.0889
Average out degree:   1.0889
----------------------------------------
../output/network/positive/article111.gml
Name: 
Type: MultiDiGraph
Number of nodes: 83
Number of edges: 75
Average in degree:   0.9036
Average out degree:   0.9036
----------------------------------------
../output/network/positive/article828.gml
Name: 
Type: MultiDiGraph
Number of nodes: 56
Number of edges: 54
Average in degree:   0.9643
Average out degree:   0.9643
----------------------------------------
../output/network/positive/article665.gml
Name: 
Type: MultiDiGraph
Number of nodes: 35
Number of edges: 30
Average in degree:   0.8571
Average out degree:   0.8571
----------------------------------------
../output/network/positive/article664.gml
Name: 
Type: MultiDiGraph
Number of nodes: 29
Number of edges: 27
Average in degree:   0.9310
Average out degree:   0.9310
----------------------------------------
../output/network/positive/article1542.gml
Name: 
Type: MultiDiGraph
Number of nodes: 45
Number of edges: 51
Average in degree:   1.1333
Average out degree:   1.1333
----------------------------------------
../output/network/positive/article4303.gml
Name: 
Type: MultiDiGraph
Number of nodes: 24
Number of edges: 19
Average in degree:   0.7917
Average out degree:   0.7917
----------------------------------------
../output/network/positive/article36.gml
Name: 
Type: MultiDiGraph
Number of nodes: 15
Number of edges: 12
Average in degree:   0.8000
Average out degree:   0.8000
----------------------------------------
../output/network/positive/article4210.gml
Name: 
Type: MultiDiGraph
Number of nodes: 63
Number of edges: 71
Average in degree:   1.1270
Average out degree:   1.1270
----------------------------------------
../output/network/positive/article620.gml
Name: 
Type: MultiDiGraph
Number of nodes: 76
Number of edges: 68
Average in degree:   0.8947
Average out degree:   0.8947
----------------------------------------
../output/network/positive/article31.gml
Name: 
Type: MultiDiGraph
Number of nodes: 48
Number of edges: 43
Average in degree:   0.8958
Average out degree:   0.8958
----------------------------------------
../output/network/positive/article5.gml
Name: 
Type: MultiDiGraph
Number of nodes: 58
Number of edges: 51
Average in degree:   0.8793
Average out degree:   0.8793
----------------------------------------
../output/network/positive/article29.gml
Name: 
Type: MultiDiGraph
Number of nodes: 74
Number of edges: 61
Average in degree:   0.8243
Average out degree:   0.8243
----------------------------------------
../output/network/positive/article46.gml
Name: 
Type: MultiDiGraph
Number of nodes: 80
Number of edges: 83
Average in degree:   1.0375
Average out degree:   1.0375
----------------------------------------
../output/network/positive/article30.gml
Name: 
Type: MultiDiGraph
Number of nodes: 17
Number of edges: 17
Average in degree:   1.0000
Average out degree:   1.0000
----------------------------------------
../output/network/positive/article609.gml
Name: 
Type: MultiDiGraph
Number of nodes: 134
Number of edges: 139
Average in degree:   1.0373
Average out degree:   1.0373
----------------------------------------
../output/network/positive/article02.gml
Name: 
Type: MultiDiGraph
Number of nodes: 36
Number of edges: 31
Average in degree:   0.8611
Average out degree:   0.8611
----------------------------------------
../output/network/positive/article37.gml
Name: 
Type: MultiDiGraph
Number of nodes: 21
Number of edges: 28
Average in degree:   1.3333
Average out degree:   1.3333
----------------------------------------
../output/network/negative/article432.gml
Name: 
Type: MultiDiGraph
Number of nodes: 100
Number of edges: 96
Average in degree:   0.9600
Average out degree:   0.9600
----------------------------------------
../output/network/negative/article06.gml
Name: 
Type: MultiDiGraph
Number of nodes: 124
Number of edges: 121
Average in degree:   0.9758
Average out degree:   0.9758
----------------------------------------
../output/network/negative/article1.gml
Name: 
Type: MultiDiGraph
Number of nodes: 140
Number of edges: 147
Average in degree:   1.0500
Average out degree:   1.0500
----------------------------------------
../output/network/negative/article99.gml
Name: 
Type: MultiDiGraph
Number of nodes: 45
Number of edges: 46
Average in degree:   1.0222
Average out degree:   1.0222
----------------------------------------
../output/network/negative/article05.gml
Name: 
Type: MultiDiGraph
Number of nodes: 22
Number of edges: 25
Average in degree:   1.1364
Average out degree:   1.1364
----------------------------------------
../output/network/negative/article774.gml
Name: 
Type: MultiDiGraph
Number of nodes: 57
Number of edges: 54
Average in degree:   0.9474
Average out degree:   0.9474
----------------------------------------
../output/network/negative/article782.gml
Name: 
Type: MultiDiGraph
Number of nodes: 84
Number of edges: 77
Average in degree:   0.9167
Average out degree:   0.9167
----------------------------------------
../output/network/negative/article152.gml
Name: 
Type: MultiDiGraph
Number of nodes: 78
Number of edges: 67
Average in degree:   0.8590
Average out degree:   0.8590
----------------------------------------
../output/network/negative/article4106.gml
Name: 
Type: MultiDiGraph
Number of nodes: 38
Number of edges: 36
Average in degree:   0.9474
Average out degree:   0.9474
----------------------------------------
../output/network/negative/article5717.gml
Name: 
Type: MultiDiGraph
Number of nodes: 62
Number of edges: 62
Average in degree:   1.0000
Average out degree:   1.0000
----------------------------------------
../output/network/negative/article621.gml
Name: 
Type: MultiDiGraph
Number of nodes: 30
Number of edges: 32
Average in degree:   1.0667
Average out degree:   1.0667
----------------------------------------
../output/network/negative/article03.gml
Name: 
Type: MultiDiGraph
Number of nodes: 18
Number of edges: 13
Average in degree:   0.7222
Average out degree:   0.7222
----------------------------------------
../output/network/negative/article5164.gml
Name: 
Type: MultiDiGraph
Number of nodes: 104
Number of edges: 119
Average in degree:   1.1442
Average out degree:   1.1442
----------------------------------------
../output/network/negative/article2308.gml
Name: 
Type: MultiDiGraph
Number of nodes: 66
Number of edges: 56
Average in degree:   0.8485
Average out degree:   0.8485
----------------------------------------
../output/network/negative/article5813.gml
Name: 
Type: MultiDiGraph
Number of nodes: 50
Number of edges: 54
Average in degree:   1.0800
Average out degree:   1.0800
----------------------------------------
../output/network/negative/article683.gml
Name: 
Type: MultiDiGraph
Number of nodes: 234
Number of edges: 236
Average in degree:   1.0085
Average out degree:   1.0085
----------------------------------------
../output/network/negative/article07.gml
Name: 
Type: MultiDiGraph
Number of nodes: 56
Number of edges: 57
Average in degree:   1.0179
Average out degree:   1.0179
----------------------------------------
../output/network/negative/article1021.gml
Name: 
Type: MultiDiGraph
Number of nodes: 64
Number of edges: 64
Average in degree:   1.0000
Average out degree:   1.0000
----------------------------------------
../output/network/negative/article3335.gml
Name: 
Type: MultiDiGraph
Number of nodes: 120
Number of edges: 128
Average in degree:   1.0667
Average out degree:   1.0667
----------------------------------------
../output/network/negative/article1001.gml
Name: 
Type: MultiDiGraph
Number of nodes: 134
Number of edges: 134
Average in degree:   1.0000
Average out degree:   1.0000
----------------------------------------
../output/network/negative/article703.gml
Name: 
Type: MultiDiGraph
Number of nodes: 282
Number of edges: 280
Average in degree:   0.9929
Average out degree:   0.9929
----------------------------------------
../output/network/neutral/article54.gml
Name: 
Type: MultiDiGraph
Number of nodes: 48
Number of edges: 37
Average in degree:   0.7708
Average out degree:   0.7708
----------------------------------------
../output/network/neutral/article532.gml
Name: 
Type: MultiDiGraph
Number of nodes: 51
Number of edges: 37
Average in degree:   0.7255
Average out degree:   0.7255
----------------------------------------
../output/network/neutral/article756.gml
Name: 
Type: MultiDiGraph
Number of nodes: 41
Number of edges: 40
Average in degree:   0.9756
Average out degree:   0.9756
----------------------------------------
../output/network/neutral/article647.gml
Name: 
Type: MultiDiGraph
Number of nodes: 54
Number of edges: 48
Average in degree:   0.8889
Average out degree:   0.8889
----------------------------------------
../output/network/neutral/article2047.gml
Name: 
Type: MultiDiGraph
Number of nodes: 52
Number of edges: 61
Average in degree:   1.1731
Average out degree:   1.1731
----------------------------------------
../output/network/neutral/article63.gml
Name: 
Type: MultiDiGraph
Number of nodes: 17
Number of edges: 15
Average in degree:   0.8824
Average out degree:   0.8824

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)


Name: compose( ,  )
Type: MultiDiGraph
Number of nodes: 948
Number of edges: 1114
Average in degree:   1.1751
Average out degree:   1.1751
Name: compose( ,  )
Type: MultiGraph
Number of nodes: 948
Number of edges: 1110
Average degree:   2.3418
Name: compose( ,  )
Type: MultiDiGraph
Number of nodes: 1563
Number of edges: 1879
Average in degree:   1.2022
Average out degree:   1.2022
Name: compose( ,  )
Type: MultiGraph
Number of nodes: 1563
Number of edges: 1870
Average degree:   2.3928
Name: compose( ,  )
Type: MultiDiGraph
Number of nodes: 250
Number of edges: 238
Average in degree:   0.9520
Average out degree:   0.9520
Name: compose( ,  )
Type: MultiGraph
Number of nodes: 250
Number of edges: 238
Average degree:   1.9040

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 [ ]: