{"id":15118824,"url":"https://github.com/Novartis/torchsurv","last_synced_at":"2025-09-28T01:31:03.086Z","repository":{"id":228439390,"uuid":"765325999","full_name":"Novartis/torchsurv","owner":"Novartis","description":"Deep survival analysis made easy ","archived":false,"fork":false,"pushed_at":"2025-09-22T14:35:06.000Z","size":6075,"stargazers_count":154,"open_issues_count":11,"forks_count":14,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-09-24T06:34:56.432Z","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-02-29T17:50:27.000Z","updated_at":"2025-09-22T07:35: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","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276722005,"owners_count":25692824,"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","status":"online","status_checked_at":"2025-09-24T02:00:09.776Z","response_time":97,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2024-09-26T01:53:38.842Z","updated_at":"2025-09-28T01:31:03.073Z","avatar_url":"https://github.com/Novartis.png","language":"Python","funding_links":[],"categories":["Ranked by starred repositories"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/Novartis/torchsurv/blob/main/docs/source/logo_firecamp.png\" width=\"300\"\u003e\n\u003c/p\u003e\n\n# Deep survival analysis made easy\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\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 consider giving a star! ⭐\n\nThis package was developed by **Novartis** and the **US Food and Drug Administration (FDA)** as part of a **research collaboration** agreement on radiogenomics. It is part of FDA’s Regulatory Science Tool Catalog. For more information, please visit [cdrh-rst.fda.gov](https://cdrh-rst.fda.gov/).\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## 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 (**recommended**)\n```bash\nconda install conda-forge::torchsurv\n```\n- Using PyPI\n```bash\npip install torchsurv\n```\n\n- Using for local installation (`latest version`)\n\n```bash\ngit clone \u003crepo\u003e\ncd \u003crepo\u003e\npip install -e .\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## 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### 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,)).bool()\n\u003e\u003e\u003e time = torch.randint(low=1, high=100, size=(n,)).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\n\u003e\u003e\u003e loss = neg_log_likelihood(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\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(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## Related Packages\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](https://github.com/Novartis/torchsurv/blob/main/docs/source/table_python_benchmark.png)\n![Survival analysis libraries in Python](https://github.com/Novartis/torchsurv/blob/main/docs/source/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](https://github.com/Novartis/torchsurv/blob/main/docs/source/table_r_benchmark.png)\n\n## Contributing\n\nWe value contributions from the community to enhance and improve this project. If you'd like to contribute, please consider the following:\n\n1. Create Issues: If you encounter bugs, have feature requests, or want to suggest improvements, please create an [issue](https://github.com/Novartis/torchsurv/issues) in the GitHub repository. Make sure to provide detailed information about the problem, including code for reproducibility, or enhancement you're proposing.\n\n2. Fork and Pull Requests: If you're willing to address an existing issue or contribute a new feature, fork the repository, create a new branch, make your changes, and then submit a pull request. Please ensure your code follows our coding conventions and include tests for any new functionality.\n\nBy contributing to this project, you agree to license your contributions under the same license as this project.\n\n## Contacts\n\n* [Thibaud Coroller](mailto:thibaud.coroller@novartis.com?subject=TorchSurv) (**Novartis**): `(creator, maintainer)`\n* [Mélodie Monod](mailto:monod.melodie@gmail.com?subject=TorchSurv) (**Imperial College London**): `(creator, maintainer)`\n* [Peter Krusche](mailto:peter.krusche@novartis.com?subject=TorchSurv) (**Novartis**): `(author, maintainer)`\n* [Qian Cao](mailto:qian.cao@fda.hhs.gov?subject=TorchSurv) (**FDA**): `(author, maintainer)`\n\nIf you have any questions, suggestions, or feedback, feel free to reach out the development team [us](https://opensource.nibr.com/torchsurv/AUTHORS.html).\n\n## Cite\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"}