Skip to main content

GeneFlowDecoder

z → X 的细胞状态解码器:Gene Transformer + Conditional Rectified Flow (CFM), 把固定的细胞表征 z 解码成 log1p 归一化表达谱 X。

设计目标:接口极简、可复现、单卡可跑 2 万+ 基因面板;z 默认冻结, 不参与反传,也不被优化器更新。

from geneflowdecoder import GeneFlowDecoder

model = GeneFlowDecoder(n_genes=2048, z_dim=256, d=512, depth=8)

model.fit(X_train, z_train,               # X: [N, G] log1p; z: [N, D]
          X_val=X_val, z_val=z_val,       # 可选,开启早停
          epochs=200, batch_size=64)

X_hat = model(z_test)           # 采样(生成式,log1p 空间)
X_hat = model(z_test, steps=8, deterministic=False)

X_rec = model.predict(z_test)  # 确定性还原:NB 均值

安装

# 从源码(开发模式)
git clone https://github.com/your-org/geneflowdecoder.git
cd geneflowdecoder
pip install -e .[dev]

# 或构建 wheel 后安装
python -m build
pip install dist/geneflowdecoder-0.1.0-py3-none-any.whl

依赖:torch >= 2.1、numpy >= 1.24。没有 GPU 也能在 CPU 上跑(只是慢)。


API

GeneFlowDecoder(...)

参数 默认 说明
n_genes — 基因数,即输出维度 G
z_dim — 细胞状态 z 的维度
d 768 Transformer 宽度
depth 16 Transformer 层数
n_heads 12 注意力头数
chunk_size 2048 基因轴分块,控制显存峰值
gradient_checkpoint True 激活检查点,省显存
cfg FlowConfig() flow / loss 超参
freeze_z True z 全程 detach,只读条件
device "auto" "auto" / "cuda" / "cpu"

model.fit(X, z, *, X_val=None, z_val=None, epochs=200, batch_size=64, lr=2e-4, weight_decay=0.02, patience=20, num_workers=0, verbose=1) -> self

  • X / X_val:[N, G] log1p 表达,numpy 或 torch 都行
  • z / z_val:[N, z_dim];freeze_z=True 时绝不会被修改或反传
  • 给了验证集 → 早停 + 自动回滚最优权重

model(z, *, steps=4, method="midpoint", deterministic=False) -> Tensor

Flow 采样,steps=4 通常够用。

model.predict(z) -> Tensor

确定性输出 E[X] 的 log1p(NB mean)。转伪计数:(log_mu.exp() - 1).clamp(min=0)。

model.save(path) / GeneFlowDecoder.load(path, device="auto")


FlowConfig

from geneflowdecoder import FlowConfig

cfg = FlowConfig(
    sigma_min=1e-4,     # t 的截断,防 t≈0 数值爆炸
    nb_weight=1.0,      # NB 似然辅助损失权重
    corr_weight=0.1,    # cell-wise Pearson 相关性正则
)
model = GeneFlowDecoder(n_genes=G, z_dim=D, cfg=cfg)

发布

python -m build          # 生成 sdist + wheel 到 dist/
twine check dist/*       # 校验元数据
twine upload dist/*      # 发布到 PyPI(需账号 token)

目录结构

geneflowdecoder/
├── pyproject.toml
├── README.md
├── LICENSE
├── src/geneflowdecoder/
│   ├── __init__.py        # 公开 API
│   ├── _core.py           # 模型 + fit/forward/predict
│   └── _version.py
└── tests/
    └── test_api.py

License

MIT

Metadata

Release files for geneflowdecoder 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geneflowdecoder 0.1.0
File Size Uploaded Provenance Actions
geneflowdecoder-0.1.0.tar.gz 13.3 kB Without provenance Details for geneflowdecoder-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for geneflowdecoder 0.1.0
File Interpreter ABI Platform Actions
geneflowdecoder-0.1.0-py3-none-any.whl Python 3 none any Details for geneflowdecoder-0.1.0-py3-none-any.whl

Total release size: 24.1 kB

Release files / geneflowdecoder-0.1.0.tar.gz

Download URL geneflowdecoder-0.1.0.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
823ad9ba1b0461f84d4e032dcd54b00953ed6ef5c11a438c6028f47ec212dfe2
BLAKE2b-256 checksum
How to use checksums
65937bf1807fdb62a4da3d38a6a1b946e89b085744069cc6fbfe14a12ec827bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Release files / geneflowdecoder-0.1.0-py3-none-any.whl

Download URL geneflowdecoder-0.1.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
169a9f7cf67a23fa407a5426685dce7ee8e964368f42f26087d3f7c55094c9d6
BLAKE2b-256 checksum
How to use checksums
4b01a19715d090b8e07bdbe3408ddae46ad5c10ba04be0aef70e50cffeadb560
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Security for geneflowdecoder 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page