-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_ipm.py
More file actions
86 lines (61 loc) · 1.73 KB
/
test_ipm.py
File metadata and controls
86 lines (61 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import pytest
import PyIPM
np.random.seed(0)
@pytest.fixture
def x() -> np.ndarray:
"""
Some input
Returns: 2 Dimensional input array with 100 data points
"""
return 5 * (np.random.rand(100, 2) - 0.5)
@pytest.fixture
def y(x: np.ndarray) -> np.ndarray:
"""
Some 1 dimensional output which is a function of the input
Args:
x: Some 2 dimensional Input
Returns:
Output array
"""
return x[:, 0] ** 2 + x[:, 1] ** 2 * np.random.rand(x.shape[0])
@pytest.fixture
def model(x: np.ndarray, y: np.ndarray) -> PyIPM.IPM:
"""
Create a 2 degree IPM from training data
Args:
x: Input data
y: Output data
Returns:
Trained IPM
"""
model = PyIPM.IPM(polynomial_degree=2)
model.fit(x, y)
return model
class TestIPM:
def test_prediction(self, model: PyIPM.IPM, x: np.ndarray):
"""
Ensure that the predicted IPM bounds match stored values
Args:
model: Trained IPM
x: Input data
"""
upper_bound, lower_bound = model.predict(x)
tolerance = 0.01
assert (
abs(upper_bound[0] - 1.15) < tolerance
), "Upper bound does not match stored value"
assert (
abs(lower_bound[0] - 0.15) < tolerance
), "Lower bound does not match stored value"
def test_reliability(self, model: PyIPM.IPM):
"""
Ensure model reliability matches stored value
Args:
model: Trained IPM
"""
reliability = model.get_model_reliability()
tolerance = 0.01
assert (
abs(reliability - 0.68) < tolerance
), "Model reliability does not match stored value"