# -*- 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.PIVSettings()
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
test_first_pass_circ()
[[ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.]] [[ 32. 32. 32. 32. 32. 32. 32.] [ 64. 64. 64. 64. 64. 64. 64.] [ 96. 96. 96. 96. 96. 96. 96.] [128. 128. 128. 128. 128. 128. 128.] [160. 160. 160. 160. 160. 160. 160.] [192. 192. 192. 192. 192. 192. 192.] [224. 224. 224. 224. 224. 224. 224.]] [[-3.48853232 -3.49172956 -3.48838473 -3.49183355 -3.49742935 -3.47646367 -3.50077889] [-3.50543698 -3.49384565 -3.49203047 -3.49415425 -3.49289956 -3.48115936 -3.51185417] [-3.49539542 -3.48705721 -3.48767375 -3.49356523 -3.48727225 -3.48108877 -3.49590096] [-3.48158163 -3.4733631 -3.47934388 -3.48862114 -3.48914016 -3.48186781 -3.48315708] [-3.48969991 -3.48666121 -3.4973138 -3.48678537 -3.49014378 -3.48997316 -3.49397751] [-3.48097908 -3.49256313 -3.4918143 -3.48857704 -3.48147885 -3.49408613 -3.49865655] [-3.48733345 -3.49729746 -3.48534812 -3.48002814 -3.48238621 -3.49751863 -3.50101603]] [[2.11057624 2.10526109 2.10024594 2.10923435 2.10985766 2.0927801 2.11601436] [2.08644311 2.10362021 2.1084678 2.11452316 2.10201947 2.09402623 2.1069 ] [2.1140416 2.08613297 2.09622104 2.11960183 2.11108846 2.09665523 2.09713337] [2.10392211 2.08523756 2.0884736 2.11173942 2.10556171 2.11053555 2.10021594] [2.09562687 2.10798021 2.11798293 2.10975703 2.10691531 2.10688041 2.10338451] [2.10331565 2.11683353 2.11485829 2.10064564 2.10782591 2.10083298 2.11816544] [2.11289354 2.11061541 2.09768621 2.09229191 2.10302082 2.11449436 2.09960184]] [[1.81352926 1.79199405 1.80774592 1.78226525 1.75932068 1.7756347 1.78004368] [1.80734449 1.83698958 1.8232435 1.83027209 1.82727144 1.84111438 1.84140538] [1.82079885 1.83376804 1.79074553 1.78790232 1.81747203 1.82335664 1.79647729] [1.78277674 1.81177381 1.82949827 1.78857486 1.77128292 1.82111768 1.82393901] [0. 1.77451802 1.78139708 1.80533828 1.7813276 1.80240252 1.82220458] [1.85165679 1.80419948 1.75948132 0. 1.81224016 1.77902939 1.77492903] [1.84570806 1.77258427 1.74792246 1.80721917 1.82004224 1.8072873 1.79183598]]
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
# 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)
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.
test_multi_pass_circ()
first pass [[ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.]] [[ 32. 32. 32. 32. 32. 32. 32.] [ 64. 64. 64. 64. 64. 64. 64.] [ 96. 96. 96. 96. 96. 96. 96.] [128. 128. 128. 128. 128. 128. 128.] [160. 160. 160. 160. 160. 160. 160.] [192. 192. 192. 192. 192. 192. 192.] [224. 224. 224. 224. 224. 224. 224.]] [[-3.48853232 -3.49172956 -3.48838473 -3.49183355 -3.49742935 -3.47646367 -3.50077889] [-3.50543698 -3.49384565 -3.49203047 -3.49415425 -3.49289956 -3.48115936 -3.51185417] [-3.49539542 -3.48705721 -3.48767375 -3.49356523 -3.48727225 -3.48108877 -3.49590096] [-3.48158163 -3.4733631 -3.47934388 -3.48862114 -3.48914016 -3.48186781 -3.48315708] [-3.48969991 -3.48666121 -3.4973138 -3.48678537 -3.49014378 -3.48997316 -3.49397751] [-3.48097908 -3.49256313 -3.4918143 -3.48857704 -3.48147885 -3.49408613 -3.49865655] [-3.48733345 -3.49729746 -3.48534812 -3.48002814 -3.48238621 -3.49751863 -3.50101603]] [[2.11057624 2.10526109 2.10024594 2.10923435 2.10985766 2.0927801 2.11601436] [2.08644311 2.10362021 2.1084678 2.11452316 2.10201947 2.09402623 2.1069 ] [2.1140416 2.08613297 2.09622104 2.11960183 2.11108846 2.09665523 2.09713337] [2.10392211 2.08523756 2.0884736 2.11173942 2.10556171 2.11053555 2.10021594] [2.09562687 2.10798021 2.11798293 2.10975703 2.10691531 2.10688041 2.10338451] [2.10331565 2.11683353 2.11485829 2.10064564 2.10782591 2.10083298 2.11816544] [2.11289354 2.11061541 2.09768621 2.09229191 2.10302082 2.11449436 2.09960184]] [[1.81352926 1.79199405 1.80774592 1.78226525 1.75932068 1.7756347 1.78004368] [1.80734449 1.83698958 1.8232435 1.83027209 1.82727144 1.84111438 1.84140538] [1.82079885 1.83376804 1.79074553 1.78790232 1.81747203 1.82335664 1.79647729] [1.78277674 1.81177381 1.82949827 1.78857486 1.77128292 1.82111768 1.82393901] [0. 1.77451802 1.78139708 1.80533828 1.7813276 1.80240252 1.82220458] [1.85165679 1.80419948 1.75948132 0. 1.81224016 1.77902939 1.77492903] [1.84570806 1.77258427 1.74792246 1.80721917 1.82004224 1.8072873 1.79183598]] Pass 1 [[ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.]] [[ 32. 32. 32. 32. 32. 32. 32.] [ 64. 64. 64. 64. 64. 64. 64.] [ 96. 96. 96. 96. 96. 96. 96.] [128. 128. 128. 128. 128. 128. 128.] [160. 160. 160. 160. 160. 160. 160.] [192. 192. 192. 192. 192. 192. 192.] [224. 224. 224. 224. 224. 224. 224.]] [[-3.46819606 -3.46196401 -3.45996385 -3.46706241 -3.47526366 -3.4552267 -3.48095954] [-3.48155309 -3.46529768 -3.463782 -3.46989522 -3.46804431 -3.45617136 -3.49951748] [-3.47709193 -3.46084229 -3.45967993 -3.46910624 -3.46284121 -3.45524955 -3.4816784 ] [-3.46804183 -3.45226712 -3.45460863 -3.46421938 -3.46367197 -3.45694527 -3.46580752] [-3.47170824 -3.46288995 -3.47116135 -3.46100004 -3.46335815 -3.46180295 -3.46715589] [-3.46513078 -3.46412313 -3.46108682 -3.46317767 -3.45767195 -3.4624377 -3.46475909] [-3.47405689 -3.46751574 -3.45585426 -3.45481529 -3.45694188 -3.46316328 -3.464548 ]] [[2.15934053 2.14987154 2.14694138 2.15368101 2.15739179 2.14453415 2.16310633] [2.13663195 2.14993466 2.15577666 2.16018883 2.15099187 2.14782257 2.15796213] [2.1594902 2.14011762 2.14802483 2.16472292 2.16146046 2.15105055 2.15277773] [2.15325188 2.14278581 2.14247068 2.15753976 2.15193169 2.15929168 2.15424183] [2.15073235 2.15685759 2.16001291 2.15346531 2.15307135 2.15250646 2.15120581] [2.14980804 2.15948896 2.16032347 2.15109047 2.15870273 2.14818107 2.16242959] [2.1603061 2.15873311 2.15438247 2.15443984 2.15798069 2.16750976 2.15676539]] [[2.0117968 2.09561645 2.11193313 2.07238217 2.03538224 2.04118063 2.05079841] [2.09323312 2.13464658 2.09439893 2.12055214 2.14787127 2.08150408 2.03725822] [2.13448417 2.09875988 2.03759985 2.10207825 2.11810804 2.12407383 2.10729111] [2.0551192 2.11055174 2.12680077 2.07448945 2.01454955 2.12819346 0. ] [1.99702304 2.01578993 2.09669981 2.08970366 2.0327472 2.0987961 2.08872672] [2.07224418 2.01624501 2.00210923 2.08976345 2.1256853 2.06647399 2.03711142] [2.0591963 2.01974604 1.94969323 2.05258436 2.14950646 2.11453414 2.01887863]]
# 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
test_first_pass_lin()
[[ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.]] [[ 32. 32. 32. 32. 32. 32. 32.] [ 64. 64. 64. 64. 64. 64. 64.] [ 96. 96. 96. 96. 96. 96. 96.] [128. 128. 128. 128. 128. 128. 128.] [160. 160. 160. 160. 160. 160. 160.] [192. 192. 192. 192. 192. 192. 192.] [224. 224. 224. 224. 224. 224. 224.]] [[-3.4874333 -3.48452903 -3.47870176 -3.48798008 -3.48996055 -3.47483023 -3.49255314] [-3.49266817 -3.48957753 -3.48334849 -3.49154857 -3.47795129 -3.47394717 -3.50282379] [-3.48573237 -3.48200457 -3.48070694 -3.48061541 -3.47390209 -3.47678846 -3.48565032] [-3.48315734 -3.46993561 -3.47402663 -3.47785741 -3.47211044 -3.4772208 -3.47781476] [-3.47602967 -3.47837697 -3.49059034 -3.48745475 -3.47565969 -3.48445661 -3.48300368] [-3.47591488 -3.48961535 -3.47839428 -3.4837702 -3.47768968 -3.48775101 -3.48779934] [-3.4811472 -3.48522047 -3.4691494 -3.48324336 -3.47865186 -3.48429515 -3.49352827]] [[2.10287501 2.10104647 2.09144698 2.10726152 2.10242996 2.08654261 2.07725958] [2.10288942 2.10109452 2.09645556 2.10596037 2.09376904 2.08925968 2.10180859] [2.10380861 2.08595647 2.09326765 2.11082962 2.10353122 2.0917707 2.09698629] [2.09816742 2.07987084 2.08389989 2.09704682 2.09509361 2.10314901 2.09232321] [2.08755 2.10013601 2.10901295 2.09816471 2.09923149 2.09983673 2.09076204] [2.09299599 2.11070858 2.10016598 2.09485894 2.10102032 2.10019601 2.10360319] [2.10437906 2.10234183 2.08296238 2.09278772 2.09396813 2.10384811 2.1060308 ]] [[1.91288059 1.8483264 1.84058477 1.86865934 1.81863043 1.7776604 1.80775279] [1.91275544 1.88391063 1.88309204 1.90011074 1.85109131 1.8776228 1.82188715] [1.92465043 1.9168179 1.84240027 1.7947965 1.8763702 1.84853388 1.8795694 ] [1.90376129 1.88461054 1.90525227 1.83471614 1.83130003 1.86271922 1.8452683 ] [1.92503434 1.82274077 1.85827292 1.86184775 1.80149779 1.80584238 1.84009779] [1.90654304 1.85628739 1.82547077 1.79370324 1.81768979 1.7875507 1.81384008] [1.87782094 1.79412872 1.78655143 1.87131955 1.90265783 1.90085625 1.81785346]]
from importlib_resources import files
from pathlib import Path
def test_invert_and_piv():
""" Test windef.piv with invert option """
settings = windef.PIVSettings()
'Data related settings'
# Folder with the images to process
settings.filepath_images = Path(files('openpiv')) / "data" / "test1"
settings.save_path = settings.filepath_images.parent
# Root name of the output Folder for Result Files
settings.save_folder_suffix = 'test_invert'
# 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 = True
settings.invert = True
windef.piv(settings)
test_invert_and_piv()
/home/user/Documents/repos/openpiv-python/openpiv/data/test1 exp1_001_a.bmp True [PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test1/exp1_001_a.bmp')]
Image Pair 1 exp1_001_a exp1_001_b
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_method = 'peak2peak'
settings.sig2noise_threshold = 1.0
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.
test_multi_pass_lin()
[[ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.] [ 32. 64. 96. 128. 160. 192. 224.]] [[ 32. 32. 32. 32. 32. 32. 32.] [ 64. 64. 64. 64. 64. 64. 64.] [ 96. 96. 96. 96. 96. 96. 96.] [128. 128. 128. 128. 128. 128. 128.] [160. 160. 160. 160. 160. 160. 160.] [192. 192. 192. 192. 192. 192. 192.] [224. 224. 224. 224. 224. 224. 224.]] [[-3.50369112 -3.49711136 -3.48140639 -3.48992762 -3.48999298 -3.47977266 -3.50521113] [-3.5037635 -3.50124862 -3.4895115 -3.49899346 -3.48338022 -3.48027408 -3.51016152] [-3.50008667 -3.49901987 -3.48730436 -3.48713212 -3.47359573 -3.48156511 -3.49075837] [-3.49325286 -3.48233213 -3.47807257 -3.48459519 -3.47060743 -3.48235851 -3.47888616] [-3.47451083 -3.47886602 -3.49290744 -3.49102889 -3.47642039 -3.49024405 -3.49187324] [-3.47730625 -3.49449726 -3.48611028 -3.48913645 -3.47897962 -3.48409569 -3.49032778] [-3.48429056 -3.48824751 -3.48023333 -3.49194925 -3.48151559 -3.48882126 -3.50065144]] [[2.08881689 2.08848623 2.08056864 2.10440631 2.10068828 2.08448171 2.10515411] [2.06777508 2.07526586 2.09213718 2.10651757 2.08930812 2.07899545 2.099949 ] [2.07986363 2.08309326 2.09896879 2.11886932 2.10241937 2.08590018 2.08656713] [2.09159631 2.06815803 2.08075063 2.10098149 2.09190458 2.10667068 2.09028113] [2.07782264 2.09451806 2.1153872 2.09711472 2.0909525 2.10057534 2.08951575] [2.08335363 2.10576398 2.0954512 2.09107554 2.10346308 2.09639421 2.09537896] [2.11057657 2.10707194 2.07800039 2.0916871 2.09885941 2.0965485 2.07534143]] [[2.88901528 2.73510461 2.64497217 2.74815366 2.80431788 2.69327536 2.71799319] [2.71486182 2.73496908 2.72551338 2.78701577 2.81224889 2.88396068 2.78529335] [2.77380864 2.82946039 2.77290692 2.65205403 2.87251569 2.78261897 2.76665221] [2.93369045 2.91090617 2.94538271 2.82671706 2.80486451 2.83155473 2.79975837] [3.02733229 2.78426961 2.87038234 2.92722961 2.67209151 2.71411969 2.79894018] [2.88649528 2.77208974 2.72291515 2.72810689 2.76317908 2.69770868 2.75467125] [2.89330468 2.58738178 2.66814755 2.78335507 2.83384286 2.8603153 2.73812439]]
from openpiv.pyprocess import extended_search_area_piv, get_field_shape, get_coordinates
u, v, s2n = extended_search_area_piv(
frame_a,
frame_b,
window_size=settings.windowsizes[0],
overlap=settings.overlap[0],
search_area_size=settings.windowsizes[0],
width=settings.sig2noise_mask,
subpixel_method=settings.subpixel_method,
sig2noise_method=settings.sig2noise_method,
correlation_method=settings.correlation_method,
normalized_correlation=settings.normalized_correlation
)
shapes = np.array(get_field_shape(frame_a.shape,
settings.windowsizes[0],
settings.overlap[0]))
u = u.reshape(shapes)
v = v.reshape(shapes)
s2n = s2n.reshape(shapes)
x, y = get_coordinates(frame_a.shape,
settings.windowsizes[0],
settings.overlap[0])
# return x, y, u, v, s2n
def test_static_masking():
""" Test windef.piv with invert option """
from importlib_resources import files
settings = windef.PIVSettings()
'Data related settings'
# Folder with the images to process
settings.filepath_images = files('openpiv') / "data" / "test2"
settings.save_path = pathlib.Path('.')
# Root name of the output Folder for Result Files
settings.save_folder_suffix = 'test'
# Format and Image Sequence
settings.frame_pattern_a = '2image_*.tif'
settings.frame_pattern_b = '(1+2),(3+4)'
from openpiv.tools import imread
images = sorted(settings.filepath_images.glob(settings.frame_pattern_a))
frame_a = imread(images[0])
settings.static_mask = np.where(frame_a > 150, True, False)
# print(settings.static_mask)
plt.imshow(settings.static_mask)
settings.num_iterations = 1
settings.show_plot = True
settings.scale_plot = 50
settings.show_all_plots = False
settings.invert = False
windef.piv(settings)
test_static_masking()
/home/user/Documents/repos/openpiv-python/openpiv/data/test2 2image_*.tif True [PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_00.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_01.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_10.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_11.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_20.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_21.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_30.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_31.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_40.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_41.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_50.tif'), PosixPath('/home/user/Documents/repos/openpiv-python/openpiv/data/test2/2image_51.tif')] Inside display_vector_field OpenPIV_results_64_test/field_A0000.txt
Image Pair 1 2image_00 2image_01 Inside display_vector_field OpenPIV_results_64_test/field_A0001.txt
Image Pair 2 2image_10 2image_11 Inside display_vector_field OpenPIV_results_64_test/field_A0002.txt
Image Pair 3 2image_20 2image_21 Inside display_vector_field OpenPIV_results_64_test/field_A0003.txt
Image Pair 4 2image_30 2image_31 Inside display_vector_field OpenPIV_results_64_test/field_A0004.txt
Image Pair 5 2image_40 2image_41 Inside display_vector_field OpenPIV_results_64_test/field_A0005.txt
Image Pair 6 2image_50 2image_51