import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20compare%20the%20OpenPIV%20Python%20with%20PIVLab%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aapp._unparsable_cell(%0A%20%20%20%20r%22%22%22%0A%20%20%20%20Analysis%20of%20the%20Karman%20images%0A%20%20%20%20final%20int%20area%206%20pixels%20and%2050%25%20overlap%2C%20%0A%20%20%20%20vector%20validation%20is%20allowed%2C%20but%20no%20smoothing%20after%20the%20last%20correlation.%20%0A%20%20%20%20Only%20the%20circle%20in%20the%20middle%20must%20be%20masked%2C%20not%20the%20shadows.%0A%0A%20%20%20%20Then%20we%20can%20compare%20the%20vorticity%20maps%20(color%20bar%20scale%20of%20uncalibrated%20data%20-0.3%201%2Fframe%20until%20%2B0.3%201%2Fframe%2C%20%0A%20%20%20%20color%20map%20preferably%20%22parula%22%2C%20but%20%22jet%22%20is%20also%20ok).%20That%20might%20give%20an%20idea%20about%20the%20%22quality%22...%3F%0A%20%20%20%20%22%22%22%2C%0A%20%20%20%20name%3D%22_%22%0A)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20magic%20command%20not%20supported%20in%20marimo%3B%20please%20file%20an%20issue%20to%20add%20support%0A%20%20%20%20%23%20%25reload_ext%20watermark%0A%20%20%20%20%23%20magic%20command%20not%20supported%20in%20marimo%3B%20please%20file%20an%20issue%20to%20add%20support%0A%20%20%20%20%23%20%25watermark%20-v%20-m%20-p%20numpy%2Copenpiv%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20openpiv%20import%20windef%0A%20%20%20%20from%20openpiv%20import%20tools%2C%20scaling%2C%20validation%2C%20filters%2C%20preprocess%0A%20%20%20%20from%20openpiv.pyprocess%20import%20extended_search_area_piv%2C%20get_field_shape%2C%20get_coordinates%0A%20%20%20%20from%20openpiv%20import%20smoothn%0A%20%20%20%20from%20openpiv.preprocess%20import%20mask_coordinates%0A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20os%0A%20%20%20%20from%20time%20import%20time%0A%20%20%20%20import%20warnings%0A%0A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%0A%20%20%20%20import%20matplotlib%0A%20%20%20%20matplotlib.rcParams%5B'figure.figsize'%5D%20%3D%20(10.0%2C%208.0)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20filters%2C%0A%20%20%20%20%20%20%20%20mask_coordinates%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20os%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20preprocess%2C%0A%20%20%20%20%20%20%20%20scaling%2C%0A%20%20%20%20%20%20%20%20smoothn%2C%0A%20%20%20%20%20%20%20%20tools%2C%0A%20%20%20%20%20%20%20%20validation%2C%0A%20%20%20%20%20%20%20%20windef%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(windef)%3A%0A%20%20%20%20settings%20%3D%20windef.PIVSettings()%0A%0A%20%20%20%20%23%20'Data%20related%20settings'%0A%20%20%20%20%23%20Folder%20with%20the%20images%20to%20process%0A%20%20%20%20settings.filepath_images%20%3D%20'..%2Ftest9%2F'%0A%20%20%20%20%23%20Folder%20for%20the%20outputs%0A%20%20%20%20settings.save_path%20%3D%20'.'%0A%20%20%20%20%23%20Root%20name%20of%20the%20output%20Folder%20for%20Result%20Files%0A%20%20%20%20settings.save_folder_suffix%20%3D%20''%0A%20%20%20%20%23%20Format%20and%20Image%20Sequence%0A%20%20%20%20settings.frame_pattern_a%20%3D%20'karman_16Hz_000_A.jpg'%0A%20%20%20%20settings.frame_pattern_b%20%3D%20'karman_16Hz_000_B.jpg'%0A%0A%20%20%20%20'Region%20of%20interest'%0A%20%20%20%20%23%20(50%2C300%2C50%2C300)%20%23Region%20of%20interest%3A%20(xmin%2Cxmax%2Cymin%2Cymax)%20or%20'full'%20for%20full%20image%0A%20%20%20%20settings.ROI%20%3D%20'full'%0A%20%20%20%20%23%20settings.ROI%20%3D%20(200%2C400%2C600%2C850)%0A%0A%0A%0A%20%20%20%20settings.deformation_method%20%3D%20'symmetric'%20%23%20or%20'second%20image'%0A%0A%0A%20%20%20%20settings.num_iterations%20%3D%204%20%20%23%20select%20the%20number%20of%20PIV%20passes%0A%0A%20%20%20%20%23%20add%20the%20interrogation%20window%20size%20for%20each%20pass.%20%0A%20%20%20%20%23%20For%20the%20moment%2C%20it%20should%20be%20a%20power%20of%202%20%0A%20%20%20%20settings.windowsizes%3D(32%2C%2016%2C%208%2C%206)%0A%20%20%20%20settings.overlap%3D(16%2C%208%2C%204%2C%203)%0A%0A%20%20%20%20%23%20settings.windowsizes%20%3D%20(128%2C%2064%2C%2032%2C%2016%2C%208)%20%23%20if%20longer%20than%20n%20iteration%20the%20rest%20is%20ignored%0A%20%20%20%20%23%20The%20overlap%20of%20the%20interroagtion%20window%20for%20each%20pass.%0A%20%20%20%20%23%20settings.overlap%20%3D%20(64%2C%2032%2C%2016%2C%208%2C%204)%20%23%20This%20is%2050%25%20overlap%0A%0A%0A%20%20%20%20%23%20Has%20to%20be%20a%20value%20with%20base%20two.%20In%20general%20window%20size%2F2%20is%20a%20good%20choice.%0A%20%20%20%20%23%20methode%20used%20for%20subpixel%20interpolation%3A%20'gaussian'%2C'centroid'%2C'parabolic'%0A%20%20%20%20settings.subpixel_method%20%3D%20'gaussian'%0A%0A%20%20%20%20%23%20order%20of%20the%20image%20interpolation%20for%20the%20window%20deformation%0A%20%20%20%20settings.interpolation_order%20%3D%201%0A%20%20%20%20settings.scaling_factor%20%3D%201%20%20%23%20scaling%20factor%20pixel%2Fmeter%0A%20%20%20%20settings.dt%20%3D%201%20%20%23%20time%20between%20to%20frames%20(in%20seconds)%0A%20%20%20%20'Signal%20to%20noise%20ratio%20options%20(only%20for%20the%20last%20pass)'%0A%20%20%20%20%23%20It%20is%20possible%20to%20decide%20if%20the%20S%2FN%20should%20be%20computed%20(for%20the%20last%20pass)%20or%20not%0A%20%20%20%20%23%20settings.extract_sig2noise%20%3D%20True%20%20%23%20'True'%20or%20'False'%20(only%20for%20the%20last%20pass)%0A%20%20%20%20%23%20method%20used%20to%20calculate%20the%20signal%20to%20noise%20ratio%20'peak2peak'%20or%20'peak2mean'%0A%20%20%20%20settings.sig2noise_method%20%3D%20'peak2peak'%0A%20%20%20%20%23%20select%20the%20width%20of%20the%20masked%20to%20masked%20out%20pixels%20next%20to%20the%20main%20peak%0A%20%20%20%20settings.sig2noise_mask%20%3D%202%0A%20%20%20%20%23%20If%20extract_sig2noise%3D%3DFalse%20the%20values%20in%20the%20signal%20to%20noise%20ratio%0A%20%20%20%20%23%20output%20column%20are%20set%20to%20NaN%0A%0A%20%20%20%20%23%20only%20effecting%20the%20first%20pass%20of%20the%20interrogation%20the%20following%20passes%0A%20%20%20%20%23%20in%20the%20multipass%20will%20be%20validated%0A%0A%20%20%20%20'Output%20options'%0A%20%20%20%20%23%20Select%20if%20you%20want%20to%20save%20the%20plotted%20vectorfield%3A%20True%20or%20False%0A%20%20%20%20settings.save_plot%20%3D%20False%0A%20%20%20%20%23%20Choose%20wether%20you%20want%20to%20see%20the%20vectorfield%20or%20not%20%3ATrue%20or%20False%0A%20%20%20%20settings.show_plot%20%3D%20True%0A%20%20%20%20settings.scale_plot%20%3D%20200%20%20%23%20select%20a%20value%20to%20scale%20the%20quiver%20plot%20of%20the%20vectorfield%0A%20%20%20%20%23%20run%20the%20script%20with%20the%20given%20settings%0A%0A%0A%0A%20%20%20%20%23%20'Processing%20Parameters'%0A%20%20%20%20settings.correlation_method%3D'linear'%20%20%23%20'circular'%20or%20'linear'%0A%20%20%20%20settings.normalized_correlation%20%3D%20True%0A%0A%20%20%20%20%23%20'vector%20validation%20options'%0A%20%20%20%20%23%20choose%20if%20you%20want%20to%20do%20validation%20of%20the%20first%20pass%3A%20True%20or%20False%0A%20%20%20%20settings.validation_first_pass%20%3D%20True%0A%0A%0A%20%20%20%20settings.filter_method%20%3D%20'localmean'%0A%20%20%20%20%23%20maximum%20iterations%20performed%20to%20replace%20the%20outliers%0A%20%20%20%20settings.max_filter_iteration%20%3D%2010%0A%20%20%20%20settings.filter_kernel_size%20%3D%203%20%20%23%20kernel%20size%20for%20the%20localmean%20method%0A%0A%20%20%20%20settings.replace_vectors%20%3D%20True%0A%0A%20%20%20%20settings.MinMax_U_disp%20%3D%20(-5%2C%205)%0A%20%20%20%20settings.MinMax_V_disp%20%3D%20(-5%2C%205)%0A%0A%20%20%20%20%23%20The%20second%20filter%20is%20based%20on%20the%20global%20STD%20threshold%0A%20%20%20%20settings.std_threshold%20%3D%203%20%20%23%20threshold%20of%20the%20std%20validation%0A%0A%20%20%20%20%23%20The%20third%20filter%20is%20the%20median%20test%20(not%20normalized%20at%20the%20moment)%0A%20%20%20%20settings.median_threshold%20%3D%203%20%20%23%20threshold%20of%20the%20median%20validation%0A%20%20%20%20%23%20On%20the%20last%20iteration%2C%20an%20additional%20validation%20can%20be%20done%20based%20on%20the%20S%2FN.%0A%20%20%20%20settings.median_size%3D1%20%23defines%20the%20size%20of%20the%20local%20median%2C%20it'll%20be%203%20x%203%0A%0A%0A%20%20%20%20settings.dynamic_masking_method%20%3D%20'intensity'%0A%20%20%20%20settings.dynamic_masking_threshold%20%3D%200.1%0A%20%20%20%20settings.dynamic_masking_filter_size%20%3D%2021%0A%20%20%20%20return%20(settings%2C)%0A%0A%0A%40app.cell%0Adef%20_(settings)%3A%0A%20%20%20%20vars(settings)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Read%20and%20crop%20the%20images%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(os%2C%20settings%2C%20tools)%3A%0A%20%20%20%20file_a%20%3D%20settings.frame_pattern_a%0A%20%20%20%20file_b%20%3D%20settings.frame_pattern_b%0A%0A%20%20%20%20%23%20%22%20read%20images%20into%20numpy%20arrays%22%0A%20%20%20%20frame_a%20%3D%20tools.imread(os.path.join(settings.filepath_images%2C%20file_a))%0A%20%20%20%20frame_b%20%3D%20tools.imread(os.path.join(settings.filepath_images%2C%20file_b))%0A%0A%20%20%20%20%23%20%22%20crop%20to%20ROI%22%0A%20%20%20%20if%20settings.ROI%20%3D%3D%20%22full%22%3A%0A%20%20%20%20%20%20%20%20pass%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20frame_a%20%3D%20frame_a%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.ROI%5B0%5D%3Asettings.ROI%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.ROI%5B2%5D%3Asettings.ROI%5B3%5D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20frame_b%20%3D%20frame_b%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.ROI%5B0%5D%3Asettings.ROI%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20settings.ROI%5B2%5D%3Asettings.ROI%5B3%5D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20return%20frame_a%2C%20frame_b%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Show%20the%20images%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame_a%2C%20plt)%3A%0A%20%20%20%20plt.imshow(frame_a%2Ccmap%3Dplt.cm.gray)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Image%20masking%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame_a%2C%20frame_b%2C%20plt%2C%20preprocess%2C%20settings)%3A%0A%20%20%20%20%23%20'Image%20preprocessing'%0A%20%20%20%20%23%20'None'%20for%20no%20masking%2C%20'edges'%20for%20edges%20masking%2C%20'intensity'%20for%20intensity%20masking%0A%20%20%20%20%23%20WARNING%3A%20This%20part%20is%20under%20development%20so%20better%20not%20to%20use%20MASKS%0A%20%20%20%20if%20settings.dynamic_masking_method%20%3D%3D%20'edge'%20or%20'intensity'%3A%0A%20%20%20%20%20%20%20%20frame_a_1%2C%20image_mask_a%20%3D%20preprocess.dynamic_masking(frame_a%2C%20method%3Dsettings.dynamic_masking_method%2C%20filter_size%3Dsettings.dynamic_masking_filter_size%2C%20threshold%3Dsettings.dynamic_masking_threshold)%0A%20%20%20%20%20%20%20%20frame_b_1%2C%20image_mask_b%20%3D%20preprocess.dynamic_masking(frame_b%2C%20method%3Dsettings.dynamic_masking_method%2C%20filter_size%3Dsettings.dynamic_masking_filter_size%2C%20threshold%3Dsettings.dynamic_masking_threshold)%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%202)%0A%20%20%20%20ax%5B0%5D.imshow(frame_a_1)%0A%20%20%20%20ax%5B1%5D.imshow(frame_b_1)%0A%20%20%20%20return%20frame_a_1%2C%20frame_b_1%2C%20image_mask_a%2C%20image_mask_b%0A%0A%0A%40app.cell%0Adef%20_(image_mask_a%2C%20image_mask_b%2C%20np%2C%20plt)%3A%0A%20%20%20%20%23%20let's%20combine%20the%20two%20masks%20if%20the%20body%20is%20slightly%20moving%0A%20%20%20%20image_mask%20%3D%20np.logical_and(image_mask_a%2C%20image_mask_b)%0A%20%20%20%20plt.imshow(image_mask)%0A%20%20%20%20return%20(image_mask%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Exract%20coordinates%20of%20the%20mask%20as%20a%20list%20of%20coordinates%20of%20a%20polygon%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(image_mask%2C%20mask_coordinates)%3A%0A%20%20%20%20mask_coords%20%3D%20mask_coordinates(image_mask)%0A%20%20%20%20return%20(mask_coords%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Run%20the%20first%20pass%0A%0A%20%20%20%20We%20use%20typically%20the%20most%20robust%20approach%3A%20linear%20correlation%20(with%20zero%20padding)%0A%20%20%20%20%20%20%20%20and%20normalized%20correlation%20function%20(0..1)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame_a_1%2C%20frame_b_1%2C%20np%2C%20settings%2C%20windef)%3A%0A%20%20%20%20%23%20In%20order%20to%20convert%20the%20image%20mask%20to%20the%20data%20mask%20in%20x%2Cy%20%0A%20%20%20%20%23%20coordinates%2C%20we%20have%20to%20either%20run%20first%20pass%20or%20%0A%20%20%20%20%23%20use%20get_coordinates%0A%20%20%20%20%23%20Since%20we%20do%20not%20know%20how%20to%20use%20the%20image_mask%20in%20the%20%0A%20%20%20%20%23%20first%20pass%20with%20the%20vectorized%20correlations%2C%20i.e.%20how%20to%20%0A%20%20%20%20%23%20save%20some%20computational%20time%20by%20skipping%20the%20interrogation%0A%20%20%20%20%23%20windows%20within%20the%20image%20mask%2C%20we%20just%20run%20the%20first%20pass%0A%20%20%20%20x%2C%20y%2C%20u%2C%20v%2C%20sig2noise_ratio%20%3D%20windef.first_pass(frame_a_1%2C%20frame_b_1%2C%20settings)%0A%20%20%20%20u0%20%3D%20u.copy()%0A%20%20%20%20%23%20%22first%20pass%22%0A%20%20%20%20v0%20%3D%20v.copy()%0A%0A%20%20%20%20def%20status_message(u)%3A%0A%20%20%20%20%20%20%20%20print(f'%7Bnp.isnan(u).sum()%20%2F%20u.size%20*%20100%3A.2f%7D%25%20invalid%20vectors%20out%20of%20%7Bu.size%7D%20vectors')%0A%20%20%20%20%23%20store%20for%20the%20comparison%20of%20the%20following%20steps%0A%20%20%20%20status_message(u)%0A%20%20%20%20return%20sig2noise_ratio%2C%20status_message%2C%20u%2C%20u0%2C%20v%2C%20v0%2C%20x%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(image_mask%2C%20mask_coords%2C%20np%2C%20plt%2C%20x%2C%20y)%3A%0A%20%20%20%20%23%20Now%20we%20can%20convert%20the%20image%20mask%20to%20the%20data%20mask%20in%20x%2Cy%20coordinates%0A%0A%20%20%20%20from%20skimage.measure%20import%20points_in_poly%0A%0A%20%20%20%20%23%20mark%20those%20points%20on%20the%20grid%20of%20PIV%20inside%20the%20mask%0A%20%20%20%20xymask%20%3D%20points_in_poly(np.c_%5By.flatten()%2Cx.flatten()%5D%2Cmask_coords)%0A%0A%20%20%20%20plt.imshow(~image_mask%2Ccmap%3Dplt.cm.gray)%0A%20%20%20%20plt.plot(x.flat%5Bxymask%5D%2Cy.flat%5Bxymask%5D%2C'x')%0A%20%20%20%20return%20(xymask%2C)%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20u%2C%20v%2C%20x%2C%20xymask)%3A%0A%20%20%20%20%23%20mask%20the%20velocity%20maps%20for%20the%20future%20use%20in%20validation%0A%20%20%20%20tmp%20%3D%20np.zeros_like(x%2C%20dtype%3Dbool)%0A%20%20%20%20tmp.flat%5Bxymask%5D%20%3D%20True%0A%20%20%20%20u_1%20%3D%20np.ma.masked_array(u%2C%20mask%3Dtmp)%0A%20%20%20%20v_1%20%3D%20np.ma.masked_array(v%2C%20mask%3Dtmp)%0A%20%20%20%20return%20u_1%2C%20v_1%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20sig2noise_ratio%2C%20u_1%2C%20v_1%2C%20x%2C%20xymask%2C%20y)%3A%0A%20%20%20%20%23%20we%20need%20to%20remove%20those%20values%20for%20the%20display%0A%20%20%20%20def%20quick_quiver()%3A%0A%20%20%20%20%20%20%20%20%22%22%22%20u%2Cv%20expected%20to%20have%20a%20mask%20%22%22%22%0A%20%20%20%20%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio%2C%20scale%3D50%2C%20color%3D'b')%0A%20%20%20%20%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20%20%20%20%20plt.gca().set_aspect(1)%0A%20%20%20%20%20%20%20%20plt.plot(x.flat%5Bxymask%5D%2C%20y.flat%5Bxymask%5D%2C%20'rx')%0A%20%20%20%20%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%0A%20%20%20%20return%20(quick_quiver%2C)%0A%0A%0A%40app.cell%0Adef%20_(quick_quiver)%3A%0A%20%20%20%20quick_quiver()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20sig2noise_ratio)%3A%0A%20%20%20%20%23%20see%20the%20distribution%20of%20the%20signal%20to%20noise%20ratio%0A%20%20%20%20tmp_1%20%3D%20sig2noise_ratio.copy()%0A%20%20%20%20tmp_1%5Btmp_1%20%3E%2010%5D%20%3D%2010%20%20%23%20there%20are%20some%20extra%20high%20values%201e7%20...%0A%20%20%20%20plt.imshow(tmp_1)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%20(tmp_1%2C)%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20tmp_1)%3A%0A%20%20%20%20plt.hist(tmp_1.flatten())%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20settings%2C%20sig2noise_ratio)%3A%0A%20%20%20%20%23%20let's%20consider%205%25%20of%20signoise%20ratio%20problems.%20%0A%20%20%20%20sig2noise_threshold%20%3D%20np.percentile(sig2noise_ratio%2C(2.5))%0A%20%20%20%20print(f%22S2N%20threshold%20is%20estimated%20as%20%7Bsig2noise_threshold%3A.3f%7D%22)%0A%0A%20%20%20%20settings.sig2noise_threshold%20%3D%201.2%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(settings%2C%20sig2noise_ratio%2C%20status_message%2C%20u_1%2C%20validation)%3A%0A%20%20%20%20mask_s2n%20%3D%20validation.sig2noise_val(sig2noise_ratio%2C%20threshold%3Dsettings.sig2noise_threshold)%0A%20%20%20%20status_message(u_1)%0A%20%20%20%20return%20(mask_s2n%2C)%0A%0A%0A%40app.cell%0Adef%20_(mask_s2n%2C%20plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_1%2C%20v0%2C%20v_1%2C%20x%2C%20y)%3A%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio)%0A%20%20%20%20plt.quiver(x%5Bmask_s2n%5D%2C%20y%5Bmask_s2n%5D%2C%20u0%5Bmask_s2n%5D%2C%20-v0%5Bmask_s2n%5D%2C%20color%3D'r')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20x)%3A%0A%20%20%20%20%23%20False%20everywhere%2C%20all%20passes%0A%20%20%20%20outliers_mask%20%3D%20np.zeros_like(x%2C%20dtype%3Dbool)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20v_1)%3A%0A%20%20%20%20plt.hist(v_1.flatten())%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(settings%2C%20status_message%2C%20u_1%2C%20v_1%2C%20validation)%3A%0A%20%20%20%20%23%20'Validation%20Parameters'%0A%20%20%20%20%23%20The%20validation%20is%20done%20at%20each%20iteration%20based%20on%20three%20filters.%0A%20%20%20%20%23%20The%20first%20filter%20is%20based%20on%20the%20min%2Fmax%20ranges.%20Observe%20that%20these%20values%20are%20defined%20in%0A%20%20%20%20%23%20terms%20of%20minimum%20and%20maximum%20displacement%20in%20pixel%2Fframes.%0A%20%20%20%20mask_g%20%3D%20validation.global_val(u_1%2C%20v_1%2C%20settings.MinMax_U_disp%2C%20settings.MinMax_V_disp)%0A%20%20%20%20status_message(u_1)%0A%20%20%20%20return%20(mask_g%2C)%0A%0A%0A%40app.cell%0Adef%20_(mask_g%2C%20plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_1%2C%20v0%2C%20v_1%2C%20x%2C%20y)%3A%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio)%0A%20%20%20%20plt.quiver(x%5Bmask_g%5D%2C%20y%5Bmask_g%5D%2C%20u0%5Bmask_g%5D%2C%20-v0%5Bmask_g%5D%2C%20color%3D'r')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%23%20also%20global%20std%20should%20take%20masked%20array%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(settings%2C%20status_message%2C%20u_1%2C%20v_1%2C%20validation)%3A%0A%20%20%20%20%23%20The%20second%20filter%20is%20based%20on%20the%20global%20STD%20threshold%0A%20%20%20%20settings.std_threshold%20%3D%205%20%20%23%20threshold%20of%20the%20std%20validation%0A%20%20%20%20mask_s%20%3D%20validation.global_std(u_1%2C%20v_1%2C%20std_threshold%3Dsettings.std_threshold)%0A%20%20%20%20status_message(u_1)%0A%20%20%20%20return%20(mask_s%2C)%0A%0A%0A%40app.cell%0Adef%20_(mask_s%2C%20plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_1%2C%20v0%2C%20v_1%2C%20x%2C%20y)%3A%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio)%0A%20%20%20%20plt.quiver(x%5Bmask_s%5D%2C%20y%5Bmask_s%5D%2C%20u0%5Bmask_s%5D%2C%20-v0%5Bmask_s%5D%2C%20color%3D'r')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%23%20validation.local_median_val%20should%20also%20take%20masked%20array%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(settings%2C%20status_message%2C%20u_1%2C%20v_1%2C%20validation)%3A%0A%20%20%20%20%23%20The%20third%20filter%20is%20the%20median%20test%20(not%20normalized%20at%20the%20moment)%0A%20%20%20%20settings.median_threshold%20%3D%203%20%20%23%20threshold%20of%20the%20median%20validation%0A%20%20%20%20%23%20On%20the%20last%20iteration%2C%20an%20additional%20validation%20can%20be%20done%20based%20on%20the%20S%2FN.%0A%20%20%20%20settings.median_size%20%3D%201%20%20%23defines%20the%20size%20of%20the%20local%20median%0A%20%20%20%20mask_m%20%3D%20validation.local_median_val(u_1%2C%20v_1%2C%20u_threshold%3Dsettings.median_threshold%2C%20v_threshold%3Dsettings.median_threshold%2C%20size%3Dsettings.median_size)%0A%20%20%20%20status_message(u_1)%0A%20%20%20%20return%20(mask_m%2C)%0A%0A%0A%40app.cell%0Adef%20_(mask_m%2C%20plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_1%2C%20v0%2C%20v_1%2C%20x%2C%20y)%3A%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio)%0A%20%20%20%20plt.quiver(x%5Bmask_m%5D%2C%20y%5Bmask_m%5D%2C%20u0%5Bmask_m%5D%2C%20-v0%5Bmask_m%5D%2C%20color%3D'r')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mask_g%2C%20mask_m%2C%20mask_s%2C%20mask_s2n)%3A%0A%20%20%20%20%23%20Combining%20masks%0A%20%20%20%20outliers_mask_1%20%3D%20mask_g%20%2B%20mask_m%20%2B%20mask_s%20%2B%20mask_s2n%0A%20%20%20%20return%20(outliers_mask_1%2C)%0A%0A%0A%40app.cell%0Adef%20_(outliers_mask_1%2C%20plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_1%2C%20v0%2C%20v_1%2C%20x%2C%20y)%3A%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_1%2C%20-v_1%2C%20sig2noise_ratio)%0A%20%20%20%20plt.quiver(x%5Boutliers_mask_1%5D%2C%20y%5Boutliers_mask_1%5D%2C%20u0%5Boutliers_mask_1%5D%2C%20-v0%5Boutliers_mask_1%5D%2C%20color%3D'r')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(status_message%2C%20u_1)%3A%0A%20%20%20%20status_message(u_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(filters%2C%20outliers_mask_1%2C%20settings%2C%20u_1%2C%20v_1)%3A%0A%20%20%20%20%23%20%22filter%20to%20replace%20the%20values%20that%20where%20marked%20by%20the%20validation%22%0A%20%20%20%20%23%20if%20settings.num_iterations%20%3E%201%3A%0A%20%20%20%20u_2%2C%20v_2%20%3D%20filters.replace_outliers(u_1%2C%20v_1%2C%20outliers_mask_1%2C%20method%3Dsettings.filter_method%2C%20max_iter%3Dsettings.max_filter_iteration%2C%20kernel_size%3Dsettings.filter_kernel_size)%0A%20%20%20%20return%20u_2%2C%20v_2%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20u_2%2C%20v_2%2C%20x%2C%20xymask)%3A%0A%20%20%20%20%23%20mask%20the%20velocity%20maps%0A%20%20%20%20tmp_2%20%3D%20np.zeros_like(x%2C%20dtype%3Dbool)%0A%20%20%20%20tmp_2.flat%5Bxymask%5D%20%3D%201%0A%20%20%20%20u_3%20%3D%20np.ma.masked_array(u_2%2C%20mask%3Dtmp_2)%0A%20%20%20%20v_3%20%3D%20np.ma.masked_array(v_2%2C%20mask%3Dtmp_2)%0A%20%20%20%20return%20u_3%2C%20v_3%0A%0A%0A%40app.cell%0Adef%20_(quick_quiver)%3A%0A%20%20%20%20quick_quiver()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20settings%2C%20smoothn%2C%20u_3%2C%20v_3%2C%20x%2C%20xymask)%3A%0A%20%20%20%20%23%20Smoothing%20after%20the%20first%20pass%0A%20%20%20%20settings.smoothn%20%3D%20True%20%20%23Enables%20smoothing%20of%20the%20displacemenet%20field%0A%20%20%20%20settings.smoothn_p%20%3D%200.5%20%20%23%20This%20is%20a%20smoothing%20parameter%0A%20%20%20%20u_4%2C%20dummy_u1%2C%20dummy_u2%2C%20dummy_u3%20%3D%20smoothn.smoothn(u_3%2C%20s%3Dsettings.smoothn_p)%0A%20%20%20%20v_4%2C%20dummy_v1%2C%20dummy_v2%2C%20dummy_v3%20%3D%20smoothn.smoothn(v_3%2C%20s%3Dsettings.smoothn_p)%0A%20%20%20%20tmp_3%20%3D%20np.zeros_like(x%2C%20dtype%3Dbool)%0A%20%20%20%20tmp_3.flat%5Bxymask%5D%20%3D%201%0A%20%20%20%20u_4%20%3D%20np.ma.masked_array(u_4%2C%20mask%3Dtmp_3)%0A%20%20%20%20%23%20mask%20the%20velocity%20maps%0A%20%20%20%20v_4%20%3D%20np.ma.masked_array(v_4%2C%20mask%3Dtmp_3)%0A%20%20%20%20return%20u_4%2C%20v_4%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20sig2noise_ratio%2C%20u0%2C%20u_4%2C%20v0%2C%20v_4%2C%20x%2C%20xymask%2C%20y)%3A%0A%20%20%20%20%23%20x%2C%20y%2C%20u%2C%20v%20%3D%20tools.transform_coordinates(x%2C%20y%2C%20u%2C%20v)%0A%20%20%20%20plt.figure()%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u0%2C%20-v0%2C%20color%3D'r'%2C%20scale%3D30%2C%20alpha%3D0.5)%0A%20%20%20%20plt.quiver(x%2C%20y%2C%20u_4%2C%20-v_4%2C%20sig2noise_ratio%2C%20scale%3D30)%0A%20%20%20%20plt.plot(x.flat%5Bxymask%5D%2C%20y.flat%5Bxymask%5D%2C%20'ro')%0A%20%20%20%20plt.gca().invert_yaxis()%0A%20%20%20%20plt.colorbar(orientation%3D'horizontal')%0A%20%20%20%20plt.gca().set_aspect(1.0)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Multi-pass%20loop%20with%20window%20deformation%2C%20validation%20and%20smoothing%0A%0A%20%20%20%20**Note**%3A%20no%20smoothing%20on%20the%20last%20step%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame_a_1%2C%20frame_b_1%2C%20settings%2C%20u_4%2C%20v_4%2C%20windef%2C%20x%2C%20y)%3A%0A%20%20%20%20%23%20TODO%3A%20study%20the%20sig2noise%20validation%20in%20multipass%0A%20%20%20%20settings.sig2noise_validate%20%3D%20False%0A%20%20%20%20for%20i%20in%20range(1%2C%20settings.num_iterations)%3A%0A%20%20%20%20%20%20%20%20x_1%2C%20y_1%2C%20u_5%2C%20v_5%2C%20sig2noise_ratio_1%2C%20mask%20%3D%20windef.multipass_img_deform(frame_a_1%2C%20frame_b_1%2C%20i%2C%20x%2C%20y%2C%20u_4%2C%20v_4%2C%20settings)%20%20%23%23%20all%20other%20passes%0A%20%20%20%20return%20mask%2C%20u_5%2C%20v_5%2C%20x_1%2C%20y_1%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Save%20the%20outcome%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mask%2C%20os%2C%20plt%2C%20scaling%2C%20settings%2C%20tools%2C%20u_5%2C%20v_5%2C%20x_1%2C%20y_1)%3A%0A%20%20%20%20save_path%20%3D%20'.'%0A%20%20%20%20counter%20%3D%200%0A%20%20%20%20u_6%20%3D%20u_5%20%2F%20settings.dt%0A%20%20%20%20%23%20%22pixel%2Fframe-%3Epixel%2Fsec%22%0A%20%20%20%20v_6%20%3D%20v_5%20%2F%20settings.dt%0A%20%20%20%20x_2%2C%20y_2%2C%20u_6%2C%20v_6%20%3D%20scaling.uniform(x_1%2C%20y_1%2C%20u_6%2C%20v_6%2C%20scaling_factor%3Dsettings.scaling_factor)%0A%20%20%20%20x_2%2C%20y_2%2C%20u_6%2C%20v_6%20%3D%20tools.transform_coordinates(x_2%2C%20y_2%2C%20u_6%2C%20v_6)%0A%20%20%20%20%23%20%22scales%20the%20results%20pixel-%3E%20meter%22%0A%20%20%20%20tools.save(os.path.join(save_path%2C%20'field_A%2503d.txt'%20%25%20counter)%2C%20x_2%2C%20y_2%2C%20u_6%2C%20v_6%2C%20mask%2C%20delimiter%3D'%5Ct')%0A%20%20%20%20settings.show_plot%20%3D%20True%0A%20%20%20%20settings.save_plot%20%3D%20True%0A%20%20%20%20if%20settings.show_plot%20is%20True%20or%20settings.save_plot%20is%20True%3A%0A%20%20%20%20%20%20%20%20plt.close('all')%0A%20%20%20%20%23%20%22save%20to%20a%20file%22%0A%20%20%20%20%20%20%20%20plt.ioff()%0A%20%20%20%20%20%20%20%20filename%20%3D%20os.path.join(save_path%2C%20'Image_A%2503d.png'%20%25%20counter)%0A%20%20%20%20%20%20%20%20tools.display_vector_field(os.path.join(save_path%2C%20'field_A%2503d.txt'%20%25%20counter)%2C%20scale%3Dsettings.scale_plot)%0A%20%20%20%20%20%20%20%20if%20settings.save_plot%20is%20True%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20plt.savefig(filename)%0A%20%20%20%20%20%20%20%20if%20settings.show_plot%20is%20True%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20plt.show()%0A%20%20%20%20%23%20%22some%20other%20stuff%20that%20one%20might%20want%20to%20use%22%0A%20%20%20%20print('Image%20Pair%20'%20%2B%20str(counter%20%2B%201))%0A%20%20%20%20return%20(counter%2C)%0A%0A%0A%40app.cell%0Adef%20_(counter%2C%20plt)%3A%0A%20%20%20%20import%20glob%0A%20%20%20%20import%20xarray%20as%20xr%0A%20%20%20%20from%20pivpy%20import%20io%2C%20pivpy%0A%20%20%20%20file_list%20%3D%20sorted(glob.glob('field_A%2503d.txt'%20%25%20counter))%0A%20%20%20%20print(file_list)%0A%20%20%20%20data%20%3D%20%5B%5D%0A%20%20%20%20frame%20%3D%200%0A%20%20%20%20for%20f%20in%20file_list%3A%0A%20%20%20%20%20%20%20%20data.append(io.load_openpiv_txt(f%2C%20frame%3Dframe))%0A%20%20%20%20%20%20%20%20frame%20%3D%20frame%20%2B%201%0A%20%20%20%20data%20%3D%20xr.concat(data%2C%20dim%3D't')%0A%20%20%20%20data.attrs%5B'units'%5D%20%3D%20%5B'pix'%2C%20'pix'%2C%20'pix%2Fdt'%2C%20'pix%2Fdt'%5D%0A%20%20%20%20data.piv.vorticity()%0A%0A%20%20%20%20def%20plot_data(data)%3A%0A%20%20%20%20%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(20%2C%2012))%0A%20%20%20%20%20%20%20%20ax.quiver(data.x.data%2C%20data.y.data%2C%20data.u.isel(t%3D0).data.T%2C%20data.v.isel(t%3D0).data.T%2C%20color%3D'r'%2C%20scale%3D120)%0A%20%20%20%20%20%20%20%20s%20%3D%20ax.pcolor(data.x%2C%20data.y%2C%20data.w.T.isel(t%3D0)%2C%20shading%3D'interp'%2C%20vmin%3D-0.3%2C%20vmax%3D0.3%2C%20alpha%3D0.7)%0A%20%20%20%20%20%20%20%20ax.set_aspect(1)%0A%20%20%20%20%20%20%20%20fig.colorbar(s%2C%20ax%3Dax)%0A%20%20%20%20%20%20%20%20plt.show()%0A%20%20%20%20plot_data(data)%20%20%23%20for%20ax%20in%20axs%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
a0fba5afb0afea3638eac482a16d4e09