Skip to main content

autoFRK: Automatic Fixed Rank Kriging. The Python version with PyTorch

Project description

autoFRK-python

PyPI Version License: GPL v3 Downloads GitHub stars

autoFRK-python is a Python implementation of the R package autoFRK v1.4.3 (Tzeng S et al., 2021). autoFRK provides a Resolution Adaptive Fixed Rank Kriging (FRK) approach for handling regular and irregular spatial data, reducing computational cost through multi-resolution basis functions.

Features

  • Spatial modeling based on multi-resolution basis functions
  • Supports single or multiple time points
  • Offers approximate or EM-based model estimation
  • Suitable for global latitude-longitude data
  • Implemented in PyTorch, supporting CPU and GPU (requires PyTorch with CUDA support for GPU)

Installation

Install via pip:

pip install autoFRK

Install directly from GitHub:

pip install git+https://github.com/Josh-test-lab/autoFRK-python.git

Or clone and install manually:

git clone https://github.com/Josh-test-lab/autoFRK-python.git
cd autoFRK-python
pip install .

Usage

1. Import and Initialize

import torch
from autoFRK import AutoFRK

# Initialize the autoFRK model
model = AutoFRK(dtype=torch.float64, device="cpu")

2. Model Fitting

# Assume `data` is (n, T) observations and `loc` is (n, d) spatial coordinates
data = torch.randn(100, 1)  # Example data
loc = torch.rand(100, 2)    # Example 2D coordinates

result = model.forward(
    data=data,
    loc=loc,
    maxit=50,
    tolerance=1e-6,
    method="fast",          # "fast" or "EM"
    n_neighbor=3,
    maxK=50,
    calculate_with_spherical=False
)

print(result.keys())
# ['M', 's', 'negloglik', 'w', 'V', 'G', 'LKobj', 'calculate_with_spherical']

forward() returns a dictionary including:

  • M: Covariance matrix of random effects
  • s: Measurement error variance
  • negloglik: Final negative log-likelihood
  • w: Estimated random effects for each time point
  • V: Prediction error covariance of w
  • G: Basis function matrix used

3. Predicting New Data

# Assume `newloc` contains new spatial coordinates
newloc = torch.rand(20, 2)

pred = model.predict(
    obj=result,
    newloc=newloc,
    se_report=True
)

print(pred['pred.value'].shape)  # Predicted values
print(pred.get('se'))            # Standard errors

predict() can optionally return standard errors (se_report=True). If obj is not provided, the most recent forward() result is used.

Advanced Settings

forward() supports various parameters:

Parameter Description Default
mu Mean value (scalar or tensor) 0.0
D Measurement error covariance None (identity matrix used)
G Basis function matrix (optional) None (TPS basis auto-generated)
finescale Include fine-scale process η[t] False
maxit Maximum iterations 50
tolerance Convergence tolerance 1e-6
maxK Maximum number of basis functions Auto-set based on n
method Model estimation method "fast"
n_neighbor Number of neighbors for fast method 3
calculate_with_spherical Use spherical distance calculation False

Example Code

import torch
from autoFRK import AutoFRK

# Generate fake data
n, T = 200, 1
data = torch.randn(n, T)
loc = torch.rand(n, 2)

# Initialize model
model = AutoFRK(device="cpu")

# Fit model
res = model.forward(
    data=data,
    loc=loc,
    maxit=100,
    method="fast"
)

# Predict new data
newloc = torch.rand(10, 2)
pred = model.predict(obj=res, newloc=newloc, se_report=True)

print("Predicted values:", pred['pred.value'])
print("Prediction standard errors:", pred.get('se'))

Experimental Features

  • Spherical coordinate basis function computation
  • Gradient tracking (torch's requires_grad_()).

Authors

License

License: GPL v3

  • GPL (>= 3)

Development and Contribution

  • Built with PyTorch, supporting GPU acceleration
  • Report bugs or request features on GitHub issues

References

Citation

  • To cite the Python package autoFRK-python in publications use:
  Yao-Chih Hsu (2025). _autoFRK-python: Automatic Fixed Rank Kriging. The Python version with PyTorch_. Python package version 1.0.0, 
  <https://github.com/Josh-test-lab/autoFRK-python>.
  • A BibTeX entry for LaTeX users is:
  @Manual{,
    title = {autoFRK-python: Automatic Fixed Rank Kriging. The Python version with PyTorch},
    author = {Yao-Chih Hsu},
    year = {2025},
    note = {Python package version 1.0.0},
    url = {https://github.com/Josh-test-lab/autoFRK-python},
  }
  • To cite the original R package autoFRK:
  Tzeng S, Huang H, Wang W, Nychka D, Gillespie C (2021). _autoFRK: Automatic Fixed Rank Kriging_. R package version 1.4.3,
  <https://CRAN.R-project.org/package=autoFRK>.
  • A BibTeX entry for the original R package is:
  @Manual{,
    title = {autoFRK: Automatic Fixed Rank Kriging},
    author = {ShengLi Tzeng and Hsin-Cheng Huang and Wen-Ting Wang and Douglas Nychka and Colin Gillespie},
    year = {2021},
    note = {R package version 1.4.3},
    url = {https://CRAN.R-project.org/package=autoFRK},
  }

Release Notes

v1.1.0

  • Added dtype and device parameters to AutoFRK.predict() and MRTS.predict().
  • Added logger_level parameter to AutoFRK.__init__() and MRTS.__init__() (default: 20). Options include NOTSET(0), DEBUG(10), INFO(20), WARNING(30), ERROR(40), CRITICAL(50).
  • Enhanced automatic device selection, including MPS support.
  • Fixed device assignment issue when device is not specified, preventing redundant parameter transfers.

v1.0.0

  • Ported R package autoFRK to Python.

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

autofrk-1.1.0.tar.gz (56.9 kB view details)

Uploaded Source

Built Distribution

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

autofrk-1.1.0-py3-none-any.whl (60.7 kB view details)

Uploaded Python 3

File details

Details for the file autofrk-1.1.0.tar.gz.

File metadata

  • Download URL: autofrk-1.1.0.tar.gz
  • Upload date:
  • Size: 56.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for autofrk-1.1.0.tar.gz
Algorithm Hash digest
SHA256 80020bb5e7b66071db012f8e7d96039af9355706c678416a8371376f8485f960
MD5 872fe9881c70e3783f876b7205146b9b
BLAKE2b-256 309f2929d80e63bd090714700f3e08129970d982082d2f86337250ac972e1590

See more details on using hashes here.

File details

Details for the file autofrk-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: autofrk-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 60.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for autofrk-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3106076d55be7057d7e4765cfe5be53749ed07a76e6c9570d96f7512e1d5fd8
MD5 0355daa6e2222d9bb6a5e40084178a82
BLAKE2b-256 759076d267035944afbd552aad305ad4059176508a098d9b7c32c1094b979a22

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