-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_reward.py
More file actions
645 lines (520 loc) · 24.2 KB
/
Copy pathplot_reward.py
File metadata and controls
645 lines (520 loc) · 24.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
import os
import glob
import argparse
from collections import OrderedDict
from copy import deepcopy
import seaborn as sns
import csv
import pickle
def save_plot_data_to_csv(master, output_dir='./log/plots/plot_data/'):
os.makedirs(output_dir, exist_ok=True)
for exp_name, data in master.items():
filename = f"{output_dir}/{exp_name.replace(' ', '_')}_curve.csv"
with open(filename, mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Iteration', 'MeanReturn', 'ConfidenceInterval'])
for x, y, cfi in zip(data['xdata'], data['ydata'], data['ydata_cfi']):
writer.writerow([x, y, cfi])
def export_cumulative_to_csv(master, cumulative_return, cumulative_cfi, xdata, output_path='log/plots/summed_return.csv'):
import csv
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Iteration', 'TotalAverageReturn', 'TotalConfidenceInterval'])
for epoch, avg, cfi in zip(xdata, cumulative_return, cumulative_cfi):
writer.writerow([epoch, avg, cfi])
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
def cfi_delta(data, conf_int_param=0.95): # confidence interval
mean = np.mean(data, axis=1)
if data.ndim == 1:
std_error_of_mean = st.sem(data, axis=1)
lb, ub = st.t.interval(conf_int_param, df=len(data)-1, loc=mean, scale=std_error_of_mean)
cfi_delta = ub - mean
elif data.ndim == 2:
std_error_of_mean = st.sem(data, axis=1)
#print(std_error_of_mean)
lb, ub = st.t.interval(conf_int_param, df=data.shape[0]-1, loc=mean, scale=std_error_of_mean)
cfi_delta = ub - mean
cfi_delta[np.isnan(cfi_delta)] = 0.
else:
raise ValueError('`data` with > 2 dim not expected. Expect either a 1 or 2 dimensional tensor.')
return cfi_delta
def plot(master, title='', xaxis_label='Iteration', yaxis_label='Return'):
#fig = plt.figure(figsize=(25, 6)) # For wide graph
fig = plt.figure(figsize=(30, 6))
ax = fig.subplots()
ax.set_xlabel(xaxis_label)
ax.xaxis.label.set_fontsize(35) # Originally 30
ax.set_ylabel(yaxis_label)
ax.yaxis.label.set_fontsize(35) # Originally 30
ax.set_ylim(0, 1.0)
# axis ticks
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
ax.tick_params(axis='both', which='major', labelsize=35)
# remove right and top spines
#ax.spines['right'].set_visible(False)
#ax.spines['top'].set_visible(False)
# set left and bottom spines at (0, 0) co-ordinate
#ax.spines['left'].set_position(('data', 0.0))
#ax.spines['right'].set_position(('data', 0.0))
# draw dark line at the (0, 0) co-ordinate
#ax.axhline(y=-0.1, color='k')
#ax.axvline(x=0, color='k')
# set grid lines
ax.grid(True, which='both')
for method_name, result_dict in master.items():
xdata = result_dict['xdata']
ydata = result_dict['ydata']
cfi = result_dict['ydata_cfi']
plot_colour = result_dict['plot_colour']
ax.plot(xdata, ydata, linewidth=3, label=method_name, alpha=0.5)
ax.fill_between(xdata, ydata - cfi, ydata + cfi, alpha=0.2)
# legend
ax.legend(loc='lower center', prop={'size': 25}, ncols=7, bbox_to_anchor=(0.5, -0.51))
return fig
def staircase_transform(master, task_len=100):
"""
Transform per-iteration returns into a staircase curve by offsetting each task
segment by the cumulative final performance of all previous tasks.
"""
master_stair = deepcopy(master)
for method_name, result_dict in master.items():
y = np.asarray(result_dict['ydata'])
if y.size == 0:
continue
y_stair = y.copy()
n = len(y)
n_tasks = (n + task_len - 1) // task_len
final_values = []
for t in range(n_tasks):
end_idx = min((t + 1) * task_len, n) - 1
final_values.append(y[end_idx])
offsets = np.concatenate(([0.0], np.cumsum(final_values[:-1])))
for t in range(n_tasks):
start = t * task_len
end = min((t + 1) * task_len, n)
y_stair[start:end] = y[start:end] + offsets[t]
master_stair[method_name]['ydata'] = y_stair
return master_stair
def plot_staircase(master, title='', xaxis_label='Iteration', yaxis_label='Cumulative Task Return'):
fig = plt.figure(figsize=(5, 4))
ax = fig.subplots()
ax.set_xlabel(xaxis_label)
ax.xaxis.label.set_fontsize(25)
ax.set_ylabel(yaxis_label)
ax.yaxis.label.set_fontsize(25)
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
ax.tick_params(axis='both', which='major', labelsize=20)
ax.grid(True, which='both')
max_y = 0.0
for method_name, result_dict in master.items():
xdata = result_dict['xdata']
ydata = result_dict['ydata']
cfi = result_dict['ydata_cfi']
ax.plot(xdata, ydata, linewidth=3, label=method_name, alpha=0.5)
ax.fill_between(xdata, ydata - cfi, ydata + cfi, alpha=0.2)
if len(ydata) > 0:
max_y = max(max_y, np.max(ydata))
if max_y > 0:
ax.set_ylim(0, max_y * 1.05)
ax.legend(loc='lower center', prop={'size': 14}, ncols=7, bbox_to_anchor=(0.5, -0.42))
return fig
def plot_sum(fig, ax, master, title='', xaxis_label='Iteration', yaxis_label='Summed Return'):
"""
This function creates a visualization of the cumulative return (sum of average returns)
across iterations for multiple experiments in the provided data structure.
Args:
master (dict): A dictionary containing data for each experiment.
- Key: Name of the experiment (method_name)
- Value: A dictionary containing:
- xdata (np.array): Iteration numbers.
- ydata (np.array): Average return across seed runs for each iteration.
- ydata_cfi (np.array): Confidence interval for the average return at each iteration.
- plot_colour (str): Color to be used for plotting the experiment's results.
title (str, optional): Title for the plot (defaults to '').
xaxis_label (str, optional): Label for the x-axis (defaults to 'Iteration').
yaxis_label (str, optional): Label for the y-axis (defaults to 'Cumulative Return').
"""
# Initialize cumulative return (zeros for the same shape as one experiment's ydata)
cumulative_return = np.zeros_like(master[list(master.keys())[0]]['ydata'])
cumulative_cfi = np.zeros_like(cumulative_return)
# Create output directory
os.makedirs('./log/plots/summed_returns/', exist_ok=True)
# Loop through experiments and plot individual lines with confidence interval fill
for method_name, result_dict in master.items():
xdata = result_dict['xdata']
ydata = result_dict['ydata']
cfi = result_dict['ydata_cfi']
plot_colour = result_dict['plot_colour']
#ax.plot(xdata, ydata, linewidth=3, label=method_name, alpha=0.5)
#ax.fill_between(xdata, ydata - cfi, ydata + cfi, alpha=0.2, color=plot_colour)
# Add current experiment's average return to cumulative return
cumulative_return += result_dict['ydata']
# Add current experiment's CFI to cumulative CFI (element-wise)
cumulative_cfi += result_dict['ydata_cfi']
output_path = f'./log/plots/summed_returns/{title.replace(" ", "_")}_summed.csv'
with open(output_path, mode='w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Iteration', 'SummedAverageReturn'])
for epoch, value in zip(xdata, cumulative_return):
writer.writerow([epoch, value])
writer.writerow(['max', max(cumulative_return)])
writer.writerow(['min', min(cumulative_return)])
# Compute milestone epochs
max_perf = 14
milestones = {
'first_nonzero_epoch': next((i for i, val in enumerate(cumulative_return) if val > 0), None),
'epoch_25_percent': next((i for i, val in enumerate(cumulative_return) if val >= 0.25 * max_perf), None),
'epoch_50_percent': next((i for i, val in enumerate(cumulative_return) if val >= 0.50 * max_perf), None),
'epoch_75_percent': next((i for i, val in enumerate(cumulative_return) if val >= 0.75 * max_perf), None),
}
# Append milestone data to CSV
writer.writerow(['first_epoch_gt_0', milestones['first_nonzero_epoch']])
writer.writerow(['epoch_25_percent', milestones['epoch_25_percent']])
writer.writerow(['epoch_50_percent', milestones['epoch_50_percent']])
writer.writerow(['epoch_75_percent', milestones['epoch_75_percent']])
# Plot the cumulative return line
ax.plot(xdata, cumulative_return, linewidth=3, label=title) # Adjust color as needed
ax.fill_between(xdata, cumulative_return - cumulative_cfi/2, cumulative_return + cumulative_cfi/2, alpha=0.2) # Adjust color as needed
# Legend
ax.legend(
loc='upper center',
bbox_to_anchor=(0.5, 1.5), # tweak -0.2 to fit under x-axis label
ncol=3,
prop={'size': 25}
)
return fig, ax
def plot_seperate(fig, axs, master, plot_name, title='', xaxis_label='Iteration', yaxis_label='Summed Return'):
for ax, (method_name, result_dict) in zip(axs.flat, master.items()):
xdata = result_dict['xdata']
ydata = result_dict['ydata']
cfi = result_dict['ydata_cfi']
plot_colour = result_dict['plot_colour']
ax.plot(xdata, ydata, linewidth=3, label=plot_name, alpha=0.5)
ax.fill_between(xdata, ydata - cfi, ydata + cfi, alpha=0.2)
ax.set_title(method_name)
ax.grid(True)
#ax.set_ylim(0.0, 1.05)
#ax.set_xlim(-0.05, 200)
#axs.flat[0].legend(
# loc='upper center',
# bbox_to_anchor=(0.5, 2.0), # tweak -0.2 to fit under x-axis label
# ncol=5,
# prop={'size': 15}
#)
supxlabel = fig.supxlabel('Iteration')
supylabel = fig.supylabel('Return')
return fig, axs
def plot_box(fig, ax, master, title='', xaxis_label='Iteration', yaxis_label='Summed Return'):
return
def assess_policy_stability(rewards, window_size=10, threshold_ratio=0.95):
"""
This function assesses the stability of an RL policy based on moving average reward and a threshold ratio of the maximum reward.
Args:
rewards: A list of rewards obtained during training (length 199 in your case).
window_size: The window size for calculating the moving average reward.
threshold_ratio: The threshold ratio of the maximum observed reward for assessing stability.
Returns:
A dictionary containing:
- stable_timestep: The timestep at which the policy is considered stable (None if not found).
- sample_efficiency: None (not calculated in this version).
"""
results = {"stable_timestep": None}
# Calculate maximum reward (assuming all rewards are positive)
max_reward = np.max(rewards)
# Calculate moving average reward
moving_average_rewards = np.convolve(rewards, np.ones(window_size) / window_size, mode='valid')
# Identify potential stable periods
stable_periods = []
threshold_reward = max_reward * threshold_ratio # Dynamic threshold based on max_reward
for i in range(len(moving_average_rewards) - 1):
if moving_average_rewards[i] >= threshold_reward and moving_average_rewards[i + 1] >= threshold_reward:
stable_periods.append((i, i + 1))
# Check if any stable periods exist
if not stable_periods:
return results
# Identify the most recent stable period with the longest duration
longest_stable_period = max(stable_periods, key=lambda p: p[1] - p[0])
results["stable_timestep"] = longest_stable_period[0]
return results
def plot_tra(master, title='', xaxis_label='', yaxis_label=''):
results = {}
names = []
for experiment_name, experiment_data in master.items():
results[experiment_name] = {}
for name, data, in experiment_data.items():
max_reward = np.amax(data['ydata'])
rewards = data['ydata']
for i, reward in enumerate(rewards):
if reward >= 0.8 * max_reward:
max_index = data['xdata'][i]
break
#print(experiment_name, name, max_index)
if max_index == 0: max_index = np.amax(data['xdata'])
names.append(name)
results[experiment_name][name] = {
"max_y" : max_reward,
"x_at_max_y" : max_index,
"max_index_diff" : None
}
x_data = []
y_data = []
names = list(OrderedDict.fromkeys(names))
#names = list(sorted(set(names)))
experiments = list(results.keys())
#print(names)
for name in names:
for exp1 in experiments:
if exp1 == 'Isolated agents':
for exp2 in experiments:
if exp2 == 'C3L':
value1 = results[exp1][name]["x_at_max_y"]
value2 = results[exp2][name]["x_at_max_y"]
diff = value1 - value2
if diff >= 0:
#print(exp1, exp2, name, diff, value1, value2)
results[exp1][name]["max_index_diff"] = diff
y_data.append(diff)
x_data.append(name)
#print(x_data)
#print(y_data)
fig = plt.figure(figsize=(30, 6))
ax = fig.subplots()
ax.bar(x_data, y_data)
# Calculate the y-axis offset for text placement (adjust as needed)
y_offset = 0.1
# Loop through data and add text annotations above each bar
for i, value in enumerate(y_data):
ax.text(x_data[i], value + y_offset, str(value), ha='center', va='bottom', fontsize=12) # Adjust ha, va, and fontsize as needed
ax.set_xlabel("Task")
ax.set_ylabel("Time Reduction Advantage (TRA)")
ax.tick_params(axis='x', rotation=90)
fig.savefig(f'./log/plots/tra.pdf', dpi=256, format='pdf', bbox_inches='tight')
# NEURIPS
neurips_mctgraph = {
'Finetuning (PPO)' : {
'Finetuning (PPO)' : 'raw/ct28-interleaved-PPO/Titerationreward/',
},
'Mask-RI' : {
'Mask-RI' : 'raw/ct28-interleaved-MaskRI/Titerationreward/',
},
'oEWC (MH)' : {
'oEWC (MH)' : 'raw/ct28-interleaved-EWC-MH/Titerationreward/',
},
'SI (MH)' : {
'SI (MH)' : 'raw/ct28-interleaved-SI-MH/Titerationreward/',
},
'CLEAR' : {
'CLEAR' : 'raw/ct28-interleaved-CLEAR/Titerationreward/',
},
'SER' : {
'SER' : 'raw/ct28-interleaved-SER/Titerationreward/',
},
'Mask-LC' : {
'Mask-LC' : 'raw/ct28-interleaved-MaskLC/Titerationreward/',
},
'Mask-BLC' : {
'Mask-BLC' : 'raw/ct28-interleaved-MaskBLC/Titerationreward/',
},
'CL HNet' : {
'CL HNet' : 'raw/ct28-interleaved-CLHNET/Titerationreward/',
},
'PNN' : {
'PNN' : 'raw/ct28-interleaved-PNN/Titerationreward/',
},
'CKA-RL' : {
'CKA-RL' : 'raw/ct28-interleaved-CKA/Titerationreward/',
},
'Mask-SC (Top-3)' : {
'Mask-SC (Top-3)' : 'raw/ct28-interleaved-MaskSC-top-3/Titerationreward/',
},
'Mask-SC (Oracle)' : {
'Mask-SC (Oracle)' : 'raw/ct28-interleaved-MaskSC-top-dense/Titerationreward/',
},
#'Mask-SC (Top-1)' : {
# 'Mask-SC (Top-1)' : 'raw/ct28-interleaved-MaskSC-top-1/Titerationreward/',
#},
#'Mask-SC-5' : {
# 'Mask-SC-5' : 'raw/ct28-interleaved-MaskSC-top-5/Titerationreward/',
#}
}
neurips_mctgraph_grid_search = {
'Mask-SC (Top-1)' : {
'Mask-SC (Top-1)' : 'raw/ct28-interleaved-MaskSC-top-1/Titerationreward/',
},
'Mask-SC (Top-3)' : {
'Mask-SC (Top-3)' : 'raw/ct28-interleaved-MaskSC-top-3/Titerationreward/',
},
'Mask-SC (Top-5)' : {
'Mask-SC (Top-5)' : 'raw/ct28-interleaved-MaskSC-top-5/Titerationreward/',
},
'Mask-SC (Top-7)' : {
'Mask-SC (Top-7)' : 'raw/ct28-interleaved-MaskSC-top-dense/Titerationreward/',
},
}
neurips_mctgraph_causation_ablation = {
'Mask-RI (No Priors)' : {
'Mask-RI (No Priors)' : 'raw/ct28-interleaved-MaskRI/Titerationreward/',
},
'Mask-LC' : {
'Mask-LC' : 'raw/ct28-interleaved-MaskLC/Titerationreward/',
},
'Mask-SC (Top-3)' : {
'Mask-SC (Top-3)' : 'raw/ct28-interleaved-MaskSC-top-3/Titerationreward/',
},
'Mask-SC (Random-3)' : {
'Mask-SC (Random-3)' : 'raw/ct28-interleaved-MaskSC-top-3-random/Titerationreward/',
},
'Mask-SC (Oracle)' : {
'Mask-SC (Oracle)' : 'raw/ct28-interleaved-MaskSC-top-dense/Titerationreward/',
},
}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--plot_name', help='paths to the experiment folder for single'\
'agent lifelong learning (support paths to multiple seeds)', type=str, default=None)
parser.add_argument('--exp_name', help='name of experiment', default='metrics_plot')
parser.add_argument('--num_agents', help='number of agents in the experiment', type=int, nargs='+', default=1)
parser.add_argument('--interval', help='interval', type=int, default=1)
parser.add_argument('--staircase_from_pickle', help='path to master*.pkl for staircase plot', type=str, default=None)
parser.add_argument('--task_len', help='number of iterations per task for staircase plot', type=int, default=100)
parser.add_argument('--staircase_out', help='output path for staircase plot pdf', type=str, default=None)
args = parser.parse_args()
if args.staircase_from_pickle:
with open(args.staircase_from_pickle, 'rb') as f:
master = pickle.load(f)
master_stair = staircase_transform(master, task_len=args.task_len)
fig = plot_staircase(master_stair, yaxis_label='Cumulative Task Return')
if args.staircase_out:
output_path = args.staircase_out
else:
base = os.path.splitext(os.path.basename(args.staircase_from_pickle))[0]
output_path = f'./log/plots/staircase_{base}.pdf'
os.makedirs(os.path.dirname(output_path) or '.', exist_ok=True)
fig.savefig(output_path, dpi=256, format='pdf', bbox_inches='tight')
print(f'Saved staircase plot to {output_path}')
raise SystemExit(0)
MYPATHS = neurips_mctgraph_causation_ablation
fig2 = plt.figure(figsize=(15, 6.5), constrained_layout=True)
ax2 = fig2.subplots()
# Set up axis labels, fonts, and limits
ax2.set_xlabel('Iteration')
ax2.xaxis.label.set_fontsize(29)
ax2.set_ylabel('Summed Return')
ax2.yaxis.label.set_fontsize(29)
ax2.set_ylim(-0.5, 28.0)
ax2.set_xlim(-1.0, 200.0)
# Axis ticks and grid
ax2.xaxis.tick_bottom()
ax2.yaxis.tick_left()
ax2.tick_params(axis='both', which='major', labelsize=25)
ax2.grid(True, which='both')
# Remove right and top spines
#ax2.spines['right'].set_visible(False)
#ax2.spines['top'].set_visible(False)
# Set left and bottom spines at (0, 0) co-ordinate
#ax2.spines['left'].set_position(('data', 0.0))
#ax2.spines['bottom'].set_position(('data', 0.0))
# Draw dark lines at (0, 0)
#ax2.axhline(y=0, color='k')
#ax2.axvline(x=0, color='k')
fig3 = deepcopy(fig2)
ax3 = deepcopy(ax2)
master = {}
master2 = {}
# Store data for box plot
boxplot_data = []
boxplot_labels = []
#interval_steps = [0, 25, 50, 75, 100, 125, 150, 175, 199] # Example intervals for box plots
#interval_steps = [0, 50, 100, 150, 199]
interval_steps = list(range(0, 200, 1))
fig4, axs4 = plt.subplots(7, 2, figsize=(10, 12.5), layout='constrained')
for plot_name, paths in MYPATHS.items():
print('NAMES:', plot_name, 'PATHS:', paths)
master2[plot_name] = {}
for name, path in paths.items():
print(path)
data = pd.DataFrame()
experiment_summed_rewards = []
for i, filepath in enumerate(sorted(glob.glob(f'{path}*.csv'))):
# Load data into a pandas dataframe
df = pd.read_csv(filepath)
# Select data from second column for each seed run
data.loc[:, i] = df['Value']
print(data)
master[name] = {}
master[name]['xdata'] = np.arange(data.shape[0])
master[name]['ydata'] = np.mean(data, axis=1)
master[name]['ydata_cfi'] = cfi_delta(data)
master[name]['plot_colour'] = 'green'
master2[plot_name][name] = {}
master2[plot_name][name]['xdata'] = np.arange(data.shape[0])
master2[plot_name][name]['ydata'] = np.mean(data, axis=1)
master2[plot_name][name]['ydata_cfi'] = cfi_delta(data)
master2[plot_name][name]['plot_colour'] = 'green'
# For each seed, calculate the sum of rewards at the specified interval steps
for seed in data.columns:
# Get the rewards for the specific seed
seed_data = data[seed]
# Sum the rewards at the specified interval steps for this seed
values = []
for step in interval_steps:
if step < len(seed_data):
#print(seed_data[step])
values.append(seed_data[step])
summed_seed_reward = np.average(values)
#summed_seed_reward = np.average([seed_data[step] for step in interval_steps if step < len(seed_data)])
# Append the summed reward for this seed and task to the overall experiment rewards
experiment_summed_rewards.append(summed_seed_reward)
# Once all tasks in the experiment are summed, append the data for boxplot
boxplot_data.extend(experiment_summed_rewards) # Add all the summed rewards for this experiment
boxplot_labels.extend([plot_name] * len(experiment_summed_rewards)) # Label with the experiment name
"""
for step in interval_steps:
if step < len(master[name]['ydata']): # Ensure the step is within bounds
boxplot_data.append(master[name]['ydata'][step]) # Summed reward at the step
boxplot_labels.append(f'{name[0]}{name[-1]}_step_{step}') # Label for the box plot"""
if not os.path.exists('./log/plots/'): os.makedirs('./log/plots/')
fig1 = plot(master, yaxis_label='Return')
fig1.savefig(f'./log/plots/{plot_name}.pdf', dpi=256, format='pdf', bbox_inches='tight')
fig2, ax2 = plot_sum(fig2, ax2, master, title=plot_name, yaxis_label='Instant Cumulative Return')
fig4, axs4 = plot_seperate(fig4, axs4, master, plot_name)
#save_plot_data_to_csv(master)
with open(f'./log/plots/master{plot_name}.pkl', 'wb') as f:
pickle.dump(master, f)
#fig3, ax3 = plot_box(fig3, ax3, master, title=plot_name, yaxis_label='Summed Return')
print(len(boxplot_data))
# Convert boxplot_data to the format required by seaborn
boxplot_data_df = pd.DataFrame({
"Average Reward": boxplot_data,
"Experiment": boxplot_labels
})
def remove_duplicates(original_list):
unique_list = []
for item in original_list:
if item not in unique_list:
unique_list.append(item)
return unique_list
botplot_ticks = remove_duplicates(boxplot_labels)
# Create a new figure for box plots
fig_box, ax_box = plt.subplots(figsize=(8, 6))
#plt.boxplot(boxplot_data)
sns.boxplot(x="Experiment", y="Average Reward", data=boxplot_data_df, ax=ax_box)
ax_box.set_xticklabels(botplot_ticks, rotation=45, ha='right')
ax_box.set_xlabel('Experiment')
ax_box.set_ylabel('Reward distrubtion across all tasks')
# Save figures
fig_box.savefig('./log/plots/boxplot_comparison.pdf', dpi=256, bbox_inches='tight') # Save the figure
fig2.savefig(f'./log/plots/cumulative.pdf', dpi=256, format='pdf', bbox_inches='tight')
fig4.savefig(f'./log/plots/individual.pdf', dpi=256, format='pdf', bbox_inches='tight')
with open("./log/plots/cumulative.pkl", "wb") as f:
pickle.dump(fig2, f)
# Plot TRA metric
plot_tra(master2)