forked from OpenPIV/openpiv-python-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_vectorized_functions.py
More file actions
489 lines (374 loc) · 11.1 KB
/
test_vectorized_functions.py
File metadata and controls
489 lines (374 loc) · 11.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# /// script
# requires-python = ">="3.11"
# dependencies = [
# "marimo",
# "openpiv",
# "numpy",
# "matplotlib",
# "imageio",
# ]
# ///
import marimo
__generated_with = "0.23.0"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Comparison with vectorized and original functions
#### Edited by Erich Zimmer
#### Created at 20210817, 2109 CTZ
""")
return
@app.cell
def _():
from glob import glob
import matplotlib.pyplot as plt
import numpy as np
from openpiv.pyprocess import (
find_first_peak,
vectorized_correlation_to_displacements,
)
from openpiv.tools import imread
return (
find_first_peak,
glob,
imread,
np,
plt,
vectorized_correlation_to_displacements,
)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Vectorized solution for subpixel estimation
""")
return
@app.cell
def _(np):
N = 64
corr = np.zeros((N,N))
corr[2:5,2:5] = 1
corr[3,3] = 2
corr[3,4] = 3
corr[3,5] = 1
corr
return N, corr
@app.cell
def _(corr, find_first_peak):
pos,height = find_first_peak(corr)
return height, pos
@app.cell
def _(height, pos):
pos,height
return
@app.cell
def _():
from openpiv.pyprocess import find_subpixel_peak_position
return (find_subpixel_peak_position,)
@app.cell
def _(corr, find_subpixel_peak_position):
find_subpixel_peak_position(corr)
return
@app.cell
def _(corr, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr[np.newaxis, :, :]) + np.floor(corr.shape[0] / 2))
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## let's find some corner cases
""")
return
@app.cell
def _(N, np):
# peak on the border
corr_1 = np.zeros((N, N))
corr_1[:3, :3] = 1
corr_1[0, 0] = 2
corr_1[0, 2] = 3
corr_1[0, 3] = 1
corr_1
return (corr_1,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Corner case 1: peak on the border
it is disregarded in our function because we cannot define well the subpixel
position. Or do we?
""")
return
@app.cell
def _(corr_1, find_subpixel_peak_position):
find_subpixel_peak_position(corr_1)
return
@app.cell
def _(corr_1, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr_1[np.newaxis, :, :]) + np.floor(corr_1.shape[0] / 2))
return
@app.cell
def _(corr_1, np):
# peak on the border
corr_2 = np.flipud(corr_1)
corr_2
return (corr_2,)
@app.cell
def _(corr_2, find_subpixel_peak_position):
find_subpixel_peak_position(corr_2)
return
@app.cell
def _(corr_2, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr_2[np.newaxis, :, :]) + np.floor(corr_2.shape[0] / 2))
return
@app.cell
def _(corr_2, np):
corr_3 = np.fliplr(corr_2)
corr_3[-2, -1] = 5
corr_3
return (corr_3,)
@app.cell
def _(corr_3, find_subpixel_peak_position):
find_subpixel_peak_position(corr_3)
return
@app.cell
def _(corr_3, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr_3[np.newaxis, :, :]) + np.floor(corr_3.shape[0] / 2))
return
@app.cell
def _():
## Corner case 2: negative value next to peak - the log(n<0) fails
return
@app.cell
def _(N, np):
corr_4 = np.zeros((N, N))
corr_4[2:5, 2:5] = 1
corr_4[3, 3] = 2
corr_4[3, 4] = 3
corr_4 = corr_4 - 0.5
# corr[3,5] = 1
corr_4
return (corr_4,)
@app.cell
def _(corr_4, find_subpixel_peak_position):
find_subpixel_peak_position(corr_4) # automatically uses parabolic method
return
@app.cell
def _(corr_4, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr_4[np.newaxis, :, :]) + np.floor(corr_4.shape[0] / 2))
return
@app.cell
def _():
## Corner case 3: zero next to the peak - the log(0) fails
return
@app.cell
def _(N, np):
corr_5 = np.zeros((N, N))
corr_5[2:5, 2:5] = 1
corr_5[3, 3] = 2
corr_5[3, 4] = 3
# corr[3,5] = 1
corr_5
return (corr_5,)
@app.cell
def _(corr_5, find_subpixel_peak_position):
find_subpixel_peak_position(corr_5)
return
@app.cell
def _(corr_5, np, vectorized_correlation_to_displacements):
np.flip(vectorized_correlation_to_displacements(corr_5[np.newaxis, :, :]) + np.floor(corr_5.shape[0] / 2))
return
@app.cell
def _(corr_5, find_subpixel_peak_position):
eps = 1e-07
for _method in ['gaussian', 'parabolic', 'centroid']:
_i, _j = find_subpixel_peak_position(corr_5, _method)
print(_i, _j)
_i, _j = find_subpixel_peak_position(corr_5 + eps, _method)
print(_i, _j)
return
@app.cell
def _(corr_5, np, vectorized_correlation_to_displacements):
for _method in ['gaussian', 'parabolic', 'centroid']:
_j, _i = vectorized_correlation_to_displacements(corr_5[np.newaxis, :, :], subpixel_method=_method) + np.floor(corr_5.shape[0] / 2)
print(_i, _j)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Speed increase demonstration
""")
return
@app.cell
def _():
import pylab
return (pylab,)
@app.cell
def _(imread, np, pylab):
frame_a = imread("data/test11/A001_1.tif")
frame_b = imread("data/test11/A001_2.tif")
pylab.imshow(np.c_[frame_a,np.ones((frame_a.shape[0],20)),frame_b],
cmap=pylab.cm.gray)
return frame_a, frame_b
@app.cell
def _(frame_a, frame_b):
_window_size = 32
_overlap = 16
from openpiv.pyprocess import (
correlation_to_displacement,
fft_correlate_images,
get_coordinates,
get_field_shape,
moving_window_array,
)
n_rows, n_cols = get_field_shape(frame_a.shape, _window_size, _overlap)
x, y = get_coordinates(frame_a.shape, _window_size, _overlap)
_aa = moving_window_array(frame_a, _window_size, _overlap)
_bb = moving_window_array(frame_b, _window_size, _overlap)
corr_6 = fft_correlate_images(_aa, _bb, correlation_method='circular', normalized_correlation=True)
return (
corr_6,
correlation_to_displacement,
fft_correlate_images,
get_field_shape,
moving_window_array,
n_cols,
n_rows,
)
@app.cell
def _(corr_6):
from openpiv.pyprocess import find_all_second_peaks, find_second_peak
peaks_v = find_all_second_peaks(corr_6)[0]
peaks_o = []
for _i in range(len(corr_6)):
(k, m), _ = find_second_peak(corr_6[_i, :, :])
peaks_o.append([_i, k, m])
print(['original', 'vectorized'])
for _i in range(len(peaks_v)):
if peaks_v[_i][1] != peaks_o[_i][1] or peaks_v[_i][2] != peaks_o[_i][2]:
print(False)
return
@app.cell
def _(corr_6, correlation_to_displacement, n_cols, n_rows):
u_o, _v_o = correlation_to_displacement(corr_6, n_rows, n_cols, subpixel_method="gaussian")
return (u_o,)
@app.cell
def _(corr_6, n_cols, n_rows, vectorized_correlation_to_displacements):
u_v, _v_v = vectorized_correlation_to_displacements(corr_6, n_rows, n_cols, subpixel_method="gaussian")
return (u_v,)
@app.cell
def _(np, u_o, u_v):
# slight descrepancies possibly caused by setting eps to 1e-10
print("[u original, u vectorized]")
print(np.stack((u_o[0, 0:12], u_v[0, 0:12])).T)
print((np.nanmean(u_o), np.nanmean(u_v)))
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Vectorized solution for signal-to-noise calculation
""")
return
@app.cell
def _():
from openpiv.pyprocess import sig2noise_ratio, vectorized_sig2noise_ratio
return sig2noise_ratio, vectorized_sig2noise_ratio
@app.cell
def _(corr_6, sig2noise_ratio):
# magic command not supported in marimo; please file an issue to add support
# %%time
peak2peak_o = sig2noise_ratio(corr_6, "peak2peak")
return (peak2peak_o,)
@app.cell
def _(corr_6, vectorized_sig2noise_ratio):
# magic command not supported in marimo; please file an issue to add support
# %%time
peak2peak_v = vectorized_sig2noise_ratio(corr_6, "peak2peak")
return (peak2peak_v,)
@app.cell
def _(corr_6, sig2noise_ratio):
# magic command not supported in marimo; please file an issue to add support
# %%time
peak2mean_o = sig2noise_ratio(corr_6, "peak2mean")
return (peak2mean_o,)
@app.cell
def _(corr_6, vectorized_sig2noise_ratio):
# magic command not supported in marimo; please file an issue to add support
# %%time
peak2mean_v = vectorized_sig2noise_ratio(corr_6, "peak2mean")
return (peak2mean_v,)
@app.cell
def _(np, peak2peak_o, peak2peak_v):
print("[original, vectorized]")
print(np.stack((peak2peak_o[0:10], peak2peak_v[0:10])).T)
print((peak2peak_o.mean(), peak2peak_v.mean()))
return
@app.cell
def _(np, peak2mean_o, peak2mean_v):
print("[original, vectorized]")
print(np.stack((peak2mean_o[0:10], peak2mean_v[0:10])).T)
print((peak2mean_o.mean(), peak2mean_v.mean()))
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Test for bias errors
""")
return
@app.cell
def _(glob):
files = glob("test14/*.bmp")
files_a = files[::2]
files_b = files[1::2]
return files_a, files_b
@app.cell
def _(
correlation_to_displacement,
fft_correlate_images,
files_a,
files_b,
get_field_shape,
imread,
moving_window_array,
np,
vectorized_correlation_to_displacements,
):
bias_error_original = []
bias_error_vectorized = []
_window_size = 32
_overlap = 16
real_disp = 3
n = 1 / 32
for _i in range(len(files_a)):
frame_a_1 = imread(files_a[_i])
frame_b_1 = imread(files_b[_i])
n_rows_1, n_cols_1 = get_field_shape(frame_a_1.shape, _window_size, _overlap)
_aa = moving_window_array(frame_a_1, _window_size, _overlap)
_bb = moving_window_array(frame_b_1, _window_size, _overlap)
corr_7 = fft_correlate_images(_aa, _bb, 'circular', False)
u_o_1, _v_o = correlation_to_displacement(corr_7, n_rows_1, n_cols_1, "gaussian")
u_v_1, _v_v = vectorized_correlation_to_displacements(corr_7, n_rows_1, n_cols_1, "gaussian")
u_o_1 = u_o_1[2:-2, 2:-2]
u_v_1 = u_v_1[2:-2, 2:-2]
_v_o = _v_o[2:-2, 2:-2]
_v_v = _v_v[2:-2, 2:-2]
bias_error_original.append(np.hypot(real_disp, real_disp) - np.nanmean(np.hypot(u_o_1, _v_o)))
bias_error_vectorized.append(np.hypot(real_disp, real_disp) - np.nanmean(np.hypot(u_v_1, _v_v)))
real_disp = real_disp + n
return bias_error_original, bias_error_vectorized, n
@app.cell
def _(bias_error_original, bias_error_vectorized, n, np, plt):
fig, ax = plt.subplots()
ax.set_ylabel("Bias error [px]")
ax.set_xlabel("Real u and v displacements [px]")
ax.plot(np.mgrid[3:4+n:n], bias_error_original)
ax.plot(np.mgrid[3:4+n:n], bias_error_vectorized)
ax.legend(
['orginal', 'vectorized'],
loc = 'upper right'
)
return
if __name__ == "__main__":
app.run()