PySurv

PyPI - Python Version PyPI - Version PyPI - License PyPI Downloads PyPI Downloads GitHub Repo stars

PySurv is a Python package for generating and plotting Kaplan-Meier survival curves. It is designed to be simple and intuitive and is inspired from MatSurv.

KM Curve Example Summary Example

Features

Installation

You can install PySurv via PyPI:

pip install pysurv

Usage

Here’s a detailed example of how to use PySurv for generating data, plotting KM curves, and customizing the output:

import pysurv

# Generate synthetic data
data = pysurv.generate_time_to_event_data(n_samples=2000, hazard_ratio=0.5, censoring_rate=0.3)

# Plot Kaplan-Meier curve with custom labels and colors
pysurv.plot_km_curve(
    data, 
    time_col='time', 
    event_col='event', 
    group_col='group', 
    group_labels=('Control Group', 'Treatment Group'),
    title="Kaplan-Meier Survival Curve",
    y_label="Survival Probability",
    x_label="Time (months)",
    colors=['orange', 'blue'], 
    show_ci=True,
    survival_time_points=[12, 24],  # Show % survival at 12 and 24 months
    return_summary=False,
    savepath='./KMCurve.svg'
)

Output:

Function Reference

plot_km_curve

Plots Kaplan-Meier survival curves and calculates hazard ratios, p-values, and confidence intervals.

Parameters:

*Note:

Maintained by

If you would like to request a new feature, please feel free to raise an issue on GitHub or reach out via email. If you found this code useful, you can support by leaving a star.