Skip to main content

A Python package for plotting Kaplan-Meier survival curves.

Project description

PySurv

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

  • Generate Kaplan-Meier (KM) survival curves
  • Calculate hazard ratios (HR) with 95% confidence intervals using Cox PH method
  • Option to show or hide confidence intervals (CI) for KM curves
  • Flexible plotting with customizable styles, colors, and labels
  • Generate synthetic time-to-event data by specifying hazard ratio and censoring rate.

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:

  • A Kaplan-Meier curve
  • Summary statistics, including median survival time and percentage survival at given time point(s)
  • Hazard ratio and p-value displayed on the plot

Function Reference

plot_km_curve

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

Parameters:

  • data: A Pandas DataFrame containing time-to-event data.
  • time_col: Column name for the time data.
  • event_col: Column name for the event data (1 for event, 0 for censored).
  • group_col: Column name for the binary group data.
  • group_labels: Labels for the groups (default: None).
  • title: Title for the plot (default: None).
  • y_label: Label for the y-axis (default: 'Survival Probability').
  • x_label: Label for the x-axis (default: 'Time (months)').
  • colors: List of colors for the groups (default: ['r', 'b']). If more than two groups, please manually provide a list of colors.
  • line_styles: Line styles for the groups (default: ’-‘).
  • fontsize: Font size for text on KM curve including title, axis labels, and risk tables (default: 18).
  • linewidth: Line width of KM curves (default: 2.5).
  • show_ci: Whether to show confidence intervals on the KM curves (default: False).
  • show_inverted_hr: Whether to show inverted hazard ratio (default: False).
  • survival_time_points: One or more time point(s) at which to estimate percentage survival (default: None).
  • return_summary: Whether to return a summary of survival and hazard ratio statistics (default: False).
  • savepath: Complete path (including filename and extension) to save the KM curve plot (default: None).
  • **kwargs: Additional matplotlib arguments to pass for plotting KM curves.

*Note:

  • PySurv uses a univariable Cox regression model on the 'group_col' variable to compute hazard ratios and p-values.
  • MatSurv deafaults to the 'Mantel-Haenszel' (MH) and 'log rank' method.
  • Under the proportional hazards assumption, Cox regression is equivalent, if not better, to the MH/log-rank method.
  • For more information, see this and this.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysurv-1.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysurv-1.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pysurv-1.0.1.tar.gz.

File metadata

  • Download URL: pysurv-1.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.13

File hashes

Hashes for pysurv-1.0.1.tar.gz
Algorithm Hash digest
SHA256 85f9d00a986fbab6c46193355e2ab3fd0748bd463766a13bb6f6608a847be2fa
MD5 f9131f589c349146ca89beaa2552df13
BLAKE2b-256 dfffa92bef767d578097b0610db622b6eb699515c03090194b7ea5461b26a1af

See more details on using hashes here.

File details

Details for the file pysurv-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pysurv-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.8.13

File hashes

Hashes for pysurv-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee7dafa6f3dec4be9e510fb601bf452c5d600157396c0226e4e161590a4d3fcb
MD5 9d3fb2f18e0efb06c520b2ecb41e0627
BLAKE2b-256 19d808ec21f97c9e69835f01efebdb06bf5d117d9764d7b82cb9d12379c55f93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page