Skip to content

Commit 00aea08

Browse files
style: pre-commit fixes
1 parent ff1ed4c commit 00aea08

6 files changed

Lines changed: 85 additions & 52 deletions

File tree

src/dspeed/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,24 @@ def __init__(
9494
self.ufunc = fun
9595
else:
9696
otypes = types[-self.nout :] if self.nout > 0 else None
97-
if self.nout>0:
97+
if self.nout > 0:
9898
self.ufunc = np.vectorize(fun, otypes=otypes, signature=self.signature)
9999
else:
100100
# vectorize requires an output, so we'll give it an output
101101
def fun_return(*args):
102102
fun(*args)
103103
return True
104-
self.ufunc = np.vectorize(lambda *args: fun, otypes="?", signature=self.signature+"->()")
104+
105+
self.ufunc = np.vectorize(
106+
lambda *args: fun, otypes="?", signature=self.signature + "->()"
107+
)
105108

106109
def __call__(self, *args):
107110
"""Call wrapped function with "in place" outputs"""
108111

109112
assert len(args) == self.nargs
110113

111-
if self.copy_out and self.nout>0:
114+
if self.copy_out and self.nout > 0:
112115
ins = args[: self.nin]
113116
outs = args[-self.nout :]
114117
# print([i.shape for i in ins], [o.shape for o in outs])

tests/conftest.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import copy
2-
import inspect
32
import os
4-
import re
53
import shutil
64
import uuid
75
from getpass import getuser
@@ -84,7 +82,7 @@ def numba_vs_python(func, *inputs):
8482
if func.signature:
8583
sig = func.signature
8684
else:
87-
sig = ",".join(["()"]*func.nin) + "->" + ",".join(["()"]*func.nout)
85+
sig = ",".join(["()"] * func.nin) + "->" + ",".join(["()"] * func.nout)
8886

8987
if len(inputs) == func.nargs:
9088
# outputs passed as inputs; required when size of output
@@ -96,8 +94,10 @@ def numba_vs_python(func, *inputs):
9694
outputs_numba = [copy.deepcopy(arg) for arg in inputs]
9795

9896
# unwrapped python outputs
99-
#func_unwrapped = np.vectorize(inspect.unwrap(func), signature=func.signature)
100-
func_unwrapped = GUFuncWrapper(func, sig, ''.join([np.array(ar).dtype.char for ar in outputs_numba]))
97+
# func_unwrapped = np.vectorize(inspect.unwrap(func), signature=func.signature)
98+
func_unwrapped = GUFuncWrapper(
99+
func, sig, "".join([np.array(ar).dtype.char for ar in outputs_numba])
100+
)
101101
func_unwrapped(*inputs)
102102
outputs_python = [copy.deepcopy(arg) for arg in inputs]
103103

@@ -110,16 +110,20 @@ def numba_vs_python(func, *inputs):
110110
outputs_numba = [outputs_numba]
111111

112112
# unwrapped python outputs
113-
types = ''.join([np.array(ar).dtype.char for ar in outputs_numba]) + "->" + ''.join([np.array(ar).dtype.char for ar in outputs_numba])
113+
types = (
114+
"".join([np.array(ar).dtype.char for ar in outputs_numba])
115+
+ "->"
116+
+ "".join([np.array(ar).dtype.char for ar in outputs_numba])
117+
)
114118
func_unwrapped = GUFuncWrapper(func, sig, types)
115119

116120
# now outputs must be passed as args. Scalars must be
117121
# converted to rank 1 arrays
118122
outputs_python = []
119123
scalars = []
120124
for i, ar in enumerate(outputs_numba):
121-
if len(ar.shape)==0:
122-
outputs_python.append(np.zeros_like(ar).reshape((1)))
125+
if len(ar.shape) == 0:
126+
outputs_python.append(np.zeros_like(ar).reshape(1))
123127
scalars.append(i)
124128
else:
125129
outputs_python.append(np.zeros_like(ar))
@@ -134,9 +138,12 @@ def numba_vs_python(func, *inputs):
134138

135139
# assert that numba and python are the same up to floating point
136140
# precision, setting nans to be equal
137-
assert all(np.allclose(o_nb, o_py, equal_nan=True) for o_nb, o_py in zip(outputs_numba, outputs_python))
141+
assert all(
142+
np.allclose(o_nb, o_py, equal_nan=True)
143+
for o_nb, o_py in zip(outputs_numba, outputs_python)
144+
)
138145

139146
# return value for comparison with expected solution
140-
return outputs_numba if len(outputs_numba)>1 else outputs_numba[0]
147+
return outputs_numba if len(outputs_numba) > 1 else outputs_numba[0]
141148

142149
return numba_vs_python

tests/processors/test_fixed_time_pickoff.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@ def test_fixed_time_pickoff(compare_numba_vs_python):
1515
# test for nan if w_in has a nan
1616
w_in = np.ones(len_wf)
1717
w_in[4] = np.nan
18-
assert np.isnan(compare_numba_vs_python(fixed_time_pickoff, w_in, 1, np.int8(ord("i"))))
18+
assert np.isnan(
19+
compare_numba_vs_python(fixed_time_pickoff, w_in, 1, np.int8(ord("i")))
20+
)
1921

2022
# test for nan if nan is passed to t_in
2123
w_in = np.ones(len_wf)
22-
assert np.isnan(compare_numba_vs_python(fixed_time_pickoff, w_in, np.nan, np.int8(ord("i"))))
24+
assert np.isnan(
25+
compare_numba_vs_python(fixed_time_pickoff, w_in, np.nan, np.int8(ord("i")))
26+
)
2327

2428
# test for nan if t_in is negative
2529
w_in = np.ones(len_wf)
26-
assert np.isnan(compare_numba_vs_python(fixed_time_pickoff, w_in, -1, np.int8(ord("i"))))
30+
assert np.isnan(
31+
compare_numba_vs_python(fixed_time_pickoff, w_in, -1, np.int8(ord("i")))
32+
)
2733

2834
# test for nan if t_in is too large
2935
w_in = np.ones(len_wf)
30-
assert np.isnan(compare_numba_vs_python(fixed_time_pickoff, w_in, len_wf, np.int8(ord("i"))))
36+
assert np.isnan(
37+
compare_numba_vs_python(fixed_time_pickoff, w_in, len_wf, np.int8(ord("i")))
38+
)
3139

3240
# test for DSPFatal errors being raised
3341
# noninteger t_in with integer interpolation
@@ -54,7 +62,10 @@ def test_fixed_time_pickoff(compare_numba_vs_python):
5462
sols = [4, 3, 4, 3.5, 3.5, 3.5]
5563

5664
for char, sol in zip(chars, sols):
57-
assert compare_numba_vs_python(fixed_time_pickoff, w_in, 3.5, np.int8(ord(char))) == sol
65+
assert (
66+
compare_numba_vs_python(fixed_time_pickoff, w_in, 3.5, np.int8(ord(char)))
67+
== sol
68+
)
5869

5970
# sine wave tests
6071
w_in = np.sin(np.arange(len_wf))
@@ -71,7 +82,8 @@ def test_fixed_time_pickoff(compare_numba_vs_python):
7182

7283
for char, sol in zip(chars, sols):
7384
assert np.isclose(
74-
compare_numba_vs_python(fixed_time_pickoff, w_in, 3.25, np.int8(ord(char))), sol
85+
compare_numba_vs_python(fixed_time_pickoff, w_in, 3.25, np.int8(ord(char))),
86+
sol,
7587
)
7688

7789
# last few corner cases of 'h'
@@ -84,5 +96,6 @@ def test_fixed_time_pickoff(compare_numba_vs_python):
8496

8597
for ftp, sol in zip(ftps, sols):
8698
assert np.isclose(
87-
compare_numba_vs_python(fixed_time_pickoff, w_in, ftp, np.int8(ord("h"))), sol
99+
compare_numba_vs_python(fixed_time_pickoff, w_in, ftp, np.int8(ord("h"))),
100+
sol,
88101
)

tests/processors/test_inl_correction.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def test_inl_correction(compare_numba_vs_python):
2121
w_in = np.arange(len_wf, dtype=np.int32)
2222
inl = np.full(len_inl, 0.5, dtype=np.float32)
2323
expected = w_in.astype(np.float32) + 0.5
24-
assert np.allclose(
25-
compare_numba_vs_python(inl_correction, w_in, inl), expected
26-
)
24+
assert np.allclose(compare_numba_vs_python(inl_correction, w_in, inl), expected)
2725

2826
# Case 3: ADC code out of range
2927
w_in = np.array([300], dtype=np.int32)
@@ -35,6 +33,4 @@ def test_inl_correction(compare_numba_vs_python):
3533
w_in = np.array([0, 1, 2, 3], dtype=np.int32)
3634
inl = np.array([0.1, -0.1, 0.2, -0.2] + [0.0] * (len_inl - 4), dtype=np.float32)
3735
expected = w_in.astype(np.float32) + inl[:4]
38-
assert np.allclose(
39-
compare_numba_vs_python(inl_correction, w_in, inl), expected
40-
)
36+
assert np.allclose(compare_numba_vs_python(inl_correction, w_in, inl), expected)

tests/processors/test_multi_a.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,22 @@ def test_multi_a_filter(compare_numba_vs_python):
2525
multi_a_filter(w_in, maxs_in, np.zeros(21))
2626

2727
# test vt_max_len is 0
28-
assert (
29-
len(compare_numba_vs_python(multi_a_filter, w_in, np.array([])))
30-
== 0
31-
)
28+
assert len(compare_numba_vs_python(multi_a_filter, w_in, np.array([]))) == 0
3229

3330
# test all nan
3431
assert np.isnan(
3532
compare_numba_vs_python(multi_a_filter, w_in, np.array([np.nan]))
3633
).all()
3734

3835
# test output
39-
assert (
40-
compare_numba_vs_python(multi_a_filter, w_in, np.array([10]))[0]
41-
== 1
42-
)
36+
assert compare_numba_vs_python(multi_a_filter, w_in, np.array([10]))[0] == 1
4337

4438
# test output with remaining nans
45-
out = compare_numba_vs_python(
46-
multi_a_filter, w_in, np.array([10, np.nan, np.nan])
47-
)
39+
out = compare_numba_vs_python(multi_a_filter, w_in, np.array([10, np.nan, np.nan]))
4840
assert out[0] == 1
4941
assert np.isnan(out[1:]).all()
5042

5143
# test output with 1 nan
52-
out = compare_numba_vs_python(
53-
multi_a_filter, w_in, np.array([10, np.nan, 12])
54-
)
44+
out = compare_numba_vs_python(multi_a_filter, w_in, np.array([10, np.nan, 12]))
5545
assert out[0] == 1 and out[2] == 1
5646
assert np.isnan(out[1])

tests/processors/test_time_point_thresh.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def test_interpolated_time_point_thresh(compare_numba_vs_python):
9393
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
9494
w_in[4] = np.nan
9595
assert np.isnan(
96-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1.0, 11.0, 0, np.int8(105))
96+
compare_numba_vs_python(
97+
interpolated_time_point_thresh, w_in, 1.0, 11.0, 0, np.int8(105)
98+
)
9799
)
98100

99101
# test for nan if nan is passed to a_threshold
@@ -115,76 +117,98 @@ def test_interpolated_time_point_thresh(compare_numba_vs_python):
115117
# test for nan if t_start out of range
116118
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
117119
assert np.isnan(
118-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1.0, 12, 0, np.int8(105))
120+
compare_numba_vs_python(
121+
interpolated_time_point_thresh, w_in, 1.0, 12, 0, np.int8(105)
122+
)
119123
)
120124

121125
# test walk backward mode 'i'
122126
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
123127
assert (
124-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(105))
128+
compare_numba_vs_python(
129+
interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(105)
130+
)
125131
== 7.0
126132
)
127133

128134
# test walk forward mode 'i'
129135
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
130136
assert (
131-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(105))
137+
compare_numba_vs_python(
138+
interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(105)
139+
)
132140
== 4.0
133141
)
134142

135143
# test walk backward mode 'f'
136144
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
137145
assert (
138-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(102))
146+
compare_numba_vs_python(
147+
interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(102)
148+
)
139149
== 8.0
140150
)
141151

142152
# test walk forward mode 'f'
143153
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
144154
assert (
145-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(102))
155+
compare_numba_vs_python(
156+
interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(102)
157+
)
146158
== 5.0
147159
)
148160

149161
# test walk backward mode 'f'
150162
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
151163
assert (
152-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(99))
164+
compare_numba_vs_python(
165+
interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(99)
166+
)
153167
== 7.0
154168
)
155169

156170
# test walk forward mode 'f'
157171
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
158172
assert (
159-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(99))
173+
compare_numba_vs_python(
174+
interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(99)
175+
)
160176
== 4.0
161177
)
162178

163179
# test walk backward mode 'n'
164180
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
165181
assert (
166-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(110))
182+
compare_numba_vs_python(
183+
interpolated_time_point_thresh, w_in, 1, 11, 0, np.int8(110)
184+
)
167185
== 7.5
168186
)
169187

170188
# test walk forward mode 'n'
171189
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
172190
assert (
173-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(110))
191+
compare_numba_vs_python(
192+
interpolated_time_point_thresh, w_in, 3, 0, 1, np.int8(110)
193+
)
174194
== 4.5
175195
)
176196

177197
# test walk backward mode 'l'
178198
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
179199
assert (
180-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 1.5, 11, 0, np.int8(108))
200+
compare_numba_vs_python(
201+
interpolated_time_point_thresh, w_in, 1.5, 11, 0, np.int8(108)
202+
)
181203
== 8.5
182204
)
183205

184206
# test walk forward mode 'l'
185207
w_in = np.concatenate([np.arange(-1, 5, 1), np.arange(-1, 5, 1)], dtype="float")
186208
assert (
187-
compare_numba_vs_python(interpolated_time_point_thresh, w_in, 3.5, 0, 1, np.int8(108))
209+
compare_numba_vs_python(
210+
interpolated_time_point_thresh, w_in, 3.5, 0, 1, np.int8(108)
211+
)
188212
== 4.5
189213
)
190214

0 commit comments

Comments
 (0)