-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_functions.py
343 lines (272 loc) · 9.65 KB
/
test_functions.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright (C) 2014 ETH Zurich, Institute for Astronomy
"""
Test functions for `hope` module.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import numpy as np
import itertools
import pytest
import copy
from test.utilities import random, check, make_test, dtypes, shapes
import hope
from hope.exceptions import UnsupportedFeatureException
# TODO: fix for np.float32
@pytest.mark.parametrize("dtype", [np.float64, float])
def test_func_interp(dtype):
def fkt(x, y, x0, y0, s0, l0, r0):
y0[:] = np.interp(x0, x, y)
l0[:] = np.interp(x0 - 1, x, y, left=-1)
r0[:] = np.interp(x0 + 1, x, y, right=2)
s0[0] = np.interp(x0[0], x, y)
hfkt = hope.jit(fkt)
x0 = np.linspace(0, 1, 50)
x = np.linspace(0, 1, 10).astype(dtype)
y = np.linspace(0, 1, 10).astype(dtype)
xo, xh = np.linspace(0, 1, 50).astype(dtype), np.linspace(0, 1, 50).astype(dtype)
yo, yh = np.zeros_like(x0), np.zeros_like(x0)
so, sh = np.zeros_like(x0), np.zeros_like(x0)
lo, lh = np.zeros_like(x0), np.zeros_like(x0)
ro, rh = np.zeros_like(x0), np.zeros_like(x0)
fkt(x, y, xo, yo, so, lo, ro), hfkt(x, y, xh, yh, sh, lh, rh)
assert check(xo, xh)
assert check(yo, yh)
assert check(so, sh)
assert check(lo, lh)
assert check(ro, rh)
@pytest.mark.parametrize("dtype", [np.float64, float])
def test_func_interp_bounds(dtype):
def fkt(x,y,x0,y0):
y0[:] = np.interp(x0, x, y)
hfkt = hope.jit(fkt)
x = np.arange(5,15).astype(dtype)
y = -x
x0 = np.arange(0, 20).astype(dtype)
yo, yh = np.zeros_like(x0), np.zeros_like(x0)
fkt(x, y, x0, yo), hfkt(x, y, x0, yh)
assert check(yo, yh)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_sin(a, b, c): return np.sin(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_cos(a, b, c): return np.cos(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_tan(a, b, c): return np.tan(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_sign(a, b, c): return np.sign(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_arcsin(dtype, shape):
def fkt(a):
return np.arcsin(a)
hfkt = hope.jit(fkt)
a = 2 * np.random.random(shape) - 1
b = copy.deepcopy(a)
co = fkt(a)
ch = hfkt(b)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_arccos(dtype, shape):
def fkt(a):
return np.arccos(a)
hfkt = hope.jit(fkt)
a = 2 * np.random.random(shape) - 1
b = copy.deepcopy(a)
co = fkt(a)
ch = hfkt(b)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_arctan(a, b, c): return np.arctan(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_sinh(dtype, shape):
def fkt(a):
return np.sinh(a)
hfkt = hope.jit(fkt)
a = np.log(np.abs(random(dtype, shape)[0]) / 2.)
b = copy.deepcopy(a)
co = fkt(a)
ch = hfkt(b)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_cosh(dtype, shape):
def fkt(a):
return np.cosh(a)
hfkt = hope.jit(fkt)
a = np.log(np.abs(random(dtype, shape)[0]) / 2.)
b = copy.deepcopy(a)
co = fkt(a)
ch = hfkt(b)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_tanh(a, b, c): return np.tanh(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_floor(a, b, c): return np.floor(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_ceil(a, b, c): return np.ceil(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_trunc(a, b, c): return np.trunc(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_sqrt(dtype, shape):
def fkt(a):
return np.sqrt(a)
hfkt = hope.jit(fkt)
(ao, ah) = random(dtype, shape)
ao, ah = np.abs(ao), np.abs(ah)
co = fkt(ao)
ch = hfkt(ah)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
@make_test
def test_func_fabs(a, b, c): return np.fabs(a)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_exp(dtype, shape):
def fkt(a):
return np.exp(a)
hfkt = hope.jit(fkt)
ao, ah = np.log(np.abs(random(dtype, shape))).astype(dtype)
co = fkt(ao)
ch = hfkt(ah)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product([np.float32, np.float64, float], shapes))
def test_func_log(dtype, shape):
def fkt(a):
return np.log(a)
hfkt = hope.jit(fkt)
a, b = np.abs(random(dtype, shape))
co = fkt(a)
ch = hfkt(b)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes))
def test_func_sum_var(dtype, shape):
def fkt(a):
return np.sum(a)
hfkt = hope.jit(fkt)
ao, ah = random(dtype, shape)
ao, ah = ao / 1200, ah / 1200
co, ch = fkt(ao), hfkt(ah)
assert check(co, ch)
co, ch = fkt(ao), hfkt(ah)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes))
def test_func_sum_expr(dtype, shape):
def fkt(a, b):
return np.sum(a + b)
hfkt = hope.jit(fkt)
(ao, ah), (bo, bh) = random(dtype, shape), random(dtype, shape)
ao, ah = np.abs(ao) / 2400, np.abs(ah) / 2400
bo, bh = np.abs(bo) / 2400, np.abs(bh) / 2400
co, ch = fkt(ao, bo), hfkt(ah, bh)
assert check(co, ch)
co, ch = fkt(ao, bo), hfkt(ah, bh)
assert check(co, ch)
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes))
def test_func_sum_dtype(dtype, shape):
def fkt(a):
return np.sum(a, dtype=np.float64)
hfkt = hope.jit(fkt)
ao, ah = random(dtype, shape)
ao, ah = ao / 1200, ah / 1200
co, ch = fkt(ao), hfkt(ah)
assert check(co, ch)
assert co.dtype == np.float64
assert ch.dtype == np.float64
def test_func_sum_invalid_args():
a = np.random.random(1)
b = np.empty_like(a)
def fkt(a, b):
return np.sum(a, b)
hfkt = hope.jit(fkt)
with pytest.raises(UnsupportedFeatureException):
hfkt(a, b)
def fkt_axis(a):
return np.sum(a, axis=0)
hfkt = hope.jit(fkt_axis)
with pytest.raises(UnsupportedFeatureException):
hfkt(a)
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes))
def test_func_sum_in_if(dtype, shape):
def fkt(a):
if True:
val = np.sum(a)
else:
val = 1
return val
hfkt = hope.jit(fkt)
ao, ah = random(dtype, shape)
ao, ah = ao / 1200, ah / 1200
co, ch = fkt(ao), hfkt(ah)
assert check(co, ch)
def test_create_empty_array():
def fkt(shape):
return np.empty(shape)
hfkt = hope.jit(fkt)
assert fkt(5).size == hfkt(5).size
def fkt_dtype(shape):
return np.empty(shape, dtype=np.float64)
hfkt = hope.jit(fkt)
co, ch = fkt(5), hfkt(5)
assert co.size == ch.size
assert co.dtype == np.float64
assert ch.dtype == np.float64
def test_create_zeros_array():
def fkt(shape):
return np.zeros(shape)
hfkt = hope.jit(fkt)
assert fkt(5).size == hfkt(5).size
def fkt_dtype(shape):
return np.zeros(shape, dtype=np.float64)
hfkt = hope.jit(fkt)
co, ch = fkt(5), hfkt(5)
assert co.size == ch.size
assert co.dtype == np.float64
assert ch.dtype == np.float64
def test_create_ones_array():
def fkt(shape):
return np.ones(shape)
hfkt = hope.jit(fkt)
assert fkt(5).size == hfkt(5).size
def fkt_dtype(shape):
return np.ones(shape, dtype=np.float64)
hfkt = hope.jit(fkt_dtype)
co, ch = fkt(5), hfkt(5)
assert co.size == ch.size
assert co.dtype == np.float64
assert ch.dtype == np.float64
def test_create_array_invalid_args():
def fkt(shape):
return np.empty(shape, 0)
hfkt = hope.jit(fkt)
with pytest.raises(UnsupportedFeatureException):
hfkt(5)
def fkt_order(shape):
return np.empty(shape, order="c")
hfkt = hope.jit(fkt_order)
with pytest.raises(NotImplementedError):
hfkt(5)
def test_unsupported_np_func():
def fkt(a):
return np.alen(a)
hfkt = hope.jit(fkt)
a = np.random.random(1)
with pytest.raises(UnsupportedFeatureException):
hfkt(a)
def test_np_func_invalid_args():
def fkt(a, b):
return np.log(a, b)
hfkt = hope.jit(fkt)
a = np.random.random(1)
b = np.empty_like(a)
with pytest.raises(UnsupportedFeatureException):
hfkt(a, b)
def fkt_kwargs(a, b):
return np.log(a, out=b)
hfkt = hope.jit(fkt_kwargs)
with pytest.raises(UnsupportedFeatureException):
hfkt(a, b)
# TODO: add tests for remaining functions