Metadata-Version: 2.4
Name: biomed-parity
Version: 0.5.0
Summary: Native Python implementations and R reference checks for biomedical statistics
Author: xiangjiawei1234-prog
License-Expression: MIT
Project-URL: Homepage, https://github.com/xiangjiawei1234-prog/biomed-parity
Project-URL: Repository, https://github.com/xiangjiawei1234-prog/biomed-parity
Project-URL: PyPI, https://pypi.org/project/biomed-parity/
Project-URL: Issues, https://github.com/xiangjiawei1234-prog/biomed-parity/issues
Project-URL: Changelog, https://github.com/xiangjiawei1234-prog/biomed-parity/releases
Keywords: biostatistics,statistics,survival-analysis,competing-risks,roc-analysis,clinical-trials,epidemiology,reproducibility,r-migration
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy<3,>=1.26
Requires-Dist: scipy<2,>=1.12
Provides-Extra: test
Requires-Dist: pytest<10,>=8; extra == "test"
Provides-Extra: plot
Requires-Dist: matplotlib<4,>=3.8; extra == "plot"
Dynamic: license-file

# biomed-parity

[![PyPI](https://img.shields.io/pypi/v/biomed-parity.svg)](https://pypi.org/project/biomed-parity/)
[![CI](https://github.com/xiangjiawei1234-prog/biomed-parity/actions/workflows/ci.yml/badge.svg)](https://github.com/xiangjiawei1234-prog/biomed-parity/actions/workflows/ci.yml)
[![MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/LICENSE)

biomed-parity 用 Python 实现一组生物医学统计方法，适用于将分析流程从 R 迁到 Python、复现既有结果或核对统计口径。常规计算仅依赖 NumPy 和 SciPy，无需安装 R。方法文档记录对应的 R 调用、版本、数值对照与已知差异。

*Native Python implementations of selected biostatistical methods with reproducible R reference cases.*

## 安装

需要 Python 3.11 或更高版本。

```sh
python -m pip install biomed-parity
```

使用 ROC、RMST 或区间删失等绘图功能时，安装可选的 Matplotlib 依赖。

```sh
python -m pip install "biomed-parity[plot]"
```

## 快速上手

### 比较生存曲线

用 log-rank 检验比较两组右删失数据。事件编码为 `1`，右删失编码为 `0`。

```python
from biomed_parity.survival import survdiff

result = survdiff(
    time=[1, 2, 3, 4, 5, 6, 7, 8],
    event=[1, 0, 1, 1, 0, 1, 1, 0],
    group=["A", "A", "A", "A", "B", "B", "B", "B"],
    rho=0,
)
print(f"chi2={result.statistic:.3f}, df={result.df}, p={result.pvalue:.4f}")
# chi2=6.118, df=1, p=0.0134
```

### 估计 ROC AUC 和置信区间

标签 `1` 表示病例，`0` 表示对照。默认分数越高越倾向于病例。

```python
from biomed_parity.roc import delong_auc

result = delong_auc(
    labels=[0, 1, 0, 1, 0, 1, 1],
    scores=[1, 2, 2, 3, 3, 4, 5],
)
print(round(result.auc, 3), tuple(round(x, 3) for x in result.confidence_interval))
# 0.833 (0.517, 1.0)
```

## 方法与文档

| 领域 | 主要功能 | 方法文档 |
| --- | --- | --- |
| 生存与多状态 | log-rank、RMST、区间删失、Royston–Parmar 样条、多状态转移和纵向–生存联合模型 | [区间删失](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/interval-regression.md) · [样条生存](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/royston-parmar-regression.md) · [多状态](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/multistate.md) · [联合模型](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/joint-prediction.md) |
| 诊断准确性 | DeLong 检验、部分 AUC、Bootstrap、阈值、平滑和绘图 | [DeLong](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/roc-delong.md) · [ROC Bootstrap](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/roc-bootstrap.md) · [绘图](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/roc-plot.md) |
| 试验设计 | 疗效、无效与伤害的组序贯边界，加权 log-rank、MaxCombo 和精确二项设计 | [组序贯设计](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/group-sequential.md) · [MaxCombo](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/group-sequential-maxcombo.md) · [精确二项](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/group-sequential-binomial-spending.md) |
| 临床预测与竞争风险 | 惩罚逻辑回归和有序回归、预测校准、时间依赖 AUC 与 Brier 评分 | [有序回归](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/rms-ordinal.md) · [竞争风险评分](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/competing-risk-scores.md) · [校准](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/competing-risk-calibration.md) |
| 多重插补 | 链式插补、两层模型、非随机缺失敏感性分析和推断合并 | [链式插补](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/chained-imputation.md) · [合并推断](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/mi-pooling.md) |
| 复杂抽样 | 调查加权 GLM 和 Cox 模型、权重校准、Taylor 方差和重复权重 | [GLM](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/survey-glm-standard.md) · [Cox](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/survey-cox.md) · [重复权重](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/survey-replicates.md) |
| Meta 分析 | REML、相关效应量、多层与多结局模型、随机斜率和空间协方差 | [多层模型](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/meta-multilevel.md) · [随机斜率](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/meta-random-slopes.md) |
| 计数模型与表达分析 | 零膨胀混合模型、负二项似然比检验、VST、rlog 和逐特征混合模型 | [零膨胀负二项](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/zinb2-random-intercept.md) · [rlog](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/deseq2-rlog.md) · [dream](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/methods/dream.md) |

逐项覆盖范围、R 包对应关系及未覆盖场景见[完整方法文档](https://github.com/xiangjiawei1234-prog/biomed-parity/tree/main/docs/methods)和[迁移目录](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/catalog/progress.json)。

## 如何验证

仓库中的 `validation/` 保存参考输入、R 脚本和带版本信息的数值快照。默认测试读取快照，无需安装 R。Bootstrap 通常使用固定重采样索引，随机插补按分布和蒙特卡洛误差核对。各方法的容差、兼容范围和已知差异见方法文档及[验证记录](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/docs/validation.md)。

从源码运行默认测试。

```sh
git clone https://github.com/xiangjiawei1234-prog/biomed-parity.git
cd biomed-parity
python3 -m venv .venv
.venv/bin/python -m pip install -e '.[test]'
.venv/bin/python -m pytest -q
```

安装 R 后，可在同一源码目录运行实时对照，例如复核生存曲线比较。各方法所需的 R 包版本及独立 R 库设置见对应方法文档。

```sh
Rscript --vanilla validation/install_r.R
R_LIBS_USER="$PWD/.r-lib" BIOMED_R_PARITY=1 .venv/bin/python -m pytest -q -W error tests/test_survival.py
```

## 贡献与许可

项目处于 Alpha 阶段，面向研究与方法复现。欢迎提交数值差异、覆盖需求或文档问题。[提交 Issue](https://github.com/xiangjiawei1234-prog/biomed-parity/issues) · [贡献指南](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/CONTRIBUTING.md) · [版本记录](https://github.com/xiangjiawei1234-prog/biomed-parity/releases) · [MIT 许可证](https://github.com/xiangjiawei1234-prog/biomed-parity/blob/main/LICENSE)
