-
Notifications
You must be signed in to change notification settings - Fork 1
/
thesis_subplotter.py
133 lines (114 loc) · 4.98 KB
/
thesis_subplotter.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
import os
import pickle
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rcParams
markers = [
'o', 'p', 'v', 's', '*'
]
SCALE = 4.5
# SCALE = 2.5
FONT_SIZE = 20
LINE_STYLE = '-'
MARKER_SIZE = 10
TICK_SIZE = 16
def subplotall(args):
def define_style(rcParams):
rcParams['figure.figsize'] = SCALE*4, SCALE*1.7
rcParams['figure.subplot.wspace'] = 0.2
rcParams['figure.subplot.hspace'] = 0.15
rcParams['lines.markersize'] = MARKER_SIZE
rcParams['xtick.labelsize'] = TICK_SIZE
rcParams['ytick.labelsize'] = TICK_SIZE
rcParams['axes.labelsize'] = FONT_SIZE
rcParams['axes.titlesize'] = FONT_SIZE
rcParams['legend.fontsize'] = FONT_SIZE-4
rcParams['font.sans-serif'] = 'Microsoft YaHei'
rcParams['axes.grid'] = True
define_style(rcParams)
# plt.style.use('seaborn')
# plt.rc('axes', labelsize=20, titlesize=20)
# plt.rc('xtick', labelsize=15)
# plt.rc('ytick', labelsize=15)
# plt.rc('legend', fontsize=19)
linestyles = ('d--', 'o--', 'd-', 'o-', '*--')
fig, ax_matrix = plt.subplots(2, 4)
# fig.subplots_adjust(left=0.05, right=0.98,top=0.95, bottom=0.16, hspace=0.3,wspace=0.31)
for i in range(len(args.lams)):
sbe_name = 'sbe'+'_'+args.network+'_a'+str(args.attack)+'_lam'+str(args.lams[i])+'.pkl'
ace_name = 'ce'+'_'+args.network+'_a'+str(args.attack)+'_lam'+str(args.lams[i])+'.pkl'
file_dir = os.path.dirname(os.path.abspath(__file__))
dir_png_path = os.path.join(file_dir, 'record')
sbe_path = os.path.join(dir_png_path, sbe_name)
ace_path = os.path.join(dir_png_path, ace_name)
f = open(sbe_path, 'rb')
asbe = np.array(pickle.load(f))
f.close()
f = open(ace_path, 'rb')
ace = np.array(pickle.load(f))
f.close()
# fig.add_subplot(2, len(args.lams), i+1, xlabel='Step', ylabel='MSBE', title=r'$\lambda=$%.1g' %args.lams[i])
axes = ax_matrix[0][i]
for k in range(np.shape(asbe)[-1]):
y = asbe[:,:,k]
y_mean = np.mean(y, 0)
x = np.arange(len(y_mean))
axes.set_title(r'$\lambda=$%.1g' %args.lams[i])
axes.plot(x, y_mean, linestyles[k], linewidth=2.25, markevery=30)
plt.ticklabel_format(axis='y', style='sci', scilimits=(-2,2))
# if args.lnk:
# fig.add_subplot(2, len(args.lams), i+1+len(args.lams), xlabel='Step', ylabel=r'MCE$\times k/\ln(k)$')
# else:
# fig.add_subplot(2, len(args.lams), i+1+len(args.lams), xlabel='Step', ylabel='MCE')
axes = ax_matrix[1][i]
for k in range(np.shape(ace)[-1]):
y = ace[:,:,k]
y_mean = np.mean(y, 0)
try:
if args.lnk:
for j in np.arange(1,len(y_mean)):
y_mean[j] = y_mean[j]*(j+1)/np.log(j+1)
except:
pass
x = np.arange(len(y_mean))
axes.plot(x, y_mean, linestyles[k], linewidth=2.5, markevery=30)
axes.set_yscale('log')
axes.set_xlabel('迭代次数')
# plt.ticklabel_format(axis='y', style='sci', scilimits=(-2,2))
ax_matrix[0][0].set_ylabel('MSBE')
ax_matrix[1][0].set_ylabel('一致误差')
fig.legend(
(r'加权平均(无攻击)',
r'截尾均值(无攻击)',
r'加权平均(有攻击)',
r'截尾均值(有攻击)',
r'无通信'),
loc='lower left', bbox_to_anchor=(0.12, -0.05), fancybox=False, shadow=False,
ncol=5, borderaxespad=0., frameon=True)
# fig.tight_layout()
pic_name = 'all_'+args.network+'_a'+str(args.attack)
file_dir = os.path.dirname(os.path.abspath(__file__))
dir_png_path = os.path.join(file_dir, 'thesis-figure', 'png')
dir_pdf_path = os.path.join(file_dir, 'thesis-figure', 'pdf')
if not os.path.isdir(dir_pdf_path):
os.makedirs(dir_pdf_path)
if not os.path.isdir(dir_png_path):
os.makedirs(dir_png_path)
pic_png_path = os.path.join(dir_png_path, pic_name + '.png')
pic_pdf_path = os.path.join(dir_pdf_path, pic_name + '.pdf')
plt.savefig(pic_png_path, format='png', bbox_inches='tight')
plt.savefig(pic_pdf_path, format='pdf', bbox_inches='tight')
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Plotter for robust TD')
parser.add_argument('--network', type=str, default='complete',
help='name of the network (h1b1, h3b1, b3b2, renyi, complete)')
parser.add_argument('--attack', type=int, default=2,
help='Type of attack')
parser.add_argument('--lams', type=float, nargs='+',
help='lambda list', default=[0.0,0.3,0.6,0.9])
parser.add_argument('--lnk', action='store_true',
help='whether to divide aggregate ce by ln(epoch) or epoch')
args = parser.parse_args()
subplotall(args)