mineself2016 commited on
Commit
7c81fa2
·
verified ·
1 Parent(s): 5c3061a

Remove stale file: examples/12_batch_integration_eval.py

Browse files
examples/12_batch_integration_eval.py DELETED
@@ -1,79 +0,0 @@
1
- """
2
- Batch integration downstream example:
3
- - Extract embeddings with frozen GeneMamba
4
- - Evaluate simple batch mixing score proxy (silhouette by batch)
5
-
6
- Expected h5ad columns:
7
- - obs['batch']
8
- """
9
-
10
- import argparse
11
- import numpy as np
12
- import scanpy as sc
13
- import torch
14
- from sklearn.metrics import silhouette_score
15
- from sklearn.preprocessing import LabelEncoder
16
- from transformers import AutoModel
17
-
18
-
19
- def build_ranked_input_ids(adata, symbol2id, seq_len=2048, pad_id=1):
20
- gene_names = np.array(adata.var_names)
21
- X = adata.X
22
- out = np.full((adata.n_obs, seq_len), pad_id, dtype=np.int64)
23
-
24
- for i in range(adata.n_obs):
25
- row = X[i]
26
- if hasattr(row, "toarray"):
27
- expr = row.toarray().ravel()
28
- else:
29
- expr = np.asarray(row).ravel()
30
-
31
- nz = np.where(expr > 0)[0]
32
- if len(nz) == 0:
33
- continue
34
-
35
- genes = gene_names[nz]
36
- vals = expr[nz]
37
- order = np.argsort(-vals)
38
- ranked_genes = genes[order]
39
- ids = [symbol2id[g] for g in ranked_genes if g in symbol2id][:seq_len]
40
- out[i, : len(ids)] = ids
41
-
42
- return out
43
-
44
-
45
- def main():
46
- parser = argparse.ArgumentParser()
47
- parser.add_argument("--model_path", required=True)
48
- parser.add_argument("--h5ad", required=True)
49
- parser.add_argument("--symbol2id_npy", required=True)
50
- parser.add_argument("--seq_len", type=int, default=2048)
51
- parser.add_argument("--batch_size", type=int, default=64)
52
- args = parser.parse_args()
53
-
54
- adata = sc.read_h5ad(args.h5ad)
55
- assert "batch" in adata.obs, "h5ad must include obs['batch']"
56
-
57
- symbol2id = np.load(args.symbol2id_npy, allow_pickle=True).item()
58
- input_ids = build_ranked_input_ids(adata, symbol2id, seq_len=args.seq_len)
59
-
60
- model = AutoModel.from_pretrained(args.model_path, trust_remote_code=True)
61
- model.eval().cuda()
62
-
63
- embeds = []
64
- with torch.no_grad():
65
- for s in range(0, input_ids.shape[0], args.batch_size):
66
- batch = torch.tensor(input_ids[s : s + args.batch_size], dtype=torch.long, device="cuda")
67
- out = model(batch)
68
- embeds.append(out.pooled_embedding.detach().cpu().numpy())
69
- embeds = np.concatenate(embeds, axis=0)
70
-
71
- batch_labels = LabelEncoder().fit_transform(adata.obs["batch"].values)
72
- score = silhouette_score(embeds, batch_labels, metric="euclidean")
73
-
74
- print("silhouette_by_batch:", score)
75
- print("(Closer to 0 typically indicates better batch mixing than very high positive values.)")
76
-
77
-
78
- if __name__ == "__main__":
79
- main()