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