autoFRK: Automatic Fixed Rank Kriging. The Python version with PyTorch
Project description
autoFRK-python
- Repository: https://github.com/Josh-test-lab/autoFRK-python
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
- ShengLi Tzeng — Original Paper Author
- Hsin-Cheng Huang — Original Paper Author
- Wen-Ting Wang — R Package Author
- Yao-Chih Hsu — Python Package Author
- Yi-Xuan Xie — Python Package Tester
- Xuan-Chun Wang — Python Package Tester
License
- GPL (>= 3)
Development and Contribution
- Built with PyTorch, supporting GPU acceleration
- Report bugs or request features on GitHub issues
References
- 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
- Wang, J. W.-T. (n.d.). autoFRK. GitHub. Retrieved January 7, 2023, from https://egpivo.github.io/autoFRK/
- Tzeng, S. & Huang, H.-C. (2018). Resolution Adaptive Fixed Rank Kriging. Technometrics. https://doi.org/10.1080/00401706.2017.1345701
- Nychka, D., Hammerling, D., Sain, S., & Lenssen, N. (2016). LatticeKrig: Multiresolution Kriging Based on Markov Random Fields
Citation
- To cite the Python package
autoFRK-pythonin 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
dtypeanddeviceparameters toAutoFRK.predict()andMRTS.predict(). - Added
logger_levelparameter toAutoFRK.__init__()andMRTS.__init__()(default: 20). Options includeNOTSET(0),DEBUG(10),INFO(20),WARNING(30),ERROR(40),CRITICAL(50). - Enhanced automatic device selection, including MPS support.
- Fixed device assignment issue when
deviceis not specified, preventing redundant parameter transfers.
v1.0.0
- Ported R package
autoFRKto 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80020bb5e7b66071db012f8e7d96039af9355706c678416a8371376f8485f960
|
|
| MD5 |
872fe9881c70e3783f876b7205146b9b
|
|
| BLAKE2b-256 |
309f2929d80e63bd090714700f3e08129970d982082d2f86337250ac972e1590
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3106076d55be7057d7e4765cfe5be53749ed07a76e6c9570d96f7512e1d5fd8
|
|
| MD5 |
0355daa6e2222d9bb6a5e40084178a82
|
|
| BLAKE2b-256 |
759076d267035944afbd552aad305ad4059176508a098d9b7c32c1094b979a22
|