Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tnipen committed Mar 12, 2024
1 parent 5905f98 commit 9cceaf4
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 47 deletions.
101 changes: 101 additions & 0 deletions tests/test_barnes_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from __future__ import print_function
import unittest
import gridpp
import numpy as np


class Test(unittest.TestCase):
def test_basic(self):
x = [0, 1000, 2000, 3000, np.nan]
structure_corr = dict()
barnes = gridpp.BarnesStructure(2000)
structure_corr[barnes] = [1, 0.8824968934059143, 0.6065306663513184, 0.32465246319770813, 0]
structure_corr[gridpp.CressmanStructure(2000)] = [1, 0.6, 0, 0, 0]
structure_corr[gridpp.CrossValidation(barnes, 1000)] = [0, 0, 0.6065306663513184, 0.32465246319770813, 0]
N = len(x)
for structure, corr in structure_corr.items():
is_cv = isinstance(structure, gridpp.CrossValidation)
for i in range(N):
with self.subTest(structure=type(structure), i=i):
p1 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
p2 = gridpp.Point(x[i], 0, 0, 0, gridpp.Cartesian)
funcs = [structure.corr, structure.corr_background]
if is_cv:
funcs = [structure.corr_background]
for func in funcs:
self.assertAlmostEqual(corr[i], func(p1, p2))

# Check that changing the order does not change the results
self.assertAlmostEqual(corr[i], func(p2, p1))

# Check identical points
if not is_cv and not np.isnan(x[i]):
self.assertAlmostEqual(1, func(p2, p2))

def test_invalid_h(self):
hs = [-1, np.nan]
for h in hs:
with self.subTest(h=h):
with self.assertRaises(Exception) as e:
structure = gridpp.BarnesStructure(h)

def test_invalid_hmax(self):
with self.assertRaises(Exception) as e:
structure = gridpp.BarnesStructure(2000, 100, 0, -1)

def test_spatial(self):
y = [[0, 0]]
x = [[0, 2500]]
grid = gridpp.Grid(y, x, y, y, gridpp.Cartesian)
h = [[2500, 1]]
v = [[0, 0]]
l = [[0, 0]]
min_rho = 0.1
structure = gridpp.BarnesStructure(grid, h, v, l, min_rho)
p1 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
p2 = gridpp.Point(0, 2500, 0, 0, gridpp.Cartesian)

# Length scale at p1 is 2500
expected = np.sqrt(-2*np.log(min_rho)) * 2500
self.assertAlmostEqual(expected, structure.localization_distance(p1), 4)
self.assertAlmostEqual(0.6, structure.corr(p1, p2), 1)

# Length scale at p2 is 1
expected = np.sqrt(-2*np.log(min_rho)) * 1
self.assertAlmostEqual(expected, structure.localization_distance(p2), 4)
self.assertAlmostEqual(0, structure.corr(p2, p1), 1)

def test_spatial_invalid_arguments(self):
"""Check dimension mismatch"""
y, x = np.meshgrid(np.linspace(0, 1, 2), np.linspace(0,1,3))
grid = gridpp.Grid(y, x, y, y, gridpp.Cartesian)

valid = np.ones([3, 2])
invalid = [np.ones([3,4]), np.ones([2, 2]), np.ones([2, 4])]
structure = gridpp.BarnesStructure(grid, valid, valid, valid)

for inval in invalid:
with self.assertRaises(ValueError) as e:
structure = gridpp.BarnesStructure(grid, inval, valid, valid)
with self.assertRaises(ValueError) as e:
structure = gridpp.BarnesStructure(grid, valid, inval, valid)
with self.assertRaises(ValueError) as e:
structure = gridpp.BarnesStructure(grid, valid, valid, inval)

def test_hmax(self):
hmaxs = [0, 1000, 2000, 10000]
p0 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
dist_ans = {0:1, 1000:0.8824968934059143, 2000:0.6065306663513184, 3000:0.32465246319770813}
for hmax in hmaxs:
for dist, ans in dist_ans.items():
with self.subTest(hmax=hmax, dist=dist):
structure = gridpp.BarnesStructure(2000, 0, 0, hmax)
corr = structure.corr(p0, gridpp.Point(dist, 0, 0, 0, gridpp.Cartesian))
if dist > hmax:
self.assertEqual(0, corr)
else:
self.assertEqual(ans, corr)


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions tests/test_qnh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_invalid_input(self):

def test_invalid_values(self):
self.assertTrue(np.isnan(gridpp.qnh([-1], [0])))
self.assertTrue(np.isnan(gridpp.qnh([101325], [np.nan])))
self.assertTrue(np.isnan(gridpp.qnh([np.nan], [0])))

def test_1(self):
p = [101325, 90000, 90000, 110000]
Expand Down
80 changes: 33 additions & 47 deletions tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,7 @@


class Test(unittest.TestCase):
def test_basic(self):
x = [0, 1000, 2000, 3000, np.nan]
structure_corr = dict()
barnes = gridpp.BarnesStructure(2000)
structure_corr[barnes] = [1, 0.8824968934059143, 0.6065306663513184, 0.32465246319770813, 0]
structure_corr[gridpp.CressmanStructure(2000)] = [1, 0.6, 0, 0, 0]
structure_corr[gridpp.CrossValidation(barnes, 1000)] = [0, 0, 0.6065306663513184, 0.32465246319770813, 0]
N = len(x)
for structure, corr in structure_corr.items():
is_cv = isinstance(structure, gridpp.CrossValidation)
for i in range(N):
with self.subTest(structure=type(structure), i=i):
p1 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
p2 = gridpp.Point(x[i], 0, 0, 0, gridpp.Cartesian)
funcs = [structure.corr, structure.corr_background]
if is_cv:
funcs = [structure.corr_background]
for func in funcs:
self.assertAlmostEqual(corr[i], func(p1, p2))

# Check that changing the order does not change the results
self.assertAlmostEqual(corr[i], func(p2, p1))

# Check identical points
if not is_cv and not np.isnan(x[i]):
self.assertAlmostEqual(1, func(p2, p2))

def test_invalid_h(self):
structures = list()
structures = [gridpp.BarnesStructure, gridpp.CressmanStructure]
for structure in structures:
for h in [-1, np.nan]:
Expand All @@ -49,20 +21,6 @@ def test_barnes_h(self):
with self.assertRaises(Exception) as e:
structure = gridpp.BarnesStructure(h)

def test_barnes_hmax(self):
hmaxs = [0, 1000, 2000, 10000]
p0 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
dist_ans = {0:1, 1000:0.8824968934059143, 2000:0.6065306663513184, 3000:0.32465246319770813}
for hmax in hmaxs:
for dist, ans in dist_ans.items():
with self.subTest(hmax=hmax, dist=dist):
structure = gridpp.BarnesStructure(2000, 0, 0, hmax)
corr = structure.corr(p0, gridpp.Point(dist, 0, 0, 0, gridpp.Cartesian))
if dist > hmax:
self.assertEqual(0, corr)
else:
self.assertEqual(ans, corr)

def test_invalid_elevation(self):
"""Check that point elevations are ignored if one is missing"""
structures = [gridpp.BarnesStructure, gridpp.CressmanStructure]
Expand All @@ -78,7 +36,6 @@ def test_invalid_elevation(self):
self.assertAlmostEqual(s2.corr(p1, p3), s2.corr(p1, p2))

def test_invalid_v(self):
structures = list()
structures = [gridpp.BarnesStructure, gridpp.CressmanStructure]
h = 2000
p1 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
Expand All @@ -97,10 +54,6 @@ def test_invalid_w(self):
with self.assertRaises(Exception) as e:
s = structure(h, v, w)

def test_invalid_hmax(self):
with self.assertRaises(Exception) as e:
structure = gridpp.BarnesStructure(2000, 100, 0, -1)

def test_invalid_cv(self):
barnes = gridpp.BarnesStructure(2000)
for dist in [-1, np.nan]:
Expand Down Expand Up @@ -166,6 +119,39 @@ def test_multiple_structure_corr_vec(self):
output = gridpp.optimal_interpolation(grid, background, points, pobs, pratios, pbackground, structure, max_points)
np.testing.assert_array_almost_equal(output, [0.3, 0.3, 0.6**3/2])

def test_clone(self):
s1 = gridpp.CressmanStructure(5000, 11, 22)
s2 = gridpp.CressmanStructure(33, 200, 44)
s3 = gridpp.CressmanStructure(55, 66, 2)
structure = gridpp.MultipleStructure(s1, s2, s3)
structure_clone = structure.clone()
del structure

# Check that clone still works
p1 = gridpp.Point(0, 0)
p2 = gridpp.Point(0, 0)
structure_clone.corr(p1, p2)

def test_same_corr_after_clone(self):
h = 850
v = 92
w = 0.44
structures = [gridpp.BarnesStructure(h, v, w), gridpp.CressmanStructure(h, v, w)]
structures += [gridpp.MultipleStructure(gridpp.BarnesStructure(1.3*h, v, w),
gridpp.BarnesStructure(h, 1.3*v, w), gridpp.BarnesStructure(h, v, 1.3*w))]
structures += [gridpp.CrossValidation(gridpp.BarnesStructure(h, v, w), 1000)]
p1 = gridpp.Point(0, 0, 0, 0, gridpp.Cartesian)
p2 = gridpp.Point(500, 0, 50, 0.25, gridpp.Cartesian)
for structure in structures:
structure_clone = structure.clone()
ans = structure.corr(p1, p2)
ans_clone = structure_clone.corr(p1, p2)
self.assertEqual(ans, ans_clone)

ans = structure.corr_background(p1, p2)
ans_clone = structure_clone.corr_background(p1, p2)
self.assertEqual(ans, ans_clone)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9cceaf4

Please sign in to comment.