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%0Adef%20_()%3A%0A%20%20%20%20from%20openpiv%20import%20windef%20%23%20%3C----%20see%20windef.py%20for%20details%0A%20%20%20%20from%20openpiv%20import%20tools%2C%20scaling%2C%20validation%2C%20filters%2C%20preprocess%0A%20%20%20%20import%20openpiv.pyprocess%20as%20process%0A%20%20%20%20from%20openpiv%20import%20pyprocess%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pathlib%0A%20%20%20%20import%20importlib_resources%0A%20%20%20%20from%20time%20import%20time%0A%20%20%20%20import%20warnings%0A%20%20%20%20import%20cv2%20as%20cv%0A%20%20%20%20from%20scipy.ndimage%20import%20gaussian_filter%2C%20gaussian_filter1d%0A%20%20%20%20import%20sys%0A%20%20%20%20sys.path.append(r%22D%3A%5CESPCI%5Ccodes%5CmultgridPIV%22)%20%20%23%20Add%20the%20folder%20containing%20the%20script%20to%20the%20Python%20path%0A%20%20%20%20%23import%20windef_modified%20%20%23%20Import%20the%20script%20as%20a%20module%0A%20%20%20%20import%20openpiv.filters%0A%20%20%20%20from%20matplotlib.patches%20import%20Circle%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%20%20%20%20return%20Circle%2C%20gaussian_filter%2C%20np%2C%20openpiv%2C%20plt%2C%20pyprocess%2C%20tools%2C%20windef%0A%0A%0A%40app.cell%0Adef%20_(gaussian_filter%2C%20tools%2C%20windef)%3A%0A%20%20%20%20%23%23%23%20When%20I%20am%20reading%20from%20ND2.%20I%20comment%20this%20line%20for%20now%20since%20I%20am%20getting%20same%20error%20in%20v%20with%20ND2%20as%20with%20png%2Ftiff%0A%20%20%20%20%23%23im1%20%3D%20ND2Reader('ten0.01_sam20230331_eco_sll2_1mm_FP_3um_0.03_t_2x_test001.nd2')%0A%20%20%20%20t1%20%3D%2023%0A%20%20%20%20t2%20%3D%2024%0A%20%20%20%20gauss_bg%20%3D%20101%0A%20%20%20%20gauss_ns%20%3D%201%0A%20%20%20%20im1_t0%20%3D%20tools.imread(%22..%2Ftest20%2Ft_23.png%22)%23im1%5Bt1%5D%0A%20%20%20%20im2_t0%20%3D%20tools.imread('..%2Ftest20%2Ft_24.png')%23im1%5Bt2%5D%0A%20%20%20%20%23%23%20Correcting%20for%20background%20and%20noise%0A%20%20%20%20im1_t0_fil%20%3D%20gaussian_filter(im1_t0%2Fgaussian_filter(im1_t0%2C%20gauss_bg)%2Cgauss_ns)%0A%20%20%20%20im2_t0_fil%20%3D%20gaussian_filter(im2_t0%2Fgaussian_filter(im2_t0%2C%20gauss_bg)%2Cgauss_ns)%0A%0A%20%20%20%20%23%23%23%20Importing%20the%20setting%20class%20from%20windef%0A%20%20%20%20settings%20%3D%20windef.PIVSettings()%0A%20%20%20%20return%20im1_t0_fil%2C%20im2_t0_fil%2C%20settings%0A%0A%0A%40app.cell%0Adef%20_(settings)%3A%0A%20%20%20%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%20%23%23%23settings.dynamic_masking_method%20%3D%20'None'%0A%20%20%20%20%23%23%23settings.dynamic_masking_threshold%20%3D%200.005%0A%20%20%20%20%23%23%23settings.dynamic_masking_filter_size%20%3D%207%0A%20%20%20%20settings.roi%20%3D%20'full'%0A%0A%20%20%20%20settings.deformation_method%20%3D%20'symmetric'%0A%0A%20%20%20%20'Processing%20Parameters'%0A%20%20%20%20settings.correlation_method%3D'circular'%20%20%23%20'circular'%20or%20'linear'%0A%20%20%20%20settings.normalized_correlation%3DFalse%0A%0A%20%20%20%20settings.num_iterations%20%3D%201%20%232%20%23%20select%20the%20number%20of%20PIV%20passes%0A%20%20%20%20%23%20add%20the%20interroagtion%20window%20size%20for%20each%20pass.%0A%20%20%20%20%23%20For%20the%20moment%2C%20it%20should%20be%20a%20power%20of%202%0A%20%20%20%20settings.windowsizes%20%3D%20(%5B64%2C32%5D)%23(%5B64%2C32%5D)%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%20settings.overlap%20%3D%20(%5B16%5D)%23(%5B16%2C8%5D)%20%23%20This%20is%2050%25%20overlap%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%20%20%20%20%23%20order%20of%20the%20image%20interpolation%20for%20the%20window%20deformation%0A%20%20%20%20settings.interpolation_order%20%3D%203%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%20two%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%20%20%20%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%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%20%20%20%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%20settings.min_max_u_disp%20%3D%20(-30%2C%2030)%0A%20%20%20%20settings.min_max_v_disp%20%3D%20(-30%2C%2030)%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%207%20%20%23%20threshold%20of%20the%20std%20validation%0A%20%20%20%20%23%20The%20third%20filter%20is%20the%20median%20test%20(not%20normalized%20at%20the%20moment)%0A%20%20%20%20%23%23settings.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%20%23%23settings.median_size%3D1%20%23defines%20the%20size%20of%20the%20local%20median%0A%20%20%20%20'Validation%20based%20on%20the%20signal%20to%20noise%20ratio'%0A%20%20%20%20%23%20Note%3A%20only%20available%20when%20extract_sig2noise%3D%3DTrue%20and%20only%20for%20the%20last%0A%20%20%20%20%23%20pass%20of%20the%20interrogation%0A%20%20%20%20%23%20Enable%20the%20signal%20to%20noise%20ratio%20validation.%20Options%3A%20True%20or%20False%0A%20%20%20%20%23%20settings.do_sig2noise_validation%20%3D%20False%20%23%20This%20is%20time%20consuming%0A%20%20%20%20%23%20minmum%20signal%20to%20noise%20ratio%20that%20is%20need%20for%20a%20valid%20vector%0A%20%20%20%20%23%23settings.sig2noise_threshold%20%3D%201.2%0A%20%20%20%20'Outlier%20replacement%20or%20Smoothing%20options'%0A%20%20%20%20%23%20Replacment%20options%20for%20vectors%20which%20are%20masked%20as%20invalid%20by%20the%20validation%0A%20%20%20%20settings.replace_vectors%20%3D%20True%20%23%20Enable%20the%20replacment.%20Chosse%3A%20True%20or%20False%0A%20%20%20%20settings.smoothn%3DTrue%20%23Enables%20smoothing%20of%20the%20displacemenet%20field%0A%20%20%20%20settings.smoothn_p%3D0.5%20%23%20This%20is%20a%20smoothing%20parameter%0A%20%20%20%20%23%20select%20a%20method%20to%20replace%20the%20outliers%3A%20'localmean'%2C%20'disk'%2C%20'distance'%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%204%20%234%0A%20%20%20%20settings.filter_kernel_size%20%3D%201%20%20%23%20kernel%20size%20for%20the%20localmean%20method%0A%20%20%20%20%23%23'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%20%23%23settings.save_plot%20%3D%20True%0A%20%20%20%20%23%20Choose%20wether%20you%20want%20to%20see%20the%20vectorfield%20or%20not%20%3ATrue%20or%20False%0A%20%20%20%20%23%23settings.show_plot%20%3D%20True%0A%20%20%20%20%23%23settings.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%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(im1_t0_fil%2C%20im2_t0_fil%2C%20settings%2C%20windef)%3A%0A%20%20%20%20x%2C%20y%2C%20u%2C%20v%2C%20_flags%20%3D%20windef.simple_multipass(im1_t0_fil%2C%20im2_t0_fil%2C%20settings)%0A%20%20%20%20u%20%3D%20u%20%2F%20settings.dt%0A%20%20%20%20v%20%3D%20v%20%2F%20settings.dt%0A%20%20%20%20return%20u%2C%20v%2C%20x%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(Circle%2C%20im1_t0_fil%2C%20np%2C%20plt%2C%20u%2C%20v%2C%20x%2C%20y)%3A%0A%20%20%20%20_fig%2C%20_axs%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(6%2C%206))%0A%20%20%20%20_axs.imshow(im1_t0_fil)%0A%20%20%20%20_fig.gca().invert_yaxis()%0A%20%20%20%20%23axs%5B0%5D.imshow(np.dstack((scalevalues(fil_im1%2C0%2C100)%2Cscalevalues(fil_im2%2C0%2C100)%2C%20np.zeros(imgray1.shape)))%5B...%2C%5B0%2C1%2C0%5D%5D%2C%20origin%20%3D%20'lower')%0A%20%20%20%20_axs.quiver(x%2C%20y%2C%20u%2C%20v%2C%20color%3D'white'%2C%20scale%3D0.05%2C%20scale_units%3D'xy')%0A%20%20%20%20_a%20%3D%20x%5Bnp.where(u%20%3D%3D%20u%5B1%2C%207%5D)%5D%20%20%23Adjust%20this%20to%20your%20desired%20x-coordinate%0A%20%20%20%20_b%20%3D%20y%5Bnp.where(u%20%3D%3D%20u%5B1%2C%207%5D)%5D%20%20%23%20Adjust%20this%20to%20your%20desired%20y-coordinate%0A%20%20%20%20_radius%20%3D%2020%0A%20%20%20%20_circle%20%3D%20Circle((_a%2C%20_b)%2C%20_radius%2C%20fill%3DFalse%2C%20color%3D'red'%2C%20linewidth%3D2)%0A%20%20%20%20_axs.add_patch(_circle)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(u)%3A%0A%20%20%20%20print%20('the%20maximum%20and%20minim%20of%20u%20are%20%7B%7D%20and%20%7B%7D'.format(u.max()%2Cu.min()))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(v)%3A%0A%20%20%20%20print%20('the%20maximum%20and%20minim%20of%20v%20are%20%7B%7D%20and%20%7B%7D'.format(v.max()%2Cv.min()))%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%23%20The%20issue%20with%20above%20is%20the%20u%20displacement%20are%20reasonable%20and%20in%20the%20expected%20range.%20However%2C%20the%20v%20have%20wrong%20sign%20and%20also%20a%20wrong%20sense.%20The%20larger%20quiver%20should%20be%20on%20the%20top%20and%20pointing%20downwards.%20Besides%2C%20also%20the%20magnitudes%20are%20fishy%20if%20for%20example%20I%20add%20in%20real%20multigrid%20with%20multiple%20window%20size%20instead%20of%202%20%2C%20the%20maximum%20and%20minim%20of%20v%20are%202.0496276329883063%20and%20-0.028171378284373553%20(for%20u%20not%20so%20drastic%20change)%0A%20%20%20%20%22%22%22)%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%20I%20try%20the%20extended_search_area_piv%20to%20compare%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(im1_t0_fil%2C%20im2_t0_fil%2C%20np%2C%20openpiv%2C%20pyprocess)%3A%0A%20%20%20%20ws%20%3D%2064%20%20%23%20Window%20size%0A%20%20%20%20ol%20%3D%2016%20%20%23%20Overlap%20size%0A%20%20%20%20sas%20%3D%2064%20%20%23%20Search%20area%20size%0A%20%20%20%20dt%20%3D%201%0A%20%20%20%20u_1%2C%20v_1%2C%20sig2noise%20%3D%20pyprocess.extended_search_area_piv(im1_t0_fil%2C%20im2_t0_fil%2C%20window_size%3Dws%2C%20overlap%3Dol%2C%20dt%3Ddt%2C%20search_area_size%3Dsas%2C%20sig2noise_method%3D'peak2peak')%0A%20%20%20%20u_f%2C%20v_f%20%3D%20openpiv.filters.replace_outliers(u_1%2C%20v_1%2C%20flags%3Dnp.zeros((u_1.shape%5B0%5D%2C%20u_1.shape%5B1%5D)).astype('int16')%2C%20method%3D'localmean'%2C%20kernel_size%3D1)%0A%20%20%20%20%23%20Replace%20outliers%20in%20velocity%20fields%0A%20%20%20%20%23%20Get%20coordinates%20for%20plotting%0A%20%20%20%20x_1%2C%20y_1%20%3D%20pyprocess.get_coordinates(im1_t0_fil.shape%2C%20search_area_size%3Dsas%2C%20overlap%3Dol)%0A%20%20%20%20return%20u_1%2C%20u_f%2C%20v_f%2C%20x_1%2C%20y_1%0A%0A%0A%40app.cell%0Adef%20_(u_f)%3A%0A%20%20%20%20print%20('the%20maximum%20and%20minim%20of%20u%20from%20ESA_PIV%20are%20%7B%7D%20and%20%7B%7D'.format(u_f.max()%2Cu_f.min()))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(v_f)%3A%0A%20%20%20%20print%20('the%20maximum%20and%20minim%20of%20v%20from%20ESA_PIV%20are%20%7B%7D%20and%20%7B%7D'.format(v_f.max()%2Cv_f.min()))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Circle%2C%20im1_t0_fil%2C%20np%2C%20plt%2C%20u_1%2C%20u_f%2C%20v_f%2C%20x_1%2C%20y_1)%3A%0A%20%20%20%20_fig%2C%20_axs%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(8%2C%208))%0A%20%20%20%20_axs.imshow(im1_t0_fil)%0A%20%20%20%20_fig.gca().invert_yaxis()%0A%20%20%20%20_axs.quiver(x_1%2C%20y_1%2C%20u_f%2C%20v_f%2C%20color%3D'white'%2C%20scale%3D0.05%2C%20scale_units%3D'xy')%0A%20%20%20%20_a%20%3D%20x_1%5Bnp.where(u_1%20%3D%3D%20u_1%5B1%2C%207%5D)%5D%0A%20%20%20%20_b%20%3D%20y_1%5Bnp.where(u_1%20%3D%3D%20u_1%5B1%2C%207%5D)%5D%0A%20%20%20%20_radius%20%3D%2020%0A%20%20%20%20_circle%20%3D%20Circle((_a%2C%20_b)%2C%20_radius%2C%20fill%3DFalse%2C%20color%3D'red'%2C%20linewidth%3D2)%0A%20%20%20%20_axs.add_patch(_circle)%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%20This%20corresponds%20to%20the%20more%20precise%20measurement%20expected%20from%20simple_multipass.%0A%20%20%20%20However%2C%20If%20I%20do%20certain%20operations%20such%20as%20reversing%20the%20'y'%20output%20and%20taking%20the%20negative%20of%20the%20'v'%20output%2C%20the%20results%20concur%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(im1_t0_fil%2C%20im2_t0_fil%2C%20settings%2C%20windef)%3A%0A%20%20%20%20x_2%2C%20y_2%2C%20u_2%2C%20v_2%2C%20_flags%20%3D%20windef.simple_multipass(im1_t0_fil%2C%20im2_t0_fil%2C%20settings)%0A%20%20%20%20u_2%20%3D%20u_2%20%2F%20settings.dt%0A%20%20%20%20v_2%20%3D%20v_2%20%2F%20settings.dt%0A%20%20%20%20y_2%20%3D%20y_2%5B%3A%3A-1%2C%20%3A%5D%0A%20%20%20%20v_2%20%3D%20-v_2%0A%20%20%20%20return%20u_2%2C%20v_2%2C%20x_2%2C%20y_2%0A%0A%0A%40app.cell%0Adef%20_(Circle%2C%20im1_t0_fil%2C%20np%2C%20plt%2C%20u_2%2C%20v_2%2C%20x_2%2C%20y_2)%3A%0A%20%20%20%20_fig%2C%20_axs%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(8%2C%208))%0A%20%20%20%20_axs.imshow(im1_t0_fil)%0A%20%20%20%20_fig.gca().invert_yaxis()%0A%20%20%20%20_axs.quiver(x_2%2C%20y_2%2C%20u_2%2C%20v_2%2C%20color%3D'white'%2C%20scale%3D0.05%2C%20scale_units%3D'xy')%0A%20%20%20%20_a%20%3D%20x_2%5Bnp.where(u_2%20%3D%3D%20u_2%5B1%2C%207%5D)%5D%0A%20%20%20%20_b%20%3D%20y_2%5Bnp.where(u_2%20%3D%3D%20u_2%5B1%2C%207%5D)%5D%0A%20%20%20%20_radius%20%3D%2020%0A%20%20%20%20_circle%20%3D%20Circle((_a%2C%20_b)%2C%20_radius%2C%20fill%3DFalse%2C%20color%3D'red'%2C%20linewidth%3D2)%0A%20%20%20%20_axs.add_patch(_circle)%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
e0354e16eb91f3bd2523461e5af4cf98