{"id":29624924,"url":"https://github.com/novartis/torchsurv","last_synced_at":"2026-01-26T21:16:07.909Z","repository":{"id":228439390,"uuid":"765325999","full_name":"Novartis/torchsurv","owner":"Novartis","description":"Deep survival analysis made easy ","archived":false,"fork":false,"pushed_at":"2025-07-01T18:53:12.000Z","size":5549,"stargazers_count":141,"open_issues_count":7,"forks_count":13,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-07-15T04:21:38.373Z","etag":null,"topics":["deep-learning","pytorch","survival-analysis"],"latest_commit_sha":null,"homepage":"http://opensource.nibr.com/torchsurv/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Novartis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-02-29T17:50:27.000Z","updated_at":"2025-07-08T18:22:58.000Z","dependencies_parsed_at":"2024-11-08T18:29:33.685Z","dependency_job_id":"509213dd-92ee-4ea0-b535-a2bea16657a4","html_url":"https://github.com/Novartis/torchsurv","commit_stats":null,"previous_names":["novartis/torchsurv"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Novartis/torchsurv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Novartis%2Ftorchsurv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Novartis%2Ftorchsurv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Novartis%2Ftorchsurv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Novartis%2Ftorchsurv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Novartis","download_url":"https://codeload.github.com/Novartis/torchsurv/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Novartis%2Ftorchsurv/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266248501,"owners_count":23899056,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","pytorch","survival-analysis"],"created_at":"2025-07-21T06:03:07.447Z","updated_at":"2026-01-26T21:16:07.897Z","avatar_url":"https://github.com/Novartis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n      \u003c/picture\u003e\n\u003cimg src=docs/_static/logo_firecamp.png width=\"200\"\u003e\n  \u003c/picture\u003e\n\u003c/p\u003e\n\u003ch1 align=\"center\"\u003e\nDeep survival analysis made easy\n\u003c/h1\u003e\n\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Python](https://img.shields.io/pypi/pyversions/torchsurv?label=Python)](https://pypi.org/project/torchsurv/)\n[![PyPI - Version](https://img.shields.io/pypi/v/torchsurv?color=green\u0026label=PyPI)](https://pypi.org/project/torchsurv/)\n[![Conda](https://img.shields.io/conda/v/conda-forge/torchsurv?label=Conda\u0026color=green)](https://anaconda.org/conda-forge/torchsurv)\n[![PyPI Downloads](https://img.shields.io/pypi/dm/torchsurv.svg?label=PyPI%20downloads)](\nhttps://pypi.org/project/torchsurv/)\n[![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/torchsurv.svg?label=Conda%20downloads)](\nhttps://anaconda.org/conda-forge/torchsurv)\n\n![CodeQC](https://github.com/Novartis/torchsurv/actions/workflows/codeqc.yml/badge.svg?branch=main)\n![Docs](https://github.com/Novartis/torchsurv/actions/workflows/docs.yml/badge.svg?branch=main)\n[![CodeFactor](https://www.codefactor.io/repository/github/novartis/torchsurv/badge/main)](https://www.codefactor.io/repository/github/novartis/torchsurv/overview/main)\n[![JOSS](https://joss.theoj.org/papers/02d7496da2b9cc34f9a6e04cabf2298d/status.svg)](https://joss.theoj.org/papers/02d7496da2b9cc34f9a6e04cabf2298d)\n[![License](https://img.shields.io/badge/License-MIT-black)](https://opensource.org/licenses/MIT)\n[![Documentation](https://img.shields.io/badge/GithubPage-Sphinx-blue)](https://opensource.nibr.com/torchsurv/)\n\n`TorchSurv` is a Python package that serves as a companion tool to perform deep survival modeling within the `PyTorch` environment. Unlike existing libraries that impose specific parametric forms on users, `TorchSurv` enables the use of custom `PyTorch`-based deep survival models.  With its lightweight design, minimal input requirements, full `PyTorch` backend, and freedom from restrictive survival model parameterizations, `TorchSurv` facilitates efficient survival model implementation, particularly beneficial for high-dimensional input data scenarios.\n\nIf you find this repository useful, please give us a star!   🌟 ⭐ 🌟\n\n---\n\n## TL;DR\n\nOur idea is to **keep things simple**. You are free to use any model architecture you want! Our code has 100% PyTorch backend and behaves like any other functions (losses or metrics) you may be familiar with.\n\nOur functions are designed to support you, not to make you jump through hoops. Here's a pseudo code illustrating how easy is it to use `TorchSurv` to fit and evaluate a Cox proportional hazards model:\n\n```python\nfrom torchsurv.loss import cox\nfrom torchsurv.metrics.cindex import ConcordanceIndex\n\n# Pseudo training loop\nfor data in dataloader:\n    x, event, time = data\n    estimate = model(x)  # shape = torch.Size([64, 1]), if batch size is 64\n    loss = cox.neg_partial_log_likelihood(estimate, event, time)\n    loss.backward()  # native torch backend\n\n# You can check model performance using our evaluation metrics, e.g, the concordance index with\ncindex = ConcordanceIndex()\ncindex(estimate, event, time)\n\n# You can obtain the confidence interval of the c-index\ncindex.confidence_interval()\n\n# You can test whether the observed c-index is greater than 0.5 (random estimator)\ncindex.p_value(method=\"noether\", alternative=\"two_sided\")\n\n# You can even compare the metrics between two models (e.g., vs. model B)\ncindex.compare(cindexB)\n```\n\n---\n\n## A collaborative project\n\nThis package was developed by **Novartis** and the **US Food and Drug Administration (FDA)** as part of a **research collaboration** agreement on [radiogenomics](https://www.medrxiv.org/content/10.1101/2023.08.30.23294367v1.full.pdf).\n\n`TorchSurv` is now part of the **FDA’s [Regulatory Science Tool Catalog](https://cdrh-rst.fda.gov/torchsurv-deep-learning-tools-survival-analysis)**  🎉.\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cem\u003eDisclaimer Regarding the Catalog of Regulatory Science Tools\u003c/em\u003e\u003c/summary\u003e\n\u003cbr\u003e\nThe enclosed tool is part of the [Catalog of Regulatory Science Tools](https://cdrh-rst.fda.gov/), which provides a peer-reviewed resource for stakeholders to use where standards and qualified Medical Device Development Tools (MDDTs) do not yet exist. These tools do not replace FDA-recognized standards or MDDTs. This catalog collates a variety of regulatory science tools that the FDA's Center for Devices and Radiological Health's (CDRH) Office of Science and Engineering Labs (OSEL) developed. These tools use the most innovative science to support medical device development and patient access to safe and effective medical devices. If you are considering using a tool from this catalog in your marketing submissions, note that these tools have not been qualified as \u003ca href=\"https://www.fda.gov/medical-devices/medical-device-development-tools-mddt\"\u003eMedical Device Development Tools\u003c/a\u003e and the FDA has not evaluated the suitability of these tools within any specific context of use. You may \u003ca href=\"https://www.fda.gov/regulatory-information/search-fda-guidance-documents/requests-feedback-and-meetings-medical-device-submissions-q-submission-program\"\u003erequest feedback or meetings for medical device submissions\u003c/a\u003e as part of the Q-Submission Program.\nFor more information about the Catalog of Regulatory Science Tools, email \u003ca href=\"mailto:RST_CDRH@fda.hhs.gov\"\u003eRST_CDRH@fda.hhs.gov\u003c/a\u003e.\n\n\u003cbr\u003e\nTool Reference\n\n* RST Reference Number: RST24AI17.01\n* Date of Publication: 10/16/2025\n* Recommended Citation: \u003ca href=\"https://cdrh-rst.fda.gov/torchsurv-deep-learning-tools-survival-analysis\"\u003eTorchSurv: Deep Learning Tools for Survival Analysis (RST24AI17.01).\u003c/a\u003e U.S. Food and Drug Administration (2025).\n\n\u003c/details\u003e\n\n---\n\n## Installation and dependencies\n\nFirst, install the package using either [PyPI]([https://pypi.org/](https://pypi.org/project/torchsurv/)) or [Conda]([https://anaconda.org/anaconda/conda](https://anaconda.org/conda-forge/torchsurv))\n\n- Using conda\n```bash\nconda install conda-forge::torchsurv\n```\n- Using PyPI\n```bash\npip install torchsurv\n```\n\u003cdetails\u003e\n\u003csummary\u003e\u003cem\u003eOther installation details \u003c/em\u003e\u003c/summary\u003e\n\u003cbr\u003e\n-  Using for local installation (latest version for example)\n\n```bash\ngit clone \u003crepo\u003e\ncd \u003crepo\u003e\npip install -e .\n```\n\n\nAdditionally, to build the documentation (`notebooks`, `sphinx`) and for package development (`tests`), please see [the development notes](https://opensource.nibr.com/torchsurv/devnotes.html) and\n[dev/environment.yml](dev/environment.yml). This step is **not required** to use `TorchSurv` in your projects but only for optional features.\n\n\u003c/details\u003e\n\n## Getting started\n\nWe recommend starting with the [introductory guide](https://opensource.nibr.com/torchsurv/notebooks/introduction.html), where you'll find an overview of the package's functionalities.\n\n### Create survival data\n\nWe simulate a random batch of 64 subjects. Each subject is associated with a binary event status (= `True` if event occurred), a time-to-event or censoring and 16 covariates.\n\n```python\n\u003e\u003e\u003e import torch\n\u003e\u003e\u003e _ = torch.manual_seed(52)\n\u003e\u003e\u003e n = 64\n\u003e\u003e\u003e x = torch.randn((n, 16))\n\u003e\u003e\u003e event = torch.randint(low=0, high=2, size=(n,), dtype=torch.bool)\n\u003e\u003e\u003e time = torch.randint(low=1, high=100, size=(n,), dtype=torch.float)\n```\n\n### Cox proportional hazards model\n\nThe user is expected to have defined a model that outputs the estimated *log relative hazard* for each subject. For illustrative purposes, we define a simple linear model that generates a linear combination of the covariates.\n\n```python\n\u003e\u003e\u003e from torch import nn\n\u003e\u003e\u003e model_cox = nn.Sequential(nn.Linear(16, 1))\n\u003e\u003e\u003e log_hz = model_cox(x)\n\u003e\u003e\u003e print(log_hz.shape)\ntorch.Size([64, 1])\n```\n\nGiven the estimated log relative hazard and the survival data, we calculate the current loss for the batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.loss.cox import neg_partial_log_likelihood\n\u003e\u003e\u003e loss = neg_partial_log_likelihood(log_hz, event, time)\n\u003e\u003e\u003e print(loss)\ntensor(4.1723, grad_fn=\u003cDivBackward0\u003e)\n```\n\nWe obtain the concordance index for this batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.metrics.cindex import ConcordanceIndex\n\u003e\u003e\u003e with torch.no_grad(): log_hz = model_cox(x)\n\u003e\u003e\u003e cindex = ConcordanceIndex()\n\u003e\u003e\u003e print(cindex(log_hz, event, time))\ntensor(0.4872)\n```\n\nWe obtain the Area Under the Receiver Operating Characteristic Curve (AUC) at a new time t = 50 for this batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.metrics.auc import Auc\n\u003e\u003e\u003e new_time = torch.tensor(50.)\n\u003e\u003e\u003e auc = Auc()\n\u003e\u003e\u003e print(auc(log_hz, event, time, new_time=50))\ntensor([0.4737])\n```\n\n### Weibull accelerated failure time (AFT) model\n\nThe user is expected to have defined a model that outputs for each subject the estimated *log scale* and optionally the *log shape* of the Weibull distribution that the event density follows. In case the model has a single output, `TorchSurv` assume that the shape is equal to 1, resulting in the event density to be an exponential distribution solely parametrized by the scale.\n\nFor illustrative purposes, we define a simple linear model that estimate two linear combinations of the covariates (log scale and log shape parameters).\n\n```python\n\u003e\u003e\u003e from torch import nn\n\u003e\u003e\u003e model_weibull = nn.Sequential(nn.Linear(16, 2))\n\u003e\u003e\u003e log_params = model_weibull(x)\n\u003e\u003e\u003e print(log_params.shape)\ntorch.Size([64, 2])\n```\n\nGiven the estimated log scale and log shape and the survival data, we calculate the current loss for the batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.loss.weibull import neg_log_likelihood_weibull\n\u003e\u003e\u003e loss = neg_log_likelihood_weibull(log_params, event, time)\n\u003e\u003e\u003e print(loss)\ntensor(82931.5078, grad_fn=\u003cDivBackward0\u003e)\n```\n\nTo evaluate the predictive performance of the model, we calculate subject-specific log hazard and survival function evaluated at all times with:\n\n```python\n\u003e\u003e\u003e from torchsurv.loss.weibull import log_hazard\n\u003e\u003e\u003e from torchsurv.loss.weibull import survival_function_weibull\n\u003e\u003e\u003e with torch.no_grad(): log_params = model_weibull(x)\n\u003e\u003e\u003e log_hz = log_hazard(log_params, time)\n\u003e\u003e\u003e print(log_hz.shape)\ntorch.Size([64, 64])\n\u003e\u003e\u003e surv = survival_function_weibull(log_params, time)\n\u003e\u003e\u003e print(surv.shape)\ntorch.Size([64, 64])\n```\n\nWe obtain the concordance index for this batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.metrics.cindex import ConcordanceIndex\n\u003e\u003e\u003e cindex = ConcordanceIndex()\n\u003e\u003e\u003e print(cindex(log_hz, event, time))\ntensor(0.4062)\n```\n\nWe obtain the AUC at a new time t = 50 for this batch with:\n\n```python\n\u003e\u003e\u003e from torchsurv.metrics.auc import Auc\n\u003e\u003e\u003e new_time = torch.tensor(50.)\n\u003e\u003e\u003e log_hz_t = log_hazard(log_params, time=new_time)\n\u003e\u003e\u003e auc = Auc()\n\u003e\u003e\u003e print(auc(log_hz_t, event, time, new_time=new_time))\ntensor([0.3509])\n```\n\nWe obtain the integrated brier-score with:\n\n```python\n\u003e\u003e\u003e from torchsurv.metrics.brier_score import BrierScore\n\u003e\u003e\u003e brier_score = BrierScore()\n\u003e\u003e\u003e bs = brier_score(surv, event, time)\n\u003e\u003e\u003e print(brier_score.integral())\ntensor(0.4447)\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eRelated packages\u003c/summary\u003e\n\nThe table below compares the functionalities of `TorchSurv` with those of\n[auton-survival](https://proceedings.mlr.press/v182/nagpal22a.html),\n[pycox](http://jmlr.org/papers/v20/18-424.html),\n[torchlife](https://sachinruk.github.io/torchlife//index.html),\n[scikit-survival](https://jmlr.org/papers/v21/20-729.html),\n[lifelines](https://joss.theoj.org/papers/10.21105/joss.01317), and\n[deepsurv](https://bmcmedresmethodol.biomedcentral.com/articles/10.1186/s12874-018-0482-1).\nWhile several libraries offer survival modelling functionalities, no existing library provides the flexibility to use a custom PyTorch-based neural networks to define the survival model parameters.\n\nThe outputs of both the log-likelihood functions and the evaluation metrics functions have **undergone thorough comparison with benchmarks generated** using `Python` and `R` packages. The comparisons (at time of publication) are summarised in the [Related packages summary](https://opensource.nibr.com/torchsurv/benchmarks.html).\n\n![Survival analysis libraries in Python](/docs/_static/table_python_benchmark.png)\n![Survival analysis libraries in Python](/docs/_static/table_python_benchmark_legend.png)\n\nSurvival analysis libraries in R. For obtaining the evaluation metrics, packages `survival`, `riskRegression`, `SurvMetrics` and `pec` require the fitted model object as input (a specific object format) and `RisksetROC` imposes a smoothing method. Packages `timeROC`, `riskRegression` and pec force the user to choose a form for subject-specific\nweights (e.g., inverse probability of censoring weighting (IPCW)). Packages `survcomp` and `SurvivalROC` do not implement the general AUC but the censoring-adjusted AUC estimator proposed by Heagerty et al. (2000).\n\n![Survival analysis libraries in R](/docs/_static/table_r_benchmark.png)\n\n\u003c/details\u003e\n\n\n## Citation\n\nIf you use this project in academic work or publications, we appreciate citing it using the following BibTeX entry:\n\n```\n@article{Monod2024,\n    doi = {10.21105/joss.07341},\n    url = {https://doi.org/10.21105/joss.07341},\n    year = {2024},\n    publisher = {The Open Journal},\n    volume = {9},\n    number = {104},\n    pages = {7341},\n    author = {Mélodie Monod and Peter Krusche and Qian Cao and Berkman Sahiner and Nicholas Petrick and David Ohlssen and Thibaud Coroller},\n    title = {TorchSurv: A Lightweight Package for Deep Survival Analysis}, journal = {Journal of Open Source Software}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovartis%2Ftorchsurv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnovartis%2Ftorchsurv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnovartis%2Ftorchsurv/lists"}