aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py
diff options
context:
space:
mode:
Diffstat (limited to 'Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py')
-rw-r--r--Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py b/Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py
new file mode 100644
index 00000000..2f39ca93
--- /dev/null
+++ b/Metrics/Metrics-Calculation/metrics_plot/model comparison/src/plot_ks_stats.py
@@ -0,0 +1,98 @@
1import os, sys
2lib_path = os.path.abspath(os.path.join('..', '..', 'utils'))
3sys.path.append(lib_path)
4import glob
5import random
6from sklearn.manifold import MDS
7import matplotlib.pyplot as plt
8from scipy import stats
9import numpy as np
10from GraphType import GraphCollection
11
12def main():
13 # read models
14 human = GraphCollection('../input/humanOutput/', 500, 'Human')
15 viatra30 = GraphCollection('../input/viatraOutput30/', 500, 'Viatra (30 nodes)')
16 # viatra60 = GraphCollection('../input/viatraOutput60/', 500, 'Viatra (60 nodes)')
17 # viatra100 = GraphCollection('../input/viatraOutput100/', 500, 'Viatra (100 nodes)')
18 # random = GraphCollection('../input/randomOutput/', 500, 'Random')
19 # alloy = GraphCollection('../input/alloyOutput/', 500, 'Alloy (30 nodes)')
20
21 models_to_compare = [human, viatra30]
22
23 # define output folder
24 outputFolder = '../output/'
25
26 #calculate metrics
27 metricStat(models_to_compare, 'Node Activity', nodeActivity, 0, outputFolder)
28 metricStat(models_to_compare, 'Out Degree', outDegree, 1, outputFolder)
29 metricStat(models_to_compare, 'MPC', mpc, 2, outputFolder)
30
31def calculateKSMatrix(dists):
32 dist = []
33
34 for i in range(len(dists)):
35 dist = dist + dists[i]
36 matrix = np.empty((len(dist),len(dist)))
37
38 for i in range(len(dist)):
39 matrix[i,i] = 0
40 for j in range(i+1, len(dist)):
41 value, p = stats.ks_2samp(dist[i], dist[j])
42 matrix[i, j] = value
43 matrix[j, i] = value
44 return matrix
45
46
47def calculateMDS(dissimilarities):
48 embedding = MDS(n_components=2, dissimilarity='precomputed')
49 trans = embedding.fit_transform(X=dissimilarities)
50 return trans
51
52def plot(graphTypes, coords, title='',index = 0, savePath = ''):
53 half_length = int(coords.shape[0] / len(graphTypes))
54 color = ['blue', 'red', 'green', 'yellow']
55 plt.figure(index, figsize=(7, 4))
56 plt.title(title)
57 for i in range(len(graphTypes)):
58 x = (coords[(i*half_length):((i+1)*half_length), 0].tolist())
59 y = (coords[(i*half_length):((i+1)*half_length), 1].tolist())
60 plt.plot(x, y, color=color[i], marker='o', label = graphTypes[i].name, linestyle='', alpha=0.7)
61 plt.legend(loc='upper right')
62 plt.savefig(fname = savePath, dpi=150)
63 #graph.show()
64
65def mkdir_p(mypath):
66 '''Creates a directory. equivalent to using mkdir -p on the command line'''
67
68 from errno import EEXIST
69 from os import makedirs,path
70
71 try:
72 makedirs(mypath)
73 except OSError as exc: # Python >2.5
74 if exc.errno == EEXIST and path.isdir(mypath):
75 pass
76 else: raise
77
78def metricStat(graphTypes, metricName, metric, graphIndex, outputFolder):
79 metrics = []
80 for graph in graphTypes:
81 metrics.append(metric(graph))
82 outputFolder = outputFolder + graph.name + '-'
83 print('calculate' + metricName +' for ' + outputFolder)
84 mkdir_p(outputFolder)
85 out_d_coords = calculateMDS(calculateKSMatrix(metrics))
86 plot(graphTypes, out_d_coords, metricName, graphIndex,outputFolder + '/'+ metricName+'.png')
87
88def nodeActivity(graphType):
89 return graphType.nas
90
91def outDegree(graphType):
92 return graphType.out_ds
93
94def mpc(graphType):
95 return graphType.mpcs
96
97if __name__ == '__main__':
98 main() \ No newline at end of file