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,
Gilbert-Elliott).
Most patterns can be combined with any mechanism, making it easy to create
controlled missing-data scenarios for evaluating imputation methods.
gilbert_elliott is MCAR-only because it models an independent burst-loss
channel rather than value- or driver-dependent missingness.
Statement of Need
Time-series imputation studies need reproducible missing-data conditions, but lightweight tools do not always separate the statistical reason values are missing from their temporal arrangement or expose how requested rates are measured. TSGap provides a NumPy-native interface for constructing controlled, seeded masks while preserving target dimensions and pre-existing gaps.
The package is designed for researchers and practitioners evaluating time-series imputation methods. It is especially useful in healthcare monitoring, wearable sensing, environmental sensing, longitudinal studies, and machine-learning workflows where observed data are intentionally masked to benchmark reconstruction quality under explicit missing-data assumptions.
How TSGap Differs From Similar Tools
Related tools include PyGrinder and BenchPOTS from the PyPOTS ecosystem. PyGrinder provides several Python-native missingness generators, and BenchPOTS supports benchmarking workflows for partially observed time-series datasets. TSGap focuses specifically on lightweight missingness simulation for arbitrary NumPy arrays: it exposes missingness mechanisms and temporal patterns as composable axes in one API, supports scale-aware block lengths, preserves pre-existing NaNs and target dimensions, and documents how each mechanism-pattern combination controls the final mask.
Features
- MCAR, MAR, and MNAR missingness mechanisms
- Pointwise, block, monotone, temporal decay, Markov-chain, and Gilbert-Elliott 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.11 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: dimension 0 drives missingness in non-driver dimensions, then block
# reshapes the selected missingness budget
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
# The default block pattern uses block_len=10 and block_density=1.0.
# block_frac is recommended
# for long wearable-style series where fixed 10-sample blocks are too short.
X_miss, mask = simulate_missingness(
X, "mcar", 0.20, seed=42,
pattern="block", block_frac=0.01
)
# Sample each block length between 1% and 5% of the time axis
X_miss, mask = simulate_missingness(
X, "mcar", 0.20, seed=42,
pattern="block", block_frac=(0.01, 0.05)
)
# MNAR: extreme values missing with monotone dropout
X_miss, mask = simulate_missingness(
X, "mnar", 0.20, seed=42,
pattern="monotone", mnar_mode="extreme"
)
# Gilbert-Elliott: bursty packet-loss with leaky good/bad periods.
# Burst length is controlled by `persist` (not block_frac): higher persist
# means longer bursts. `bad_loss` < 1 lets some values through during a bad
# period, and `good_loss` > 0 drops the occasional value during a good period.
X_miss, mask = simulate_missingness(
X, "mcar", 0.20, seed=42,
pattern="gilbert_elliott", persist=0.9,
bad_loss=0.8, good_loss=0.02
)
eligible = ~np.isnan(X)
artificial_rate = ((~mask) & eligible).sum() / eligible.sum()
print(f"Artificial missing rate: {artificial_rate:.4f}")
The returned mask uses:
mask == True -> observed
mask == False -> missing
Scientific Interpretation and Rate Semantics
missing_rate is measured over eligible entries: originally observed values
in the requested target dimensions. Pre-existing NaNs and non-target dimensions
are excluded from its denominator. The achieved artificial rate is:
eligible = ~np.isnan(X) # additionally restrict this mask for target dimensions
achieved_rate = ((~mask) & eligible).sum() / eligible.sum()
Pointwise MAR and MNAR directly assign value- or driver-dependent
probabilities to individual entries. With block, monotone, decay, or
markov, the mechanism supplies a missingness budget or influence signal that
the temporal pattern rearranges. These outputs should be described as
temporally structured, mechanism-informed masks, not automatically as strict
final-mask MAR or MNAR processes.
Rate guarantees depend on the mechanism and pattern. MCAR pointwise differs
from the requested rate by at most 0.5 / number_of_eligible_entries because
its count is rounded. Block, monotone, and decay preserve the mechanism count
when feasible. MAR, MNAR, Markov, and Gilbert-Elliott are stochastic and target
the rate in expectation, so no deterministic per-run tolerance is guaranteed;
report the achieved rate and use multiple seeds when comparing methods.
Infeasible Markov and Gilbert-Elliott configurations raise ValueError.
Documentation
The documentation is built with Sphinx from the Markdown files in docs/.
After the documentation workflow runs on GitHub, the online documentation is
available at:
To build the documentation locally:
pip install -e ".[docs]"
sphinx-build -b html docs docs/_build/html
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)
evaluation_idx = ~mask & ~np.isnan(X)
rmse = np.sqrt(np.mean((X[evaluation_idx] - X_imputed[evaluation_idx]) ** 2))
mae = np.mean(np.abs(X[evaluation_idx] - X_imputed[evaluation_idx]))
Use ~mask when the original data are complete. If the original data already
contain NaNs, ~mask & ~np.isnan(X) evaluates only values that TSGap
artificially hid.
See the benchmarking workflow for a complete baseline workflow.
The repository also includes a runnable benchmark example:
python examples/benchmark_imputation.py
Testing
pytest tsgap/tests/ -v
GitHub Actions runs the test suite on Python 3.11 through 3.14 and builds the
Sphinx documentation on every pull request to main.
Contributing
Contributions are welcome. See CONTRIBUTING.md for development setup, testing, and pull request guidelines.
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},
version = {0.8.0},
doi = {10.5281/zenodo.21365453},
url = {https://github.com/feruzoripov/tsgap}
}
License
MIT
AI Usage Disclosure
Generative AI tools, including OpenAI Codex/ChatGPT and Anthropic Claude, have been used to assist with code review, test generation, documentation organization, and manuscript drafting during development. All AI-assisted code and text are reviewed, tested, and validated by the package authors, who retain full responsibility for the project.
Metadata
Release files for tsgap 0.8.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 | Provenance | Actions |
|---|---|---|---|---|
| tsgap-0.8.0.tar.gz | 575.6 kB | Without provenance | Details for tsgap-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tsgap-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for tsgap-0.8.0-py3-none-any.whl |
Total release size: 609.5 kB
Release files / tsgap-0.8.0.tar.gz
| Download URL | tsgap-0.8.0.tar.gz |
|---|---|
| Size | 575.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a892310dd2c6d6aa775d055fba18afc3431b5807e157876f7ccf915857373744
|
|
BLAKE2b-256 checksum How to use checksums |
a328f30c3be9a39f3ebc1eb34ab5719ddf477fd14b3ebb09e39348c456dca7a7
|
| 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.8.0-py3-none-any.whl
| Download URL | tsgap-0.8.0-py3-none-any.whl |
|---|---|
| Size | 33.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3a7e72f1f4bc91bffb9c3856ad9ab7631d7861c67fb07427b837ba9fedd87644
|
|
BLAKE2b-256 checksum How to use checksums |
6183fe498a41d3ba07844ca87fdd053de165931fda47604c352b94448f1f6a8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Security for tsgap 0.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.