mineself2016 commited on
Commit
d1718a0
·
verified ·
1 Parent(s): a601e72

Remove stale file: examples/00_preprocess_to_input_ids.py

Browse files
examples/00_preprocess_to_input_ids.py DELETED
@@ -1,75 +0,0 @@
1
- import argparse
2
- import json
3
- from pathlib import Path
4
-
5
- import numpy as np
6
- import pandas as pd
7
- import scanpy as sc
8
- import pyarrow as pa
9
-
10
-
11
- def load_vocab(tokenizer_json_path: str):
12
- with open(tokenizer_json_path, "r") as f:
13
- tokenizer = json.load(f)
14
- vocab = tokenizer["model"]["vocab"]
15
- pad_id = vocab.get("[PAD]", 1)
16
- unk_id = vocab.get("[UNK]", 0)
17
- return vocab, pad_id, unk_id
18
-
19
-
20
- def ranked_gene_ids_for_cell(expr_values, gene_names, vocab):
21
- nonzero_idx = np.where(expr_values > 0)[0]
22
- if len(nonzero_idx) == 0:
23
- return []
24
-
25
- genes = np.array(gene_names)[nonzero_idx]
26
- values = expr_values[nonzero_idx]
27
-
28
- order = np.argsort(-values)
29
- ranked_genes = genes[order]
30
-
31
- token_ids = [vocab[g] for g in ranked_genes if g in vocab]
32
- return token_ids
33
-
34
-
35
- def main():
36
- parser = argparse.ArgumentParser(description="Convert h5ad to GeneMamba input_ids (Arrow)")
37
- parser.add_argument("--h5ad", required=True, help="Input h5ad file")
38
- parser.add_argument("--tokenizer_json", required=True, help="Path to tokenizer.json or gene_tokenizer.json")
39
- parser.add_argument("--output_arrow", required=True, help="Output arrow file path")
40
- parser.add_argument("--max_cells", type=int, default=None, help="Optional: process first N cells only")
41
- args = parser.parse_args()
42
-
43
- adata = sc.read_h5ad(args.h5ad)
44
- vocab, _, _ = load_vocab(args.tokenizer_json)
45
-
46
- gene_names = list(adata.var_names)
47
- n_cells = adata.n_obs if args.max_cells is None else min(args.max_cells, adata.n_obs)
48
-
49
- rows = []
50
- X = adata.X
51
-
52
- for i in range(n_cells):
53
- row = X[i]
54
- if hasattr(row, "toarray"):
55
- expr = row.toarray().ravel()
56
- else:
57
- expr = np.asarray(row).ravel()
58
-
59
- token_ids = ranked_gene_ids_for_cell(expr, gene_names, vocab)
60
- rows.append(token_ids)
61
-
62
- df = pd.DataFrame({"input_ids": rows})
63
- table = pa.Table.from_pandas(df)
64
-
65
- output_path = Path(args.output_arrow)
66
- output_path.parent.mkdir(parents=True, exist_ok=True)
67
- with pa.OSFile(str(output_path), "wb") as sink:
68
- with pa.ipc.new_stream(sink, table.schema) as writer:
69
- writer.write_table(table)
70
-
71
- print(f"Saved {len(rows)} cells to {output_path}")
72
-
73
-
74
- if __name__ == "__main__":
75
- main()