A PyThon implementation of NNGLS
Project description
GeospaNN  Neural networks for geospatial data
Authors: Wentao Zhan (wzhan3@jhu.edu), Abhirup Datta (abhidatta@jhu.edu)
A package based on the paper: Neural networks for geospatial data
GeospaNN is a formal implementation of NNGLS, the Neural Networks for geospatial data proposed in Zhan et.al (2023), that explicitly accounts for spatial correlation in the data. The package is developed using PyTorch and under the framework of PyG library. NNGLS is a geographicallyinformed Graph Neural Network (GNN) for analyzing large and irregular geospatial data, that combines multilayer perceptrons, Gaussian processes, and generalized least squares (GLS) loss. NNGLS offers both regression function estimation and spatial prediction, and can scale up to sample sizes of hundreds of thousands. Users are welcome to provide any helpful suggestions and comments.
Overview
The Python package geospaNN stands for 'geospatial Neural Networks', where we implement NNGLS, neural networks tailored for analysis of geospatial data that explicitly accounts for spatial dependence (Zhan et.al, 2023). Geospatial data naturally exhibits spatial correlation or dependence and traditional geostatistical analysis often relies on modelbased approaches to handle the spatial dependency, treating the spatial outcome $y(s)$ as a linear regression on covariates $x(s)$ and modeling dependency through the spatially correlated errors. For example, using Gaussian processes (GP) to model dependent errors, simple techniques like kriging can provide powerful prediction performance by properly aggregating the neighboring information. On the other hand, artificial Neural Networks (NN), one of the most popular machine learning approaches, could be used to estimate nonlinear regression functions. However, common neural networks like multilayer perceptrons (MLP) does not incorporate correlation among data units.
Our package geospaNN takes the advantages from both perspectives and provides an efficient tool for geospatial data analysis. In NNGLS, an MLP is used to model the nonlinear regression function while a GP is used to model the spatial dependence. The resulting loss function then becomes a generalized least squares (GLS) loss informed by the GP covariance matrix, thereby explicitly incorporating spatial correlation into the neural network optimization. The idea mimics the extension of ordinary least squares (OLS) loss to GLS loss in linear regression for dependent data.
Zhan and Datta, 2023 shows that neural networks with GLS loss can be represented as a graph neural network, with the GP covariances guiding the neighborhood aggregation on the output layer. Thus NNGLS is implemented in geospaNN with the framework of Graph Neural Networks (GNN), and is highly generalizable. (The implementation of geospaNN' uses the 'torch_geom' module.)
geospaNN provides an estimate of regression function 𝑓(𝑥) as well as accurate spatial predictions using Gaussian process (kriging), and thus constitutes a complete geospatial analysis pipeline. To accelerate the training process for the GP, geospaNN approximates the working correlation structure using Nearest Neighbor Gaussian Process (NNGP) (Datta et al., 2016) which makes it suitable for larger datasets towards a size of 1 million.
Installation
(Currently) to install the development version of the package, a preinstalled PyTorch and PyG libraries are needed. Installation in the following order is recommended to avoid any compilation issue.
 To install PyTorch, find and install the binary suitable for your machine here.
 Then to install the PyG library, find and install the proper binary here.
 Make sure to also install the dependencies including pyg_lib, torch_scatter, torch_sparse, torch_cluster, and torch_spline_conv.
pip install numpy==1.26 nocachedir
pip install torch==2.0.0 nocachedir
pip install torchscatter f https://data.pyg.org/whl/torch2.0.0.html nocachedir
pip install torchsparse f https://data.pyg.org/whl/torch2.0.0.html nocachedir
pip install torchcluster f https://data.pyg.org/whl/torch2.0.0.html nocachedir
pip install torch_geometric nocachedir
Once PyTorch and PyG are successfully installed, use the following command in the terminal for the latest version:
pip install https://github.com/WentaoZhan1998/geospaNN/archive/main.zip
To install the pypi version, use the following command in the terminal:
pip install geospaNN
An easy running sample:
First import the modules and set up the parameters
 Define the Friedman's function, and specify the dimension of input covariates.
 Set the parameters for the spatial process.
 Set the hyperparameters of the data.
import torch
import geospaNN
import numpy as np
# 1.
def f5(X): return (10*np.sin(np.pi*X[:,0]*X[:,1]) + 20*(X[:,2]0.5)**2 + 10*X[:,3] +5*X[:,4])/6
p = 5; funXY = f5
# 2.
sigma = 1
phi = 3/np.sqrt(2)
tau = 0.01
theta = torch.tensor([sigma, phi, tau])
# 3.
n = 1000 # Size of the simulated sample.
nn = 20 # Neighbor size used for NNGP.
batch_size = 50 # Batch size for training the neural networks.
Next, simulate and split the data.
 Simulate the spatially correlated data with spatial coordinates randomly sampled on a [0, 10]^2 squared domain.
 Build the nearest neighbor graph, as a torch_geometric.data.Data object.
 Split data into training, validation, testing sets.
# 1.
torch.manual_seed(2024)
X, Y, coord, cov, corerr = geospaNN.Simulation(n, p, nn, funXY, theta, range=[0, 10])
# 2.
data = geospaNN.make_graph(X, Y, coord, nn)
# 3.
data_train, data_val, data_test = geospaNN.split_data(X, Y, coord, neighbor_size=20,
test_proportion=0.2)
Compose the mlp structure and train easily.
 Define the mlp structure (torch.nn) to use.
 Define the NNGLS corresponding model.
 Define the NNGLS training class with learning rate and tolerance.
 Train the model.
# 1.
mlp = torch.nn.Sequential(
torch.nn.Linear(p, 50),
torch.nn.ReLU(),
torch.nn.Linear(50, 20),
torch.nn.ReLU(),
torch.nn.Linear(20, 10),
torch.nn.ReLU(),
torch.nn.Linear(10, 1),
)
# 2.
model = geospaNN.nngls(p=p, neighbor_size=nn, coord_dimensions=2, mlp=mlp, theta=torch.tensor([1.5, 5, 0.1]))
# 3.
nngls_model = geospaNN.nngls_train(model, lr = 0.01, min_delta = 0.001)
# 4.
training_log = nngls_model.train(data_train, data_val, data_test,
Update_init = 10, Update_step = 10)
Estimation from the model. The variable is a torch.Tensor object of the same dimension
train_estimate = model.estimate(data_train.x)
Kriging prediction from the model. The first variable is supposed to be the data used for training, and the second variable a torch_geometric.data.Data object which can be composed by geospaNN.make_graph()'.
test_predict = model.predict(data_train, data_test)
Running examples:

Python packages time, pandas, seaborn, geopandas, and matplotlib are required to run the following experiments.

A simulation experiment with a common spatial setting is shown here.

For the linear regression case, a performance comparison with the R package BRISC is shown here.

A real data experiment is shown here.

The PM2.5 data is collected from the U.S. Environmental Protection Agency datasets for each state are collected and bound together to obtain 'pm25_2022.csv'. daily PM2.5 files are subsets of 'pm25_2022.csv' produced by 'realdata_preprocess.py'. One can skip the preprocessing and use the daily files directory.

The meteorological data is collected from the National Centers for Environmental Prediction’s (NCEP) North American Regional Reanalysis (NARR) product. The '.nc' (netCDF) files should be downloaded from the website and saved in the root directory to run 'realdata_preprocess.py'. Otherwise, one may skip the preprocessing and use covariate files directly.
Citation
Please cite the following paper when you use geospaNN:
Zhan, Wentao, and Abhirup Datta. "Neural networks for geospatial data." Journal of the American Statistical Association justaccepted (2024): 121.
References
Datta, Abhirup, Sudipto Banerjee, Andrew O. Finley, and Alan E. Gelfand. "Hierarchical nearestneighbor Gaussian process models for large geostatistical datasets." Journal of the American Statistical Association 111, no. 514 (2016): 800812. link
Zhan, Wentao, and Abhirup Datta. "Neural networks for geospatial data." Journal of the American Statistical Association justaccepted (2024): 121.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.