/
test_windef.py
186 lines (155 loc) · 5.12 KB
/
test_windef.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 4 14:33:21 2019
@author: Theo
"""
import numpy as np
from openpiv import windef
from openpiv.test import test_process
from openpiv import preprocess
import pathlib
import os
import matplotlib.pyplot as plt
frame_a, frame_b = test_process.create_pair(image_size=256)
shift_u, shift_v, threshold = test_process.shift_u, test_process.shift_v, \
test_process.threshold
# this test are created only to test the displacement evaluation of the
# function the validation methods are not tested here ant therefore
# are disabled.
settings = windef.Settings()
settings.windowsizes = (64,)
settings.overlap = (32,)
settings.num_iterations = 1
settings.correlation_method = 'circular'
settings.sig2noise_method = 'peak2peak'
settings.subpixel_method = 'gaussian'
settings.sig2noise_mask = 2
# circular cross correlation
def test_first_pass_circ():
""" test of the first pass """
x, y, u, v, s2n = windef.first_pass(
frame_a,
frame_b,
settings
)
print("\n", x, y, u, v, s2n)
assert np.mean(np.abs(u - shift_u)) < threshold
assert np.mean(np.abs(v - shift_v)) < threshold
def test_multi_pass_circ():
""" test fot the multipass """
settings.windowsizes = (64, 64, 16)
settings.overlap = (32, 32, 8)
settings.num_iterations = 2
settings.interpolation_order = 3
settings.validation_first_pass = True
settings.sig2noise_validate = False
# ettings.show_all_plots = True
x, y, u, v, s2n = windef.first_pass(
frame_a,
frame_b,
settings,
)
print("first pass\n")
print("\n", x, y, u, v, s2n)
assert np.allclose(u, shift_u, atol = threshold)
assert np.allclose(v, shift_v, atol = threshold)
if settings.image_mask:
image_mask = np.logical_and(mask_a, mask_b)
mask_coords = preprocess.mask_coordinates(image_mask)
# mark those points on the grid of PIV inside the mask
grid_mask = preprocess.prepare_mask_on_grid(x,y,mask_coords)
# mask the velocity
u = np.ma.masked_array(u, mask=grid_mask)
v = np.ma.masked_array(v, mask=grid_mask)
else:
mask_coords = []
u = np.ma.masked_array(u, mask=np.ma.nomask)
v = np.ma.masked_array(v, mask=np.ma.nomask)
for i in range(1,settings.num_iterations):
x, y, u, v, s2n, _ = windef.multipass_img_deform(
frame_a,
frame_b,
i,
x,
y,
u,
v,
settings
)
print(f"Pass {i}\n")
print(x)
print(y)
print(u)
print(v)
print(s2n)
assert np.mean(np.abs(u - shift_u)) < threshold
assert np.mean(np.abs(v - shift_v)) < threshold
# the second condition is to check if the multipass is done.
# It need's a little numerical inaccuracy.
# linear cross correlation
def test_first_pass_lin():
""" test of the first pass """
settings.correlation_method = 'linear'
x, y, u, v, s2n = windef.first_pass(
frame_a,
frame_b,
settings,
)
print("\n", x, y, u, v, s2n)
assert np.mean(np.abs(u - shift_u)) < threshold
assert np.mean(np.abs(v - shift_v)) < threshold
def test_invert_and_piv():
""" Test windef.piv with invert option """
settings = windef.Settings()
'Data related settings'
# Folder with the images to process
settings.filepath_images = pathlib.Path(__file__).parent / '../data/test1'
settings.save_path = '.'
# Root name of the output Folder for Result Files
settings.save_folder_suffix = 'test'
# Format and Image Sequence
settings.frame_pattern_a = 'exp1_001_a.bmp'
settings.frame_pattern_b = 'exp1_001_b.bmp'
settings.num_iterations = 1
settings.show_plot = False
settings.scale_plot = 100
settings.show_all_plots = False
settings.invert = True
windef.piv(settings)
def test_multi_pass_lin():
""" test fot the multipass """
settings.windowsizes = (64, 32, 16)
settings.overlap = (32, 16, 8)
settings.num_iterations = 1
settings.sig2noise_validate = True
settings.correlation_method = 'linear'
settings.normalized_correlation = True
settings.sig2noise_threshold = 1.0 # note the value for linear/normalized
x, y, u, v, s2n = windef.first_pass(
frame_a,
frame_b,
settings,
)
print("\n", x, y, u, v, s2n)
assert np.mean(np.abs(u - shift_u)) < threshold
assert np.mean(np.abs(v - shift_v)) < threshold
mask_coords = []
u = np.ma.masked_array(u, mask=np.ma.nomask)
v = np.ma.masked_array(v, mask=np.ma.nomask)
for i in range(1, settings.num_iterations):
x, y, u, v, s2n, _ = windef.multipass_img_deform(
frame_a,
frame_b,
i,
x,
y,
u,
v,
settings,
)
print(f"Iteration {i}")
print("\n", x, y, u, v, s2n)
assert np.allclose(u, shift_u, atol=threshold)
assert np.allclose(v, shift_v, atol=threshold)
# the second condition is to check if the multipass is done.
# It need's a little numerical inaccuracy.