/
validation.py
327 lines (237 loc) · 9.28 KB
/
validation.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
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
"""A module for spurious vector detection."""
__licence__ = """
Copyright (C) 2011 www.openpiv.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import warnings
from typing import Tuple
import numpy as np
from scipy.ndimage import generic_filter
import matplotlib.pyplot as plt
from openpiv.windef import PIVSettings
def global_val(
u: np.ndarray,
v: np.ndarray,
u_thresholds: Tuple[int, int],
v_thresholds: Tuple[int, int],
)-> np.ndarray:
"""Eliminate spurious vectors with a global threshold.
This validation method tests for the spatial consistency of the data
and outliers vector are replaced with Nan (Not a Number) if at
least one of the two velocity components is out of a specified global
range.
Parameters
----------
u : 2d np.ndarray
a two dimensional array containing the u velocity component.
v : 2d np.ndarray
a two dimensional array containing the v velocity component.
u_thresholds: two elements tuple
u_thresholds = (u_min, u_max). If ``u<u_min`` or ``u>u_max``
the vector is treated as an outlier.
v_thresholds: two elements tuple
``v_thresholds = (v_min, v_max)``. If ``v<v_min`` or ``v>v_max``
the vector is treated as an outlier.
Returns
-------
flag : boolean 2d np.ndarray
a boolean array. True elements corresponds to outliers.
"""
warnings.filterwarnings("ignore")
ind = np.logical_or(
np.logical_or(u < u_thresholds[0], u > u_thresholds[1]),
np.logical_or(v < v_thresholds[0], v > v_thresholds[1]),
)
return ind
def global_std(
u: np.ndarray,
v: np.ndarray,
std_threshold: int=5,
)->np.ndarray:
"""Eliminate spurious vectors with a global threshold defined by the
standard deviation
This validation method tests for the spatial consistency of the data
and outliers vector are replaced with NaN (Not a Number) if at least
one of the two velocity components is out of a specified global range.
Parameters
----------
u : 2d masked np.ndarray
a two dimensional array containing the u velocity component.
v : 2d masked np.ndarray
a two dimensional array containing the v velocity component.
std_threshold: float
If the length of the vector (actually the sum of squared components) is
larger than std_threshold times standard deviation of the flow field,
then the vector is treated as an outlier. [default = 3]
Returns
-------
flag : boolean 2d np.ndarray
a boolean array. True elements corresponds to outliers.
"""
# both previous nans and masked regions are not
# participating in the magnitude comparison
# def reject_outliers(data, m=2):
# return data[abs(data - np.mean(data)) < m * np.std(data)]
# create nan filled arrays where masks
# if u,v, are non-masked, ma.copy() adds false masks
tmpu = np.ma.copy(u).filled(np.nan)
tmpv = np.ma.copy(v).filled(np.nan)
ind = np.logical_or(np.abs(tmpu - np.nanmean(tmpu)) > std_threshold * np.nanstd(tmpu),
np.abs(tmpv - np.nanmean(tmpv)) > std_threshold * np.nanstd(tmpv))
if np.all(ind): # if all is True, something is really wrong
print('Warning! probably a uniform shift data, do not use this filter')
ind = ~ind
return ind
def sig2noise_val(
s2n: np.ndarray,
threshold: float=1.0,
)->np.ndarray:
""" Marks spurious vectors if signal to noise ratio is below a specified threshold.
Parameters
----------
u : 2d or 3d np.ndarray
a two or three dimensional array containing the u velocity component.
v : 2d or 3d np.ndarray
a two or three dimensional array containing the v velocity component.
s2n : 2d np.ndarray
a two or three dimensional array containing the value of the signal to
noise ratio from cross-correlation function.
w : 2d or 3d np.ndarray
a two or three dimensional array containing the w (in z-direction)
velocity component.
threshold: float
the signal to noise ratio threshold value.
Returns
-------
flag : boolean 2d np.ndarray
a boolean array. True elements corresponds to outliers.
References
----------
R. D. Keane and R. J. Adrian, Measurement Science & Technology, 1990,
1, 1202-1215.
"""
ind = s2n < threshold
return ind
def local_median_val(
u: np.ndarray,
v: np.ndarray,
u_threshold: float,
v_threshold: float,
size: int=1
)->np.ndarray:
"""Eliminate spurious vectors with a local median threshold.
This validation method tests for the spatial consistency of the data.
Vectors are classified as outliers and replaced with Nan (Not a Number) if
the absolute difference with the local median is greater than a user
specified threshold. The median is computed for both velocity components.
The image masked areas (obstacles, reflections) are marked as masked array:
u = np.ma.masked(u, flag = image_mask)
and it should not be replaced by the local median, but remain masked.
Parameters
----------
u : 2d np.ndarray
a two dimensional array containing the u velocity component.
v : 2d np.ndarray
a two dimensional array containing the v velocity component.
u_threshold : float
the threshold value for component u
v_threshold : float
the threshold value for component v
Returns
-------
flag : boolean 2d np.ndarray
a boolean array. True elements corresponds to outliers.
"""
# kernel footprint
# f = np.ones((2*size+1, 2*size+1))
# f[size,size] = 0
if np.ma.is_masked(u):
masked_u = np.where(~u.mask, u.data, np.nan)
masked_v = np.where(~v.mask, v.data, np.nan)
else:
masked_u = u
masked_v = v
um = generic_filter(masked_u, np.nanmedian, mode='constant',
cval=np.nan, size=(2*size+1, 2*size+1))
vm = generic_filter(masked_v, np.nanmedian, mode='constant',
cval=np.nan, size=(2*size+1, 2*size+1))
ind = (np.abs((u - um)) > u_threshold) | (np.abs((v - vm)) > v_threshold)
return ind
def typical_validation(
u: np.ndarray,
v: np.ndarray,
s2n: np.ndarray,
settings: "PIVSettings"
)->np.ndarray:
"""
validation using gloabl limits and std and local median,
with a special option of 'no_std' for the case of completely
uniform shift, e.g. in tests.
see windef.PIVSettings() for the parameters:
MinMaxU : two elements tuple
sets the limits of the u displacment component
Used for validation.
MinMaxV : two elements tuple
sets the limits of the v displacment component
Used for validation.
std_threshold : float
sets the threshold for the std validation
median_threshold : float
sets the threshold for the median validation
"""
if settings.show_all_plots:
plt.figure()
plt.quiver(u,v,color='b')
plt.gca().invert_yaxis()
plt.title('Before (b) and global (m) local (k)')
# flag = np.zeros(u.shape, dtype=bool)
# Global validation
flag_g = global_val(u, v, settings.min_max_u_disp, settings.min_max_v_disp)
# u[flag_g] = np.ma.masked
# v[flag_g] = np.ma.masked
# if settings.show_all_plots:
# plt.quiver(u, v, color='m')
flag_s = global_std(
u, v, std_threshold=settings.std_threshold
)
# u[flag_s] = np.ma.masked
# v[flag_s] = np.ma.masked
# print(f"std filter invalidated {sum(flag_s.flatten())} vectors")
# if settings.show_all_plots:
# plt.quiver(u,v,color='k')
flag_m = local_median_val(
u,
v,
u_threshold=settings.median_threshold,
v_threshold=settings.median_threshold,
size=settings.median_size,
)
# u[flag_m] = np.ma.masked
# v[flag_m] = np.ma.masked
# if settings.show_all_plots:
# plt.quiver(u,v,color='r')
# print(f"median filter invalidated {sum(flag_m.flatten())} vectors")
flag = flag_g | flag_m | flag_s
if settings.sig2noise_validate:
flag_s2n = sig2noise_val(s2n, settings.sig2noise_threshold)
# u[flag_s2n] = np.ma.masked
# v[flag_s2n] = np.ma.masked
# print(f"s2n filter invalidated {sum(flag_s2n.flatten())} vectors")
# if settings.show_all_plots:
# plt.quiver(u,v,color='g')
# plt.show()
if settings.show_all_plots and sum(flag_s2n.flatten()): # if not all NaN
plt.figure()
plt.hist( s2n[s2n>0], 31)
plt.show()
flag += flag_s2n
return flag