|
| 1 | +import unittest, torch, math |
| 2 | +from scimlstudio.utils import evaluate_scalar |
| 3 | +from scimlstudio.models import RBF |
| 4 | +from scimlstudio.utils import Normalize, Standardize |
| 5 | +from pyDOE3 import lhs, halton_sequence |
| 6 | + |
| 7 | +# Defining the device and data type |
| 8 | +tkwargs = {"device": torch.device("cuda" if torch.cuda.is_available() else "cpu"), "dtype": torch.float64} |
| 9 | + |
| 10 | +class TestRBF(unittest.TestCase): |
| 11 | + |
| 12 | + """ |
| 13 | + Class defining the test cases for the RBF model |
| 14 | + """ |
| 15 | + |
| 16 | + def test_rbf(self): |
| 17 | + |
| 18 | + # Generating some data for the test cases |
| 19 | + # Function used for the test cases is a sinusoidal function (https://www.sfu.ca/~ssurjano/curretal88sin.html) |
| 20 | + x_train = torch.linspace(0, 1, 10, **tkwargs) |
| 21 | + y_train = torch.sin(2*math.pi*(x_train - 0.1)) |
| 22 | + |
| 23 | + x_test = torch.linspace(0, 1, 100, **tkwargs) |
| 24 | + y_test = torch.sin(2*math.pi*(x_test - 0.1)) |
| 25 | + |
| 26 | + # Creating and fitting RBF models |
| 27 | + for basis in ["gaussian", "multiquadric", "inverse"]: |
| 28 | + rbf = RBF(x_train.reshape(-1,1), y_train.reshape(-1,1), sigma = 0.1, basis = basis) |
| 29 | + _, basis_matrix = rbf.fit() |
| 30 | + train_pred = rbf.predict(x_train.reshape(-1,1)) |
| 31 | + test_pred = rbf.predict(x_test.reshape(-1,1)) |
| 32 | + r2_value = evaluate_scalar(test_pred.reshape(-1,), y_test, "r2") |
| 33 | + |
| 34 | + torch.testing.assert_close(train_pred.reshape(-1,), y_train, rtol=0, atol=1e-6, check_device=True, check_dtype=True) # interpolation check |
| 35 | + assert basis_matrix.ndim == 2 # basis matrix must be 2D |
| 36 | + if basis == "gaussian": |
| 37 | + assert (torch.diag(basis_matrix) == 1).all() # diagonal of basis matrix must be all 1s if basis is gaussian |
| 38 | + assert round(r2_value, 6) < 1 |
| 39 | + |
| 40 | + # Creating and fitting RBF models with normalize transformations |
| 41 | + input_transform = Normalize(x_train.reshape(-1,1)) |
| 42 | + output_transform = Normalize(y_train.reshape(-1,1)) |
| 43 | + for basis in ["gaussian", "multiquadric", "inverse"]: |
| 44 | + rbf = RBF(x_train.reshape(-1,1), y_train.reshape(-1,1), sigma = 0.1, basis = basis, input_transform=input_transform, output_transform=output_transform) |
| 45 | + _, basis_matrix = rbf.fit() |
| 46 | + train_pred = rbf.predict(x_train.reshape(-1,1)) |
| 47 | + test_pred = rbf.predict(x_test.reshape(-1,1)) |
| 48 | + r2_value = evaluate_scalar(test_pred.reshape(-1,), y_test, "r2") |
| 49 | + |
| 50 | + torch.testing.assert_close(train_pred.reshape(-1,), y_train, rtol=0, atol=1e-6, check_device=True, check_dtype=True) # interpolation check |
| 51 | + assert basis_matrix.ndim == 2 # basis matrix must be 2D |
| 52 | + if basis == "gaussian": |
| 53 | + assert (torch.diag(basis_matrix) == 1).all() # diagonal of basis matrix must be all 1s if basis is gaussian |
| 54 | + assert round(r2_value, 6) < 1 |
| 55 | + |
| 56 | + def test_rbf_5D(self): |
| 57 | + |
| 58 | + # Generating some data for the test cases |
| 59 | + # Function used for the test cases is the Friedman function (https://www.sfu.ca/~ssurjano/fried.html) |
| 60 | + x_train = torch.tensor(halton_sequence(num_points=15, dimension=5), **tkwargs) |
| 61 | + y_train = 10*torch.sin(math.pi*x_train[:,0]*x_train[:,1]) + 20 * (x_train[:,2] - 0.5) ** 2 + 10 * x_train[:,3] + 5 * x_train[:,4] |
| 62 | + |
| 63 | + x_test = torch.tensor(lhs(n=5, samples=100, criterion='cm', iterations=100, seed=10), **tkwargs) |
| 64 | + y_test = 10*torch.sin(math.pi*x_test[:,0]*x_test[:,1]) + 20 * (x_test[:,2] - 0.5) ** 2 + 10 * x_test[:,3] + 5 * x_test[:,4] |
| 65 | + |
| 66 | + # Creating and fitting RBF models |
| 67 | + for basis in ["gaussian", "multiquadric", "inverse"]: |
| 68 | + rbf = RBF(x_train, y_train.reshape(-1,1), sigma = 0.1, basis = basis) |
| 69 | + _, basis_matrix = rbf.fit() |
| 70 | + train_pred = rbf.predict(x_train) |
| 71 | + test_pred = rbf.predict(x_test) |
| 72 | + r2_value = evaluate_scalar(test_pred.reshape(-1,), y_test, "r2") |
| 73 | + |
| 74 | + torch.testing.assert_close(train_pred.reshape(-1,), y_train, rtol=0, atol=1e-6, check_device=True, check_dtype=True) # interpolation check |
| 75 | + assert basis_matrix.ndim == 2 # basis matrix must be 2D |
| 76 | + if basis == "gaussian": |
| 77 | + assert (torch.diag(basis_matrix) == 1).all() # diagonal of basis matrix must be all 1s if basis is gaussian |
| 78 | + assert round(r2_value, 6) < 1 |
| 79 | + |
| 80 | + # Creating and fitting RBF models with standardize transformations |
| 81 | + input_transform = Standardize(x_train) |
| 82 | + output_transform = Standardize(y_train.reshape(-1,1)) |
| 83 | + for basis in ["gaussian", "multiquadric", "inverse"]: |
| 84 | + rbf = RBF(x_train, y_train.reshape(-1,1), sigma = 0.1, basis = basis, input_transform=input_transform, output_transform=output_transform) |
| 85 | + _, basis_matrix = rbf.fit() |
| 86 | + train_pred = rbf.predict(x_train) |
| 87 | + test_pred = rbf.predict(x_test) |
| 88 | + r2_value = evaluate_scalar(test_pred.reshape(-1,), y_test, "r2") |
| 89 | + |
| 90 | + torch.testing.assert_close(train_pred.reshape(-1,), y_train, rtol=0, atol=1e-6, check_device=True, check_dtype=True) # interpolation check |
| 91 | + assert basis_matrix.ndim == 2 # basis matrix must be 2D |
| 92 | + if basis == "gaussian": |
| 93 | + assert (torch.diag(basis_matrix) == 1).all() # diagonal of basis matrix must be all 1s if basis is gaussian |
| 94 | + assert round(r2_value, 6) < 1 |
| 95 | + |
| 96 | + def test_inputs(self): |
| 97 | + |
| 98 | + # Generate dummy data |
| 99 | + x_random = torch.rand(15) |
| 100 | + y_random = torch.rand(15) |
| 101 | + |
| 102 | + with self.assertRaises(Exception): |
| 103 | + _ = RBF(x_random.reshape(-1,1), y_random, sigma = 0.1) |
| 104 | + |
| 105 | + with self.assertRaises(Exception): |
| 106 | + _ = RBF(x_random, y_random.reshape(-1,1), sigma = 0.1) |
| 107 | + |
| 108 | + with self.assertRaises(Exception): |
| 109 | + _ = RBF(x_random.reshape(-1,1), y_random.reshape(-1,1), sigma = -0.1) |
| 110 | + |
| 111 | + with self.assertRaises(Exception): |
| 112 | + _ = RBF(x_random.reshape(-1,1), y_random.reshape(-1,1), sigma = 0.1, basis="linear") |
| 113 | + |
| 114 | + _ = RBF(x_random.reshape(-1,1), y_random.reshape(-1,1), sigma = 0.1, basis="inverse") |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main() |
| 118 | + |
0 commit comments