def _plot_profile(data, pixelsize, title, arg4main={'cmap': 'viridis'}): xxGrid, yyGrid = wpu.grid_coord(data, pixelsize) wpu.plot_profile(xxGrid*1e6, yyGrid*1e6, data[::-1, :], xlabel=r'$x [\mu m]$', ylabel=r'$y [\mu m]$', title=title, xunit='\mu m', yunit='\mu m', arg4main=arg4main)
result = frankotchellappa(dpc_x, dpc_y, reflec_pad=True) result = np.real(result) result *= -1 result -= np.min(result) np.savetxt(fname_dpc_h5[:-3] + 'FC_integration.dat', result) #============================================================================== # %% Plot integrated 2d #============================================================================== wpu.plot_profile(xx[::5, ::5]*1e6, yy[::5, ::5]*1e6, result[::-5, ::-5]) # %% fig = plt.figure(figsize=(10, 7)) ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(xx[::5, ::5]*1e6, yy[::5, ::5], -result[::-5, ::-5], rstride=result.shape[0] // 501 + 1, cstride=result.shape[1] // 501 + 1, cmap='viridis', linewidth=0.1) plt.xlabel(r'$x$ [$\mu m$]') plt.ylabel(r'$y$ [$\mu m$]') plt.colorbar(surf, shrink=.8, aspect=20)
def plot_integration(integrated, pixelsize, titleStr='Title', ctitle=' ', max3d_grid_points=101, plotProfile=True, plot3dFlag=True, saveFigFlag=False, saveFileSuf='graph', **kwarg4surf): ''' TODO: Write Docstring ''' xxGrid, yyGrid = wpu.grid_coord(integrated, pixelsize) factor_x, unit_x = wpu.choose_unit(xxGrid) factor_y, unit_y = wpu.choose_unit(yyGrid) # Plot Integration 2 if plot3dFlag: fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection='3d') rstride = integrated.shape[0] // max3d_grid_points + 1 cstride = integrated.shape[1] // max3d_grid_points + 1 surf = ax.plot_surface(xxGrid * factor_x, yyGrid * factor_y, integrated[::-1, :], rstride=rstride, cstride=cstride, cmap='viridis', linewidth=0.1, **kwarg4surf) ax_lim = np.max([np.abs(xxGrid * factor_x), np.abs(yyGrid * factor_y)]) ax.set_xlim3d(-ax_lim, ax_lim) ax.set_ylim3d(-ax_lim, ax_lim) if 'vmin' in kwarg4surf: ax.set_zlim3d(bottom=kwarg4surf['vmin']) if 'vmax' in kwarg4surf: ax.set_zlim3d(top=kwarg4surf['vmax']) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_y + ' m]$', fontsize=24) plt.title(titleStr, fontsize=24, weight='bold') cbar = plt.colorbar(surf, shrink=.8, aspect=20) cbar.ax.set_title(ctitle, y=1.01) plt.tight_layout(rect=[0, 0, 1, 1]) ax.text2D(0.05, 0.9, 'strides = {}, {}'.format(rstride, cstride), transform=ax.transAxes) if saveFigFlag: ax.view_init(elev=30, azim=60) wpu.save_figs_with_idx(saveFileSuf) ax.view_init(elev=30, azim=-120) wpu.save_figs_with_idx(saveFileSuf) plt.pause(.5) plt.show(block=False) if plotProfile: wpu.plot_profile(xxGrid * factor_x, yyGrid * factor_y, integrated[::-1, :], xlabel=r'$x [' + unit_x + ' m]$', ylabel=r'$y [' + unit_y + ' m]$', title=titleStr, xunit='\mu m', yunit='\mu m', arg4main={ 'cmap': 'viridis', 'lw': 3 }) if saveFigFlag: plt.ioff() plt.figure(figsize=(10, 8)) plt.imshow(integrated[::-1, :], cmap='viridis', extent=wpu.extent_func(integrated, pixelsize) * factor_x, **kwarg4surf) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_x + ' m]$', fontsize=24) plt.title(titleStr, fontsize=18, weight='bold') cbar = plt.colorbar() cbar.ax.set_title(ctitle, y=1.01) wpu.save_figs_with_idx(saveFileSuf) plt.close() plt.ion()
##=========================================================# # %% Plot u1 ##=========================================================# factorX, unitStrX = wpu.choose_unit(X) factorY, unitStrY = wpu.choose_unit(Y) unitStrX = unitStrX + ' m' unitStrY = unitStrY + ' m' # ## U1 wpu.plot_profile(X * factorX, Y * factorY, np.abs(u1_xy**2), r'$x [' + unitStrX + ']$', r'$y [' + unitStrY + ']$', r'Intensity [a.u.]', xo=0.0, yo=0.0, xunit=unitStrX, yunit=unitStrY) plt.show(block=True) ##=========================================================# # %% Propagation ##=========================================================# print('WG: Propagation...') #u2_xy = propTForIR(u1_xy, Lx, Ly, wavelength, zz) #titleStr = str(r'propTForIR, zz=%.3fmm, Intensity [a.u.]' # % (zz*1e3))
# %% DPC dpc_1d = unwrap_phase(dpc_1d) wpu.plot_slide_colorbar(dpc_1d / np.pi / 2.0, title=r'DPC [$\pi rad$]', xlabel=r'x [$\mu m$]', ylabel=r'y [$\mu m$]', extent=wpu.extent_func(dpc_1d, pixelSize) * 1e6) # %% xx, yy = wpu.realcoordmatrix(dpc_1d.shape[1], pixelSize, dpc_1d.shape[0], pixelSize) wpu.plot_profile(xx * 1e3, yy * 1e3, dpc_1d / np.pi / 2.0, xlabel='[mm]', ylabel='[mm]') # %% chi2 plt.figure() hist = plt.hist(chi2[np.where(chi2 < 10 * np.std(chi2))], 100, log=False) plt.title(r'$\chi^2$', fontsize=14, weight='bold') plt.show(block=False) chi2_copy = np.copy(chi2) wpu.plot_slide_colorbar(chi2_copy, title=r'$\chi^2$ sample', xlabel=r'x [$\mu m$ ]',
def plot_residual_parabolic_lens_2d(thickness, pixelsize, fitted, fitParameters, saveFigFlag=False, savePickle=False, str4title='', saveSdfData=False, vlimErrSigma=1, plotProfileFlag=True, plot3dFlag=True, makeAnimation=False): xmatrix, ymatrix = wpu.grid_coord(thickness, pixelsize) errorThickness = thickness - fitted argNotNAN = np.isfinite(errorThickness) factorx, unitx = wpu.choose_unit(xmatrix) factory, unity = wpu.choose_unit(ymatrix) factorz, unitz = wpu.choose_unit(errorThickness[argNotNAN]) ptp = np.ptp(errorThickness[argNotNAN].flatten() * factorz) wpu.print_red('PV: {0:4.3g} '.format(ptp) + unitz[-1] + 'm') sigmaError = np.std(errorThickness[argNotNAN].flatten() * factorz) wpu.print_red('SDV: {0:4.3g} '.format(sigmaError) + unitz[-1] + 'm') str4title += r'Residual, ' + \ r'R $= {:.4g} \mu m$,'.format(fitParameters[0]*1e6) + '\n' + \ r'PV $= {0:.2f}$ '.format(ptp) + '$' + unitz + ' m$, '\ 'SDV $= {0:.2f}$ '.format(sigmaError) + '$' + unitz + ' m$' # Plot Histogram plt.figure(figsize=(7, 8)) plt.hist(errorThickness[argNotNAN] * factorz, 100, color='r', histtype='step') plt.xlabel(r'Residual [$' + unitz + ' m$ ]') plt.title(str4title) if saveFigFlag: wpu.save_figs_with_idx(fname2save, extension='png') plt.show(block=False) # Plot Profiles vlimErr = wpu.mean_plus_n_sigma(errorThickness[argNotNAN] * factorz, vlimErrSigma / 2) cmap4graph = plt.cm.Spectral_r cmap4graph.set_over('m') cmap4graph.set_under('c') if plotProfileFlag: wpu.plot_profile(xmatrix * factorx, ymatrix * factory, errorThickness * factorz, title=str4title, xlabel=r'[$' + unitx + ' m$ ]', ylabel=r'[$' + unity + ' m$ ]', zlabel=r'[$' + unitz + ' m$ ]', arg4main={ 'cmap': 'Spectral_r', 'vmin': -vlimErr, 'vmax': vlimErr, 'extend': 'both' }) if savePickle or saveFigFlag: fig = plt.figure(figsize=(10, 7)) cf = plt.contourf(xmatrix * factorx, ymatrix * factory, errorThickness * factorz, 256, cmap=cmap4graph, extend='both') plt.clim(-vlimErr, vlimErr) plt.contour(cf, levels=cf.levels[::32], colors='gray') plt.xlabel(r'[$' + unitx + ' m$ ]', fontsize=22) plt.ylabel(r'[$' + unity + ' m$ ]', fontsize=22) plt.title(str4title, fontsize=22) cbar = plt.colorbar(cf, shrink=.8, aspect=20) # cbar.set_clim(-vlimErr, vlimErr) cbar.ax.set_title(r'[$' + unitz + ' m$ ]', y=1.01) plt.gca().set_aspect('equal', adjustable='box') plt.grid(color='grey') if saveFigFlag: wpu.save_figs_with_idx(fname2save, extension='png') if savePickle: wpu.save_figs_with_idx_pickle(fig, fname2save) plt.show(block=True) # Plot 3D if plot3dFlag: wpu.print_red('MESSAGE: Ploting 3d in the background') fig = plt.figure(figsize=(10, 7), facecolor="white") ax = fig.gca(projection='3d') plt.tight_layout(pad=2.5) surf = ax.plot_trisurf(xmatrix[argNotNAN].flatten() * factorx, ymatrix[argNotNAN].flatten() * factory, errorThickness[argNotNAN].flatten() * factorz, vmin=-vlimErr, vmax=vlimErr, cmap=cmap4graph, linewidth=0.1, shade=False) ax.view_init(azim=-120, elev=40) plt.xlabel(r'$x$ [$' + unitx + ' m$ ]') plt.ylabel(r'$y$ [$' + unity + ' m$ ]') plt.title(str4title) cbar = plt.colorbar(surf, shrink=.8, aspect=20, extend='both') cbar.ax.set_title(r'[$' + unitz + ' m$ ]', y=1.01) plt.tight_layout() if saveFigFlag: wpu.save_figs_with_idx(fname2save, extension='png') ax.view_init(azim=690, elev=40) wpu.save_figs_with_idx(fname2save, extension='png') if makeAnimation: # plt.show(block=False) plt.pause(1.0) wpu.rocking_3d_figure(ax, wpu.get_unique_filename(fname2save, 'gif'), elevOffset=45, azimOffset=60, elevAmp=0, azimAmpl=-1, dpi=80, npoints=5) plt.pause(1.0) plt.close('all') # plt.show(block=True) if saveSdfData: mask_for_sdf = errorThickness * 0.0 mask_for_sdf[~argNotNAN] = 1.0 errorThickness[~argNotNAN] = 00000000 wpu.save_sdf_file( errorThickness, pixelsize, wpu.get_unique_filename(fname2save + '_residual', 'sdf')) wpu.save_sdf_file( mask_for_sdf, pixelsize, wpu.get_unique_filename(fname2save + '_residual_mask', 'sdf')) return sigmaError / factorz, ptp / factorz
idx4crop = wpu.graphical_roi_idx(thickness_temp * 1e6, verbose=True) thickness = wpu.crop_matrix_at_indexes(thickness, idx4crop) xx = wpu.crop_matrix_at_indexes(xx, idx4crop) yy = wpu.crop_matrix_at_indexes(yy, idx4crop) stride = thickness.shape[0] // 125 if gui_mode: wpu.plot_profile(xx[::stride, ::stride] * 1e6, yy[::stride, ::stride] * 1e6, thickness[::stride, ::stride] * 1e6, xlabel=r'$x$ [$\mu m$ ]', ylabel=r'$y$ [$\mu m$ ]', zlabel=r'$z$ [$\mu m$ ]', arg4main={'cmap': 'Spectral_r'}) # %% Center image radius4centering = np.min(thickness.shape) * np.min(pixelSize) * .75 # thickness = -1*thickness + np.max(thickness) thickness = center_lens_array_max_fit(thickness, pixelSize, radius4centering) wpu.log_this('Array cropped to have the max at the center of the array', preffname=fname2save) # thickness = center_lens_array_max_fit(thickness, pixelSize, radius4centering)
rho_x, rho_y = wpu.reciprocalcoordmatrix(qx.shape[1], qy[1, 0] - qy[0, 0], qy.shape[0], qx[0, 1] - qx[0, 0]) rho_x *= 2 * np.pi rho_y *= 2 * np.pi # %% #plotThis = norm_abs_fftimg # *wpu.nan_mask_threshold(norm_abs_fftimg,1e-2*1j) #plotThis = scipy.ndimage.uniform_filter(norm_abs_fftimg, size=(1,1)) plotThis = scipy.ndimage.uniform_filter(norm_abs_fftimg, size=(1, 1)) plotThis[plotThis >= .01] = 0.0 wpu.plot_profile(rho_x, rho_y, plotThis, arg4main={'vmax': 1e-2}) plt.show(block=True) # %% def create_mask_peaks2D(array2D, threshold=None, order=3): if threshold is not None: mask_threshold = wpu.nan_mask_threshold(array2D, threshold=threshold) else: mask_threshold = array2D * 0.0 + 1.0 idx_x_axis_0, idx_y_axis_0 = scipy.signal.argrelmax(array2D * mask_threshold,
thickness_croped = center_max_2darray(thickness_croped) xVec_croped1 = wpu.realcoordvec(thickness_croped.shape[1], pixelsizeImg) yVec_croped1 = wpu.realcoordvec(thickness_croped.shape[0], pixelsizeImg) xmatrix_croped1, ymatrix_croped1 = np.meshgrid(xVec_croped1, yVec_croped1) # %% lim = 1 wpu.plot_profile(xmatrix_croped1[lim:-lim, lim:-lim] * 1e6, ymatrix_croped1[lim:-lim, lim:-lim] * 1e6, thickness_croped[lim:-lim, lim:-lim] * 1e6, title='Thickness centered [um]', xlabel='[um]', ylabel='[um]', arg4main={'cmap': 'Spectral_r'}) #, xo=0.0, yo=0.0) plt.show(block=True) # %% # fig = plt.figure(figsize=(10, 7)) ax = fig.add_subplot(111, projection='3d') stride = thickness_croped.shape[0] // 100 if stride == 0: stride = 1
ylabel=r'y [$\mu m$]', extent=wpu.extent_func(dpc_1d, pixelSize)*1e6) # %% DPC dpc_1d = unwrap_phase(dpc_1d) wpu.plot_slide_colorbar(dpc_1d/np.pi/2.0, title=r'DPC [$\pi rad$]', xlabel=r'x [$\mu m$]', ylabel=r'y [$\mu m$]', extent=wpu.extent_func(dpc_1d, pixelSize)*1e6) # %% xx, yy = wpu.realcoordmatrix(dpc_1d.shape[1], pixelSize, dpc_1d.shape[0], pixelSize) wpu.plot_profile(xx*1e3, yy*1e3, dpc_1d/np.pi/2.0, xlabel='[mm]', ylabel='[mm]') # %% chi2 plt.figure() hist = plt.hist(chi2[np.where(chi2 < 10*np.std(chi2))], 100, log=False) plt.title(r'$\chi^2$', fontsize=14, weight='bold') plt.show(block=False) chi2_copy = np.copy(chi2) wpu.plot_slide_colorbar(chi2_copy, title=r'$\chi^2$ sample', xlabel=r'x [$\mu m$ ]', ylabel=r'y [$\mu m$ ]', extent=wpu.extent_func(chi2, pixelSize)*1e6)
import numpy as np import wavepy.utils as wpu if len(sys.argv) != 1: fname = sys.argv[1] else: fname = easyqt.get_file_names("Data File to Plot")[0] data = dxchange.read_tiff(fname) data = wpu.crop_graphic_image(data, kargs4graph={'cmap': 'rainbow'})[0] # %% nx, ny = np.shape(data) stride = 1 if nx > 200 or ny > 200: stride = easyqt.get_int( 'Number of points: {} x {}.'.format(nx, ny) + ' Enter value for stride:', 'Enter Value', 2, 1, 101) yyGrid, xxGrid = np.mgrid[0:nx:stride, 0:ny:stride] # %% wpu.plot_profile(xxGrid, yyGrid, data[::stride, ::stride], title=fname)
# %% wpsg.error_integration(dTx*pixelsizeImg, dTy*pixelsizeImg, thickness, [pixelsizeImg, pixelsizeImg], shifthalfpixel=True, plot_flag=True) #============================================================================== # %% Thickness #============================================================================== stride = 1 wpu.plot_profile(xmatrix[::stride, ::stride]*1e6, ymatrix[::stride, ::stride]*1e6, thickness[::stride, ::stride]*1e6, title='Thickness', xlabel='[um]', ylabel='[um]', arg4main={'cmap':'spectral'}) #, xo=0.0, yo=0.0)_1Dparabol_4_fit plt.show(block=True) #============================================================================== # %% Plot #============================================================================== def plotsidebyside(array1, array2, title1='', title2='', maintitle=''): fig = plt.figure(figsize=(14, 5))
def plot_integration(integrated, pixelsize, titleStr='Title', ctitle=' ', max3d_grid_points=101, plotProfile=True, plot3dFlag=True, saveFigFlag=False, saveFileSuf='graph', **kwarg4surf): ''' TODO: Write Docstring ''' xxGrid, yyGrid = wpu.grid_coord(integrated, pixelsize) factor_x, unit_x = wpu.choose_unit(xxGrid) factor_y, unit_y = wpu.choose_unit(yyGrid) # Plot Integration 2 if plot3dFlag: fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection='3d') rstride = integrated.shape[0] // max3d_grid_points + 1 cstride = integrated.shape[1] // max3d_grid_points + 1 surf = ax.plot_surface(xxGrid*factor_x, yyGrid*factor_y, integrated[::-1, :], rstride=rstride, cstride=cstride, cmap='viridis', linewidth=0.1, **kwarg4surf) ax_lim = np.max([np.abs(xxGrid*factor_x), np.abs(yyGrid*factor_y)]) ax.set_xlim3d(-ax_lim, ax_lim) ax.set_ylim3d(-ax_lim, ax_lim) if 'vmin' in kwarg4surf: ax.set_zlim3d(bottom=kwarg4surf['vmin']) if 'vmax' in kwarg4surf: ax.set_zlim3d(top=kwarg4surf['vmax']) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_y + ' m]$', fontsize=24) plt.title(titleStr, fontsize=24, weight='bold') cbar = plt.colorbar(surf, shrink=.8, aspect=20) cbar.ax.set_title(ctitle, y=1.01) plt.tight_layout(rect=[0, 0, 1, 1]) ax.text2D(0.05, 0.9, 'strides = {}, {}'.format(rstride, cstride), transform=ax.transAxes) if saveFigFlag: ax.view_init(elev=30, azim=60) wpu.save_figs_with_idx(saveFileSuf) ax.view_init(elev=30, azim=-120) wpu.save_figs_with_idx(saveFileSuf) plt.pause(.5) plt.show(block=False) if plotProfile: wpu.plot_profile(xxGrid*factor_x, yyGrid*factor_y, integrated[::-1, :], xlabel=r'$x [' + unit_x + ' m]$', ylabel=r'$y [' + unit_y + ' m]$', title=titleStr, xunit='\mu m', yunit='\mu m', arg4main={'cmap': 'viridis', 'lw': 3}) if saveFigFlag: plt.ioff() plt.figure(figsize=(10, 8)) plt.imshow(integrated[::-1, :], cmap='viridis', extent=wpu.extent_func(integrated, pixelsize)*factor_x, **kwarg4surf) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_x + ' m]$', fontsize=24) plt.title(titleStr, fontsize=18, weight='bold') cbar = plt.colorbar() cbar.ax.set_title(ctitle, y=1.01) wpu.save_figs_with_idx(saveFileSuf) plt.close() plt.ion()
def plot_integration(integrated, pixelsize, titleStr='Title', ctitle=' ', saveFigFlag=False, saveFileSuf='graph'): ''' TODO: Write Docstring ''' xxGrid, yyGrid = wpu.grid_coord(integrated, pixelsize) factor_x, unit_x = wpu.choose_unit(xxGrid) factor_y, unit_y = wpu.choose_unit(yyGrid) wpu.plot_profile(xxGrid*factor_x, yyGrid*factor_y, integrated[::-1, :], xlabel=r'$x [' + unit_x + ' m]$', ylabel=r'$y [' + unit_y + ' m]$', title=titleStr, xunit='\mu m', yunit='\mu m', arg4main={'cmap': 'viridis', 'lw': 3}) if saveFigFlag: plt.ioff() plt.figure(figsize=(10, 8)) plt.imshow(integrated[::-1, :], cmap='viridis', extent=wpu.extent_func(integrated, pixelsize)*factor_x) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_x + ' m]$', fontsize=24) plt.title(titleStr, fontsize=18, weight='bold') cbar = plt.colorbar() cbar.ax.set_title(ctitle, y=1.01) wpu.save_figs_with_idx(saveFileSuf) # plt.show(block=False) plt.close(plt.gcf()) plt.ion() # Plot Integration 2 fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection='3d') rstride = integrated.shape[0] // 101 + 1 cstride = integrated.shape[1] // 101 + 1 surf = ax.plot_surface(xxGrid*factor_x, yyGrid*factor_y, integrated[::-1, :], rstride=rstride, cstride=cstride, cmap='viridis', linewidth=0.1) ax_lim = np.max([np.abs(xxGrid*factor_x), np.abs(yyGrid*factor_y)]) ax.set_xlim3d(-ax_lim, ax_lim) ax.set_ylim3d(-ax_lim, ax_lim) plt.xlabel(r'$x [' + unit_x + ' m]$', fontsize=24) plt.ylabel(r'$y [' + unit_y + ' m]$', fontsize=24) plt.title(titleStr, fontsize=24, weight='bold') cbar = plt.colorbar(surf, shrink=.8, aspect=20) cbar.ax.set_title(ctitle, y=1.01) fig.tight_layout() plt.tight_layout() if saveFigFlag: wpu.save_figs_with_idx(saveFileSuf) ax.text2D(0.05, 0.9, 'strides = {}, {}'.format(rstride, cstride), transform=ax.transAxes) plt.show(block=False) return ax