Metadata-Version: 2.4
Name: caliperx
Version: 0.1.0
Summary: CALIPER-X: predicting and certifying cross-cohort replicability of single-cell differential expression findings
Author: K02 KPMP ScRNA Validation
License-Expression: MIT
Keywords: single-cell,replicability,conformal prediction,differential expression,tabular foundation models
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Provides-Extra: pfn
Requires-Dist: tabpfn>=2; extra == "pfn"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Dynamic: license-file

# caliperx

**CALIPER-X** — predict and certify cross-cohort replicability of single-cell
differential-expression findings, with FDR-controlled certified gene sets.

This package is the software companion of the KPMP re-analysis study
(K02_KPMP_ScRNA_Validation). It implements the method chain:

```
SCISSOR decomposition  →  donor-level reliability  →  closed-form P_rep
        →  CALIPER feature matrix  →  tabular model  →  conformal + BH certification
```

## Install

```bash
pip install caliperx          # core (numpy/scipy/pandas/scikit-learn)
pip install "caliperx[pfn]"   # + TabPFN v3.x backbone
```

Or install from source:

```bash
pip install -e .
pip install -e ".[pfn,dev]"
```

## Quickstart (synthetic data, ~5 s)

```python
import numpy as np
from caliperx import (
    scissor_decompose, donor_reliability,
    replication_probability, core_features, certify, auprc,
)
from caliperx.simulate import simulate_dataset
from caliperx.models import cross_val_predict

# 1. simulate a discovery cohort (30 case / 30 ctrl, 5 cell types, 600 genes)
d = simulate_dataset(n_genes=600, n_case=30, n_ctrl=30, seed=1)

# 2. SCISSOR decomposition (composition vs state)
dec = scissor_decompose(d["t_std"], d["t_naive"], d["is_case"], d["is_ctrl"])

# 3. reliability layer (EB shrinkage + bootstrap sign stability)
st = donor_reliability(d["t_std"], d["is_case"], d["is_ctrl"], n_boot=200)
sn = donor_reliability(d["t_naive"], d["is_case"], d["is_ctrl"], n_boot=200)

# 4. closed-form replication probability for a future cohort of 20/20
p_rep = replication_probability(
    st["dc"].values, st["sw2"].values, st["v_obs"].values, st["lam"].values,
    n1_new=20, n0_new=20, tau=0.3,
)

# 5. CALIPER feature matrix (23 features)
n = len(dec)
F = core_features(
    dec, st, sn, p_rep,
    padj_std=np.full(n, 0.01), padj_naive=np.full(n, 0.01),
    muscat_lfc=dec["lfc_naive"].values,
    expr_mean=np.ones(n), detect_frac=np.full(n, 0.5),
)

# 6. train a tabular model under the standard 5-fold protocol
oof = cross_val_predict(F.values.astype(np.float32), d["y_true"], kind="gbm", cap=400)
print("AUPRC:", auprc(d["y_true"], oof))

# 7. FDR-controlled certification (conformal p + BH at q=0.10)
out = certify(oof, d["y_true"] == 0, q=0.10)
print("certified:", int(out["certified"].sum()), "genes")
```

Or entirely from the CLI:

```bash
caliperx simulate --out demo.npz --n-genes 600
caliperx features --input demo.npz --out features.csv
caliperx eval     --scores features.csv --label-col y_true --out metrics.json
caliperx certify  --scores features.csv --score-col p_rep --label-col y_true --q 0.10 --out certified.csv
```

## API overview

| Module | Key functions |
|---|---|
| `caliperx.decomp` | `composition_frozen_pseudobulk`, `scissor_decompose` |
| `caliperx.reliability` | `donor_reliability` (EB λ, bootstrap sign stability, AUC) |
| `caliperx.prior` | `replication_probability` (closed-form P_rep) |
| `caliperx.features` | `core_features` (CALIPER 23f matrix) |
| `caliperx.models` | `make_model`, `cross_val_predict` (5-fold, balanced cap) |
| `caliperx.certify` | `conformal_pvalues`, `bh`, `certify` |
| `caliperx.eval` | `auroc`, `auprc`, `topk_precision`, `label_efficiency` |
| `caliperx.simulate` | `simulate_dataset` (synthetic ground truth) |

## Method notes (fixed definitions)

- **SCISSOR**: composition-frozen pseudobulk `T_std = Σ_c w_c·m_c` with control
  mean fractions `w`; `comp_part = lfc_naive − lfc_std`; `phi = comp_part / lfc_naive`.
- **Reliability**: limma-style EB prior `τ² = max(Var(δ) − mean(v_obs), 1e-4)`,
  `λ = τ²/(τ²+v_obs)`; bootstrap sign stability over donor resamples.
- **P_rep**: `μ = λ·δ`, `s² = λ·v_obs + sw2·(1/n1_new + 1/n0_new)`;
  direction-aware Gaussian tail probability at threshold `τ` (or sign-only).
- **Certification**: conformal p-values against a negative reference set +
  Benjamini–Hochberg at level `q`. Simulation-verified FDR control
  (conservative under heterogeneous noise) in the companion study.

## Reproducibility & scope

The companion study applies this pipeline to KPMP + 3 external kidney cohorts
(see `../final_numbers.md`, `../methods_rigor_summary.md` for the full
evidence ledger and non-claims). The package focuses on the portable method
core; cohort-specific data ingestion lives in the study scripts.

License: MIT (the package). Note that TabPFN model weights carry their own
(non-commercial) license — the core package has no dependency on them.
