-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval.py
284 lines (232 loc) · 9.08 KB
/
eval.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
"""
Script to generate test set evaluation metrics from a training run
Usage:
python [experiment_path] [--wandb] [--perturbseq] [--batch_size {int}]
Examples:
python eval.py michael/debug/mw545rhs --wandb --perturbseq
- Saves evaluation metrics to wandb run summary
python eval.py results/example --perturbseq
- Saves evaluation metrics to results/example/test_metrics.csv (local experiment)
python eval.py {checkpoint_path}.ckpt --perturbseq
- Runs evaluation for specified checkpoint, and saves metrics to
{checkpoint_path}_test_metrics.csv
"""
import argparse
import os
from os.path import basename, join, splitext
from typing import Any, Dict, Literal
import numpy as np
import pandas as pd
import torch
import wandb
from scipy.stats import pearsonr
from sklearn.metrics import r2_score
from torch.utils.data import DataLoader
from sams_vae.data.utils.anndata import align_adatas
from sams_vae.models.utils.perturbation_lightning_module import (
TrainConfigPerturbationLightningModule,
)
def evaluate_checkpoint(
checkpoint_path: str,
average_treatment_effect_method: Literal["mean", "perturbseq"],
batch_size: int = 500,
ate_n_particles: int = 2500,
) -> Dict[str, Any]:
"""
Compute test set metrics for a given checkpoint
Parameters
----------
checkpoint_path: path to checkpoint
average_treatment_effect_method: method to compute average treatment effect. "perturbseq"
normalizes for library size and applies log transform before assessing effect
batch_size: batch size to use for IWELBO computation
Returns
-------
dictionary with test set metrics
"""
lightning_module = load_checkpoint(checkpoint_path)
data_module = lightning_module.get_data_module()
predictor = lightning_module.predictor
metrics = {}
# compute test set IWELBO
test_loader = DataLoader(
data_module.test_dataloader().dataset,
batch_size=batch_size,
)
test_iwelbo_df = predictor.compute_predictive_iwelbo(
loaders=test_loader, n_particles=100
)
test_iwelbo = test_iwelbo_df["IWELBO"].mean()
metrics["test/IWELBO"] = test_iwelbo
# assess correlation between estimated average treatment effects from model and data
data_ate = data_module.get_estimated_average_treatment_effects(
method=average_treatment_effect_method
)
if data_ate is not None:
model_ate = predictor.estimate_average_effects_data_module(
data_module=data_module,
control_label=data_ate.uns["control"],
method=average_treatment_effect_method,
n_particles=ate_n_particles,
condition_values=dict(library_size=10000 * torch.ones((1,))),
batch_size=batch_size,
)
data_ate, model_ate = align_adatas(data_ate, model_ate)
intervention_info = data_module.get_unique_observed_intervention_info()
metrics["ATE_n_particles"] = ate_n_particles
# compute average treatment effect metrics for all perturbations
ate_metrics_all_splits = get_ate_metrics(data_ate, model_ate)
for k, v in ate_metrics_all_splits.items():
metrics[f"{k}-all"] = v
# compute average treatment effect metrics for perturbations available
# in each split
for split in ["train", "val", "test"]:
split_perturbations = intervention_info[intervention_info[split]].index
idx = data_ate.obs.index.isin(split_perturbations)
ate_metrics_split = get_ate_metrics(data_ate[idx], model_ate[idx])
for k, v in ate_metrics_split.items():
metrics[f"{k}-{split}"] = v
return metrics
def get_ate_metrics(data_ate, model_ate):
metrics = {}
top_20_idx = np.argpartition(np.abs(data_ate.X.copy()), data_ate.shape[1] - 20)[
:, -20:
]
x = data_ate.X.flatten()
y = model_ate.X.flatten()
metrics["ATE_pearsonr"] = pearsonr(x, y)[0]
metrics["ATE_r2"] = r2_score(x, y)
# evaluate correlation / R2 across top 20 DE genes per perturbation
x = np.take_along_axis(data_ate.X.copy(), top_20_idx, axis=-1).flatten()
y = np.take_along_axis(model_ate.X.copy(), top_20_idx, axis=-1).flatten()
metrics["ATE_pearsonr_top20"] = pearsonr(x, y)[0]
metrics["ATE_r2_top20"] = r2_score(x, y)
return metrics
def evaluate_local_experiment(
experiment_path: str,
average_treatment_effect_method: Literal["mean", "perturbseq"],
batch_size: int = 128,
ate_n_particles: int = 2500,
):
"""
Compute and save evaluation metrics for checkpoint with best eval loss in
local experiment to `{experiment_path}/test_metrics.csv`
Parameters
----------
experiment_path: path to experiment (typically in results/ directory)
average_treatment_effect_method
batch_size: batch size used during IWELBO computation
"""
checkpoint_names = os.listdir(join(experiment_path, "checkpoints"))
# TODO: add better logic if needed
best_checkpoints = [x for x in checkpoint_names if x[:4] == "best"]
assert len(best_checkpoints) == 1
checkpoint_path = join(experiment_path, "checkpoints", best_checkpoints[0])
checkpoint_name = splitext(basename(checkpoint_path))[0]
metrics = evaluate_checkpoint(
checkpoint_path,
average_treatment_effect_method=average_treatment_effect_method,
batch_size=batch_size,
ate_n_particles=ate_n_particles,
)
metrics["checkpoint"] = checkpoint_name
metrics_df = pd.DataFrame({k: [v] for k, v in metrics.items()}).T
metrics_path = join(experiment_path, "test_metrics.csv")
metrics_df.to_csv(metrics_path)
def evaluate_local_checkpoint(
checkpoint_path: str,
average_treatment_effect_method: Literal["mean", "perturbseq"],
batch_size: int = 128,
ate_n_particles: int = 2500,
):
"""
Compute and save evaluation metrics specified checkpoint_path,
saves results to {checkpoint_path}_test_metrics.csv
Parameters
----------
experiment_path: path to experiment (typically in results/ directory)
average_treatment_effect_method
batch_size: batch size used during IWELBO computation
"""
checkpoint_base = splitext(checkpoint_path)[0]
checkpoint_name = splitext(basename(checkpoint_path))[0]
metrics = evaluate_checkpoint(
checkpoint_path,
average_treatment_effect_method=average_treatment_effect_method,
batch_size=batch_size,
ate_n_particles=ate_n_particles,
)
metrics["checkpoint"] = checkpoint_name
metrics_df = pd.DataFrame({k: [v] for k, v in metrics.items()}).T
metrics_path = checkpoint_base + "_test_metrics.csv"
metrics_df.to_csv(metrics_path)
def evaluate_wandb_experiment(
experiment_path: str,
average_treatment_effect_method: Literal["mean", "perturbseq"],
batch_size: int = 128,
ate_n_particles: int = 2500,
):
"""
Compute and save evaluation metrics for checkpoint with best eval loss
Metrics are saved to wandb run summary
"""
api = wandb.Api()
run = api.run(experiment_path)
# TODO: improve logic if needed
run_file_paths = [x.name for x in run.files()]
best_checkpoint_paths = [
x
for x in run_file_paths
if os.path.split(x)[0] == "checkpoints" and "best" in x
]
assert len(best_checkpoint_paths) == 1
wandb_file = run.file(best_checkpoint_paths[0])
# download checkpoint
basedir = run.name + "/"
os.makedirs(basedir, exist_ok=True)
checkpoint_path = wandb_file.download(root=basedir, replace=True).name
metrics = evaluate_checkpoint(
checkpoint_path,
average_treatment_effect_method=average_treatment_effect_method,
batch_size=batch_size,
ate_n_particles=ate_n_particles,
)
# save metrics to run summary
for k in metrics:
run.summary[k] = metrics[k]
run.summary.update()
def load_checkpoint(checkpoint_path: str):
lightning_module = TrainConfigPerturbationLightningModule.load_from_checkpoint(
checkpoint_path
)
return lightning_module
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("experiment_path")
parser.add_argument("--wandb", action="store_true")
parser.add_argument("--perturbseq", action="store_true")
parser.add_argument("--batch_size", type=int, default=500)
parser.add_argument("--ate_n_particles", type=int, default=2500)
args = parser.parse_args()
method: Literal["mean", "perturbseq"] = "perturbseq" if args.perturbseq else "mean"
if args.wandb:
evaluate_wandb_experiment(
args.experiment_path,
method,
batch_size=args.batch_size,
ate_n_particles=args.ate_n_particles,
)
elif os.path.isdir(args.experiment_path):
evaluate_local_experiment(
args.experiment_path,
method,
batch_size=args.batch_size,
ate_n_particles=args.ate_n_particles,
)
else:
evaluate_local_checkpoint(
args.experiment_path,
method,
batch_size=args.batch_size,
ate_n_particles=args.ate_n_particles,
)