-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathablation.py
More file actions
138 lines (120 loc) · 4.12 KB
/
ablation.py
File metadata and controls
138 lines (120 loc) · 4.12 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
# ignoring all issues with config keys
# pyright: reportArgumentType=false
from argparse import ArgumentParser
from collections.abc import Mapping
from functools import partialmethod
from itertools import count
from pathlib import Path
from typing import Any
import pandas as pd
import torch
from omegaconf import OmegaConf
from tqdm import tqdm
from ebes.data.utils import build_loaders
from ebes.model import build_model
from ebes.pipeline.utils import get_metrics
from ebes.trainer import Trainer
METRIC_FOR_DS = {
# "mimic3": "MulticlassAUROC",
# "physionet2012": "MulticlassAUROC",
# "age": "MulticlassAccuracy",
# "x5": "MulticlassAccuracy",
# "pendulum_cls": "MulticlassAccuracy",
# "taobao": "MulticlassAUROC",
"mbd": "MultiLabelMeanAUROC",
# "arabic": "MulticlassAccuracy",
# "electric_devices": "MulticlassAccuracy",
# "bpi_17": "MulticlassAUROC",
}
METHODS = [
# "mamba",
# "gru",
# "mlp",
# "primenet",
# "mtand",
# "coles",
# "mlem",
# "transformer",
"convtran",
]
def eval_ablation(exp_dir: Path, which: str, device: str):
print(f"evaluating {exp_dir.as_posix()}")
config = OmegaConf.load(exp_dir / "config.yaml")
for pl in config["test_data"]["preprocessing"].values():
tfs: list[str | Mapping[str, Any]] = pl["batch_transforms"]
if which == "time":
tfs.append("RandomTime")
elif which == "permutation":
tfs.append("RandomEventsPermutation")
elif which == "permutation_keep_last":
tfs.append({"RandomEventsPermutation": {"keep_last": True}})
elif which == "none":
pass
else:
raise ValueError("Unknown ablation type")
config = OmegaConf.to_container(config, resolve=True)
assert isinstance(config, dict)
test_loaders = build_loaders(**config["test_data"])
metrics = get_metrics(config["metrics"], "cpu")
net = build_model(config["model"])
trainer = Trainer(
model=net,
metrics=metrics,
ckpt_dir=exp_dir / "ckpt",
device=device,
)
trainer.load_best_model()
try:
test_metrics = trainer.validate(test_loaders["test"])
except:
print(config)
raise
return test_metrics
if __name__ == "__main__":
tqdm.__init__ = partialmethod(tqdm.__init__, disable=True) # type: ignore
parser = ArgumentParser()
parser.add_argument(
"which", choices=["time", "permutation", "permutation_keep_last", "none"]
)
parser.add_argument("--device", default="cuda:0")
parser.add_argument("--seeds", default="0,20")
args = parser.parse_args()
smin, smax = args.seeds.split(",")
smin, smax = int(smin), int(smax)
path = Path("log/")
res_file = path / "Ablations" / f"{args.which}_{smin}-{smax}.csv"
for i in count(1):
if not res_file.exists():
break
res_file = path / "Ablations" / f"{args.which}_{smin}-{smax}_{i}.csv"
rows = []
for ds_dir in path.iterdir():
if not ds_dir.is_dir() or ds_dir.stem not in METRIC_FOR_DS:
continue
for method in METHODS:
corr_dir = ds_dir / method / "correlation"
if not corr_dir.exists():
continue
for seed in range(smin, smax):
seed_dir = corr_dir / f"seed_{seed}"
if not seed_dir.is_dir():
continue
if (
not (seed_dir / "ckpt").exists()
or len(list((seed_dir / "ckpt").iterdir())) == 0
):
continue
row = {}
try:
metrics = eval_ablation(seed_dir, args.which, args.device)
m = metrics[METRIC_FOR_DS[ds_dir.name]]
except Exception as e:
m = float("nan")
print(e)
row["metric"] = m
row["dataset"] = ds_dir.name
row["method"] = method
row["seed"] = seed
rows.append(row)
pd.DataFrame(rows).to_csv(res_file, index=False)
torch.cuda.empty_cache()