-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
130 lines (111 loc) · 3.83 KB
/
utils.py
File metadata and controls
130 lines (111 loc) · 3.83 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
import os
from pathlib import PosixPath, Path
from reader.chunked_writer import TidyReader
from typing import List
import pandas as pd
def series_to_mean(df, threshold=4500, add_params=()):
df = df[df["Step"] > threshold]
groups = df.groupby(
["Rank", "Metric", "Type", "Agent", "Epoch", *add_params], as_index=False
).mean()
return groups
def path_to_stem(path: str):
return path.split("/")[-1]
def stem_to_params(stem: str):
"""
Helper function which takes in a path stem of the form "param:value-param2:value2..."
and returns a dictionary of parameters, e.g. "{"param":value,...}
Args:
stem: String. The name of the folder.
"""
params = {k: v for d in map(eval_d, stem.split("-")[:-1]) for k, v in d.items()}
return params
def eval_d(string: str):
"""
Helper function which parses a string of the form 'x:y' into
a dictionary {"x": "y"}. This is used for inferring the hyperparameters
from the folder names.
Args:
string: String. The input string to evaluate.
"""
k, v = string.split(":")
return {k: v}
def load_df_and_params(
posixpath: PosixPath, tag: str, columns: List[str], datafolder="data"
):
"""
Args:
posixpath: Posixpath. Path to one specific epxeriment
tag: String. The name of the metric which we want to retrieve
columns: List[String]. The column headers of the resulting dataframe
Returns:
df: DataFrame
"""
reader = TidyReader(os.path.join(posixpath, datafolder))
df = reader.read(tag=tag, columns=columns)
params = stem_to_params(posixpath.name)
return df, params
def load_data(path, tag, keys, stop_after=None):
paths = list(Path(path).glob("*"))
dfs = []
i = 0
for path in paths:
# TODO: bette to check if path has the correct form
if "DS_Store" in str(path):
continue
i += 1
df, params = load_df_and_params(path, tag, keys)
for param, value in params.items():
df[param] = value
dfs.append(df)
if stop_after is not None:
if i > stop_after:
break
return pd.concat(dfs)
def plot_over(set_params, by=None, ax=None, title="Title", path=""):
path_candidates = sorted(Path(path).glob("*"))
dfs = []
for path in path_candidates:
suffix = str(path).split("/")[-1]
params = stem_to_params(suffix)
passing = False
for p, v in set_params.items():
if params[p] != v:
passing = True
if not passing:
reader = TidyReader(str(path) + "/data")
df = reader.read(
"pred_from_latent",
["Epoch", "Rank", "Step", "Value", "Metric", "Type", "Agent"],
)
df[by] = params[by]
dfs.append(df)
df = pd.concat(dfs)
df = df[
(df["Epoch"] == 39999)
& (df["Metric"] == "Accuracy")
& (df["Type"] == "Latent")
& (df["Agent"] != "baseline_1")
& (df["Agent"] != "baseline_2")
]
df["Title"] = title
return df
def map_params_to_name(params: dict):
if params["eta_msa"] == "1.0":
# print("mapping", params, "to DTI.")
return "DTI-pure"
if params["eta_msa"] == "0.74":
# print("mapping", params, "to ALL.")
return "DTI"
if params["eta_msa"] == "0.95":
# print("mapping", params, "to AE+MTM.")
return "AE+MTM"
if params["eta_ae"] == "1.0" and params["eta_lsa"] == "0.0":
# print("mapping", params, "to AE.")
return "AE"
if params["eta_ae"] == "1.0" and params["eta_lsa"] == "0.1":
# print("mapping", params, "to AE+MTM pure.")
return "AE+MTM-pure"
def remove_legend_titles(ax):
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles, labels=labels, title="")