forked from tknapen/python_workshop-Basel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
202 lines (143 loc) · 5.51 KB
/
tests.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
import numpy as np
def test_list_indexing(todo_list):
try:
assert(todo_list[2] == 'REPLACED')
except AssertionError as e:
print("The element 'TO_REPLACE_1' is not correctly replaced!")
raise(e)
try:
assert(todo_list[-1][-1][-1] == 'REPLACED')
except AssertionError as e:
print("The element 'TO_REPLACE_2' is not correctly replaced!")
raise(e)
print('Well done!')
def test_slicing_1(lst):
c_answer = [2, 3, 4, 5, 6]
try:
assert(lst == c_answer)
except AssertionError:
print('The slice is incorrect!')
raise IncorrectAnswer(lst, c_answer)
else:
print("Well done!")
def test_slicing_2(lst):
c_answer = [5, 7, 9, 11]
try:
assert(lst == c_answer)
except AssertionError:
print('The slice is incorrect!')
raise IncorrectAnswer(lst, c_answer)
else:
print("Well done!")
def test_create_array_with_zeros(arr):
c_answer = np.zeros((2, 3, 5, 3, 7))
try:
assert(np.all(arr.shape == c_answer.shape))
except AssertionError as e:
print("Your array has the wrong shape, namely %r, but I expected %r" % (arr.shape, c_answer.shape,))
raise(e)
try:
assert(np.all(arr == 0.0))
except AssertionError as e:
print("Your array does not contain zeros ... Did you use np.zeros()?")
raise(e)
print("Well done!")
def test_fill_array_with_complement(arr):
c_answer = 1.0 / np.arange(1, 9)
try:
np.testing.assert_array_almost_equal(arr, c_answer, 4)
except AssertionError as e:
print("Your array (%r) does not match the correct answer (%r)!" % (arr, c_answer))
raise(e)
else:
print("Well done!")
def test_set_odd_indices_to_zero(arr):
c_answer = np.arange(3, 25)
c_answer[1::2] = 0.0
try:
np.testing.assert_array_almost_equal(arr, c_answer, 4)
except AssertionError as e:
print("Your array (%r) does not match the correct answer (%r)!" % (arr, c_answer))
else:
print("Well done!")
def test_set_lower_right_value_to_one(arr):
c_answer = np.zeros((3, 3))
c_answer[-1, -1] = 1.0
try:
np.testing.assert_array_almost_equal(arr, c_answer, 4)
except AssertionError as e:
print("Your array: \n\n%r\n\ndoes not match the correct answer:\n\n%r!" % (arr, c_answer))
else:
print("Well done!")
def test_bloodpressure_index(arr):
np.random.seed(42)
bp_data = np.random.normal(loc=100, scale=5, size=(20, 24, 30, 2))
c_answer = bp_data[:, :, 17, 1]
try:
assert(arr.shape == (20, 24))
except AssertionError as e:
print("The result of your indexing operation is of shape %r, "
"while it should be %r, namely 20 subjects by 24 hours" % (arr.shape, (20, 24)))
raise(e)
try:
np.testing.assert_array_almost_equal(arr, c_answer, 4)
except AssertionError as e:
print("Your answer is not correct! Did you perhaps forget that Python has zero-based indexing? (First index is 0!)")
raise(e)
print("Well done!")
def test_boolean_indexing(arr):
my_array = np.array([[0, 1, -1, -2],
[2, -5, 1, 4],
[10, -2, -4, 20]])
c_answer = my_array[my_array ** 2 > 4]
try:
np.testing.assert_array_equal(arr, c_answer)
except AssertionError as e:
print("Incorrect answer! I expected %r, but I got %r" % (c_answer, arr))
print("Well done!")
def test_tvalue_computation(arr, h0, tval_ans):
c_tval = (arr.mean() - h0) / (arr.std() / np.sqrt(arr.size))
try:
np.testing.assert_almost_equal(tval_ans, c_tval)
except AssertionError as e:
print("T-value is incorrect! Your t-value is %.3f, while it should be %.3f" % (tval_ans, c_tval))
raise(e)
print("Well done!")
def test_array_product_and_sum(arr):
arr_A = np.arange(10).reshape((5, 2))
arr_B = np.arange(10, 20).reshape((5, 2))
c_answer = (arr_A * arr_B) + 5
try:
np.testing.assert_array_equal(arr, c_answer)
except AssertionError as e:
print("Your answer is incorrect! I got:\n\n%r\n\nbut I expected:\n\n%r" % (arr, c_answer))
raise(e)
else:
print("Well done!")
def test_compute_range_vectorized(arr, ans):
c_answer = arr.max(axis=0) - arr.min(axis=0)
try:
assert(ans.shape == c_answer.shape)
except AssertionError as e:
print("The shape of your answer is incorrect! I got %r, "
"but I expected %r for input-array of shape %r" % (ans.shape, c_answer.shape, arr.shape))
raise(e)
try:
np.testing.assert_array_almost_equal(ans, c_answer, 4)
except AssertionError as e:
print("Your answer is incorrect! Your answer is:\n\n%r/n/n But I expected:\n\n%r" % (ans, c_answer))
raise(e)
print("Well done!")
def test_extract_voxel_timeseries(funcdata, ans):
c_answer = funcdata[29, 49, 22, :]
try:
np.testing.assert_array_almost_equal(ans, c_answer, 4)
except AssertionError as e:
print("Your answer is incorrect! Your answer was:\n\n%r\n\nBut I expected:\n\n%r" % (ans, c_answer))
raise(e)
else:
print("Well done!")
class IncorrectAnswer(Exception):
def __init__(self, answer, expected):
message = "Your answer '%r' is incorrect; I expected '%r'" % (answer, expected)
super().__init__(message)