-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
159 lines (129 loc) · 3.9 KB
/
common.py
File metadata and controls
159 lines (129 loc) · 3.9 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
import os
from matplotlib import pyplot as plt
from machinable import get
from machinable.utils import save_file, load_file
import pandas as pd
import numpy as np
import scienceplots
from scipy import stats
from sklearn import metrics
import matplotlib
plt.style.use(["science", "nature"])
plt.rcParams.update({"figure.dpi": "300"})
np.set_printoptions(linewidth=np.inf, suppress=True)
FIGURE_EXPORT_DIRECTORY = os.environ.get("FIGURES_MSO", "./figures")
def rc_context():
return matplotlib.rc_context(
{
"font.size": 20,
"axes.titlesize": 20,
"axes.labelsize": 20,
"xtick.labelsize": 20,
"ytick.labelsize": 20,
"legend.fontsize": 20,
"figure.titlesize": 24,
}
)
colors = {
"blue": "#0C5DA5",
"green": "#00B945",
"yellow": "#FF9500",
"red": "#FF2C00",
"violet": "#845B97",
"black": "#474747",
"grey": "#9e9e9e",
}
icolors = [c for c in colors.values()]
mcolors = {
k: icolors[i]
for i, k in enumerate(
[
"gpr",
"megp",
"o-resnet",
"c+o-resnet",
"o-fttransformer",
"c+o-fttransformer",
"-",
]
)
}
def fronts_nadir(components, **kwargs):
return (
pd.concat(components.map(lambda e: e.get_best(**kwargs)["y"])).max().to_list()
)
def compute_auc_hvs(hypervolumes):
aucs = {}
for p, models in hypervolumes.items():
aucs.setdefault(p, {})
for model_name, epoch_hvs in models.items():
x = []
ys = {}
for epoch, hvs in epoch_hvs.items():
x.append(int(epoch))
for i in range(len(hvs)):
ys.setdefault(i, [])
ys[i].append(hvs[i])
aucs[p][model_name] = [
metrics.auc(x, y) for y in ys.values() if len(y) == len(x)
]
return aucs
def population_nadirs(*interface):
if len(interface) > 1:
nds = tuple(population_nadirs(i) for i in interface)
return {
p: [float(np.max(vals)) for vals in zip(*(n[p] for n in nds))]
for p in nds[0]
}
n = {}
for p in interface[0].populations():
experiments = interface[0].components.filter(lambda x: p in x.label())
n[p] = fronts_nadir(experiments)
return n
def status(interface):
with get("interface.execution.status"):
interface.launch()
def globus_download(interface, force=False):
if not os.path.isfile(interface.components[0].output_filepath) or force:
with get("interface.storage.globus"):
with get("interface.execution.download", {"force": force}):
interface.launch()
def stash(key: str, data):
if hasattr(data, "savefig"):
data.savefig(
os.path.join(FIGURE_EXPORT_DIRECTORY, f"{key}.svg"), transparent=True
)
data.savefig(
os.path.join(FIGURE_EXPORT_DIRECTORY, f"{key}.png"), transparent=True
)
data.savefig(
os.path.join(FIGURE_EXPORT_DIRECTORY, f"{key}.pdf"), transparent=True
)
return
if "." not in key:
key += ".json"
save_file(f"./figures/data/{key}", data)
def restore(key: str, default=None):
if "." not in key:
key += ".json"
return load_file(f"./figures/data/{key}", default)
def x_to_weight_dict(d):
r = {}
for key, val in d.items():
n = ""
glue = "_"
versions = []
for fragment in key.split("."):
if fragment.startswith("("):
versions = eval(fragment)
fragment = versions[0]
if fragment == "weight":
glue = ""
n += fragment + glue
glue = "-"
if versions:
for v in versions:
r[n.replace(versions[0], v)] = val
else:
r[n] = val
return r