Skip to main content

SONATI - Spatio-temporal Ocean Neighbourhood Asymmetric Transformer for Imputation

sonati is a Python package for imputing missing values in sparse spatio-temporal data. It implements SONATI, a transformer that attends separately to the k-nearest temporal, then j-nearest spatial neighbours of each query point and returns a filled value and a per-point aleatoric and epistemic uncertainty estimate.

The model was developed for oceanographic in-situ observations but applies to any point-cloud data with latitude, longitude, depth and time coordinates.

Installation

pip install sonati

Or directly from this repository:

pip install git+https://github.com/y-jenniges/sonati.git

For local development (editable install):

git clone https://github.com/y-jenniges/sonati.git
pip install -e sonati/

Requires Python ≥ 3.11 and PyTorch ≥ 2.0.

Quick start

import numpy as np
from sonati import SONATIImputer

# X           - (n_samples, n_features), NaN marks missing values
# coords      - (n_samples, 4) as [latitude (°), longitude (°), depth (m), time]
# val_indices - row indices held out for early stopping (required), e.g. random or block hold-out
rng = np.random.default_rng(0)
val_indices = rng.choice(len(X), size=len(X) // 5, replace=False)

imputer = SONATIImputer(
    val_indices=val_indices,
    n_neighbours_space=30,
    n_neighbours_time=30,
    loss_name="hetero",     # heteroscedastic NLL - also trains a variance head
    max_epochs=100,
    device="auto",          # uses CUDA when available
)
X_filled = imputer.fit_transform(X, coords)

Coordinate format

coords must have exactly four columns in this order:

Column Meaning Unit
0 Latitude Degrees (−90 ... 90)
1 Longitude Degrees (−180 ... 180)
2 Depth Metres
3 Time Numeric year, or month 1–12 with cyclic_time=True

Latitude and longitude are mapped internally to Cartesian unit-sphere coordinates. Depth, time and features are MinMax-scaled. All inverse transforms are applied automatically before returning imputed values.

Uncertainty estimates

predict_with_uncertainty() returns per-point epistemic uncertainty from n_inference forward passes with dropout active (MC Dropout). Aleatoric uncertainty requires loss_name='hetero' (or 'beta_nll'); with 'mse', the variance head is untrained and aleatoric_std is returned as NaN.

# All data goes into the imputer; test rows are hidden from training via test_idx
imputer = SONATIImputer(val_indices=val_indices, test_idx=test_idx, n_inference=50, loss_name="hetero")
imputer.fit(X, coords)
X_filled, aleatoric_std, epistemic_std = imputer.predict_with_uncertainty(X, coords)

Graph building and reuse

The k-NN graphs depends only on the spatio-temporal location and is built in fit().

  • Training data: transform() and predict_with_uncertainty() on the training data itself reuse the graph from fit() instead of rebuilding it, which saves time on large datasets (typical imputation use case).
  • Other data: The graph is built from the passed rows only. X_test must therefore contain more than max(n_neighbours_space, n_neighbours_time) rows, and points can only attend to other rows of X_test, not to the training observations. To keep held-out rows in the graph, pass them as test_idx at construction instead (recommended).
  • Hyperparameter tuning: The graph build can dominate a trial's runtime. Build it once with the largest k under consideration; fit() slices the first n_neighbours_space / n_neighbours_time columns per trial:
from sonati import SONATIImputer, precompute_graph

graph = precompute_graph(coords, n_neighbours_space=60, n_neighbours_time=60)

for k in (15, 30, 45, 60):
    imputer = SONATIImputer(val_indices, n_neighbours_space=k, n_neighbours_time=k)
    imputer.fit(X, coords, graph=graph)  # no k-NN build in the trial

Masking strategies

Strategies for the masking argument (default: [RandomMask(ratio=0.5), CylindricalMask(radius=0.1, prob=1.0)]):

Strategy Hides
RandomMask(ratio=0.5) A random fraction of features, independently per point (row-wise)
PerFeatureMask(ratio=0.5) A random fraction of points, independently per feature (column-wise)
CylindricalMask(radius=0.1, prob=1.0) Every feature of every point inside a spatial cylinder of radius radius
CopyMask(prob=1.0) Apply missingness of a randomly drawn training row, independently per point [1]

[1] Gorla A, Wang R, Liu Z, An U, Sankararaman S. CACTI: Leveraging Copy Masking and Contextual Information to Improve Tabular Data Imputation. Proc Mach Learn Res. 2025 Jul;267:20187-20225. PMID: 41959945; PMCID: PMC13060770.

Hyperparameters

Setup options - set according to your data and hardware:

Parameter Default Description
val_indices Required Row indices of X held out as internal validation set for early stopping
test_idx None Row indices withheld entirely from training (coordinates still used for the k-NN graph)
cyclic_time False Cyclic sine/cosine encoding for the time column
max_epochs / patience 100 / 10 Training epoch limit / early-stopping patience (epochs)
n_inference 50 MC Dropout forward passes used by predict_with_uncertainty() for epistemic uncertainties
n_jobs None Parallel jobs for the spatial k-NN graph build (sklearn convention: None=1, -1=all cores)
device 'auto' 'cpu', 'cuda', or 'auto'

Common hyperparameters to tune:

Parameter Default Description
n_neighbours_space 30 Neighbours per query in the spatial graph
n_neighbours_time 30 Neighbours per query in the temporal graph
d_model 64 Transformer embedding dimension
nhead 4 Attention heads (must divide d_model)
n_spatial_layers / n_temporal_layers 3 / 3 Cross-attention layers over the spatial / temporal neighbourhood
loss_name 'hetero' 'hetero' (Gaussian NLL + variance head), 'beta_nll' (beta-NLL, [2]) or 'mse'
masking None Training masking strategies, see Masking strategies

All remaining options (optimiser, loss options, checkpointing, DataLoader settings, ...) are documented in the SONATIImputer docstring.

[2] Maximilian Seitzer, Arash Tavakoli, Dimitrije Antic, Georg Martius: On the Pitfalls of Heteroscedastic Uncertainty Estimation with Probabilistic Neural Networks. In International Conference on Learning Representations. 2022.

Examples

The examples/ directory contains runnable end-to-end scripts:

  • real_world_test.py: Air-quality (Open-Meteo API) and traffic (METR-LA) imputation with public data downloaded on run.

Running the tests

pip install pytest
pytest tests/

Citation

If you use this package, please cite:

@article{jenniges_inprep,
  title   = {{SONATI}: Spatio-temporal ocean neighbourhood asymmetric transformer for imputation of sparse, tabular data with uncertainty},
  author  = {Yvonne Jenniges and Boris Peter Koch and Maike Sonnewald and Sebastian Maneth},
  journal = {Environmental Data Science},
  year    = {in prep.},
}

License

MIT

Metadata

Release files for sonati 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sonati 0.1.0
File Size Uploaded Provenance Actions
sonati-0.1.0.tar.gz 43.6 kB Without provenance Details for sonati-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sonati 0.1.0
File Interpreter ABI Platform Actions
sonati-0.1.0-py3-none-any.whl Python 3 none any Details for sonati-0.1.0-py3-none-any.whl

Total release size: 77.2 kB

Release files / sonati-0.1.0.tar.gz

Download URL sonati-0.1.0.tar.gz
Size 43.6 kB
Tags Source
SHA-256 checksum
How to use checksums
098681befb24465037cf58d5fef48643f8838481f8e3ef5c657b4910aa2b2adc
BLAKE2b-256 checksum
How to use checksums
74ab98cb1f88c3747dfbaa8452c04adb581863efac8de62b550c4fb299c6f475
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release files / sonati-0.1.0-py3-none-any.whl

Download URL sonati-0.1.0-py3-none-any.whl
Size 33.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca261bfaef0de4150a651be98307335dda51cf24a8b29b936ef8b2b2420d26bb
BLAKE2b-256 checksum
How to use checksums
486556fbc649a309cfb5555fc969b893702240cfa650d97339d26bc104ab8a1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Security for sonati 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page