-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaper_experiment_plot.py
170 lines (143 loc) · 5.94 KB
/
paper_experiment_plot.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
# coding: utf-8
# In[13]:
get_ipython().magic('matplotlib notebook')
import matplotlib as mpl
mpl.use('pdf')
# mpl.rcParams.update({'font.size': 26})
import os
import pandas as pd
import numpy as np
from glob import glob
from matplotlib import pyplot as plt
from itertools import product, takewhile
from collections import defaultdict
from plot_utils import richify_line_style
# In[14]:
from cycler import cycler
plt.rc('axes',
prop_cycle=(
cycler('color', ['b', 'g', 'r', 'y', 'g', 'b']) +
cycler('linestyle', ['-', '-', '-', '-', '--', '-']) +
cycler('marker', ['', '', '', '', 'o', '*'])
))
# In[35]:
def plot_performance(datasets, models, methods, qs, column_names, savefig=False, savelegend=False):
dirname_template = "outputs/paper_experiment/{dataset}/{model}/{method}/qs/{q}.pkl"
result = {}
for dataset, model in product(datasets, models):
key = (dataset, model)
result[key] = {}
for method in methods:
result[key][method] = []
for q in qs_str:
path = dirname_template.format(dataset=dataset, model=model, method=method, q=q)
try:
result[key][method].append(pd.read_pickle(path))
except FileNotFoundError:
dummy = defaultdict(lambda :defaultdict(lambda: None))
result[key][method].append(dummy)
nrow = (len(datasets) if len(datasets) > 1 else len(models))
per_width, per_height, ncol = 3, 2.2, len(column_names)
for i, (dataset, model) in enumerate(product(datasets, models)):
key = (dataset, model)
# one plot
for j, column in enumerate(column_names):
if i == 0:
lines = []
idx = i * ncol + j + 1
fig = plt.figure(figsize=(per_width, per_height))
ax = fig.add_subplot(111)
for method in methods:
# one legend
try:
ys = [df[column]['mean'] for df in result[key][method]]
except KeyError:
print(dataset, method, model)
raise KeyError
lst = list(zip(*takewhile(lambda tpl: tpl[1] is not None, zip(qs, ys))))
if lst:
correct_qs, correct_ys = lst
l, = ax.plot(correct_qs, correct_ys, markersize=7.5, alpha=0.75)
if i == 0:
lines.append(l)
else:
continue
# ax.set_title(column)
column_map = {'n.prec': 'n.precision', 'n.rec': 'n.recall', 'cos-sim': 'cosine sim',
'e.prec': 'e.precision', 'e.rec': 'e.recall',
'obj': '|edges|', 'rank-corr': 'order corr'}
ax.set_ylabel(column_map.get(column, column))
ax.locator_params(axis='y', nbins=4)
ax.set_xlabel('prop. of reports')
ax.locator_params(axis='x', nbins=4)
# ylim = (0, 1)
ylim = None
if column in {"n.prec"}:
ylim = (0.5, 1.05)
if column in {'obj'}:
ylim = None
if ylim is not None:
ax.set_ylim(*ylim)
pass
if column == 'obj':
yticks = ax.get_yticks()
if yticks.max() > 100:
ax.set_yticklabels(list(map(lambda v: "{:.1f}k".format(v / 1000), yticks)))
fig.tight_layout()
import os
dirname = 'figs/paper_experiment/'
if len(datasets) > 1:
dirname += 'by_datasets/'
path = dirname + '{}-{}.pdf'.format(dataset, column_map.get(column, column).replace('|', '').replace(' ', '-'))
else:
dirname += '{}_by_models/'.format(datasets[0])
path = dirname + '{}-{}.pdf'.format(model, column_map.get(column, column).replace('|', '').replace(' ', '-'))
print(dirname)
print(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
if savefig:
fig.savefig(path)
# name_mapping = {'no-order': 'baseline', 'mst': 'closure', 'tbfs': 'delay-bfs', 'greedy': 'greedy'}
# legends = list(map(lambda m: name_mapping[m], methods))
# plt.tight_layout()
# fig.legend(lines, legends, loc='lower center', ncol=3, bbox_to_anchor=(0.5, -0.01), frameon=True)
# if len(datasets) > 1:
# fig.savefig('figs/paper_experiment/measure_by_datasets.pdf')
# else:
# print('figs/paper_experiment/{}_measure_by_models.pdf'.format(datasets[0]))
# fig.savefig('figs/paper_experiment/{}_measure_by_models.pdf'.format(datasets[0]))
figlegend = plt.figure(figsize=(2 * len(methods), 0.5))
ax = fig.add_subplot(111)
name_mapping = {'no-order': 'baseline', 'mst': 'closure', 'tbfs': 'delay-bfs', 'greedy': 'greedy'}
legends = list(map(lambda m: name_mapping[m], methods))
figlegend.legend(lines, legends, 'center', ncol=len(legends))
fig.show()
figlegend.show()
if savelegend:
figlegend.savefig(dirname + 'legend.pdf')
return result
# In[41]:
if False:
datasets = ['arxiv-hep-th', 'enron-email']
models = ['si']
methods = ["greedy", "tbfs"]
else:
# datasets = ['p2p-gnutella08']
datasets = ['barabasi-64']
models = ['si', 'ct']
methods = ["mst", "tbfs", "no-order"]
if datasets[0] not in {'barabasi-64', 'grid-64'}:
qs = np.linspace(0.005, 0.1, 20)
else:
qs = np.linspace(0.1, 1.0, 19)
qs_str = list(map(str, qs))
print(qs_str)
column_names = []
# column_names += ['n.prec', 'n.rec', 'obj']
# column_names = ['obj', 'rank-corr', 'n.prec', 'n.rec']
# column_names = ['rank-corr']
# column_names = ['n.prec-t', 'n.rec-t']
# column_names += ['e.prec', 'e.rec']
column_names += ['rank-corr', 'order accuracy']
result = plot_performance(datasets, models, methods, qs, column_names, savefig=True)