/
plot_metrics.py
executable file
·245 lines (206 loc) · 8.42 KB
/
plot_metrics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#! /usr/bin/python
import sys, os, getopt
import modules.extra, imp
from modules.plot import Plot
brewer2mpl = imp.load_source('brewer2mpl', 'lib/brewer2mpl/brewer2mpl.py')
### GLOBAL VARIABLES ###
nodes_top = []
nodes_bot = []
parts = ["top", "bot"]
metrics = []
correlations = []
metrics_log = []
colorset = brewer2mpl.get_map('Set1', 'qualitative', 8).mpl_colors
list_markers = ["o", "v", "D", "x", "*", "h", "4"]
########################
def usage():
usage = "Usage : plot_metrics.py -d BIPARTITE_DIRECTORY\n\n"
usage += "Option\tLong option\tDescription\n"
usage += "-h\t--help\t\tDisplay this message\n"
usage += "-d\t--directory\t\tDirectory where metrics are saved <[MANDATORY]>\n"
print usage
def create_directories(directory):
directory_plot = directory + "/plots"
if not os.path.isdir(directory_plot):
os.mkdir(directory_plot)
#### METRICS ####
if not os.path.isdir(directory + "/plots/metrics"):
os.mkdir(directory + "/plots/metrics")
directory_plot_metrics_top = directory_plot + "/metrics/top"
if not os.path.isdir(directory_plot_metrics_top):
os.mkdir(directory_plot_metrics_top)
directory_plot_metrics_bot = directory_plot + "/metrics/bot"
if not os.path.isdir(directory_plot_metrics_bot):
os.mkdir(directory_plot_metrics_bot)
#### CORRELATIONS ####
if not os.path.isdir(directory + "/plots/correlations"):
os.mkdir(directory + "/plots/correlations")
directory_plot_correlations_top = directory_plot + "/correlations/top"
if not os.path.isdir(directory_plot_correlations_top):
os.mkdir(directory_plot_correlations_top)
directory_plot_correlations_bot = directory_plot + "/correlations/bot"
if not os.path.isdir(directory_plot_correlations_bot):
os.mkdir(directory_plot_correlations_bot)
return directory_plot_metrics_top, directory_plot_metrics_bot, directory_plot_correlations_top, directory_plot_correlations_bot
def plot_metrics(directory_metrics, parts_directory_plot_metrics, legend):
print "\t\t[ Plotting metrics... ]"
for metric in metrics:
for i in range(0, 2):
d = directory_metrics + "/" + parts[i] + "/" + metric + "_" + parts[i]
print "\tPlotting %s ..." % d
if not os.path.isdir(d):
print "[%s directory not found]" % d
else:
xlog = False
ylog = False
if metric in metrics_log:
xlog = True
ylog = True
########################
# PLOT DISTRIBUTION
########################
data_distribution = {}
with open(d + "/" + metric + "_" + parts[i] + "_distribution.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
data_distribution[modules.extra.num(line[0])] = modules.extra.num(line[1])
title = ""
xlabel = metric
ylabel = ""
plot_file = parts_directory_plot_metrics[i] + "/" + xlabel + "_distribution"
plot_parameters = dict(marker="o", markersize=11, linewidth=0, alpha=0.5, color=colorset[0])
if data_distribution:
P = Plot(plot_file, title, xlabel, ylabel, xlog, ylog, [legend])
P.plot_single(data_distribution, plot_parameters)
else:
print "[Impossible to plot because no distribution data]"
########################
########################
# PLOT CDF
########################
data_reverse_cdf = {}
with open(d + "/" + metric + "_" + parts[i] + "_reverse_cdf.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
data_reverse_cdf[modules.extra.num(line[0])] = modules.extra.num(line[1])
title = ""
xlabel = metric
ylabel = ""
plot_file = parts_directory_plot_metrics[i] + "/" + xlabel + "_reverse_cdf"
plot_parameters = dict(linestyle="-", marker="s", markersize=8, linewidth=2.5, markeredgecolor=colorset[0], color=colorset[0])
if data_reverse_cdf:
P = Plot(plot_file, title, xlabel, ylabel, False, False, [legend], formatter=True)
P.plot_single(data_reverse_cdf, plot_parameters)
else:
print "[Impossible to plot because no reverse CDF data]"
########################
def plot_correlations(directory_correlations, parts_directory_plot_correlations, legend):
print "\t\t[ Plotting correlations... ]"
for correlation in correlations:
metrics = correlation.split("-")
for i in range(0, 2):
correlation = metrics[0] + "_" + parts[i] + "-" + metrics[1] + "_" + parts[i]
d = directory_correlations + "/" + parts[i] + "/" + correlation
if not os.path.isdir(d):
print "[%s directory not found]" % d
else:
xlog = False
ylog = False
if metrics[0] in metrics_log:
xlog = True
if metrics[1] in metrics_log:
ylog = True
print "\tPlotting %s ..." % d
list_plot_parameters = []
list_data = []
########################
# SCATTER POINTS
########################
data_scatter = {}
with open(d + "/" + correlation + "_scatter.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
data_scatter[modules.extra.num(line[0])] = modules.extra.num(line[1])
highest = max(data_scatter.values())
list_plot_parameters.append(dict(marker="o", markersize=3, linewidth=0, alpha=0.5, color=colorset[0]))
list_data.append(data_scatter)
########################
# CURVE FITTING (AVG)
########################
data_avg_curve = {}
with open(d + "/" + correlation + "_avg_curve.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
data_avg_curve[modules.extra.num(line[0])] = modules.extra.num(line[1])
list_plot_parameters.append(dict(linestyle="--", marker="s", markersize=8, linewidth=2.5, color=colorset[1]))
list_data.append(data_avg_curve)
'''data_linear = {}
with open(d + "/" + correlation + "_linear_regression.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
if line[0] in nodes or not nodes:
data_linear[num(line[0])] = num(line[1])
list_plot_parameters.append(dict(linestyle="-", marker="o", markersize=6, linewidth=1.5, alpha=0.75))
list_data.append(data_linear)'''
'''data_fitting_curve = {}
with open(d + "/" + correlation + "_fitting_curve.data", 'r') as file:
for line in file:
line = line.replace("\n", "").split()
if line[0] in nodes or not nodes:
data_fitting_curve[num(line[0])] = num(line[1])
list_plot_parameters.append(dict(linestyle="-", marker="o", markersize=6, linewidth=1.5, alpha=0.75))
list_data.append(data_fitting_curve)'''
title = ""
xlabel = metrics[0]
ylabel = metrics[1]
plot_file = parts_directory_plot_correlations[i] + "/" + correlation
try:
P = Plot(plot_file, title, xlabel, ylabel, True, True, [legend], ylimit=[0, highest])
P.plot_multiple(list_data, list_plot_parameters)
except Exception, e:
print "Error during plotting process : %s" % e
def main(argv):
try:
opts, args = getopt.getopt(argv, "hd:", ["help", "directory="])
except getopt.GetoptError, e:
usage()
print "Error >>> %s" % str(e)
sys.exit(2)
directory = ""
node_file = ""
part_node_file = ""
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
elif opt in ("-d", "--directory"):
directory = arg
if directory[len(directory) - 1] == "/":
directory = directory[:len(directory) - 1]
### CHECK IF DIRECTORIES ARE CORRECT
if not os.path.isdir(directory):
usage()
print "Bad directory location : '%s'" % directory
sys.exit(2)
directory_metrics = directory + "/metrics"
if not os.path.isdir(directory_metrics):
usage()
print "Error metrics directory doesn't exist"
sys.exit(2)
directory_correlations = directory + "/correlations"
if not os.path.isdir(directory_correlations):
usage()
print "Error correlations directory doesn't exist"
sys.exit(2)
#legend = directory.split("/")[0]
legend = directory.split("/")
legend = legend[len(legend)-1]
### CREATE DIRECTORIES
directory_plot_metrics_top, directory_plot_metrics_bot, directory_plot_correlations_top, directory_plot_correlations_bot = create_directories(directory)
parts_directory_plot_metrics = [directory_plot_metrics_top, directory_plot_metrics_bot]
parts_directory_plot_correlations = [directory_plot_correlations_top, directory_plot_correlations_bot]
global metrics, correlations, metrics_log
metrics, correlations, metrics_log = modules.extra.get_plotting_configurations()
plot_metrics(directory_metrics, parts_directory_plot_metrics, legend)
plot_correlations(directory_correlations, parts_directory_plot_correlations, legend)
if __name__ == "__main__":
main(sys.argv[1:])