File size: 10,624 Bytes
9f5e507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Distributional evaluation metrics for perturbation prediction.

Complements cell-eval (which focuses on conditional mean accuracy) with
metrics that measure distributional fidelity:

1. Per-perturbation MMD (Maximum Mean Discrepancy)
2. Per-perturbation Energy Distance
3. Variance Ratio (per-gene variance match)
4. Gene-Gene Correlation Preservation
5. Classifier Two-Sample Test (C2ST)
6. k-NN Precision / Recall
"""

import numpy as np
import anndata as ad
import pandas as pd
from scipy.spatial.distance import cdist
from scipy.stats import pearsonr
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler


# ── Helpers ──────────────────────────────────────────────────────────

def _get_pert_cells(adata, pert):
    mask = adata.obs["perturbation"] == pert
    X = adata[mask].X
    return np.asarray(X) if not isinstance(X, np.ndarray) else X


def _rbf_kernel(X, Y, sigma):
    D2 = cdist(X, Y, metric="sqeuclidean")
    return np.exp(-D2 / (2 * sigma ** 2))


# ── 1. MMD (multi-sigma RBF) ────────────────────────────────────────

def mmd_multi_sigma(X, Y, scales=(0.5, 1.0, 2.0, 4.0)):
    D2_all = cdist(X, X, "sqeuclidean")
    median_sq = np.median(D2_all[np.triu_indices(len(X), k=1)])
    median_sq = max(median_sq, 1e-12)

    vals = []
    for s in scales:
        sigma = np.sqrt(s * median_sq)
        Kxx = _rbf_kernel(X, X, sigma)
        Kyy = _rbf_kernel(Y, Y, sigma)
        Kxy = _rbf_kernel(X, Y, sigma)
        m, n = len(X), len(Y)
        t_xx = (Kxx.sum() - np.trace(Kxx)) / (m * (m - 1))
        t_yy = (Kyy.sum() - np.trace(Kyy)) / (n * (n - 1))
        t_xy = Kxy.mean()
        vals.append(t_xx + t_yy - 2 * t_xy)
    return float(np.mean(vals))


# ── 2. Energy Distance ──────────────────────────────────────────────

def energy_distance(X, Y):
    Dxy = cdist(X, Y, "euclidean").mean()
    Dxx = cdist(X, X, "euclidean").mean()
    Dyy = cdist(Y, Y, "euclidean").mean()
    return float(2 * Dxy - Dxx - Dyy)


# ── 3. Variance Ratio ───────────────────────────────────────────────

def variance_ratio(pred_cells, real_cells, var_threshold=0.001):
    """Per-gene variance ratio: pred_var / real_var. Perfect = 1.0.
    Only considers genes with real_var > var_threshold (skip sparse zeros)."""
    pv = pred_cells.var(axis=0)
    rv = real_cells.var(axis=0)
    mask = rv > var_threshold
    if mask.sum() < 10:
        return {"var_ratio_median": np.nan, "var_ratio_mean": np.nan,
                "var_ratio_q25": np.nan, "var_ratio_q75": np.nan,
                "n_active_genes": int(mask.sum())}
    ratio = pv[mask] / rv[mask]
    return {
        "var_ratio_median": float(np.median(ratio)),
        "var_ratio_mean": float(np.mean(ratio)),
        "var_ratio_q25": float(np.percentile(ratio, 25)),
        "var_ratio_q75": float(np.percentile(ratio, 75)),
        "n_active_genes": int(mask.sum()),
    }


# ── 4. Gene-Gene Correlation Preservation ───────────────────────────

def gene_corr_preservation(pred_cells, real_cells, top_k=200):
    """Pearson correlation between flattened gene-gene correlation matrices.
    Uses top_k highest-variance genes for tractability."""
    gene_var = real_cells.var(axis=0)
    top_idx = np.argsort(gene_var)[-top_k:]

    pred_sub = pred_cells[:, top_idx]
    real_sub = real_cells[:, top_idx]

    pred_corr = np.corrcoef(pred_sub.T)
    real_corr = np.corrcoef(real_sub.T)

    idx = np.triu_indices(top_k, k=1)
    r, _ = pearsonr(pred_corr[idx], real_corr[idx])
    return float(r)


# ── 5. C2ST (Classifier Two-Sample Test) ────────────────────────────

def c2st(pred_cells, real_cells, max_samples=500):
    """Logistic regression C2ST. Returns accuracy (0.5 = indistinguishable)."""
    n_pred = min(len(pred_cells), max_samples)
    n_real = min(len(real_cells), max_samples)
    idx_p = np.random.choice(len(pred_cells), n_pred, replace=False)
    idx_r = np.random.choice(len(real_cells), n_real, replace=False)

    X = np.vstack([pred_cells[idx_p], real_cells[idx_r]])
    y = np.concatenate([np.zeros(n_pred), np.ones(n_real)])

    scaler = StandardScaler()
    X_s = scaler.fit_transform(X)

    clf = LogisticRegression(max_iter=500, C=1.0, solver="lbfgs")
    n_cv = min(5, min(n_pred, n_real))
    if n_cv < 2:
        return float("nan")
    scores = cross_val_score(clf, X_s, y, cv=n_cv, scoring="accuracy")
    return float(scores.mean())


# ── 6. k-NN Precision / Recall ──────────────────────────────────────

def knn_precision_recall(pred_cells, real_cells, k=10):
    """
    Precision: fraction of pred cells whose k-NN ball overlaps real data.
    Recall: fraction of real cells whose k-NN ball overlaps pred data.
    """
    D_rr = cdist(real_cells, real_cells, "euclidean")
    np.fill_diagonal(D_rr, np.inf)
    real_knn_dist = np.sort(D_rr, axis=1)[:, k - 1]

    D_pp = cdist(pred_cells, pred_cells, "euclidean")
    np.fill_diagonal(D_pp, np.inf)
    pred_knn_dist = np.sort(D_pp, axis=1)[:, k - 1]

    D_pr = cdist(pred_cells, real_cells, "euclidean")
    precision = float((D_pr <= real_knn_dist[None, :]).any(axis=1).mean())

    D_rp = D_pr.T
    recall = float((D_rp <= pred_knn_dist[None, :]).any(axis=1).mean())

    return precision, recall


# ── Main ─────────────────────────────────────────────────────────────

def evaluate_model(pred_path, real_path, name):
    pred = ad.read_h5ad(pred_path)
    real = ad.read_h5ad(real_path)

    perts = [p for p in pred.obs["perturbation"].unique() if p != "control"]

    rows = []
    for pert in perts:
        pc = _get_pert_cells(pred, pert)
        rc = _get_pert_cells(real, pert)

        if len(pc) < 5 or len(rc) < 5:
            continue

        mmd = mmd_multi_sigma(pc, rc)
        edist = energy_distance(pc, rc)
        vr = variance_ratio(pc, rc)
        gc = gene_corr_preservation(pc, rc, top_k=min(200, pc.shape[1]))
        c2st_acc = c2st(pc, rc)
        prec, rec = knn_precision_recall(pc, rc, k=min(5, len(rc) - 1))

        rows.append({
            "perturbation": pert,
            "mmd": mmd,
            "energy_distance": edist,
            **vr,
            "gene_corr_preservation": gc,
            "c2st_accuracy": c2st_acc,
            "knn_precision": prec,
            "knn_recall": rec,
        })

    df = pd.DataFrame(rows)
    return df


def print_comparison(results: dict[str, pd.DataFrame]):
    metrics = [
        "mmd", "energy_distance",
        "var_ratio_median", "var_ratio_mean",
        "gene_corr_preservation", "c2st_accuracy",
        "knn_precision", "knn_recall",
    ]
    lower_better = {"mmd", "energy_distance"}
    target_metrics = {
        "var_ratio_median": 1.0,
        "var_ratio_mean": 1.0,
        "c2st_accuracy": 0.5,
    }

    names = list(results.keys())
    short = {n: n[:12] for n in names}

    header = f"{'Metric':<28}" + "".join(f"{short[n]:>14}" for n in names) + "   Best"
    print(header)
    print("-" * len(header))

    for m in metrics:
        vals = []
        for n in names:
            vals.append(float(results[n][m].mean()))

        if m in target_metrics:
            target = target_metrics[m]
            dists = [abs(v - target) for v in vals]
            best_idx = dists.index(min(dists))
        elif m in lower_better:
            best_idx = vals.index(min(vals))
        else:
            best_idx = vals.index(max(vals))

        short_names = [short[n] for n in names]
        line = f"{m:<28}" + "".join(f"{v:>14.4f}" for v in vals)
        line += f"   <- {short_names[best_idx]}"
        print(line)

    print("\n=== Variance Ratio (median [Q25, Q75], 1.0 = perfect) ===")
    for n in names:
        df = results[n]
        med = df["var_ratio_median"].mean()
        q25 = df["var_ratio_q25"].mean()
        q75 = df["var_ratio_q75"].mean()
        print(f"  {short[n]:<14} {med:.3f} [{q25:.3f}, {q75:.3f}]")


if __name__ == "__main__":
    np.random.seed(42)

    BASE = "/home/hp250092/ku50001222/qian/aivc/lfj"

    models = {
        "A6_SDE50": (
            f"{BASE}/GRN/result/SB/A6_dsm_aniso/iteration_195000/pred.h5ad",
            f"{BASE}/GRN/result/SB/A6_dsm_aniso/iteration_195000/real.h5ad",
        ),
        "A6_ODE_RK4": (
            f"{BASE}/GRN/result/SB/A6_dsm_aniso/eval_only/pred.h5ad",
            f"{BASE}/GRN/result/SB/A6_dsm_aniso/eval_only/real.h5ad",
        ),
        "A1_Euler": (
            f"{BASE}/GRN/result/SB/A1_baseline/iteration_195000/pred.h5ad",
            f"{BASE}/GRN/result/SB/A1_baseline/iteration_195000/real.h5ad",
        ),
        "A1_RK4": (
            f"{BASE}/GRN/result/SB/A1_baseline/eval_only/pred.h5ad",
            f"{BASE}/GRN/result/SB/A1_baseline/eval_only/real.h5ad",
        ),
        "scDFM": (
            f"{BASE}/GRN/baseline/flow-fusion_differential_perceiver-norman-origin-predict_y-gamma_0.5-perturbation_function_crisper-lr_5e-05-dim_model_128-infer_top_gene_1000-split_method_additive-use_mmd_loss_True-fold_1-use_negative_edge_True-topk_30/iteration_200000/pred.h5ad",
            f"{BASE}/GRN/baseline/flow-fusion_differential_perceiver-norman-origin-predict_y-gamma_0.5-perturbation_function_crisper-lr_5e-05-dim_model_128-infer_top_gene_1000-split_method_additive-use_mmd_loss_True-fold_1-use_negative_edge_True-topk_30/iteration_200000/real.h5ad",
        ),
    }

    results = {}
    for name, (pred_path, real_path) in models.items():
        print(f"\n>>> Evaluating {name} ...")
        df = evaluate_model(pred_path, real_path, name)
        results[name] = df
        out_dir = str(pred_path).rsplit("/", 1)[0]
        df.to_csv(f"{out_dir}/distributional_results.csv", index=False)
        print(f"    saved to {out_dir}/distributional_results.csv")

    print("\n" + "=" * 100)
    print("DISTRIBUTIONAL METRICS COMPARISON (mean over 39 perturbations)")
    print("=" * 100 + "\n")
    print_comparison(results)