-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_comparison.py
executable file
·1304 lines (1168 loc) · 48.5 KB
/
main_comparison.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
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import os
import traceback
from functools import partial
from typing import Dict, List
import jax
import jax.numpy as jnp
import matplotlib as mpl
import pandas as pd
from qdax.core.containers.mapelites_repertoire import MapElitesRepertoire
from analysis.archives import plot_env_paper_archives
from analysis.p_value import p_values
from analysis.summary_plots import (
pareto_categories,
pareto_plot,
summary_loss_allenv,
summary_loss_categories_allenv,
)
from analysis.utils import get_folder_name, sort_data, uniformise_xaxis
from analysis.visualisation import plot_visualisation_archive, save_html
from set_up_environment import ENV_CONTROL, ENV_OPTIMISATION, set_up_environment
def compute_loss(
name: str,
data: pd.DataFrame,
losses: Dict[str, List[float]],
prefixe: str = "",
) -> Dict[str, List[float]]:
max_eval = max(data["eval"])
original = data[data["eval"] == max_eval][prefixe + name].values[0]
losses[prefixe + name] = [original]
average = data[data["eval"] == max_eval][prefixe + "reeval_" + name].values[0]
fit_average = data[data["eval"] == max_eval][prefixe + "fit_reeval_" + name].values[
0
]
desc_average = data[data["eval"] == max_eval][
prefixe + "desc_reeval_" + name
].values[0]
losses[prefixe + "reeval_" + name] = [average]
losses[prefixe + "fit_reeval_" + name] = [fit_average]
losses[prefixe + "desc_reeval_" + name] = [desc_average]
if original == 0:
if average == 0:
losses["loss_" + prefixe + "reeval_" + name] = [0]
else:
losses["loss_" + prefixe + "reeval_" + name] = [100]
if fit_average == 0:
losses["loss_" + prefixe + "fit_reeval_" + name] = [0]
else:
losses["loss_" + prefixe + "fit_reeval_" + name] = [100]
if desc_average == 0:
losses["loss_" + prefixe + "desc_reeval_" + name] = [0]
else:
losses["loss_" + prefixe + "desc_reeval_" + name] = [100]
else:
losses["loss_" + prefixe + "reeval_" + name] = [
(original - average) / original * 100
]
losses["loss_" + prefixe + "fit_reeval_" + name] = [
(original - fit_average) / original * 100
]
losses["loss_" + prefixe + "desc_reeval_" + name] = [
(original - desc_average) / original * 100
]
return losses
def compute_time(
data: pd.DataFrame,
env_name: str,
algo: str,
size: int,
times: Dict[str, List[float]],
pourcent: float = 0.95,
prefixe: str = "",
pourcent_value: bool = False,
) -> Dict[str, List[float]]:
"""
Return time to first stricly reach pourcent % of final QD-Score value for one
replication of one given algorithm on one given task.
Args:
data: dataframe for one replication of one algo to extract value from.
times: time dictionary to complement for this replication
poucent: pourcent use
prefixe: if qd_score column name requires a prefixe
Returns:
new complemented time dictionary
"""
# Get final value of QD-Score
max_eval = max(data["eval"])
final_line = data[data["eval"] == max_eval]
final_value = final_line[prefixe + "qd_score"].values[0]
# Finding pourcent % value
pourcent_value = (
(pourcent * final_value)
if (final_value > 0)
else (2.0 - pourcent * final_value)
)
if pourcent == 1:
pourcent_epoch = max(data["epoch"].values)
else:
min_epoch = data["epoch"].drop_duplicates().nsmallest(2).iloc[-1]
pourcent_epoch = max(
min_epoch,
min(data[data[prefixe + "qd_score"] > pourcent_value]["epoch"].values),
)
pourcent_line = data[data["epoch"] == pourcent_epoch]
# Finding corresponding eval, gen, time and values
times[prefixe + "epoch"] = pourcent_epoch
times[prefixe + "eval"] = pourcent_line["eval"].values[0]
times[prefixe + "time"] = pourcent_line["time"].values[0]
if pourcent_value:
times[prefixe + "qd_score"] = pourcent_line[prefixe + "qd_score"].values[0]
times[prefixe + "coverage"] = pourcent_line[prefixe + "coverage"].values[0]
times[prefixe + "max_fitness"] = pourcent_line[prefixe + "max_fitness"].values[
0
]
times[prefixe + "reeval_qd_score"] = pourcent_line[
prefixe + "reeval_qd_score"
].values[0]
times[prefixe + "reeval_coverage"] = pourcent_line[
prefixe + "reeval_coverage"
].values[0]
times[prefixe + "reeval_max_fitness"] = pourcent_line[
prefixe + "reeval_max_fitness"
].values[0]
else:
times[prefixe + "qd_score"] = final_line[prefixe + "qd_score"].values[0]
times[prefixe + "coverage"] = final_line[prefixe + "coverage"].values[0]
times[prefixe + "max_fitness"] = final_line[prefixe + "max_fitness"].values[0]
times[prefixe + "reeval_qd_score"] = final_line[
prefixe + "reeval_qd_score"
].values[0]
times[prefixe + "reeval_coverage"] = final_line[
prefixe + "reeval_coverage"
].values[0]
times[prefixe + "reeval_max_fitness"] = final_line[
prefixe + "reeval_max_fitness"
].values[0]
return times
def compute_var(
data: pd.DataFrame,
var: Dict[str, List[float]],
num_centroids: int,
prefixe: str = "",
) -> Dict[str, List[float]]:
"""
Return final variance value for one replication of one given algorithm on
one given task.
Args:
data: dataframe for one replication of one algo to extract value from.
var: var dictionary to complement for this replication
num_centroids: used to compute average variance
prefixe: if qd_score columns name requires a prefixe
Returns:
new complemented var dictionary
"""
# Get final value of Variances
max_eval = max(data["eval"])
final_fit_var_value = data[data["eval"] == max_eval][
prefixe + "fit_var_qd_score"
].values[0]
final_desc_var_value = data[data["eval"] == max_eval][
prefixe + "desc_var_qd_score"
].values[0]
coverage = data[data["eval"] == max_eval][prefixe + "coverage"].values[0]
average_fit_var_value = final_fit_var_value / (coverage / 100 * num_centroids)
average_desc_var_value = final_desc_var_value / (coverage / 100 * num_centroids)
# Fill in Dict
var[prefixe + "fit_var_qd_score"] = final_fit_var_value
var[prefixe + "desc_var_qd_score"] = final_desc_var_value
var[prefixe + "avg_fit_var_qd_score"] = average_fit_var_value
var[prefixe + "avg_desc_var_qd_score"] = average_desc_var_value
return var
#############################################################
#########
# Input #
parser = argparse.ArgumentParser()
# Folder
parser.add_argument("--results", default="results", type=str)
parser.add_argument("--plots", default="plots", type=str)
# Analysis configuration
parser.add_argument("--paper-plot", action="store_true")
parser.add_argument("--algos", default="", type=str)
parser.add_argument("--excludes", default="", type=str)
parser.add_argument("--no-traceback", action="store_true")
# Metrics
parser.add_argument("--plot-paper-archives", action="store_true")
parser.add_argument("--plot-summary", action="store_true")
parser.add_argument("--plot-pareto", action="store_true")
parser.add_argument("--plot-p-values", action="store_true")
# Metrics parameters
parser.add_argument("--compare-batch-size", action="store_true")
parser.add_argument("--time-pourcent", default=0.95, type=float)
parser.add_argument("--pourcent-value", action="store_true")
parser.add_argument("--generations", action="store_true")
parser.add_argument("--time", action="store_true")
parser.add_argument("--metrics-legend-columns", default=2, type=int)
parser.add_argument("--summary-legend-columns", default=4, type=int)
parser.add_argument("--pareto-legend-columns", default=3, type=int)
parser.add_argument("--category-plots", action="store_true")
parser.add_argument("--paper-archives-prefixe", default="", type=str)
# Visualisation
parser.add_argument("--visualisation", action="store_true")
parser.add_argument("--save-html", action="store_true")
parser.add_argument("--deterministic", action="store_true")
parser.add_argument("--best-indiv", action="store_true")
parser.add_argument("--indiv", default=0, type=int)
parser.add_argument("--replications", default=256, type=int)
# Process inputs
args = parser.parse_args()
save_folder = args.results
plot_folder = args.plots
plot_algos = args.algos.rstrip().split("|")
exclude_algos = args.excludes.rstrip().split("|")
compare_size = "batch_size" if args.compare_batch_size else "sampling_size"
compare_title = "Batch-size" if args.compare_batch_size else "Sampling-size"
assert os.path.exists(save_folder), "\n!!!ERROR!!! Empty result folder.\n"
# Create results folder if needed
try:
if not os.path.exists(plot_folder):
os.mkdir(plot_folder)
except Exception:
if not args.no_traceback:
print("\n!!!WARNING!!! Cannot create folders for plots.")
traceback.print_exc()
################
# Find results #
# If not, open all config files in the folder
else:
print("\n\nOpening config files")
folders = [
root
for root, dirs, files in os.walk(save_folder)
for name in files
if "config.csv" in name
]
assert len(folders) > 0, "\n!!!ERROR!!! No config files in result folder.\n"
config_frame = pd.DataFrame()
for folder in folders:
config_file = os.path.join(folder, "config.csv")
sub_config_frame = pd.read_csv(config_file, index_col=False)
sub_config_frame["folder"] = folder
config_frame = pd.concat([config_frame, sub_config_frame], ignore_index=True)
assert (
config_frame.shape[0] != 0
), "\n!!!ERROR!!! No runs refered in config files.\n"
print(" Found", config_frame.shape[0], "runs:")
print(config_frame["run"].drop_duplicates().reset_index(drop=True))
##################
# Filter results #
# Filter algos that does not need to be ploted
if plot_algos != [""]:
config_frame = config_frame[config_frame["run"].isin(plot_algos)]
if exclude_algos != [""]:
config_frame = config_frame[
~config_frame["run"].str.contains("|".join(exclude_algos))
]
config_frame = config_frame.reset_index(drop=True)
print("\n After filtering, left with", config_frame.shape[0], "runs:")
print(config_frame["run"].drop_duplicates().reset_index(drop=True))
assert config_frame.shape[0] != 0, "\n!!!ERROR!!! No algos left to plot.\n"
################
# Name results #
print("\nSetting up algorithms names")
use_in_name = []
not_name = [
"folder",
"run",
"seed",
"env",
"num_iterations",
"batch_size",
"sampling_size",
"sampling_use",
"num_reevals",
"metrics_file",
"in_cell_metrics_file",
"repertoire_folder",
"reeval_repertoire_folder",
"fit_reeval_repertoire_folder",
"desc_reeval_repertoire_folder",
"fit_var_repertoire_folder",
"desc_var_repertoire_folder",
"in_cell_reeval_repertoire_folder",
"in_cell_fit_reeval_repertoire_folder",
"in_cell_desc_reeval_repertoire_folder",
"in_cell_fit_var_repertoire_folder",
"in_cell_desc_var_repertoire_folder",
"min_bd",
"max_bd",
"depth",
"num_samples",
"episode_length",
]
for column in config_frame.columns:
if column not in not_name:
if (config_frame[column] != config_frame[column][0]).any():
use_in_name.append(column)
print("\n Differences between runs:", use_in_name)
# Add algo name to each line
algos = []
algos_batch = []
for line in range(config_frame.shape[0]):
algo = config_frame["run"][line]
if "Deep-Grid" in algo and "sampling" in algo:
algo = algo.replace("-sampling-", " smpl ")
algo = algo.replace("Deep-Grid", "Deep-Grid-sampling")
algo = algo.replace("-depth-", " ")
algo = algo.replace("-archive-out-sampling-", "-out-smpl")
for name in use_in_name:
algo += " " + name + ":" + str(config_frame[name][line])
algo_batch = algo + " - " + str(config_frame[compare_size][line])
algos.append(algo)
algos_batch.append(algo_batch)
config_frame["algo"] = algos
config_frame["algo_batch"] = algos_batch
print("\n Get final names for graphs:")
print(config_frame["algo"].drop_duplicates())
config_frame = config_frame.reset_index(drop=True)
#################################
# Read progress and loss graphs #
try:
if not args.plot_summary and not args.plot_pareto and not args.plot_p_values:
error = False
assert 0
error = True
print("\nReading metrics data")
# Create the dataframe with maximum number of gens
max_gen_frame = pd.DataFrame(columns=["env", "epoch"])
# Create the replication dataframe
replications_frame = pd.DataFrame(
columns=["env", "num_reevals", "algo", compare_size, "num_rep"]
)
for env in config_frame["env"].drop_duplicates().values:
for num_reevals in config_frame["num_reevals"].drop_duplicates().values:
for size in config_frame[compare_size].drop_duplicates().values:
for algo in config_frame["algo"].drop_duplicates().values:
replications_frame = pd.concat(
[
replications_frame,
pd.DataFrame.from_dict(
{
"env": [env],
"num_reevals": [num_reevals],
"algo": [algo],
compare_size: [size],
"num_rep": [0],
}
),
],
ignore_index=True,
)
# Go through all metrics files to get the max number of epoch and replications first
for line in range(config_frame.shape[0]):
folder = config_frame["folder"][line]
metrics_file = config_frame["metrics_file"][line]
in_cell_metrics_file = config_frame["in_cell_metrics_file"][line]
metrics_file = metrics_file[metrics_file.rfind("/") + 1 :]
in_cell_metrics_file = in_cell_metrics_file[
in_cell_metrics_file.rfind("/") + 1 :
]
metrics_file = os.path.join(folder, metrics_file)
in_cell_metrics_file = os.path.join(folder, in_cell_metrics_file)
try:
data = pd.read_csv(metrics_file, index_col=False)
env = config_frame["env"][line]
num_reevals = config_frame["num_reevals"][line] # 0
size = config_frame[compare_size][line]
algo = config_frame["algo"][line]
# Get the maximum number of generations for this line
if env in max_gen_frame["env"].values:
max_gen = min(
data["epoch"].max(),
max_gen_frame[max_gen_frame["env"] == env]["epoch"].values[0],
)
max_gen_frame.loc[max_gen_frame["env"] == env, "epoch"] = max_gen
else:
max_gen = data["epoch"].max()
max_gen_frame = pd.concat(
[
max_gen_frame,
pd.DataFrame.from_dict({"env": [env], "epoch": [max_gen]}),
],
ignore_index=True,
)
# Add replication to frame
replications_frame.loc[
(replications_frame["env"] == env)
& (replications_frame["num_reevals"] == num_reevals)
& (replications_frame["algo"] == algo)
& (replications_frame[compare_size] == size),
"num_rep",
] += 1
except Exception:
if not args.no_traceback:
print("\n!!!WARNING!!! Cannot read", metrics_file, ".")
traceback.print_exc()
print("\nMax epoch for each environment:")
print(max_gen_frame)
print("\n")
# Remove empty replications from replications_frame
replications_frame = replications_frame[replications_frame["num_rep"] != 0]
# Save replications frame as csv
replications_frame = replications_frame.sort_values(
["env", "num_reevals"], ignore_index=True
)
replications_frame = sort_data(replications_frame, ["algo", compare_size])
print("\nReplications:")
print(replications_frame)
print("\n")
replications_frame.to_csv(
f"{plot_folder}/replications_frame.csv",
index=None,
sep=",",
)
# Create the metrics dataframe
all_data = pd.DataFrame()
all_losses = pd.DataFrame()
all_times = pd.DataFrame()
all_var = pd.DataFrame()
# Go through all metrics files
rep = 0
print(config_frame)
for line in range(config_frame.shape[0]):
folder = config_frame["folder"][line]
metrics_file = config_frame["metrics_file"][line]
in_cell_metrics_file = config_frame["in_cell_metrics_file"][line]
metrics_file = metrics_file[metrics_file.rfind("/") + 1 :]
in_cell_metrics_file = in_cell_metrics_file[
in_cell_metrics_file.rfind("/") + 1 :
]
metrics_file = os.path.join(folder, metrics_file)
in_cell_metrics_file = os.path.join(folder, in_cell_metrics_file)
try:
data = pd.read_csv(metrics_file, index_col=False)
# Add corresponding in_cell metrics
in_cell_data = pd.read_csv(in_cell_metrics_file, index_col=False)
data["in_cell_qd_score"] = in_cell_data["in_cell_qd_score"]
data["in_cell_coverage"] = in_cell_data["in_cell_coverage"]
data["in_cell_max_fitness"] = in_cell_data["in_cell_max_fitness"]
data["in_cell_reeval_qd_score"] = in_cell_data["in_cell_reeval_qd_score"]
data["in_cell_reeval_coverage"] = in_cell_data["in_cell_reeval_coverage"]
data["in_cell_reeval_max_fitness"] = in_cell_data[
"in_cell_reeval_max_fitness"
]
data["in_cell_fit_reeval_qd_score"] = in_cell_data[
"in_cell_fit_reeval_qd_score"
]
data["in_cell_fit_reeval_coverage"] = in_cell_data[
"in_cell_fit_reeval_coverage"
]
data["in_cell_fit_reeval_max_fitness"] = in_cell_data[
"in_cell_fit_reeval_max_fitness"
]
data["in_cell_desc_reeval_qd_score"] = in_cell_data[
"in_cell_desc_reeval_qd_score"
]
data["in_cell_desc_reeval_coverage"] = in_cell_data[
"in_cell_desc_reeval_coverage"
]
data["in_cell_desc_reeval_max_fitness"] = in_cell_data[
"in_cell_desc_reeval_max_fitness"
]
data["in_cell_fit_var_qd_score"] = in_cell_data["in_cell_fit_var_qd_score"]
data["in_cell_fit_var_coverage"] = in_cell_data["in_cell_fit_var_coverage"]
data["in_cell_fit_var_max_fitness"] = in_cell_data[
"in_cell_fit_var_max_fitness"
]
data["in_cell_desc_var_qd_score"] = in_cell_data[
"in_cell_desc_var_qd_score"
]
data["in_cell_desc_var_coverage"] = in_cell_data[
"in_cell_desc_var_coverage"
]
data["in_cell_desc_var_max_fitness"] = in_cell_data[
"in_cell_desc_var_max_fitness"
]
# Filter datas after max_gen
env = config_frame["env"][line]
num_reevals = config_frame["num_reevals"][line] # 0
size = config_frame[compare_size][line]
algo = config_frame["algo"][line]
algo_batch = config_frame["algo_batch"][line]
data = data[
data["epoch"]
<= max_gen_frame[max_gen_frame["env"] == env]["epoch"].values[0]
]
# Add losses to frames
losses: Dict[str, List[float]] = {}
losses = compute_loss("qd_score", data, losses)
losses = compute_loss("coverage", data, losses)
losses = compute_loss("max_fitness", data, losses)
losses = compute_loss("qd_score", data, losses, prefixe="in_cell_")
losses = compute_loss("coverage", data, losses, prefixe="in_cell_")
losses = compute_loss("max_fitness", data, losses, prefixe="in_cell_")
# Add time to pourcent % convergence to frames
times: Dict[str, List[float]] = {}
times = compute_time(
data,
env,
algo,
size,
times,
pourcent=args.time_pourcent,
pourcent_value=args.pourcent_value,
)
times = compute_time(
data,
env,
algo,
size,
times,
pourcent=args.time_pourcent,
prefixe="in_cell_",
pourcent_value=args.pourcent_value,
)
# Add final variances to frames
num_centroids = config_frame["num_centroids"][line]
var: Dict[str, List[float]] = {}
var = compute_var(data, var, num_centroids)
var = compute_var(data, var, num_centroids, prefixe="in_cell_")
# Add run info to frames
data["algo"] = algo
data["algo_batch"] = algo_batch
losses["algo"] = [algo]
losses["algo_batch"] = [algo_batch]
times["algo"] = [algo]
times["algo_batch"] = [algo_batch]
var["algo"] = [algo]
var["algo_batch"] = [algo_batch]
data["env"] = env
losses["env"] = [env]
times["env"] = [env]
var["env"] = [env]
data["num_reevals"] = num_reevals
losses["num_reevals"] = [num_reevals]
times["num_reevals"] = [num_reevals]
var["num_reevals"] = [num_reevals]
data[compare_size] = size
losses[compare_size] = [size]
times[compare_size] = [size]
var[compare_size] = [size]
data["rep"] = rep
losses["rep"] = [rep]
times["rep"] = [rep]
var["rep"] = [rep]
# Concatenate all frames to existing ones
all_data = pd.concat([all_data, data], ignore_index=True)
all_losses = pd.concat(
[all_losses, pd.DataFrame.from_dict(losses)], ignore_index=True
)
all_times = pd.concat(
[all_times, pd.DataFrame.from_dict(times)], ignore_index=True
)
all_var = pd.concat(
[all_var, pd.DataFrame.from_dict(var)], ignore_index=True
)
rep += 1
except Exception:
if not args.no_traceback:
print("\n!!!WARNING!!! Cannot read", metrics_file, ".")
traceback.print_exc()
# Normalise all_var per env
final_all_var = pd.DataFrame()
for env_name in all_var["env"].drop_duplicates().values:
env_all_var = all_var[all_var["env"] == env_name].reset_index(drop=True)
for column in env_all_var.columns:
if env_all_var.dtypes[column] == "float64":
env_all_var[column] = (
(env_all_var[column] - env_all_var[column].min())
/ (env_all_var[column].max() - env_all_var[column].min())
* 100
)
final_all_var = pd.concat([final_all_var, env_all_var], ignore_index=True)
all_var = final_all_var
try:
# Uniformise time values across replications
all_data = uniformise_xaxis(all_data, "time")
except Exception:
if not args.no_traceback:
print("\n!!!WARNING!!! Could not uniformise the time values across reps.")
traceback.print_exc()
except Exception:
if not args.no_traceback and error:
print("\n!!!WARNING!!! Cannot read progress graphs.")
traceback.print_exc()
#################
# Plot p-values #
try:
if not args.plot_p_values:
error = False
assert 0
error = True
# Calling function to plot all p-values
print("\nPlotting p-values")
p_values(
plot_folder=plot_folder,
compare_size=compare_size,
times_data=all_times,
losses_data=all_losses,
var_data=all_var,
)
p_values(
plot_folder=plot_folder,
compare_size=compare_size,
times_data=all_times,
losses_data=all_losses,
var_data=all_var,
prefixe="in_cell_",
prefixe_title="In-Cell ",
)
except Exception:
if not args.no_traceback and error:
print("\n!!!WARNING!!! Cannot plot p-values.")
traceback.print_exc()
#######################
# Plot summary graphs #
try:
if not args.plot_summary:
error = False
assert 0
error = True
# Customize matplotlib params
print("\nPlotting summary graphs")
font_size = 22
params = {
"axes.labelsize": font_size,
"axes.titlesize": font_size,
"legend.fontsize": font_size,
"xtick.labelsize": font_size,
"ytick.labelsize": font_size,
"text.usetex": False,
"axes.titlepad": 10,
"lines.linewidth": 2,
# "lines.markeredgecolor": "black",
# "lines.markeredgewidth": 2,
"lines.markersize": 8,
# "xtick.major.pad": 20,
# "ytick.major.pad": 20,
}
mpl.rcParams.update(params)
# Print the metrics per num_reevals
for num_reevals in all_data["num_reevals"].drop_duplicates().values:
# Extract and sort data
num_reeval_losses = all_losses[all_losses["num_reevals"] == num_reevals]
num_reeval_losses = sort_data(num_reeval_losses, ["algo", "rep", compare_size])
num_reeval_times = all_times[all_times["num_reevals"] == num_reevals]
num_reeval_times = sort_data(num_reeval_times, ["algo", "rep", compare_size])
num_reeval_var = all_var[all_var["num_reevals"] == num_reevals]
num_reeval_var = sort_data(num_reeval_var, ["algo", "rep", compare_size])
# Summary graph loss only (for paper)
print(" Loss summary", compare_title, "graphs", num_reevals)
if not args.category_plots:
summary_loss_allenv(
num_reevals=num_reevals,
compare_size=compare_size,
compare_title=compare_title,
plot_folder=plot_folder,
losses_data=num_reeval_losses,
var_data=num_reeval_var,
legend_columns=args.summary_legend_columns,
)
summary_loss_allenv(
num_reevals=num_reevals,
compare_size=compare_size,
compare_title=compare_title,
plot_folder=plot_folder,
losses_data=num_reeval_losses,
var_data=num_reeval_var,
prefixe="in_cell_",
legend_columns=args.summary_legend_columns,
)
else:
summary_loss_categories_allenv(
num_reevals=num_reevals,
compare_size=compare_size,
compare_title=compare_title,
plot_folder=plot_folder,
losses_data=num_reeval_losses,
var_data=num_reeval_var,
legend_columns=args.summary_legend_columns,
)
summary_loss_categories_allenv(
num_reevals=num_reevals,
compare_size=compare_size,
compare_title=compare_title,
plot_folder=plot_folder,
losses_data=num_reeval_losses,
var_data=num_reeval_var,
prefixe="in_cell_",
legend_columns=args.summary_legend_columns,
)
except Exception:
if not args.no_traceback and error:
print("\n!!!WARNING!!! Cannot plot summary graphs.")
traceback.print_exc()
#######################
# Plot pareto graphs #
try:
if not args.plot_pareto:
error = False
assert 0
error = True
# Customize matplotlib params
print("\nPlotting pareto graphs")
font_size = 15
params = {
"axes.labelsize": font_size,
"axes.titlesize": font_size,
"legend.fontsize": font_size,
"xtick.labelsize": font_size,
"ytick.labelsize": font_size,
"text.usetex": False,
"axes.titlepad": 10,
"lines.linewidth": 2,
# "lines.markeredgecolor": "black",
# "lines.markeredgewidth": 2,
"lines.markersize": 8,
# "xtick.major.pad": 20,
# "ytick.major.pad": 20,
}
mpl.rcParams.update(params)
# Print the metrics per num_reevals
for num_reevals in all_data["num_reevals"].drop_duplicates().values:
# Extract and sort data
num_reeval_losses = all_losses[all_losses["num_reevals"] == num_reevals]
num_reeval_losses = sort_data(num_reeval_losses, ["algo", "rep", compare_size])
num_reeval_times = all_times[all_times["num_reevals"] == num_reevals]
num_reeval_times = sort_data(num_reeval_times, ["algo", "rep", compare_size])
num_reeval_var = all_var[all_var["num_reevals"] == num_reevals]
num_reeval_var = sort_data(num_reeval_var, ["algo", "rep", compare_size])
# Pareto graph
print(" Pareto graphs", num_reevals)
if not args.category_plots:
pareto_plot(
plot_folder=plot_folder,
num_reevals=num_reevals,
compare_size=compare_size,
times_data=num_reeval_times,
legend_columns=args.pareto_legend_columns,
pareto_column=True,
)
pareto_plot(
plot_folder=plot_folder,
num_reevals=num_reevals,
compare_size=compare_size,
times_data=num_reeval_times,
prefixe="in_cell_",
legend_columns=args.pareto_legend_columns,
pareto_column=True,
)
else:
pareto_categories(
plot_folder=plot_folder,
num_reevals=num_reevals,
compare_size=compare_size,
times_data=num_reeval_times,
legend_columns=args.pareto_legend_columns,
pareto_column=args.category_plots,
)
pareto_categories(
plot_folder=plot_folder,
num_reevals=num_reevals,
compare_size=compare_size,
times_data=num_reeval_times,
prefixe="in_cell_",
legend_columns=args.pareto_legend_columns,
pareto_column=args.category_plots,
)
except Exception:
if not args.no_traceback and error:
print("\n!!!WARNING!!! Cannot plot pareto graphs.")
traceback.print_exc()
#######################
# Plot paper archives #
try:
if not args.plot_paper_archives:
error = False
assert 0
error = True
print("\nPlotting paper archive")
# Customize matplotlib params
font_size = 25
params = {
"axes.labelsize": font_size,
"axes.titlesize": font_size,
"legend.fontsize": font_size,
"xtick.labelsize": font_size,
"ytick.labelsize": font_size,
"text.usetex": False,
"axes.titlepad": 10,
"lines.linewidth": 2,
# "lines.markeredgecolor": "black",
# "lines.markeredgewidth": 2,
"lines.markersize": 8,
# "xtick.major.pad": 20,
# "ytick.major.pad": 20,
}
mpl.rcParams.update(params)
# Create dataframe
repertoire_folders = pd.DataFrame()
env_min_max = pd.DataFrame()
prefixe = args.paper_archives_prefixe
# Separate files for each env
for env in config_frame["env"].drop_duplicates().values:
for num_reevals in config_frame["num_reevals"].drop_duplicates().values:
try:
print("\n Reading for", env)
env_config_frame = config_frame[
(config_frame["env"] == env)
& (config_frame["num_reevals"] == num_reevals)
].reset_index(drop=True)
# Finding min and max for all plots
min_fitness = jnp.inf
max_fitness = -jnp.inf
min_fit_var = jnp.inf
max_fit_var = -jnp.inf
min_desc_var = jnp.inf
max_desc_var = -jnp.inf
min_bd = [
float(bd) for bd in env_config_frame["min_bd"][0][1:-1].split(" ")
]
max_bd = [
float(bd) for bd in env_config_frame["max_bd"][0][1:-1].split(" ")
]
# One archive for each algorithm
for algo in env_config_frame["algo"].drop_duplicates().values:
algo_config_frame = env_config_frame[
env_config_frame["algo"] == algo
].reset_index(drop=True)
for size in (
algo_config_frame[compare_size].drop_duplicates().values
):
if algo_config_frame[
algo_config_frame[compare_size] == size
].empty:
print(" Size", size, "does not exist for", algo)
continue
size_config_frame = algo_config_frame[
algo_config_frame[compare_size] == size
].reset_index(drop=True)
try:
reeval_repertoire_folder = get_folder_name(
size_config_frame,
f"{prefixe}reeval_repertoire_folder",
0,
)
fit_var_repertoire_folder = get_folder_name(
size_config_frame,
f"{prefixe}fit_var_repertoire_folder",
0,
)
desc_var_repertoire_folder = get_folder_name(
size_config_frame,
f"{prefixe}desc_var_repertoire_folder",
0,
)
# Open reeval repertoire to find min and max fitness
reeval_fitnesses = jnp.load(
os.path.join(reeval_repertoire_folder, "fitnesses.npy")
)
reeval_fitnesses_inf = jnp.where(
reeval_fitnesses == -jnp.inf, jnp.inf, reeval_fitnesses
)
min_fitness = min(
min_fitness, float(min(reeval_fitnesses_inf))
)
max_fitness = max(max_fitness, float(max(reeval_fitnesses)))
# Open fit_var repertoiret to find min and max fit_var
variances = jnp.load(
os.path.join(fit_var_repertoire_folder, "fitnesses.npy")
)
variances_inf = jnp.where(
variances == -jnp.inf, jnp.inf, variances
)
min_fit_var = min(min_fit_var, float(min(variances_inf)))
max_fit_var = max(max_fit_var, float(max(variances)))
# Open desc_var repertoiret to find min and max desc_var
variances = jnp.load(
os.path.join(
desc_var_repertoire_folder, "fitnesses.npy"
)
)
variances_inf = jnp.where(
variances == -jnp.inf, jnp.inf, variances
)
min_desc_var = min(min_desc_var, float(min(variances_inf)))
max_desc_var = max(max_desc_var, float(max(variances)))
# Update frame
repertoire_folders = pd.concat(
[
repertoire_folders,
pd.DataFrame.from_dict(
{
"env": [env],
"num_reevals": [num_reevals],
"algo": [algo],
compare_size: [size],
"reeval_repertoire_folder": [