Skip to main content

Neural spatial modeling with low-rank spatial adapters

Project description

Spatial Adapter

Tests codecov arXiv PyPI version

A post-hoc cascade adapter that extracts explicit low-rank spatial representations from the residuals of any frozen first-stage predictor, yielding closed-form covariance estimation and uncertainty quantification.

Installation

Requires Python 3.10+, Conda, and (optionally) CUDA 12.8+ for GPU.

git clone https://github.com/STLABTW/spatial-adapter.git
cd spatial-adapter

make conda-env                 # recommended: conda env + C++ extensions
# or
pip install -e ".[all]"        # pip-only

For Blackwell / sm_120 GPUs (e.g. RTX 5070 Ti):

pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128 --force-reinstall

Quick Start

Option A — one-shot tuned fit (recommended)

Hides the (τ₁, τ₂) grid search behind a single call: runs a short Optuna sweep, refits at the best weights, returns the trained adapter plus the chosen τ's and the trials dataframe.

import torch
from spatial_adapter import SpatialAdapter, TrendModel

trend = TrendModel(num_continuous_features=5, hidden_layer_sizes=[], n_locations=100)

result = SpatialAdapter.fit_tuned(
    trend, train_loader,
    val_cont=val_X, val_y=val_Y, locs=station_coords,
    device=torch.device("cpu"),
    latent_dim=10,
    n_trials=10,              # per-seed Optuna budget (defaults to 10)
    seed=42,
    criterion="auto",         # see table below
)
result.adapter.reconstruct(val_X, val_Y)   # trained; ready to use
result.tau1, result.tau2                    # best weights chosen by Optuna
result.trials                               # pandas DataFrame of all trials

Validation criterion — what Optuna minimizes/maximizes over (τ₁, τ₂):

criterion Task Direction What it measures
"auto" (default) any regression → rmse, binary → accuracy
"rmse" regression min point-prediction RMSE on val
"accuracy" binary max classification accuracy on val
"auc" binary max ROC AUC on val
"cov_frob" regression min relative Frobenius covariance error, ||Σ̂_pred − Σ̂_obs||_F / ||Σ̂_obs||_F
"sv_score" regression min weighted L² distance between empirical semivariograms of val and prediction (gstools)

For the paper's KAUST Table 4 ablation (RMSE vs CovFrob vs SV_score selection), use these criteria directly. For domain-specific ground-truth covariance (e.g. a known Matérn model), fall back to AdapterTuner with a custom evaluate_fn.

Option B — manual control (power users)

Bypasses tuning when you already have (τ₁, τ₂) or want direct control of the ADMM loop.

import torch
from spatial_adapter import (
    SpatialAdapter, SpatialAdapterConfig,
    SpatialBasisLearner, TrendModel,
)

trend = TrendModel(num_continuous_features=5, hidden_layer_sizes=[], n_locations=100)
basis = SpatialBasisLearner(num_locations=100, latent_dim=10)

adapter = SpatialAdapter(
    trend, basis, train_loader,
    val_cont=val_X, val_y=val_Y, locs=station_coords,
    config=SpatialAdapterConfig(),
    device=torch.device("cpu"),
    tau1=1.0, tau2=1.0,
)
adapter.pretrain_trend(epochs=5)
adapter.init_basis_dense()
adapter.run()  # ADMM optimization

Examples & paper experiments

End-to-end scripts, configs, and notebooks live under examples/. To reproduce the paper tables (data download, config → table mapping, expected output) see examples/experiments/README.md.

Project layout

Development

make conda-env       # set up environment
make build-cpp       # build C++ extensions
make test            # run tests
make test-cov        # tests with HTML coverage

Citation

@misc{wang2026spatialadapterstructuredspatial,
      title={Spatial Adapter: Structured Spatial Decomposition and Closed-Form Covariance for Frozen Predictors},
      author={Wen-Ting Wang and Wei-Ying Wu and Hao-Yun Huang and Xuan-Chun Wang},
      year={2026},
      eprint={2605.11394},
      archivePrefix={arXiv},
      primaryClass={stat.ML},
      url={https://arxiv.org/abs/2605.11394},
}

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spatial_adapter-0.6.0.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

spatial_adapter-0.6.0-cp313-cp313-manylinux_2_28_x86_64.whl (43.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

spatial_adapter-0.6.0-cp313-cp313-macosx_14_0_arm64.whl (12.0 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

spatial_adapter-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl (43.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

spatial_adapter-0.6.0-cp312-cp312-macosx_14_0_arm64.whl (12.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

spatial_adapter-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl (43.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

spatial_adapter-0.6.0-cp311-cp311-macosx_14_0_arm64.whl (12.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

spatial_adapter-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl (43.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

spatial_adapter-0.6.0-cp310-cp310-macosx_14_0_arm64.whl (12.0 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

File details

Details for the file spatial_adapter-0.6.0.tar.gz.

File metadata

  • Download URL: spatial_adapter-0.6.0.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for spatial_adapter-0.6.0.tar.gz
Algorithm Hash digest
SHA256 403cfce541798d1e55ac35b15a03b7eff7a50baebdb6d01a3c4cf4bc86b78d07
MD5 b71f865481908c3fbe7aef0e4108686f
BLAKE2b-256 ce60db503eaeac31d439328dbe49f3c797f98df2e1b5a7f102b7e8ea65fb4338

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ae000e05649ec0a10b9d73e2ea4161591355e76c63483f027f8cc645e9c4bbc2
MD5 7a55077ec32d5d64b2fbd4bdd02c274b
BLAKE2b-256 cc36844b43f5eda9fc86d67f2c986a54391142b54fdd530050ee1e7f40d1bce7

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 a7e83f2aabee3d6c5840847a493fdfeb62980b5c79f927d328623a7bb4830759
MD5 b8ab2cf5b5d5954b4b5131d324c9275d
BLAKE2b-256 8433017da7dff3158c4d514ed7bf0d7e3731484b76056b9ebd5b60e6d78853b6

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7040141c9c3b0cf292d5bd3ebe3b5187695c3a2c0703087823fa6bdd2141b0a4
MD5 f6e0f6538779a8f4f16ffdd8547367d4
BLAKE2b-256 17c06689565c5e06dfb936949fa4c8d32d846ddbb57d38134e852e01f77632fa

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d732aef3274e5924f6fd54f46375db4949905a6984a856a755b2aaf4cd60af10
MD5 2e7066b7af177d39aa6edb74de51e090
BLAKE2b-256 983fee2ce05650d203f82e5c9b964a20ea9525a0112123ae56451bc7077b4754

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c00284ecc9af373884f27ef622be82d8fdd9363fdd1c19cf008180d1b5369177
MD5 80e0012761efaa560bb02733ef80e350
BLAKE2b-256 494b48199c83a5c79ce9f3bd94d15a96ab80542740835dc0f48061f81d93cc77

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3cbc41afc6148694fb203cc9c78c98970768deb7bf495d60555cc46c89ebbda4
MD5 2e7cac4954c4a902f48c6b470ee3ca67
BLAKE2b-256 7ad55cede98c0ddc92f902c319a3617e99bd82b9a62a021759bffcde6ebcc8ba

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 485e604574aeb6ec80118ccea40840146ca5b73fdf89e31d3e3a2f875cb5633e
MD5 be362abe37e9f55093a524d8a42488f1
BLAKE2b-256 d0fe238a8b07a27330b9724f753eaf7c52d979ceb1c56ec78ba6061a7c2e98c5

See more details on using hashes here.

File details

Details for the file spatial_adapter-0.6.0-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for spatial_adapter-0.6.0-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 6afd4b8d88c00c9f5860007c6c8454efbb03de3c1c355dd4576a9c691cdb042d
MD5 94914ca904525a382fe509815905538b
BLAKE2b-256 ec1b2710fc890a64f478054964091df55d550aff5465ef21074a11aad2aaaecb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page