TSGap
A Python library for simulating realistic missingness in time-series data for imputation benchmarking.
TSGap separates mechanisms (why data is missing: MCAR, MAR, MNAR) from patterns (how data is missing: pointwise, block, monotone, decay, markov). Any mechanism can be combined with any pattern, making it easy to create controlled missing-data scenarios for evaluating imputation methods.
Features
- MCAR, MAR, and MNAR missingness mechanisms
- Pointwise, block, monotone, temporal decay, and Markov-chain patterns
- 2D
(time, features)and 3D(samples, time, features)arrays - Exact or calibrated missing-rate control
- Weighted multi-driver MAR
- Reproducible masks with NumPy random seeds
- Existing NaNs and target dimensions preserved across patterns
Installation
pip install tsgap
For development:
git clone https://github.com/feruzoripov/tsgap.git
cd tsgap
pip install -e ".[dev]"
Requires Python 3.9 or newer and NumPy 1.19 or newer.
Quick Start
import numpy as np
from tsgap import simulate_missingness
X = np.random.default_rng(42).standard_normal((1000, 6))
# MCAR: 15% scattered missing values
X_miss, mask = simulate_missingness(X, "mcar", 0.15, seed=42)
# MAR: missingness driven by dimension 0 and arranged in blocks
X_miss, mask = simulate_missingness(
X, "mar", 0.25, seed=42,
pattern="block", driver_dims=[0], block_len=10
)
# Scale block length to the sequence length for long series
X_miss, mask = simulate_missingness(
X, "mcar", 0.20, seed=42,
pattern="block", block_frac=0.01
)
# MNAR: extreme values missing with monotone dropout
X_miss, mask = simulate_missingness(
X, "mnar", 0.20, seed=42,
pattern="monotone", mnar_mode="extreme"
)
print(f"Actual missing rate: {(~mask).mean():.4f}")
The returned mask uses:
mask == True -> observed
mask == False -> missing
Documentation
Example Use Cases
- Random telemetry loss in sensor networks
- Activity-dependent dropout in wearable devices
- Participant dropout in longitudinal or clinical studies
- Gradual signal loss from sensor degradation
- Intermittent connectivity failures with bursty missingness
Evaluation Workflow
import numpy as np
from tsgap import simulate_missingness
X = np.random.default_rng(42).standard_normal((1000, 6))
X_miss, mask = simulate_missingness(X, "mar", 0.20, seed=42, driver_dims=[0])
X_imputed = your_imputation_method(X_miss)
missing_idx = ~mask
rmse = np.sqrt(np.mean((X[missing_idx] - X_imputed[missing_idx]) ** 2))
mae = np.mean(np.abs(X[missing_idx] - X_imputed[missing_idx]))
See docs/benchmarking.md for a complete baseline workflow.
The repository also includes a runnable benchmark example:
python examples/benchmark_imputation.py
Testing
pytest tsgap/tests/ -v
Citation
@software{tsgap,
author = {Oripov, Feruz and Korchagina, Kseniia and Bonsu, Enock Adu and Bilgin, Ali and Aras, Shravan},
title = {TSGap: A Python Library for Composable Time-Series Missingness Simulation},
year = {2026},
url = {https://github.com/feruzoripov/tsgap}
}
License
MIT
Metadata
Release files for tsgap 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tsgap-0.5.0.tar.gz | 390.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tsgap-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 413.6 kB
Release files / tsgap-0.5.0.tar.gz
| Download URL | tsgap-0.5.0.tar.gz |
|---|---|
| Size | 390.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f6a9500a31a87143145210c6b21386a86e80ba816701181b42b6e758a9519f1
|
|
BLAKE2b-256 checksum How to use checksums |
f813e498c1e829c4c12183e8f4dfa182c2166f4c2a231e140c55673d43ac3676
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Release files / tsgap-0.5.0-py3-none-any.whl
| Download URL | tsgap-0.5.0-py3-none-any.whl |
|---|---|
| Size | 23.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5ee25cdebef57362e47426fc3afd0bcaaf624e9f1803fe06b4a0f783d59d0916
|
|
BLAKE2b-256 checksum How to use checksums |
427bfee4f9577ee84cd2a05b5fc08e033fec5a3421c64b02e9dd5ad77bff9ed3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|