Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
44cb53a
add dependency pytest
AlexDo1 Jul 11, 2023
f7467c3
replace random.shuffle with np.random.shuffle
AlexDo1 Jul 12, 2023
5a8729d
test sgs ordinary kriging
AlexDo1 Jul 12, 2023
7c88b96
test simple kriging
AlexDo1 Jul 12, 2023
56f41c2
workflow file
AlexDo1 Jul 12, 2023
4829a7e
wip workflow
AlexDo1 Jul 12, 2023
d05fee6
test tolerance
AlexDo1 Jul 12, 2023
38fb968
wip
AlexDo1 Jul 12, 2023
88319a8
use a copy of test data for tests
AlexDo1 Jul 12, 2023
c4c2963
use test data
AlexDo1 Jul 12, 2023
27b4078
test for python 3.6, 3.7 and 3.8
AlexDo1 Jul 12, 2023
fea75ef
scikit-learn requires Pyton>=3.8
AlexDo1 Jul 12, 2023
286ed33
remove Python 3.6 and 3.7
AlexDo1 Jul 12, 2023
4f41bcb
scipy 1.1.1 does not exist, scipy 1.11.1 was installed instead, which…
AlexDo1 Jul 12, 2023
8341770
try scipy1.2.1
AlexDo1 Jul 12, 2023
2fa86a2
wip
AlexDo1 Jul 12, 2023
b7804a2
scipy>=1.7.0
AlexDo1 Jul 12, 2023
feaa3d6
upgrade pip bofore installing dependencies
AlexDo1 Jul 12, 2023
b65f069
pip install scipy
AlexDo1 Jul 12, 2023
d79ddea
fix pip install
AlexDo1 Jul 12, 2023
d6c0141
reset scipy version
AlexDo1 Jul 12, 2023
7790221
wip
AlexDo1 Jul 12, 2023
0d853b9
wip
AlexDo1 Jul 12, 2023
902e4a6
do not test for python 3.10
AlexDo1 Jul 12, 2023
e2860ac
use pytest.approx for assertion
AlexDo1 Jul 12, 2023
f301943
assert_allclose() again
AlexDo1 Jul 12, 2023
6c4ba3e
wip
AlexDo1 Jul 12, 2023
759ca17
remove dependency pytest and install in action
AlexDo1 Jul 13, 2023
03bf3a3
revert python_requires >=3
AlexDo1 Jul 13, 2023
2ef119b
round expected_sim
AlexDo1 Jul 13, 2023
0ffd20c
checkout correct branch!
AlexDo1 Jul 13, 2023
862e929
np.testing.assert_allclose, decimal 1
AlexDo1 Jul 13, 2023
747777d
atol 0.1
AlexDo1 Jul 13, 2023
248b21d
np.testing.assert_array_almost_equal(...,decimal=1)
AlexDo1 Jul 13, 2023
0e49add
fix expected_sim
AlexDo1 Jul 13, 2023
26cbd48
use data in demo/ again
AlexDo1 Jul 13, 2023
77cac61
restructure and rename tests
AlexDo1 Jul 13, 2023
a402cee
revert scipy
AlexDo1 Jul 13, 2023
9ac6342
yml: pip install -e .
AlexDo1 Jul 13, 2023
290f978
install numpy<1.24
AlexDo1 Jul 13, 2023
f391798
fix workflow
AlexDo1 Jul 13, 2023
8a9a2ed
wip
AlexDo1 Jul 13, 2023
49c22f0
test Python 3.10
AlexDo1 Jul 13, 2023
dc35b6e
add coverage
AlexDo1 Jul 13, 2023
dea2354
use random module again
AlexDo1 Jul 13, 2023
c204609
rename testfile & adapt data for seeded random
AlexDo1 Jul 13, 2023
a39e59c
tests for okrige and skrige
AlexDo1 Jul 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Run Tests

on:
push:
branches:
- "*"
pull_request:
branches:
- "*"

jobs:
tests:
runs-on: ubuntu-20.04
strategy:
matrix:
python: ["3.8", "3.9", "3.10"]

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- name: Install GStatSim
run: |
pip install "numpy<=1.23.4"
pip install -e .
pip install pytest pytest-cov
- name: Run tests
run: pytest --cov=gstatsim --cov-report=html
- name: Upload coverage report
uses: actions/upload-artifact@v3
with:
name: coverage-report
path: ./htmlcov/
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'pandas>=1.1.1',
'scipy>=1.1.1',
'tqdm>=3',
'scikit-learn>=1.1.2',
'scikit-learn>=1.1.2'
]

with open('README.md', 'r') as f:
Expand Down
287 changes: 287 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
import pytest

import os

import pandas as pd
import numpy as np
import random

import gstatsim as gs


def test_ordinary_kriging():
"""
Test of ordinary kriging.
The test is roughly based on demos/3_Simple_kriging_and_ordinary_kriging.ipynb

"""
# read demo data
data_file_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), '../demos/data/greenland_test_data.csv')
df_bed = pd.read_csv(data_file_path)

# grid data to 100 m resolution and remove coordinates with NaNs
res = 1000
df_grid, _, _, __import__ = gs.Gridding.grid_data(
df_bed, 'X', 'Y', 'Bed', res)
df_grid = df_grid[df_grid["Z"].isnull() == False]

# define coordinate grid
xmin = np.min(df_grid['X'])
xmax = np.max(df_grid['X']) # min and max x values
ymin = np.min(df_grid['Y'])
ymax = np.max(df_grid['Y']) # min and max y values

Pred_grid_xy = gs.Gridding.prediction_grid(xmin, xmax, ymin, ymax, res)

# set random seed
np.random.seed(42)

# pick ("random") points from grid
index_points = np.random.choice(
range(len(Pred_grid_xy)), size=25, replace=False)
Pred_grid_xy = Pred_grid_xy[index_points, :]

# set variogram parameters
azimuth = 0
nugget = 0

# the major and minor ranges are the same in this example because it is isotropic
major_range = 19236.
minor_range = 19236.
sill = 22399.
vtype = 'Exponential'

# save variogram parameters as a list
vario = [azimuth, nugget, major_range, minor_range, sill, vtype]

k = 100 # number of neighboring data points used to estimate a given point
rad = 50000 # 50 km search radius

# est_SK is the estimate and var_SK is the variance
est_SK, var_SK = gs.Interpolation.okrige(
Pred_grid_xy, df_grid, 'X', 'Y', 'Z', k, vario, rad)

expected_est = np.array([443.9, 299.5, 356.6, 389.8, 160.5, 82.2, 333.4, 228.8, 413.2,
376., 201.4, 319.4, 286.2, 298.5, 368.8, 399.2, 337., 132.3,
305.9, 247.2, 270.5, 115.1, 417.5, 411.4, 32.7])

expected_var = np.array([6525.9, 0., 8308.6, 12133.4, 11820.4, 8437.8, 13252.4,
11871.6, 19809.1, 8048.9, 6762.9, 20021.6, 15386.6, 10189.8,
0., 6480.3, 12443.7, 5510., 6256.2, 13197.6, 0.,
10845.6, 17350.8, 3980.5, 8585.3])

# assert
np.testing.assert_array_almost_equal(est_SK, expected_est, decimal=1)
np.testing.assert_array_almost_equal(var_SK, expected_var, decimal=1)


def test_simple_kriging():
"""
Test of simple kriging.
The test is roughly based on demos/3_Simple_kriging_and_ordinary_kriging.ipynb

"""
# read demo data
data_file_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), '../demos/data/greenland_test_data.csv')
df_bed = pd.read_csv(data_file_path)

# grid data to 100 m resolution and remove coordinates with NaNs
res = 1000
df_grid, _, _, _ = gs.Gridding.grid_data(
df_bed, 'X', 'Y', 'Bed', res)
df_grid = df_grid[df_grid["Z"].isnull() == False]

# define coordinate grid
xmin = np.min(df_grid['X'])
xmax = np.max(df_grid['X']) # min and max x values
ymin = np.min(df_grid['Y'])
ymax = np.max(df_grid['Y']) # min and max y values

Pred_grid_xy = gs.Gridding.prediction_grid(xmin, xmax, ymin, ymax, res)

# set random seed
np.random.seed(42)

# pick ("random") points from grid
index_points = np.random.choice(
range(len(Pred_grid_xy)), size=25, replace=False)
Pred_grid_xy = Pred_grid_xy[index_points, :]

# set variogram parameters
azimuth = 0
nugget = 0

# the major and minor ranges are the same in this example because it is isotropic
major_range = 19236.
minor_range = 19236.
sill = 22399.
vtype = 'Exponential'

# save variogram parameters as a list
vario = [azimuth, nugget, major_range, minor_range, sill, vtype]

k = 100 # number of neighboring data points used to estimate a given point
rad = 50000 # 50 km search radius

# est_SK is the estimate and var_SK is the variance
est_SK, var_SK = gs.Interpolation.skrige(
Pred_grid_xy, df_grid, 'X', 'Y', 'Z', k, vario, rad)

expected_est = np.array([432.3, 299.5, 354.5, 384.8, 166.9, 82.9, 327.7, 230.1, 333.,
374.8, 208.7, 287., 296.2, 297.1, 368.8, 391.5, 336.4, 133.,
307.3, 249.4, 270.5, 119.2, 369.9, 411.2, 38.1])

expected_var = np.array([6846.7, 0., 8367.2, 12287., 12029.1, 8573.8, 13565.,
12098.3, 20816.3, 8134.4, 7099.8, 20588.1, 15810.4, 10312.,
0., 6739., 12586.4, 5530.8, 6312., 13469.5, 0.,
10969.9, 18156.7, 3992.2, 8701.3])

# assert
np.testing.assert_array_almost_equal(est_SK, expected_est, decimal=1)
np.testing.assert_array_almost_equal(var_SK, expected_var, decimal=1)


def test_sequential_gaussian_simulation_ordinary_kriging():
"""
This tests the sequential gaussian simulation with ordinary kriging.
The test is roughly based on demos/4_Sequential_Gaussian_Simulation.ipynb

"""
# read demo data
data_file_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), '../demos/data/greenland_test_data.csv')
df_bed = pd.read_csv(data_file_path)

# Grid and transform data, compute variogram parameters
# grid data to 100 m resolution and remove coordinates with NaNs
res = 1000
df_grid, _, _, _ = gs.Gridding.grid_data(df_bed, 'X', 'Y', 'Bed', res)

# remove NaNs
df_grid = df_grid[df_grid["Z"].isnull() == False]

# Initialize grid
# define coordinate grid
xmin = np.min(df_grid['X'])
xmax = np.max(df_grid['X']) # min and max x values

ymin = np.min(df_grid['Y'])
ymax = np.max(df_grid['Y']) # min and max y values

Pred_grid_xy = gs.Gridding.prediction_grid(xmin, xmax, ymin, ymax, res)

# set random seeds
# this line can be removed, if we decide to use numpy.random.shuffle instead of random.shuffle
random.seed(42)
np.random.seed(42)

# pick ("random") points from grid
index_points = np.random.choice(
range(len(Pred_grid_xy)), size=25, replace=False)
Pred_grid_xy = Pred_grid_xy[index_points, :]

# Sequential Gaussian simulation
# set variogram parameters
azimuth = 0
nugget = 0
k = 48 # number of neighboring data points used to estimate a given point
rad = 50000 # 50 km search radius

# the major and minor ranges are the same in this example because it is isotropic
major_range = minor_range = 31852.
sill = 0.7
vtype = 'Exponential'

# save variogram parameters as a list
vario = [azimuth, nugget, major_range, minor_range, sill, vtype]

# ordinary kriging
sim = gs.Interpolation.okrige_sgs(
Pred_grid_xy, df_grid, 'X', 'Y', 'Z', k, vario, rad)

# as we set the numpy random seed, the simulation is deterministic and we can compare to the following (rounded) results
expected_sim = np.array([445.5, 299.5, 358.9, 395.9, 153.5, 78.3, 344.5, 226.1, 432.2,
377.1, 202.9, 319.3, 276.9, 296., 368.8, 405.2, 328.2, 134.6,
307.8, 252.3, 270.5, 117.9, 425., 411.6, 31.2])

# assert
np.testing.assert_array_almost_equal(sim, expected_sim, decimal=1)


def test_sequential_gaussian_simulation_simple_kriging():
"""
This tests the sequential gaussian simulation with simple kriging.
The test is roughly based on demos/4_Sequential_Gaussian_Simulation.ipynb

"""
data_file_path = os.path.join(os.path.dirname(
os.path.realpath(__file__)), '../demos/data/greenland_test_data.csv')
df_bed = pd.read_csv(data_file_path)

# Grid and transform data, compute variogram parameters
# grid data to 100 m resolution and remove coordinates with NaNs
res = 1000
df_grid, grid_matrix, rows, cols = gs.Gridding.grid_data(
df_bed, 'X', 'Y', 'Bed', res)

# remove NaNs
df_grid = df_grid[df_grid["Z"].isnull() == False]

# maximum range distance
maxlag = 50000
# num of bins
n_lags = 70

# Initialize grid
# define coordinate grid
xmin = np.min(df_grid['X'])
xmax = np.max(df_grid['X']) # min and max x values

ymin = np.min(df_grid['Y'])
ymax = np.max(df_grid['Y']) # min and max y values

Pred_grid_xy = gs.Gridding.prediction_grid(xmin, xmax, ymin, ymax, res)

# set random seed
# this line can be removed, if we decide to use numpy.random.shuffle instead of random.shuffle
random.seed(42)
np.random.seed(42)

# pick ("random") points from grid
index_points = np.random.choice(
range(len(Pred_grid_xy)), size=25, replace=False)
Pred_grid_xy = Pred_grid_xy[index_points, :]

# Sequential Gaussian simulation
# set variogram parameters
azimuth = 0
nugget = 0
k = 48 # number of neighboring data points used to estimate a given point
rad = 50000 # 50 km search radius

# the major and minor ranges are the same in this example because it is isotropic
major_range = minor_range = 31852.
sill = 0.7
vtype = 'Exponential'

# save variogram parameters as a list
vario = [azimuth, nugget, major_range, minor_range, sill, vtype]

# simple kriging
sim = gs.Interpolation.skrige_sgs(
Pred_grid_xy, df_grid, 'X', 'Y', 'Z', k, vario, rad)

# as we set the numpy random seed, the simulation is deterministic and we can compare to the following (rounded) results
expected_sim = np.array([440.3, 299.5, 360., 396.7, 152.1, 78.3, 344.5, 225.6, 368.2,
378.1, 205.2, 304.5, 294.2, 296.3, 368.8, 400.4, 329.9, 133.9,
307.4, 252.2, 270.5, 116.5, 403.3, 411.6, 29.1])

# assert
np.testing.assert_array_almost_equal(sim, expected_sim, decimal=1)


if __name__ == '__main__':
import pytest
pytest.main()