-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnumba_accelerated_itd.py
212 lines (166 loc) · 6.24 KB
/
numba_accelerated_itd.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
import numpy
import numba
import scipy.interpolate as interpolate
@numba.njit(numba.boolean[:](numba.int64[:], numba.int64[:]), parallel=True)
def isin(a, b):
out = numpy.empty(a.shape[0], dtype=numba.boolean)
b = set(b)
for i in numba.prange(a.shape[0]):
if a[i] in b:
out[i] = True
else:
out[i] = False
return out
@numba.njit(numba.int64[:](numba.float64[:]), cache=True)
def matlab_detect_peaks(x: list):
"""Detect peaks in data based on their amplitude and other features.
warning: this code is an optimized copy of the "Marcos Duarte, https://github.com/demotu/BMC"
matlab compliant detect peaks function intended for use with data sets that only want
rising edge and is optimized for numba, ie, for pyitd. experiment with it at your own peril.
"""
# find indexes of all peaks
x = numpy.asarray(x)
if x.size < 3:
return numpy.empty(1, numpy.int64)
dx = x[1:] - x[:-1]
dx = -dx
# handle NaN's
indnan = numpy.where(numpy.isnan(x))[0]
indl = numpy.asarray(indnan)
if indl.size != 0:
x[indnan] = numpy.inf
dx[numpy.where(numpy.isnan(dx))[0]] = numpy.inf
vil = numpy.zeros(dx.size + 1)
vil[:-1] = dx[:] # hacky solution because numba does not like hstack tuple arrays
# numpy.asarray((dx[:], [0.]))# hacky solution because numba does not like hstack
vix = numpy.zeros(dx.size + 1)
vix[1:] = dx[:]
ind = numpy.unique(numpy.where((vil > 0) & (vix <= 0))[0])
# handle NaN's
# NaN's and values close to NaN's cannot be peaks
if ind.size and indl.size:
outliers = numpy.unique(numpy.concatenate((indnan, indnan - 1, indnan + 1)))
booloutliers = isin(ind, outliers)
booloutliers = numpy.invert(booloutliers)
ind = ind[booloutliers]
# first and last values of x cannot be peaks
if ind.size and ind[0] == 0:
ind = ind[1:]
if ind.size and ind[-1] == x.size - 1:
ind = ind[:-1]
# eliminate redundant values
return ind
#annotated types for splrep
out_ty = numba.types.float64[:]
out_ty = numba.types.float64[:]
k = numba.types.int32
@numba.jit(cache=True)
def custom_splrep(x, y):
"""
Custom wrap of scipy's splrep for calculating spline coefficients,
which also check if the data is equispaced.
modified 8/2022 for SIFTED - extremaindices are always integers, so rounding not meaningful
"""
# Check if x is equispaced
x_diff = numpy.diff(x)
equi_spaced = numpy.all(x_diff == x_diff[0])
dx = x_diff[0]
with numba.objmode(t=out_ty, c = out_ty, k='int32'):
(t, c, k) = interpolate.splrep(x, y, k=3)
return (t, c, k, equi_spaced, dx)
@numba.njit(cache=True)
def numba_splev(x, coeff):
"""
Custom implementation of scipy's splev for spline interpolation,
with additional section for faster search of knot interval, if knots are equispaced.
Spline is extrapolated from the end spans for points not in the support.
Author : sreenath1994s
"""
t, c, k, equi_spaced, dx = coeff
t0 = t[0]
n = t.size
m = x.size
k1 = k + 1
k2 = k1 + 1
nk1 = n - k1
l = k1
l1 = l + 1
y = numpy.zeros(m)
h = numpy.zeros(20)
hh = numpy.zeros(19)
for i in range(m):
# fetch a new x-value arg
arg = x[i]
# search for knot interval t[l] <= arg <= t[l+1]
if (equi_spaced):
l = int((arg - t0) / dx) + k
l = min(max(l, k1), nk1)
else:
while not ((arg >= t[l - 1]) or (l1 == k2)):
l1 = l
l = l - 1
while not ((arg < t[l1 - 1]) or (l == nk1)):
l = l1
l1 = l + 1
# evaluate the non-zero b-splines at arg.
h[:] = 0.0
hh[:] = 0.0
h[0] = 1.0
for j in range(k):
for ll in range(j + 1):
hh[ll] = h[ll]
h[0] = 0.0
for ll in range(j + 1):
li = l + ll
lj = li - j - 1
if (t[li] != t[lj]):
f = hh[ll] / (t[li] - t[lj])
h[ll] += f * (t[li] - arg)
h[ll + 1] = f * (arg - t[lj])
else:
h[ll + 1] = 0.0
break
sp = 0.0
ll = l - 1 - k1
for j in range(k1):
ll += 1
sp += c[ll] * h[j]
y[i] = sp
return y
@numba.jit(cache=True)
def baseline_knot_estimation(baseline_knots: list, x: list,
extrema_indices: list):
alpha = 0.5
for k in range(1, len(extrema_indices) - 1):
baseline_knots[k] = alpha * (x[extrema_indices[k - 1]] \
+ ((extrema_indices[k] - extrema_indices[k - 1]) \
/ (extrema_indices[k + 1] - extrema_indices[k - 1])) \
* (x[extrema_indices[k + 1]] - x[extrema_indices[k - 1]])) \
+ (alpha * x[extrema_indices[k]])
return baseline_knots
@numba.jit((numba.float64[:])(numba.float64[:]), cache=True)
def itd_baseline_extract_modified(x: numpy.ndarray):
idx_max = numpy.asarray(matlab_detect_peaks(x))
idx_min = numpy.asarray(matlab_detect_peaks(-x))
num_extrema = idx_min.size + idx_max.size
if num_extrema < 10:
#recommend- warn the user here
return x
extremabuffersize = num_extrema + 2
extrema_indices = numpy.zeros(extremabuffersize, dtype=numpy.int64)
extrema_indices[1:-1] = numpy.sort((numpy.hstack((idx_max, idx_min))))
extrema_indices[-1] = len(x) - 1
baseline_knots = numpy.zeros(len(extrema_indices))
padded = numpy.zeros((x.size + 2),dtype=x.dtype)
with numba.objmode(v="float64[:]"):
v = numpy.pad(x, 1, mode='reflect', reflect_type="odd")
padded[:] = v
baseline_knots[0] = numpy.mean(padded[:2])
baseline_knots[-1] = numpy.mean(padded[-2:])
baseline_knots[:] = baseline_knot_estimation(baseline_knots[:], x[:], extrema_indices[:])
S = baseline_knots
z = numpy.arange(x.size, dtype=numpy.float64)
coeff = custom_splrep(extrema_indices, S)
baseline = numba_splev(z, coeff)
return baseline
#to get the rotation instead, just subtract the baseline from x