/
post_process_rnn_error.py
208 lines (172 loc) · 5.32 KB
/
post_process_rnn_error.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
#
# Copyright 2016 ENS LSCP (Author: Paul Michel)
#
from __future__ import print_function, division
import numpy as np
import os
from scipy.signal import convolve, argrelmax
from scipy.fftpack import rfft, rfftfreq, irfft
from peakdet import detect_peaks
def check_valleys(x, i, threshold=1):
left = True
right = True
li = i-1
ri = i+1
while li >= 0:
if li-1 < 0 or x[li-1] > x[li]: # then this is a valley
left = abs(x[i]-x[li]) >= threshold
break
li = li-1
# while ri<len(x):
# if ri+1==len(x) or x[ri+1] > x[ri]: #then this is a valley
# right = abs(x[ri]-x[i])>=0.1*x[i]
# break
# ri=ri+1
return left # and right
def cliffs(x):
potential_boundaries = argrelmax(x)[0]
ret = []
for i, pb in enumerate(potential_boundaries):
li = i-1
left = abs(x[i]-x[0])
while li >= 0:
if li-1 < 0 or x[li-1] > x[li]: # then this is a valley
left = abs(x[i]-x[li])
break
li = li-1
ret.append([pb, left])
return ret
def greedy_detect(x, times, num=5):
diffs = np.array(cliffs(x))
diffs = diffs[diffs[:, 1].argsort()]
lim = int(len(x)/num)
diffs = np.sort(diffs[-lim:, 0]).astype(int)
return times[diffs]
def baseline_like_detect(x, times, threshold=1, min_threshold=1):
#x = 1-np.exp(-x)
potential_boundaries = argrelmax(x)[0]
boundaries = []
mean = np.mean(x[potential_boundaries])
for i, pb in enumerate(potential_boundaries):
if pb == 0 or pb == len(x):
boundaries.append(pb)
continue
if x[pb] < min_threshold*mean:
continue
if not check_valleys(x, pb, threshold):
continue
# j=upper_valley(pb,valleys)
# if j>0 and valleys[j]>pb and valleys[j-1]<pb:
# if pb-valleys[j] < valley_threshold or pb-valleys[j-1] < valley_threshold:
# continue
boundaries.append(pb)
return times[boundaries]
def manual_detect(x, times, ker_len, clip, rate):
kernel = np.ones((int(ker_len))) / ker_len
x_smoothed = convolve(x, kernel)
boundaries = argrelmax(x_smoothed)[0]
boundaries = np.append(boundaries, len(x)-1)
boundaries = np.insert(boundaries, 0, 0)
boundaries = times[boundaries]
# Optionaly clip all boundaries that are too close apart
if clip > 0:
y = [boundaries[0]]
i = 0
for j in range(1, len(boundaries)):
if boundaries[j]-boundaries[i] >= clip:
boundaries[i:j] = np.mean(boundaries[i:j])
i = j
j += 1
for bound in boundaries:
if bound != y[-1]:
y.append(bound)
boundaries = np.array(y)
return boundaries
def fourier_detect(x, times, rate):
fr = rfftfreq(len(times), 1/rate)
y = rfft(x)
y[fr > int(1/0.05)] = 0
x_smoothed = irfft(y)
return times[argrelmax(x_smoothed)[0]]
def auto_detect(x, times, ker_len):
kernel = np.ones((int(ker_len))) / ker_len
x_smoothed = convolve(x, kernel)
boundaries = detect_peaks(x_smoothed, mph=np.max(x_smoothed)*0.4, mpd=2,)
boundaries = times[boundaries]
return boundaries
def post_process_file(
input_file,
output_file,
method='baseline',
time_file=None,
rate=100.0,
ker_len=3,
clip=0.03,
threshold=0.5,
min_threshold=1
):
# Load error signal
x = np.load(input_file)
x = x.reshape(x.size)
# Flatten beginning
x[:7]=0
times = np.arange(len(x))/rate
if time_file is not None:
times = np.loadtxt(time_file)
if method == 'fourier':
boundaries = fourier_detect(x, times, rate)
elif method == 'auto':
boundaries = auto_detect(x, times, ker_len)
elif method == 'manual':
boundaries = manual_detect(x, times, ker_len, clip, rate)
elif method == 'baseline':
boundaries = baseline_like_detect(
x,
times,
threshold=threshold,
min_threshold=min_threshold
)
elif method == 'greedy':
boundaries = greedy_detect(x, times, threshold)
elif method == 'none':
boundaries = times[argrelmax(x)[0]]
else:
boundaries = fourier_detect(x, times, rate)
boundaries=list(boundaries)
if not (len(x)-1)/rate in boundaries:
boundaries.append((len(x)-1)/rate)
if not 0 in boundaries:
boundaries=[0]+boundaries
np.savetxt(output_file, boundaries, fmt="%.2f")
def run(
input_dir,
output_dir,
method='baseline',
time_dir=None,
rate=100.0,
ker_len=3,
clip=0.03,
threshold=0.5,
min_threshold=1
):
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for f in os.listdir(input_dir):
if f.endswith('_loss.npy'):
ifile = input_dir+'/'+f
ofile = output_dir+'/'+f[:-9]+'.syldet'
if time_dir is not None:
tfile = time_dir + f[:-9]+'times'
else:
tfile = None
post_process_file(
ifile,
ofile,
method=method,
time_file=tfile,
rate=rate,
ker_len=ker_len,
clip=clip,
threshold=threshold,
min_threshold=min_threshold
)