Skip to content

Commit a0aac93

Browse files
authored
Merge pull request #309 from alexlib/master
tested simple_multipass
2 parents 61a67dd + 66270e3 commit a0aac93

File tree

5 files changed

+338
-189
lines changed

5 files changed

+338
-189
lines changed

openpiv/test/test_process.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
""" Testing basic PIV processes """
12
import numpy as np
2-
import matplotlib.pyplot as plt
33
from skimage.util import random_noise
44
from skimage import img_as_ubyte
55
from scipy.ndimage import shift as shift_img
@@ -8,9 +8,11 @@
88
from openpiv.pyprocess import extended_search_area_piv as piv
99
from openpiv.pyprocess import fft_correlate_images, \
1010
correlation_to_displacement
11+
from openpiv import tools
1112

1213

13-
threshold = 0.25
14+
15+
THRESHOLD = 0.25
1416

1517
# define "PIV" shift, i.e. creating u,v values that we want to get
1618
# -5.5 pixels to the left and 3.2 pixels upwards
@@ -19,18 +21,18 @@
1921
# the second value is 1 pixel positive to the right
2022
# shifted_digit_image=shift(some_digit_image,[2,1])
2123
# so we expect to get later
22-
# shift(image, [-1*shift_v, shift_u])
24+
# shift(image, [-1*SHIFT_V, SHIFT_U])
2325

2426

2527
# <------
26-
shift_u = -3.5 # shift to the left, should be placed in columns, axis=1
28+
SHIFT_U = -3.5 # shift to the left, should be placed in columns, axis=1
2729
# ^
2830
# |
2931
# |
30-
shift_v = 2.5 # shift upwards, should be placed in rows, axis=0
32+
SHIFT_V = 2.5 # shift upwards, should be placed in rows, axis=0
3133

3234

33-
def create_pair(image_size=32, u=shift_u, v=shift_v):
35+
def create_pair(image_size=32, u=SHIFT_U, v=SHIFT_V):
3436
""" creates a pair of images with a roll/shift """
3537
frame_a = np.zeros((image_size, image_size))
3638
frame_a = random_noise(frame_a)
@@ -60,16 +62,16 @@ def test_piv():
6062
# extended_search_area_piv returns image based coordinate system
6163
u, v, _ = piv(frame_a, frame_b, window_size=32)
6264
print(u, v)
63-
assert np.allclose(u, shift_u, atol=threshold)
64-
assert np.allclose(v, shift_v, atol=threshold)
65+
assert np.allclose(u, SHIFT_U, atol=THRESHOLD)
66+
assert np.allclose(v, SHIFT_V, atol=THRESHOLD)
6567

6668

6769
def test_piv_smaller_window():
6870
""" test of the search area larger than the window """
6971
frame_a, frame_b = create_pair(image_size=32, u=-3.5, v=-2.1)
7072
u, v, _ = piv(frame_a, frame_b, window_size=16, search_area_size=32)
71-
assert np.allclose(u, -3.5, atol=threshold)
72-
assert np.allclose(v, -2.1, atol=threshold)
73+
assert np.allclose(u, -3.5, atol=THRESHOLD)
74+
assert np.allclose(v, -2.1, atol=THRESHOLD)
7375

7476

7577
def test_extended_search_area():
@@ -80,10 +82,10 @@ def test_extended_search_area():
8082
search_area_size=32,
8183
overlap=0)
8284

83-
assert np.allclose(u, -3.5, atol=threshold)
84-
assert np.allclose(v, -2.1, atol=threshold)
85-
# assert dist(u, shift_u) < threshold
86-
# assert dist(v, shift_v) < threshold
85+
assert np.allclose(u, -3.5, atol=THRESHOLD)
86+
assert np.allclose(v, -2.1, atol=THRESHOLD)
87+
# assert dist(u, SHIFT_U) < THRESHOLD
88+
# assert dist(v, SHIFT_V) < THRESHOLD
8789

8890

8991
def test_extended_search_area_overlap():
@@ -94,8 +96,8 @@ def test_extended_search_area_overlap():
9496
search_area_size=32,
9597
overlap=8)
9698
print(f"\n u={u}\n v={v}\n")
97-
assert np.allclose(u, shift_u, atol=threshold)
98-
assert np.allclose(v, shift_v, atol=threshold)
99+
assert np.allclose(u, SHIFT_U, atol=THRESHOLD)
100+
assert np.allclose(v, SHIFT_V, atol=THRESHOLD)
99101

100102

101103
def test_extended_search_area_sig2noise():
@@ -110,8 +112,8 @@ def test_extended_search_area_sig2noise():
110112
subpixel_method="gaussian"
111113
)
112114

113-
assert np.allclose(u, -3.5, atol=threshold)
114-
assert np.allclose(v, 2.1, atol=threshold)
115+
assert np.allclose(u, -3.5, atol=THRESHOLD)
116+
assert np.allclose(v, 2.1, atol=THRESHOLD)
115117

116118

117119
def test_process_extended_search_area():
@@ -120,13 +122,12 @@ def test_process_extended_search_area():
120122
u, v, _ = piv(frame_a, frame_b, window_size=16,
121123
search_area_size=32, dt=2., overlap=0)
122124

123-
assert np.allclose(u, shift_u/2., atol=threshold)
124-
assert np.allclose(v, shift_v/2., atol=threshold)
125+
assert np.allclose(u, SHIFT_U/2., atol=THRESHOLD)
126+
assert np.allclose(v, SHIFT_V/2., atol=THRESHOLD)
125127

126128

127129
def test_sig2noise_ratio():
128130
""" s2n ratio test """
129-
from openpiv import tools
130131
im1 = files('openpiv.data').joinpath('test1/exp1_001_a.bmp')
131132
im2 = files('openpiv.data').joinpath('test1/exp1_001_b.bmp')
132133

@@ -148,9 +149,10 @@ def test_sig2noise_ratio():
148149

149150

150151
def test_fft_correlate():
152+
""" test of the fft correlation """
151153
frame_a, frame_b = create_pair(image_size=32)
152154
corr = fft_correlate_images(frame_a, frame_b)
153155
u, v = correlation_to_displacement(corr[np.newaxis, ...], 1, 1)
154-
assert np.allclose(u, shift_u, atol=threshold)
155-
assert np.allclose(v, shift_v, atol=threshold)
156+
assert np.allclose(u, SHIFT_U, atol=THRESHOLD)
157+
assert np.allclose(v, SHIFT_V, atol=THRESHOLD)
156158

openpiv/test/test_validation.py

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,56 @@
1+
""" Testing validation functions """
2+
from typing import Tuple
3+
import numpy as np
4+
from importlib_resources import files
5+
16
from openpiv.pyprocess import extended_search_area_piv as piv
27
from openpiv.tools import imread
3-
import pathlib
4-
5-
import numpy as np
6-
from .test_process import create_pair, shift_u, shift_v, threshold
78
from openpiv import validation
89

9-
from scipy.ndimage import generic_filter, median_filter
10-
from scipy.signal import convolve2d
11-
12-
import matplotlib.pyplot as plt
13-
from typing import List, Tuple
14-
import numpy.typing as npt
1510

16-
17-
file_a = pathlib.Path(__file__).parent / '../data/test1/exp1_001_a.bmp'
18-
file_b = pathlib.Path(__file__).parent / '../data/test1/exp1_001_b.bmp'
11+
file_a = files('openpiv.data').joinpath('test1/exp1_001_a.bmp')
12+
file_b = files('openpiv.data').joinpath('test1/exp1_001_b.bmp')
1913

2014
frame_a = imread(file_a)
2115
frame_b = imread(file_b)
2216

23-
frame_a = frame_a[:32,:32]
24-
frame_b = frame_b[:32,:32]
17+
frame_a = frame_a[:32, :32]
18+
frame_b = frame_b[:32, :32]
2519

2620

2721
def test_validation_peak2mean():
2822
"""test of the simplest PIV run
2923
default window_size = 32
3024
"""
31-
_, _, s2n = piv(frame_a, frame_b,
32-
window_size=32,
25+
_, _, s2n = piv(frame_a, frame_b,
26+
window_size=32,
3327
sig2noise_method="peak2mean")
3428

35-
assert np.allclose(s2n.min(),1.443882)
29+
assert np.allclose(s2n.min(), 1.443882)
30+
3631

3732
def test_validation_peak2peak():
3833
"""test of the simplest PIV run
3934
default window_size = 32
4035
"""
41-
_, _, s2n = piv(frame_a, frame_b,
42-
window_size=32,
36+
_, _, s2n = piv(frame_a, frame_b,
37+
window_size=32,
4338
sig2noise_method="peak2peak")
4439
assert np.allclose(np.min(s2n), 1.24009)
4540

41+
4642
def test_sig2noise_val():
4743
""" tests sig2noise validation """
48-
u = np.ones((5,5))
49-
v = np.ones((5,5))
44+
u = np.ones((5, 5))
45+
v = np.ones((5, 5))
5046
s2n_threshold = 1.05
51-
s2n = np.ones((5,5))*s2n_threshold
52-
s2n[2,2] -= 0.1
53-
47+
s2n = np.ones((5, 5))*s2n_threshold
48+
s2n[2, 2] -= 0.1
49+
5450
mask = s2n < s2n_threshold
55-
56-
assert not mask[0,0] # should be False
57-
assert mask[2,2]
5851

52+
assert not mask[0, 0] # should be False
53+
assert mask[2, 2]
5954

6055

6156
def test_local_median_validation(u_threshold=3, N=3):
@@ -66,49 +61,46 @@ def test_local_median_validation(u_threshold=3, N=3):
6661
N (int, optional): _description_. Defaults to 3.
6762
size (int, optional): _description_. Defaults to 1.
6863
"""
69-
70-
u = np.random.rand(2*N+1, 2*N+1)
71-
u[N,N] = np.median(u)*10
72-
7364

65+
u = np.random.rand(2*N+1, 2*N+1)
66+
u[N, N] = np.median(u)*10
7467

7568
# and masked array copy
7669
u = np.ma.copy(u)
77-
u[N+1:,N+1:-1] = np.ma.masked
70+
u[N+1:, N+1:-1] = np.ma.masked
7871

79-
# now we test our function which is just a decoration
72+
# now we test our function which is just a decoration
8073
# of the above steps
81-
flag = validation.local_median_val(u,u,u_threshold,u_threshold)
74+
flag = validation.local_median_val(u, u, u_threshold, u_threshold)
75+
76+
assert flag[N, N]
8277

83-
assert flag[N,N]
8478

8579
def test_global_val():
8680
""" tests global validation
8781
"""
88-
N: int=2
89-
U: Tuple[int,int]=(-10,10)
82+
N: int = 2
83+
U: Tuple[int, int] = (-10, 10)
9084

9185
u = np.random.rand(2*N+1, 2*N+1)
9286
u[N, N] = U[0]-.2
93-
u[0,0] = U[1]+.2
87+
u[0, 0] = U[1]+.2
9488

9589
u = np.ma.copy(u)
9690
v = np.ma.copy(u)
97-
v[N+1,N+1] = np.ma.masked
91+
v[N+1, N+1] = np.ma.masked
9892

99-
mask = validation.global_val(u,u,U,U)
93+
mask = validation.global_val(u, u, U, U)
10094

101-
assert mask[N,N]
102-
assert mask[0,0]
95+
assert mask[N, N]
96+
assert mask[0, 0]
10397

10498
# masked array test
10599

100+
mask1 = validation.global_val(v, v, U, U)
106101

107-
108-
mask1 = validation.global_val(v,v,U,U)
109-
110-
assert mask1[N,N]
111-
assert mask1[0,0]
102+
assert mask1[N, N]
103+
assert mask1[0, 0]
112104

113105

114106
def test_global_std():
@@ -126,32 +118,31 @@ def test_global_std():
126118
# print(np.nanstd(u))
127119

128120
u[N, N] = 10.
129-
u[0,0] = -10.
121+
u[0, 0] = -10.
130122

131123
v = np.ma.copy(u)
132-
v[N+1,N+1] = np.ma.masked
133-
124+
v[N+1, N+1] = np.ma.masked
134125

135126
mask = validation.global_std(u, u, 3)
136127

137-
assert mask[N,N]
138-
assert mask[0,0]
128+
assert mask[N, N]
129+
assert mask[0, 0]
139130

140131
mask1 = validation.global_std(v, v, std_threshold=3)
141132

142-
assert mask1[N,N]
143-
assert mask1[0,0]
133+
assert mask1[N, N]
134+
assert mask1[0, 0]
144135

145-
def test_uniform_shift_std(N: int=2):
136+
137+
def test_uniform_shift_std(N: int = 2):
146138
""" test for uniform shift """
147139
u = np.ones((2*N+1, 2*N+1))
148140
v = np.ma.copy(u)
149-
v[N+1,N+1] = np.ma.masked
141+
v[N+1, N+1] = np.ma.masked
150142

151143
mask = validation.global_std(u, u, std_threshold=3)
152144

153-
assert ~mask[N,N]
154-
145+
assert ~mask[N, N]
155146

156147
# print(f'v.data \n {v.data}')
157148
# print(f'v before \n {v}')
@@ -160,5 +151,5 @@ def test_uniform_shift_std(N: int=2):
160151

161152
v[mask1] = np.ma.masked
162153

163-
assert v.data[N,N] == 1.0
164-
assert v.mask[N+1,N+1]
154+
assert v.data[N, N] == 1.0
155+
assert v.mask[N+1, N+1]

0 commit comments

Comments
 (0)