survival
A high-performance survival analysis library written in Rust, with a Python API powered by PyO3 and maturin.
Features
- Core survival analysis routines
- Cox proportional hazards models with frailty
- Kaplan-Meier and Aalen-Johansen (multi-state) survival curves
- Nelson-Aalen estimator
- Parametric accelerated failure time models
- Fine-Gray competing risks model
- Penalized splines (P-splines) for smooth covariate effects
- Concordance index calculations
- Person-years calculations
- Score calculations for survival models
- Residual analysis (martingale, Schoenfeld, score residuals)
- Bootstrap confidence intervals
- Cross-validation for model assessment
- Statistical tests (log-rank, likelihood ratio, Wald, score, proportional hazards)
- Sample size and power calculations
- RMST (Restricted Mean Survival Time) analysis
- Landmark analysis
- Calibration and risk stratification
- Time-dependent AUC
- Conditional logistic regression
- Time-splitting utilities
Installation
From PyPI (Recommended)
From Source
Prerequisites
Install maturin:
Build and Install
Build the Python wheel:
Install the wheel:
For development:
Usage
Aalen's Additive Regression Model
=
=
# Create options with required parameters (formula, data, variable_names)
=
# Optional: modify default values via setters
# options.weights = [1.0, 1.0, 1.0]
# options.qrtol = 1e-8
# options.dfbeta = True
=
Penalized Splines (P-splines)
=
=
Concordance Index
=
=
=
= 5
=
Cox Regression with Frailty
=
Person-Years Calculation
# Low-level API: inputs should match ratetable-style dimensions/cuts.
=
Kaplan-Meier Survival Curves
# Example survival data
=
= # 1 = event, 0 = censored
= * # Optional: equal weights
=
Fine-Gray Competing Risks Model
# Example competing risks data
=
=
= # Cut points
= # Cumulative probabilities
= # Whether to extend intervals
= # Which cut points to keep
=
Parametric Survival Regression (Accelerated Failure Time Models)
# Example survival data
=
= # 1 = event, 0 = censored
=
# Fit parametric survival model
=
Cox Proportional Hazards Model
# Create a Cox PH model
=
# Or create with data
=
=
= # 1 = event, 0 = censored
=
# Fit the model
# Get results
# Predict on new data
=
=
# Calculate Brier score
=
# Compute survival curves for new covariates
=
= # Optional: specific time points
, =
# One curve per covariate set
# Create and add subjects
=
Cox Martingale Residuals
# Example survival data
=
= # 1 = event, 0 = censored
= # Risk scores
# Calculate martingale residuals
=
Survival Difference Tests (Log-Rank Test)
# Example: Compare survival between two groups
=
=
= # Group 1 and Group 2
# Perform log-rank test (rho=0 for standard log-rank)
=
Built-in Datasets
The library includes 33 classic survival analysis datasets:
# Load the lung cancer dataset
=
# Load the acute myelogenous leukemia dataset
=
# Load the veteran's lung cancer dataset
=
Available datasets:
load_lung()- NCCTG Lung Cancer Dataload_aml()- Acute Myelogenous Leukemia Survival Dataload_veteran()- Veterans' Administration Lung Cancer Studyload_ovarian()- Ovarian Cancer Survival Dataload_colon()- Colon Cancer Dataload_pbc()- Primary Biliary Cholangitis Dataload_cgd()- Chronic Granulomatous Disease Dataload_bladder()- Bladder Cancer Recurrencesload_heart()- Stanford Heart Transplant Dataload_kidney()- Kidney Catheter Dataload_rats()- Rat Treatment Dataload_stanford2()- Stanford Heart Transplant Data (Extended)load_udca()- UDCA Clinical Trial Dataload_myeloid()- Acute Myeloid Leukemia Clinical Trialload_flchain()- Free Light Chain Dataload_transplant()- Liver Transplant Dataload_mgus()- Monoclonal Gammopathy Dataload_mgus2()- Monoclonal Gammopathy Data (Updated)load_diabetic()- Diabetic Retinopathy Dataload_retinopathy()- Retinopathy Dataload_gbsg()- German Breast Cancer Study Group Dataload_rotterdam()- Rotterdam Tumor Bank Dataload_logan()- Logan Unemployment Dataload_nwtco()- National Wilms Tumor Study Dataload_solder()- Solder Joint Dataload_tobin()- Tobin's Tobit Dataload_rats2()- Rat Tumorigenesis Dataload_nafld()- Non-Alcoholic Fatty Liver Disease Dataload_cgd0()- CGD Baseline Dataload_pbcseq()- PBC Sequential Dataload_hoel()- Hoel's Cancer Survival Dataload_myeloma()- Myeloma Survival Dataload_rhdnase()- rhDNase Clinical Trial Data
API Reference
The public Python surface is broad and evolves quickly. For the most accurate, version-matched signatures, use the checked-in type stubs:
python/survival/_survival.pyi: core PyO3 bindings exposed bysurvival._survival.python/survival/sklearn_compat.py: scikit-learn-compatible estimators and streaming wrappers.survival.pyi: additional typing surface used by downstream tooling.
To inspect available symbols at runtime:
=
PSpline Options
The PSpline class provides penalized spline smoothing:
Constructor Parameters:
x: Covariate vector (list of floats)df: Degrees of freedom (integer)theta: Roughness penalty (float)eps: Accuracy for degrees of freedom (float)method: Penalty method for tuning parameter selection. Supported methods:"GCV"- Generalized Cross-Validation"UBRE"- Unbiased Risk Estimator"REML"- Restricted Maximum Likelihood"AIC"- Akaike Information Criterion"BIC"- Bayesian Information Criterion
boundary_knots: Tuple of (min, max) for the spline basisintercept: Whether to include an intercept in the basispenalty: Whether or not to apply the penalty
Methods:
fit(): Fit the spline model, returns coefficientspredict(new_x): Predict values at new x points
Properties:
coefficients: Fitted coefficients (None if not fitted)fitted: Whether the model has been fitteddf: Degrees of freedomeps: Convergence tolerance
Development
Install development dependencies:
Build the extension in your current environment:
Build the Rust library:
Run Rust tests:
Run Python tests:
Format and lint:
The codebase is organized with:
- Core routines in
src/ - Rust unit/integration tests in
src/tests/ - Python binding tests in
python/tests/ - R validation fixtures and archived reference cases in
test/ - Python bindings using PyO3
Dependencies
Primary dependencies are defined in Cargo.toml and
pyproject.toml, including:
- PyO3 and maturin for Python bindings
- numpy and ndarray for array interop
- faer, rayon, and burn for numerical compute
Compatibility
- This build is for Python only. R/extendr bindings are currently disabled.
- Python 3.11+ and Rust 1.93+ are required.
- macOS users: Ensure you are using the correct Python version and have Homebrew-installed Python if using Apple Silicon.
License
This project is licensed under the MIT License - see the LICENSE file for details.