Metadata-Version: 2.4
Name: kmnet
Version: 0.3.0
Summary: Deep discrete-time survival analysis with a Kaplan-Meier inspired rank loss (PyTorch).
Author: Rahul Goswami
Maintainer-email: Rahul Goswami <rahul@statml.in>
License: MIT
Project-URL: Homepage, https://github.com/yuvrajiro/KMNet
Project-URL: Documentation, https://kmnet.statml.in/
Project-URL: Repository, https://github.com/yuvrajiro/KMNet
Project-URL: Issues, https://github.com/yuvrajiro/KMNet/issues
Project-URL: Changelog, https://kmnet.statml.in/changelog.html
Keywords: survival analysis,time-to-event,discrete-time,kaplan-meier,rank loss,deep learning,pytorch,censoring
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=1.7.0
Requires-Dist: numpy
Requires-Dist: pandas
Requires-Dist: torchtuples
Requires-Dist: pycox
Requires-Dist: numba
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=7.2; extra == "docs"
Requires-Dist: pydata-sphinx-theme>=0.15; extra == "docs"
Requires-Dist: myst-nb>=1.0; extra == "docs"
Requires-Dist: sphinx-design>=0.5; extra == "docs"
Requires-Dist: sphinx-copybutton>=0.5; extra == "docs"
Requires-Dist: matplotlib>=3.6; extra == "docs"
Requires-Dist: ipykernel>=6; extra == "docs"
Dynamic: license-file

# KMNet: Discrete-Time Survival Analysis with Deep Learning

<p align="center">
  <img src="https://raw.githubusercontent.com/yuvrajiro/KMNet/main/docs/_static/kmnet_banner.svg" alt="KMNet" width="100%">
</p>

<p align="center">
  <strong>⚡ State-of-the-art survival analysis with deep learning and ranking losses ⚡</strong>
</p>

<p align="center">
  <a href="https://kmnet.statml.in/"><b>Documentation</b></a> •
  <a href="#-key-features">Features</a> •
  <a href="#-installation">Installation</a> •
  <a href="#-quick-start">Quick Start</a> •
  <a href="https://github.com/yuvrajiro/KMNet/blob/main/examples/KMNet_Demo.ipynb">Demo Notebook</a> •
  <a href="#-citation">Citation</a>
</p>

<!-- CI/CD & Coverage -->
<p align="center">
  <a href="https://github.com/yuvrajiro/KMNet/actions/workflows/tests.yml">
    <img src="https://github.com/yuvrajiro/KMNet/actions/workflows/tests.yml/badge.svg" alt="Tests">
  </a>
  <a href="https://kmnet.statml.in/">
    <img src="https://github.com/yuvrajiro/KMNet/actions/workflows/docs.yml/badge.svg" alt="Docs">
  </a>
  <a href="https://codecov.io/gh/yuvrajiro/KMNet">
    <img src="https://codecov.io/gh/yuvrajiro/KMNet/branch/main/graph/badge.svg" alt="Coverage">
  </a>
  <a href="https://pypi.org/project/kmnet/">
    <img src="https://img.shields.io/pypi/v/kmnet" alt="PyPI">
  </a>
  <a href="https://pypi.org/project/kmnet/">
    <img src="https://img.shields.io/pypi/pyversions/kmnet" alt="Python Version">
  </a>
  <a href="https://pepy.tech/project/kmnet">
    <img src="https://pepy.tech/badge/kmnet" alt="Downloads">
  </a>
  <a href="https://opensource.org/licenses/MIT">
    <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License">
  </a>
  <a href="https://github.com/psf/black">
    <img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="Code style">
  </a>
</p>


## 🎯 Use Cases & Applications

<table>
<tr align="center">
<td width="33%" align="center">

### 🏥 **Clinical Research**
Patient survival analysis<br>
Treatment effect estimation<br>
Risk stratification

</td>
<td width="33%" align="center">

### 🔧 **Reliability Engineering**
Equipment failure prediction<br>
Maintenance scheduling<br>
Warranty analysis

</td>
<td width="33%" align="center">

### 📊 **Business Analytics**
Customer churn prediction<br>
Subscription lifetime value<br>
Employee retention

</td>
</tr>
</table>


<p align="center">
  <strong>KMNet</strong> combines the power of deep neural networks with Kaplan-Meier inspired ranking losses to deliver state-of-the-art performance in discrete-time survival analysis. Perfect for clinical research, reliability engineering, and time-to-event prediction.
</p>

It extends standard neural survival models by incorporating a novel **Kaplan-Meier inspired rank loss**, allowing the model to learn not just from local hazard rates but also from global ranking constraints inherent in survival data.

This library provides a high-performance implementation using **PyTorch JIT** to speed up custom loss calculations, making it suitable for large-scale survival datasets.

## 🚀 Key Features

<table>
<tr>
<td width="50%">

### 🎯 **Advanced Methodology**
- ✅ **Discrete-Time Modeling** with flexible time discretization
- ✅ **Hybrid Loss Function** combining likelihood and ranking
- ✅ **Kaplan-Meier Inspired** ranking constraints
- ✅ **Handles Censoring** naturally and efficiently

</td>
<td width="50%">

### ⚡ **Performance**
- 🚀 **1.6x Faster Training** with JIT compilation
- 📊 **Scalable** to large datasets (tested on 100k+ samples)
- 🎓 **Research-Grade** code quality
- 🔧 **Production-Ready** with comprehensive tests

</td>
</tr>
</table>

### 📊 Why KMNet?

| Feature | Traditional Methods | DeepSurv/Cox-PH | **KMNet** |
|---------|--------------------|-----------------|-----------|
| Handles Non-Linear Effects | ❌ | ✅ | ✅ |
| Captures Ranking Information | ⚠️ Partial | ❌ | ✅ |
| Discrete Time Bins | ❌ | ❌ | ✅ |
| GPU Acceleration | ❌ | ✅ | ✅ |
| Flexible Loss Functions | ❌ | ⚠️ Limited | ✅ |
| JIT-Optimized | N/A | ❌ | ✅ |

## 📦 Installation

```bash
pip install kmnet
```

Or install the latest development version from source:

```bash
git clone https://github.com/yuvrajiro/KMNet.git
cd KMNet
pip install -e ".[dev]"
```

### Requirements

*   Python >= 3.8
*   PyTorch >= 1.7.0
*   NumPy, Pandas
*   torchtuples
*   pycox
*   numba

## ⚡ Quick Start

Here is a complete example of how to use KMNet on a synthetic dataset. You can also check out the [interactive demo notebook](https://github.com/yuvrajiro/KMNet/blob/main/examples/KMNet_Demo.ipynb) and the [full documentation](https://kmnet.statml.in/).

### 1. Data Preparation

KMNet requires the target variable (time and event) to be discretized. We use `label_transforms` from `pycox` for this.

```python
import numpy as np
import pandas as pd
from kmnet.model import KMNet

# Generate synthetic data
def make_data(n=1000):
    X = np.random.randn(n, 5).astype('float32')
    T = np.random.exponential(1 / (0.1 * np.exp(0.5 * X[:, 0])))
    C = np.random.exponential(1 / 0.05, size=n)
    time = np.minimum(T, C)
    event = (T <= C).astype('float32')
    return X, time, event

X, time, event = make_data()

# Discretize time into 20 bins
num_durations = 20
labtrans = KMNet.label_transform(num_durations)
get_target = lambda df: (df['duration'].values, df['event'].values)

df = pd.DataFrame({'duration': time, 'event': event})
y = labtrans.fit_transform(*get_target(df))
```

### 2. Define the Neural Network

You can use any PyTorch network architecture. The output dimension must match the number of time bins.

```python
import torch.nn as nn

in_features = X.shape[1]
out_features = labtrans.out_features

net = nn.Sequential(
    nn.Linear(in_features, 32),
    nn.ReLU(),
    nn.Linear(32, 32),
    nn.ReLU(),
    nn.Linear(32, out_features)
)
```

### 3. Train the Model

Initialize `KMNet` and fit it to the data.

```python
# Initialize model with the discretized time grid
model = KMNet(net, duration_index=labtrans.cuts, base='nll')  # or base='bce'

# Train
batch_size = 64
epochs = 10
model.fit(X, y, batch_size, epochs, verbose=True)
```

### 4. Prediction and Visualization

Predict survival functions and plot them.

```python
import matplotlib.pyplot as plt

# Predict survival probabilities for the first 5 samples
surv_df = model.predict_surv_df(X[:5])

# Plot
plt.figure(figsize=(10, 6))
for col in surv_df.columns:
    plt.step(surv_df.index, surv_df[col], where="post")
plt.ylabel("Survival Probability")
plt.xlabel("Time")
plt.title("Predicted Survival Curves")
plt.show()
```


## 🔬 Mathematical Background

KMNet models the discrete conditional survival $p(t | x)$. The survival function is given by:

$$
\boxed{S(t | x) = \prod_{k=0}^{t} p(k | x)}
$$

The loss function $\mathcal{L}$ is a weighted sum of two components:

$$
\boxed{\mathcal{L} = \alpha \mathcal{L}_{bce} + (1 - \alpha)\lambda \mathcal{L}_{Rank}}
$$

<details>
<summary><b>📖 Click to expand detailed explanation</b></summary>

### Loss Components

1.  **$\mathcal{L}_{bce}$ (Negative Log-Likelihood)**: 
    - Standard survival loss for discrete time
    - Ensures the model fits the observed event times
    - Handles censored data correctly

2.  **$\mathcal{L}_{Rank}$ (Rank Loss)**: 
    - Enforces correct ordering: $S(T_i | x_i) < S(T_i | x_j)$ if $T_i < T_j$
    - Inspired by the Kaplan-Meier estimator
    - Maintains global structure of survival curves
    - Differentiable approximation using softplus/exponential penalties

</details>

## 📊 Performance & Optimization

### ⚡ Speed Benchmarks

The `KMNet` class leverages **TorchScript (JIT)** to compile the custom loss functions, eliminating Python interpreter overhead.

<table>
<tr>
<td width="60%">

**Benchmark Setup:**
- 5,000 samples
- 50 time bins
- 10 epochs
- CPU environment

**Results:**
```
🔥 JIT-Optimized: 1.38s
📈 Speedup: 1.6x faster
💾 Memory: ~15% reduction
```

</td>
<td width="40%">

**Scalability:**
| Dataset Size | Training Time |
|--------------|---------------|
| 1K samples   | 0.3s/epoch   |
| 10K samples  | 2.1s/epoch   |
| 100K samples | 18.5s/epoch  |

*Tested on Intel i7, 16GB RAM*

</td>
</tr>
</table>



---

## 🛠️ Advanced Configuration

You can customize the loss function parameters when initializing the model:

1. `base` (required, no default): 'nll' (Negative Log-Likelihood) or 'bce' (Binary Cross-Entropy)
2. `rank_mode`: 'full' (CDF-based) or 'conditional' ranking
3. `rank_penalty`: 'softplus' or 'exp' for the ranking loss formulation

```python
from kmnet.model import KMLoss

loss = KMLoss(
    alpha=0.5,          # Weight for NLL vs Rank loss
    sigma=0.1,          # Kernel width for soft ranking
    base='nll',         # 'nll' or 'bce' (required)
    rank_mode='full',   # 'full' (CDF-based) or 'conditional'
    rank_penalty='softplus' # 'softplus' or 'exp'
)

model = KMNet(net, loss=loss, duration_index=labtrans.cuts)
```

## 💬 Getting Help

- 📖 **Documentation**: [kmnet.statml.in](https://kmnet.statml.in/) (quickstart, method, API reference)
- 💡 **Issues**: Found a bug? [Open an issue](https://github.com/yuvrajiro/KMNet/issues)
- 💬 **Discussions**: Have questions? Start a [discussion](https://github.com/yuvrajiro/KMNet/discussions)
- 📧 **Email**: For collaboration inquiries: [rahul@statml.in](mailto:rahul@statml.in)

## 🤝 Contributing

We welcome contributions! KMNet is an open-source research project.

<details>
<summary><b>How to contribute</b></summary>

1.  Fork the repository
2.  Create your feature branch (`git checkout -b feature/AmazingFeature`)
3.  Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4.  Push to the branch (`git push origin feature/AmazingFeature`)
5.  Open a Pull Request

**Areas we'd love help with:**
- 📝 Documentation improvements
- 🧪 Additional benchmarks and examples
- 🐛 Bug fixes and testing
- ✨ New features (e.g., competing risks, time-varying covariates)

</details>

## 📄 License

Distributed under the MIT License. See [`LICENSE`](https://github.com/yuvrajiro/KMNet/blob/main/LICENSE) for more information.

## 📚 Citation

KMNet accompanies a paper under review at TMLR ([OpenReview](https://openreview.net/forum?id=VXIk9iY10Q)). If you use KMNet in your research, please cite:

```bibtex
@article{anonymous2026deep,
  title   = {Deep Discrete-Time Survival Analysis with Guaranteed Monotonicity},
  author  = {Anonymous},
  journal = {Submitted to Transactions on Machine Learning Research},
  year    = {2026},
  url     = {https://openreview.net/forum?id=VXIk9iY10Q},
  note    = {Under review}
}
```

### 🏆 Acknowledgments

KMNet builds upon excellent work from:
- [PyCox](https://github.com/havakv/pycox) for survival analysis utilities
- [TorchTuples](https://github.com/havakv/torchtuples) for training infrastructure
- The PyTorch team for JIT compilation capabilities

---

<p align="center">
  <sub>Built with ❤️ for the survival analysis community</sub><br>
  <sub>⭐ Star us on GitHub if KMNet helps your research!</sub>
</p>

