-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example_GraphSummarization.py
65 lines (53 loc) · 2.39 KB
/
Example_GraphSummarization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Created on Sun Jul 08 05:03:01 2018
@Project Title: Learning and Summarizing Graphical Models using Eigen Analysis of Graph Laplacian: An Application in Analysis of Multiple Chronic Conditions
@Project: EAGL (Simplification Based Graph Summarization)
@author: Syed Hasib AKhter Faruqui
"""
print('##############################################################################')
print('############################# Sample Example #################################')
print('##############################################################################')
## Load Necessary Library
import EAGL as gc # Graph Compression Library
from scipy import io as sc
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
### Setting Random Seeds for reproducibility
from numpy.random import seed
seed(123)
import os
os.environ['PYTHONHASHSEED']='0'
import random as rn
rn.seed(123)
# Create Random Graph
# A. L. Barabási and R. Albert “Emergence of scaling in random networks”, Science 286, pp 509-512, 1999.
G=nx.barabasi_albert_graph(10,5, seed=123)
DAG=nx.to_numpy_array(G)
# Main Calculation
# Method :'False' = Single edge reduction (Default)
# 'True' = Multiple edge reduction
Updated_DAG,EigenValue,NumberofEdges=gc.GraphCompression(DAG,Method='False')
# Print relevent Information
print('Number of Edges on the Updated DAG:',np.count_nonzero(Updated_DAG))
Percentage = np.abs(np.count_nonzero(Updated_DAG)-np.count_nonzero(DAG))/np.count_nonzero(DAG)
print('Compression:',Percentage*100,'%')
## Plot the Tree's
pos = nx.random_layout(nx.DiGraph(DAG))
plt.figure(1)
plt.subplot(1, 2, 1)
gc.plot_Graph(DAG,pos)
plt.title('Original DAG')
plt.subplot(1, 2, 2)
gc.plot_Graph(Updated_DAG,pos)
plt.title('Summarized DAG')
plt.tight_layout() # Fitting the plot
## Plot Number of Edges Reduced
Compression=np.count_nonzero(Updated_DAG)/np.count_nonzero(DAG)
plt.figure(2)
gc.plot_Edge_Reduction(NumberofEdges,"DAG_Unsupervised_2nd_Eigen_Comp:"+str((1-Compression)*100)+'%',mark='x',Color=np.random.random(3))
## Remove Temporary Files Created
os.remove("Dummy_DAG.mat")
print('##############################################################################')
print('############################ Example Complete! ###############################')
print('##############################################################################')