-
Notifications
You must be signed in to change notification settings - Fork 231
/
test_morris_util.py
316 lines (255 loc) · 10 KB
/
test_morris_util.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from __future__ import division
from nose import with_setup
from nose.tools import raises
from numpy.testing import assert_equal, assert_array_equal, \
assert_almost_equal, assert_allclose
import numpy as np
from SALib.sample.morris_util import generate_P_star, \
compute_B_star, \
compute_delta, \
generate_trajectory, \
compute_distance, \
compute_distance_matrix, \
find_most_distant, find_maximum, \
make_index_list, \
check_input_sample, \
find_local_maximum, \
sum_distances, get_max_sum_ind, add_indices
def setup():
input_2 = [[0, 1 / 3.], [2 / 3., 1 / 3.], [2 / 3., 1.]]
input_1 = [[0, 1 / 3.], [0, 1.], [2 / 3., 1.]]
input_3 = [[2 / 3., 0], [2 / 3., 2 / 3.], [0, 2 / 3.]]
input_4 = [[1 / 3., 1.], [1., 1.], [1, 1 / 3.]]
input_5 = [[1 / 3., 1.], [1 / 3., 1 / 3.], [1, 1 / 3.]]
input_6 = [[1 / 3., 2 / 3.], [1 / 3., 0], [1., 0]]
return np.concatenate([input_1, input_2, input_3, input_4, input_5,
input_6])
def test_generate_P_star():
'''
Matrix P* - size (g * g) - describes order in which groups move
each row contains one element equal to 1, all others are 0
no two columns have 1s in the same position
'''
for i in range(1, 100):
output = generate_P_star(i)
if np.any(np.sum(output, 0) != np.ones(i)):
raise AssertionError("Not legal P along axis 0")
elif np.any(np.sum(output, 1) != np.ones(i)):
raise AssertionError("Not legal P along axis 1")
def test_compute_delta():
fixture = np.arange(2, 10)
output = [compute_delta(f) for f in fixture]
desired = np.array([1.00, 0.75, 0.66, 0.62,
0.60, 0.58, 0.57, 0.56])
assert_almost_equal(output, desired, decimal=2)
def test_generate_trajectory():
# Two groups of three factors
G = np.array([[1, 0], [0, 1], [0, 1]])
# Four levels, grid_jump = 2
num_levels, grid_jump = 4, 2
output = generate_trajectory(G, num_levels, grid_jump)
if np.any((output > 1) | (output < 0)):
raise AssertionError("Bound not working")
assert_equal(output.shape[0], 3)
assert_equal(output.shape[1], 3)
def test_compute_B_star():
'''
Tests for expected output
Taken from example 3.2 in Saltelli et al. (2008) pg 122
'''
k = 3
g = 2
x_star = np.matrix(np.array([1. / 3, 1. / 3, 0.]))
J = np.matrix(np.ones((g + 1, k)))
G = np.matrix('1,0;0,1;0,1')
D_star = np.matrix('1,0,0;0,-1,0;0,0,1')
P_star = np.matrix('1,0;0,1')
delta = 2. / 3
B = np.matrix(np.tril(np.ones([g + 1, g], dtype=int), -1))
desired = np.array([[1. / 3, 1, 0], [1, 1, 0], [1, 1. / 3, 2. / 3]])
output = compute_B_star(J, x_star, delta, B, G, P_star, D_star)
assert_array_equal(output, desired)
def test_distance():
'''
Tests the computation of the distance of two trajectories
'''
input_1 = np.matrix([[0, 1 / 3.], [0, 1.], [2 / 3., 1.]], dtype=np.float32)
input_3 = np.matrix([[2 / 3., 0], [2 / 3., 2 / 3.], [0, 2 / 3.]], dtype=np.float32)
output = compute_distance(input_1, input_3)
assert_allclose(output, 6.18, atol=1e-2)
def test_distance_of_identical_matrices_is_min():
input_1 = np.matrix([[ 1. ,1. ],
[ 1. ,0.33333333],
[ 0.33333333 ,0.33333333]])
input_2 = input_1.copy()
actual = compute_distance(input_1, input_2)
desired = 0
assert_allclose(actual, desired, atol=1e-2)
def test_distance_fail_with_difference_size_ip():
input_1 = np.matrix([[0, 1 / 3.], [0, 1.]], dtype=np.float32)
input_3 = np.matrix([[2 / 3., 0], [2 / 3., 2 / 3.], [0, 2 / 3.]], dtype=np.float32)
try:
compute_distance(input_1, input_3, 2)
except:
pass
else:
raise AssertionError("Different size matrices did not trigger error")
def test_compute_distance_matrix():
'''
Tests that a distance matrix is computed correctly
for an input of six trajectories and two parameters
'''
sample_inputs = setup()
output = compute_distance_matrix(sample_inputs, 6, 2)
expected = np.zeros((6, 6), dtype=np.float32)
expected[1, :] = [5.50, 0, 0, 0, 0, 0]
expected[2, :] = [6.18, 5.31, 0, 0, 0, 0]
expected[3, :] = [6.89, 6.18, 6.57, 0, 0, 0]
expected[4, :] = [6.18, 5.31, 5.41, 5.5, 0, 0]
expected[5, :] = [7.52, 5.99, 5.52, 7.31, 5.77, 0]
assert_allclose(output, expected, rtol=1e-2)
def test_compute_distance_matrix_local():
'''
Tests that a distance matrix is computed correctly for the local distance optimization.
The only change is that the local method needs the upper triangle of
the distance matrix instead of the lower one.
This is for an input of six trajectories and two parameters
'''
sample_inputs = setup()
output = compute_distance_matrix(sample_inputs, 6, 2, local_optimization=True)
expected = np.zeros((6, 6), dtype=np.float32)
expected[0, :] = [0, 5.50, 6.18, 6.89, 6.18, 7.52]
expected[1, :] = [5.50, 0, 5.31, 6.18, 5.31, 5.99]
expected[2, :] = [6.18, 5.31, 0, 6.57, 5.41, 5.52]
expected[3, :] = [6.89, 6.18, 6.57, 0, 5.50, 7.31]
expected[4, :] = [6.18, 5.31, 5.41, 5.5, 0, 5.77]
expected[5, :] = [7.52, 5.99, 5.52, 7.31, 5.77, 0 ]
assert_allclose(output, expected, rtol=1e-2)
def test_sum_distances():
'''
Tests whether the combinations are summed correctly.
'''
sample_inputs = setup()
dist_matr = compute_distance_matrix(sample_inputs, 6, 2, local_optimization=True)
indices = (1,3,2)
distance = sum_distances(indices, dist_matr)
expected = 10.47
assert_allclose(distance, expected, rtol=1e-2)
def test_get_max_sum_ind():
'''
Tests whether the right maximum indices are returned.
'''
indices = np.array([(1,2,4),(3,2,1),(4,2,1)])
distances = np.array([20, 40, 50])
output = get_max_sum_ind(indices, distances, 0, 0)
expected = (4,2,1)
assert_equal(output, expected)
def test_add_indices():
'''
Tests whether the right indices are added.
'''
indices = (1,3,4)
matr = np.zeros((6,6), dtype = np.int16)
ind_extra = add_indices(indices, matr)
expected = [(1,3,4,0),(1,3,4,2),(1,3,4,5)]
assert_equal(ind_extra, expected)
def test_combo_from_find_most_distant():
'''
Tests whether the correct combination is picked from the fixture drawn
from Saltelli et al. 2008, in the solution to exercise 3a,
Chapter 3, page 134.
'''
sample_inputs = setup()
N = 6
num_params = 2
k_choices = 4
scores = find_most_distant(sample_inputs, N, num_params, k_choices)
output = find_maximum(scores, N, k_choices)
expected = [0, 2, 3, 5] # trajectories 1, 3, 4, 6
assert_equal(output, expected)
def test_find_local_maximum_distance():
'''
Test whether finding the local maximum distance equals the global maximum distance
in a simple case. From Saltelli et al. 2008, in the solution to exercise 3a,
Chapter 3, page 134.
'''
sample_inputs = setup()
N=6
num_params = 2
k_choices = 4
scores_global = find_most_distant(sample_inputs, N, num_params, k_choices)
output_global = find_maximum(scores_global, N, k_choices)
output_local = find_local_maximum(sample_inputs, N, num_params, k_choices)
assert_equal(output_global, output_local)
def test_scores_from_find_most_distant():
'''
Checks whether array of scores from (6 4) is correct.
Data is derived from Saltelli et al. 2008,
in the solution to exercise 3a, Chapter 3, page 134.
'''
sample_inputs = setup()
N = 6
num_params = 2
k_choices = 4
output = find_most_distant(sample_inputs, N, num_params, k_choices)
expected = np.array([15.022, 13.871, 14.815, 14.582, 16.178, 14.912, 15.055, 16.410,
15.685, 16.098, 14.049, 15.146, 14.333, 14.807, 14.825],
dtype=np.float32)
assert_allclose(output, expected, rtol=1e-1, atol=1e-2)
def test_find_maximum():
scores = np.array(range(15))
k_choices = 4
N = 6
output = find_maximum(scores, N, k_choices)
expected = [2, 3, 4, 5]
assert_equal(output, expected)
def test_catch_combos_too_large():
N = 1e6
k_choices = 4
num_params = 2
input_sample = np.random.random_sample((N, num_params))
try:
find_most_distant(input_sample, N, num_params, k_choices)
except:
pass
else:
raise AssertionError("Test did not fail when number of \
combinations exceeded system size")
def test_make_index_list():
N = 4
num_params = 2
groups = None
actual = make_index_list(N, num_params, groups)
desired = [np.array([0, 1, 2]), np.array([3, 4, 5]), np.array([6, 7, 8]), np.array([9, 10, 11])]
assert_equal(desired, actual)
def test_make_index_list_with_groups():
N = 4
num_params = 3
groups = 2
actual = make_index_list(N, num_params, groups)
desired = [np.array([0, 1, 2]), np.array([3, 4, 5]), np.array([6, 7, 8]), np.array([9, 10, 11])]
assert_equal(desired, actual)
@raises(ValueError)
def test_get_max_sum_ind_Error():
indices = [(1,2,4),(3,2,1),(4,2,1)]
distances_wrong = [20,40]
get_max_sum_ind(indices, distances_wrong, 0, 0)
@raises(AssertionError)
def test_check_input_sample_N():
input_sample = setup()
num_params = 4
N = 5
check_input_sample(input_sample, num_params, N)
@raises(AssertionError)
def test_check_input_sample_num_vars():
input_sample = setup()
num_params = 3
N = 6
check_input_sample(input_sample, num_params, N)
@raises(AssertionError)
def test_check_input_sample_range():
input_sample = setup()
input_sample *= 100
num_params = 4
N = 6
check_input_sample(input_sample, num_params, N)