A high-performance survival analysis library written in Rust with Python bindings
This project has been archived.
The maintainers of this project have marked this project as archived. No new releases are expected.
Project description
survival-rs
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)
pip install survival-rs
From Source
Prerequisites
Install maturin:
pip install maturin
Build and Install
Build the Python wheel:
maturin build --release
Install the wheel:
pip install target/wheels/survival_rs-*.whl
For development:
maturin develop
Usage
Aalen's Additive Regression Model
from survival import AaregOptions, aareg
data = [
[1.0, 0.0, 0.5],
[2.0, 1.0, 1.5],
[3.0, 0.0, 2.5],
]
variable_names = ["time", "event", "covariate1"]
# Create options with required parameters (formula, data, variable_names)
options = AaregOptions(
formula="time + event ~ covariate1",
data=data,
variable_names=variable_names,
)
# Optional: modify default values via setters
# options.weights = [1.0, 1.0, 1.0]
# options.qrtol = 1e-8
# options.dfbeta = True
result = aareg(options)
print(result)
Penalized Splines (P-splines)
from survival import PSpline
x = [0.1 * i for i in range(100)]
pspline = PSpline(
x=x,
df=10,
theta=1.0,
eps=1e-6,
method="GCV",
boundary_knots=(0.0, 10.0),
intercept=True,
penalty=True,
)
pspline.fit()
Concordance Index
from survival import perform_concordance1_calculation
time_data = [1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
indices = [0, 1, 2, 3, 4]
ntree = 5
result = perform_concordance1_calculation(time_data, weights, indices, ntree)
print(f"Concordance index: {result['concordance_index']}")
Cox Regression with Frailty
from survival import perform_cox_regression_frailty
result = perform_cox_regression_frailty(
time_data=[...],
status_data=[...],
covariates=[...],
# ... other parameters
)
Person-Years Calculation
from survival import perform_pyears_calculation
result = perform_pyears_calculation(
time_data=[...],
weights=[...],
# ... other parameters
)
Kaplan-Meier Survival Curves
from survival import survfitkm, SurvFitKMOutput
# Example survival data
time = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
status = [1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0] # 1 = event, 0 = censored
weights = [1.0] * len(time) # Optional: equal weights
result = survfitkm(
time=time,
status=status,
weights=weights,
entry_times=None, # Optional: entry times for left-truncation
position=None, # Optional: position flags
reverse=False, # Optional: reverse time order
computation_type=0 # Optional: computation type
)
print(f"Time points: {result.time}")
print(f"Survival estimates: {result.estimate}")
print(f"Standard errors: {result.std_err}")
print(f"Number at risk: {result.n_risk}")
Fine-Gray Competing Risks Model
from survival import finegray, FineGrayOutput
# Example competing risks data
tstart = [0.0, 0.0, 0.0, 0.0]
tstop = [1.0, 2.0, 3.0, 4.0]
ctime = [0.5, 1.5, 2.5, 3.5] # Cut points
cprob = [0.1, 0.2, 0.3, 0.4] # Cumulative probabilities
extend = [True, True, False, False] # Whether to extend intervals
keep = [True, True, True, True] # Which cut points to keep
result = finegray(
tstart=tstart,
tstop=tstop,
ctime=ctime,
cprob=cprob,
extend=extend,
keep=keep
)
print(f"Row indices: {result.row}")
print(f"Start times: {result.start}")
print(f"End times: {result.end}")
print(f"Weights: {result.wt}")
Parametric Survival Regression (Accelerated Failure Time Models)
from survival import survreg, SurvivalFit, DistributionType
# Example survival data
time = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
status = [1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0] # 1 = event, 0 = censored
covariates = [
[1.0, 2.0],
[1.5, 2.5],
[2.0, 3.0],
[2.5, 3.5],
[3.0, 4.0],
[3.5, 4.5],
[4.0, 5.0],
[4.5, 5.5],
]
# Fit parametric survival model
result = survreg(
time=time,
status=status,
covariates=covariates,
weights=None, # Optional: observation weights
offsets=None, # Optional: offset values
initial_beta=None, # Optional: initial coefficient values
strata=None, # Optional: stratification variable
distribution="weibull", # "extreme_value", "logistic", "gaussian", "weibull", or "lognormal"
max_iter=20, # Optional: maximum iterations
eps=1e-5, # Optional: convergence tolerance
tol_chol=1e-9, # Optional: Cholesky tolerance
)
print(f"Coefficients: {result.coefficients}")
print(f"Log-likelihood: {result.log_likelihood}")
print(f"Iterations: {result.iterations}")
print(f"Variance matrix: {result.variance_matrix}")
print(f"Convergence flag: {result.convergence_flag}")
Cox Proportional Hazards Model
from survival import CoxPHModel, Subject
# Create a Cox PH model
model = CoxPHModel()
# Or create with data
covariates = [[1.0, 2.0], [2.0, 3.0], [1.5, 2.5]]
event_times = [1.0, 2.0, 3.0]
censoring = [1, 1, 0] # 1 = event, 0 = censored
model = CoxPHModel.new_with_data(covariates, event_times, censoring)
# Fit the model
model.fit(n_iters=10)
# Get results
print(f"Baseline hazard: {model.baseline_hazard}")
print(f"Risk scores: {model.risk_scores}")
print(f"Coefficients: {model.get_coefficients()}")
# Predict on new data
new_covariates = [[1.0, 2.0], [2.0, 3.0]]
predictions = model.predict(new_covariates)
print(f"Predictions: {predictions}")
# Calculate Brier score
brier = model.brier_score()
print(f"Brier score: {brier}")
# Compute survival curves for new covariates
new_covariates = [[1.0, 2.0], [2.0, 3.0]]
time_points = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] # Optional: specific time points
times, survival_curves = model.survival_curve(new_covariates, time_points)
print(f"Time points: {times}")
print(f"Survival curves: {survival_curves}") # One curve per covariate set
# Create and add subjects
subject = Subject(
id=1,
covariates=[1.0, 2.0],
is_case=True,
is_subcohort=True,
stratum=0
)
model.add_subject(subject)
Cox Martingale Residuals
from survival import coxmart
# Example survival data
time = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
status = [1, 1, 0, 1, 0, 1, 1, 0] # 1 = event, 0 = censored
score = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2] # Risk scores
# Calculate martingale residuals
residuals = coxmart(
time=time,
status=status,
score=score,
weights=None, # Optional: observation weights
strata=None, # Optional: stratification variable
method=0, # Optional: method (0 = Breslow, 1 = Efron)
)
print(f"Martingale residuals: {residuals}")
Survival Difference Tests (Log-Rank Test)
from survival import survdiff2, SurvDiffResult
# Example: Compare survival between two groups
time = [1.0, 2.0, 3.0, 4.0, 5.0, 1.5, 2.5, 3.5, 4.5, 5.5]
status = [1, 1, 0, 1, 0, 1, 1, 1, 0, 1]
group = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2] # Group 1 and Group 2
# Perform log-rank test (rho=0 for standard log-rank)
result = survdiff2(
time=time,
status=status,
group=group,
strata=None, # Optional: stratification variable
rho=0.0, # 0.0 = log-rank, 1.0 = Wilcoxon, other = generalized
)
print(f"Observed events: {result.observed}")
print(f"Expected events: {result.expected}")
print(f"Chi-squared statistic: {result.chi_squared}")
print(f"Degrees of freedom: {result.degrees_of_freedom}")
print(f"Variance matrix: {result.variance}")
API Reference
Classes
Core Models:
AaregOptions: Configuration options for Aalen's additive regression modelPSpline: Penalized spline class for smooth covariate effectsCoxPHModel: Cox proportional hazards model classSubject: Subject data structure for Cox PH modelsConditionalLogisticRegression: Conditional logistic regression modelClogitDataSet: Dataset for conditional logistic regression
Survival Curves:
SurvFitKMOutput: Output from Kaplan-Meier survival curve fittingSurvFitAJ: Output from Aalen-Johansen survival curve fittingNelsonAalenResult: Output from Nelson-Aalen estimatorStratifiedKMResult: Output from stratified Kaplan-Meier
Parametric Models:
SurvivalFit: Output from parametric survival regressionDistributionType: Distribution types for parametric models (extreme_value, logistic, gaussian, weibull, lognormal)FineGrayOutput: Output from Fine-Gray competing risks model
Statistical Tests:
SurvDiffResult: Output from survival difference testsLogRankResult: Output from log-rank testTrendTestResult: Output from trend testsTestResult: General test result outputProportionalityTest: Output from proportional hazards test
Validation:
BootstrapResult: Output from bootstrap confidence interval calculationsCVResult: Output from cross-validationCalibrationResult: Output from calibration analysisPredictionResult: Output from prediction functionsRiskStratificationResult: Output from risk stratificationTdAUCResult: Output from time-dependent AUC calculation
RMST and Survival Metrics:
RMSTResult: Output from RMST calculationRMSTComparisonResult: Output from RMST comparison between groupsMedianSurvivalResult: Output from median survival calculationCumulativeIncidenceResult: Output from cumulative incidence calculationNNTResult: Number needed to treat result
Landmark Analysis:
LandmarkResult: Output from landmark analysisConditionalSurvivalResult: Output from conditional survival calculationHazardRatioResult: Output from hazard ratio calculationSurvivalAtTimeResult: Output from survival at specific timesLifeTableResult: Output from life table calculation
Power and Sample Size:
SampleSizeResult: Output from sample size calculationsAccrualResult: Output from accrual calculations
Utilities:
CoxCountOutput: Output from Cox counting functionsSplitResult: Output from time-splittingLinkFunctionParams: Link function parametersCchMethod: Case-cohort method specificationCohortData: Cohort data structure
Functions
Model Fitting:
aareg(options): Fit Aalen's additive regression modelsurvreg(...): Fit parametric accelerated failure time modelsperform_cox_regression_frailty(...): Fit Cox proportional hazards model with frailty
Survival Curves:
survfitkm(...): Fit Kaplan-Meier survival curvessurvfitaj(...): Fit Aalen-Johansen survival curves (multi-state)nelson_aalen_estimator(...): Calculate Nelson-Aalen estimatorstratified_kaplan_meier(...): Calculate stratified Kaplan-Meier curvesagsurv4(...): Anderson-Gill survival calculations (version 4)agsurv5(...): Anderson-Gill survival calculations (version 5)
Statistical Tests:
survdiff2(...): Perform survival difference tests (log-rank, Wilcoxon, etc.)logrank_test(...): Perform log-rank testfleming_harrington_test(...): Perform Fleming-Harrington weighted testlogrank_trend(...): Perform log-rank trend testlrt_test(...): Likelihood ratio testwald_test_py(...): Wald testscore_test_py(...): Score testph_test(...): Proportional hazards assumption test
Residuals:
coxmart(...): Calculate Cox martingale residualsagmart(...): Calculate Anderson-Gill martingale residualsschoenfeld_residuals(...): Calculate Schoenfeld residualscox_score_residuals(...): Calculate Cox score residuals
Concordance:
perform_concordance1_calculation(...): Calculate concordance index (version 1)perform_concordance3_calculation(...): Calculate concordance index (version 3)perform_concordance_calculation(...): Calculate concordance index (version 5)concordance(...): General concordance calculation
Validation:
bootstrap_cox_ci(...): Bootstrap confidence intervals for Cox modelsbootstrap_survreg_ci(...): Bootstrap confidence intervals for parametric modelscv_cox_concordance(...): Cross-validation for Cox model concordancecv_survreg_loglik(...): Cross-validation for parametric model log-likelihoodcalibration(...): Model calibration assessmentpredict_cox(...): Predictions from Cox modelsrisk_stratification(...): Risk group stratificationtd_auc(...): Time-dependent AUC calculationbrier(...): Calculate Brier scoreintegrated_brier(...): Calculate integrated Brier score
RMST and Survival Metrics:
rmst(...): Calculate restricted mean survival timermst_comparison(...): Compare RMST between groupssurvival_quantile(...): Calculate survival quantiles (median, etc.)cumulative_incidence(...): Calculate cumulative incidencenumber_needed_to_treat(...): Calculate NNT
Landmark Analysis:
landmark_analysis(...): Perform landmark analysislandmark_analysis_batch(...): Perform batch landmark analysis at multiple time pointsconditional_survival(...): Calculate conditional survivalhazard_ratio(...): Calculate hazard ratiossurvival_at_times(...): Calculate survival at specific time pointslife_table(...): Generate life table
Power and Sample Size:
sample_size_survival(...): Calculate required sample sizesample_size_survival_freedman(...): Sample size using Freedman's methodpower_survival(...): Calculate statistical powerexpected_events(...): Calculate expected number of events
Utilities:
finegray(...): Fine-Gray competing risks model data preparationperform_pyears_calculation(...): Calculate person-years of observationperform_pystep_calculation(...): Perform step calculationsperform_pystep_simple_calculation(...): Perform simple step calculationsperform_score_calculation(...): Calculate score statisticsperform_agscore3_calculation(...): Calculate score statistics (version 3)survsplit(...): Split survival data at specified timestmerge(...): Merge time-dependent covariatestmerge2(...): Merge time-dependent covariates (version 2)tmerge3(...): Merge time-dependent covariates (version 3)collapse(...): Collapse survival datacoxcount1(...): Cox counting process calculationscoxcount2(...): Cox counting process calculations (version 2)agexact(...): Exact Anderson-Gill calculationsnorisk(...): No-risk calculationscipoisson(...): Poisson confidence intervalscipoisson_exact(...): Exact Poisson confidence intervalscipoisson_anscombe(...): Anscombe Poisson confidence intervalscox_callback(...): Cox model callback for iterative fitting
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 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
Build the Rust library:
cargo build
Run tests:
cargo test
Format code:
cargo fmt
The codebase is organized with:
- Core routines in
src/ - Tests and examples in
test/ - Python bindings using PyO3
Dependencies
- PyO3 - Python bindings
- ndarray - N-dimensional arrays
- faer - Pure-Rust linear algebra
- itertools - Iterator utilities
- rayon - Parallel computation
Compatibility
- This build is for Python only. R/extendr bindings are currently disabled.
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file survival_rs-1.1.2.tar.gz.
File metadata
- Download URL: survival_rs-1.1.2.tar.gz
- Upload date:
- Size: 179.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c055521472384fff1704b6720d5d36ae8a1a5c98cf78786f61eb2c01e384cbf
|
|
| MD5 |
80e90abf7bedfe5f903008a975952151
|
|
| BLAKE2b-256 |
56962280137ff76a94b031a2eb10e9a8ddc60e7a4bc4b85105cb5fc79ccf658a
|
File details
Details for the file survival_rs-1.1.2-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: survival_rs-1.1.2-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0a7aa77eb10d8d22788193a2a792780002bf30a0e03f83da2d4b8884d435889
|
|
| MD5 |
e49ddd876febe83207f5c3d29346b19b
|
|
| BLAKE2b-256 |
cd018be5f4a0bbe7cc8a61f5efe84e01e8c2e19f252306d47fb65fcd1871e5cf
|
File details
Details for the file survival_rs-1.1.2-cp314-cp314-manylinux_2_39_x86_64.whl.
File metadata
- Download URL: survival_rs-1.1.2-cp314-cp314-manylinux_2_39_x86_64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.14, manylinux: glibc 2.39+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06f2fae95eb4ff0d9ee9cb75a2dc8ebb72e710d1b54a68b1d98e785789a3cc2d
|
|
| MD5 |
00fb14de5087194f0945dc8202abbd04
|
|
| BLAKE2b-256 |
cc4ce2e346df5a77ca96cdb2009f2d6d1d8ec1694893dc6c69b75e07bedc95cf
|
File details
Details for the file survival_rs-1.1.2-cp314-cp314-manylinux_2_39_aarch64.whl.
File metadata
- Download URL: survival_rs-1.1.2-cp314-cp314-manylinux_2_39_aarch64.whl
- Upload date:
- Size: 959.1 kB
- Tags: CPython 3.14, manylinux: glibc 2.39+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79dbf6257f4833bc5193aae42d79587ce1caddf858d2a8cbd9f3c89074b046b1
|
|
| MD5 |
60fe6fb1d8a831f84d6391ddb1246ca1
|
|
| BLAKE2b-256 |
51dbf45c9bde8a5ed30785d9190030b453a0a3ae8f092a6353ff8a34d550711b
|
File details
Details for the file survival_rs-1.1.2-cp314-cp314-macosx_11_0_arm64.whl.
File metadata
- Download URL: survival_rs-1.1.2-cp314-cp314-macosx_11_0_arm64.whl
- Upload date:
- Size: 844.1 kB
- Tags: CPython 3.14, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95aad12d5e83e3f3db7296fa259187f5f04c9980737a6f087bebf4f04401e1b1
|
|
| MD5 |
fd261b3c342c0d92fe630373b4216ef2
|
|
| BLAKE2b-256 |
ea17fc61a70b7775be73ef98ea7fc135f98612ca98a66dd135360fcb494cb7ac
|
File details
Details for the file survival_rs-1.1.2-cp314-cp314-macosx_10_12_x86_64.whl.
File metadata
- Download URL: survival_rs-1.1.2-cp314-cp314-macosx_10_12_x86_64.whl
- Upload date:
- Size: 1.0 MB
- Tags: CPython 3.14, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eacea145e349d226d22120a12c7e4f3cc3e82e00be0bdd000fe069577b90ae84
|
|
| MD5 |
fde90c95889ffa5f883a72987c2ccb11
|
|
| BLAKE2b-256 |
7972625f99134ca109983601cd28854ad649fe63f022b0805f4dd65bf165ce7e
|