def zernikesurface(self, label = True, zlim=[], matrix = False): """ ------------------------------------------------ zernikesurface(self, label_1 = True): Return a 3D Zernike Polynomials surface figure label_1: default show label ------------------------------------------------ """ theta = __np__.linspace(0, 2*__np__.pi, 100) rho = __np__.linspace(0, 1, 100) [u,r] = __np__.meshgrid(theta,rho) X = r*__cos__(u) Y = r*__sin__(u) Z = __zernikepolar__(self.__coefficients__,r,u) fig = __plt__.figure(figsize=(12, 8), dpi=80) ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha = 0.6) if zlim == []: v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-v*5, v*5) cset = ax.contourf(X, Y, Z, zdir='z', offset=-v*5, cmap=__cm__.RdYlGn) else: ax.set_zlim(zlim[0], zlim[1]) cset = ax.contourf(X, Y, Z, zdir='z', offset=zlim[0], cmap=__cm__.RdYlGn) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) fig.colorbar(surf, shrink=1, aspect=30) p2v = round(__tools__.peak2valley(Z),5) rms1 = round(__tools__.rms(Z),5) label_1 = self.listcoefficient()[0]+"P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1) if label == True: __plt__.title('Zernike Polynomials Surface',fontsize=18) ax.text2D(0.02, 0.1, label_1, transform=ax.transAxes,fontsize=14) else: pass __plt__.show() if matrix == True: return Z else: pass
def zernikesurface(self, label = True, zlim=[], matrix = False): """ ------------------------------------------------ zernikesurface(self, label_1 = True): Return a 3D Zernike Polynomials surface figure label_1: default show label ------------------------------------------------ """ theta = __np__.linspace(0, 2*__np__.pi, 100) rho = __np__.linspace(0, 1, 100) [u,r] = __np__.meshgrid(theta,rho) X = r*__cos__(u) Y = r*__sin__(u) Z = __interferometer__.__zernikepolar__(self.__coefficients__,r,u) fig = __plt__.figure(figsize=(12, 8), dpi=80) ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha = 0.6) if zlim == []: v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-v*5, v*5) cset = ax.contourf(X, Y, Z, zdir='z', offset=-v*5, cmap=__cm__.RdYlGn) else: ax.set_zlim(zlim[0], zlim[1]) cset = ax.contourf(X, Y, Z, zdir='z', offset=zlim[0], cmap=__cm__.RdYlGn) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) fig.colorbar(surf, shrink=1, aspect=30) p2v = round(__tools__.peak2valley(Z),5) rms1 = round(__tools__.rms(Z),5) label_1 = self.listcoefficient()[0]+"P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1) if label == True: __plt__.title('Zernike Polynomials Surface',fontsize=18) ax.text2D(0.02, 0.1, label_1, transform=ax.transAxes,fontsize=14) else: pass __plt__.show() if matrix == True: return Z else: pass
def spherical_surf(l1): R = 1.02 l1 = l1 #surface matrix length theta = __np__.linspace(0, 2*__np__.pi, l1) rho = __np__.linspace(0, 1, l1) [u,r] = __np__.meshgrid(theta,rho) X = r*__cos__(u) Y = r*__sin__(u) Z = __sqrt__(R**2-r**2)-__sqrt__(R**2-1) v_1 = max(abs(Z.max()),abs(Z.min())) noise = (__np__.random.rand(len(Z),len(Z))*2-1)*0.05*v_1 Z = Z+noise fig = __plt__.figure(figsize=(12, 8), dpi=80) ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn,\ linewidth=0, antialiased=False, alpha = 0.6) v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-1, 2) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) cset = ax.contourf(X, Y, Z, zdir='z', offset=-1, cmap=__cm__.RdYlGn) fig.colorbar(surf, shrink=1, aspect=30) __plt__.title('Test Surface: Spherical surface with some noise',fontsize=16) __plt__.show() #Generate test surface matrix from a detector x = __np__.linspace(-1, 1, l1) y = __np__.linspace(-1, 1, l1) [X,Y] = __np__.meshgrid(x,y) Z = __sqrt__(R**2-(X**2+Y**2))-__sqrt__(R**2-1)+noise for i in range(len(Z)): for j in range(len(Z)): if x[i]**2+y[j]**2>1: Z[i][j]=0 return Z
def fitting(Z,n,remain3D=False,remain2D=False,barchart=False,interferogram=False,removepiston=True): """ ------------------------------------------------ fitting(Z,n) Fitting an aberration to several orthonormal Zernike polynomials. Return: n-th Zernike coefficients for a fitting surface aberration Zernike coefficients barchart Remaining aberration Fiting surface plot Input: Z: A surface or aberration matrix measure from inteferometer or something else. n: How many order of Zernike Polynomials you want to fit reamin(default==Flase): show the surface after remove fitting aberrations. removepiston: if remove piston, default = True ------------------------------------------------ """ fitlist = [] l = len(Z) x2 = __np__.linspace(-1, 1, l) y2 = __np__.linspace(-1, 1, l) [X2,Y2] = __np__.meshgrid(x2,y2) r = __np__.sqrt(X2**2 + Y2**2) u = __np__.arctan2(Y2, X2) for i in range(n): C = [0]*i+[1]+[0]*(37-i-1) ZF = __zernikepolar__(C,r,u) for i in range(l): for j in range(l): if x2[i]**2+y2[j]**2>1: ZF[i][j]=0 a = sum(sum(Z*ZF))*2*2/l/l/__np__.pi fitlist.append(round(a,3)) l1 = len(fitlist) fitlist = fitlist+[0]*(37-l1) Z_new = Z - __zernikepolar__(fitlist,r,u) for i in range(l): for j in range(l): if x2[i]**2+y2[j]**2>1: Z_new[i][j]=0 #plot bar chart of zernike if barchart == True: fitlist1 = fitlist[0:n] index = __np__.arange(n) fig = __plt__.figure(figsize=(9, 6), dpi=80) xticklist = [] width = 0.6 for i in index: xticklist.append('Z'+str(i+1)) barfigure = __plt__.bar(index, fitlist1, width,color = '#2E9AFE',edgecolor = '#2E9AFE') __plt__.xticks( index+width/2, xticklist ) __plt__.xlabel('Zernike Polynomials',fontsize=18) __plt__.ylabel('Coefficient',fontsize=18) __plt__.title('Fitting Zernike Polynomials Coefficient',fontsize=18) __plt__.show() else: pass if remain3D == True: fig = __plt__.figure(figsize=(12, 8), dpi=80) ax = fig.gca(projection='3d') surf = ax.plot_surface(X2, Y2, Z_new, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha = 0.6) v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-v, v) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) cset = ax.contourf(X2, Y2, Z_new, zdir='z', offset=-v, cmap=__cm__.RdYlGn) fig.colorbar(surf, shrink=1, aspect=30) __plt__.title('Remaining Aberration',fontsize=18) p2v = round(__tools__.peak2valley(Z_new),5) rms1 = round(__tools__.rms(Z_new),5) label_new = "P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1) ax.text2D(0.02, 0.1,label_new, transform=ax.transAxes) __plt__.show() else: pass if remain2D == True: fig = __plt__.figure(figsize=(9, 6), dpi=80) ax = fig.gca() im = __plt__.pcolormesh(X2, Y2, Z_new, cmap=__cm__.RdYlGn) __plt__.colorbar() __plt__.title('Remaining Aberration',fontsize=18) ax.set_aspect('equal', 'datalim') __plt__.show() else: pass if interferogram == True: zernike_coefficient = Coefficient(fitlist) __interferometer__.twyman_green(zernike_coefficient) else: pass if removepiston == True: fitlist[0] = 0 else: pass C = Coefficient(fitlist) #output zernike Coefficient class __tools__.zernikeprint(fitlist) return fitlist,C
def fitting(Z,n,remain3D=False,remain2D=False,barchart=False,interferogram=False,removepiston=True): """ ------------------------------------------------ fitting(Z,n) Fitting an aberration to several orthonormal Zernike polynomials. Return: n-th Zernike coefficients for a fitting surface aberration Zernike coefficients barchart Remaining aberration Fiting surface plot Input: Z: A surface or aberration matrix measure from inteferometer or something else. n: How many order of Zernike Polynomials you want to fit reamin(default==Flase): show the surface after remove fitting aberrations. removepiston: if remove piston, default = True ------------------------------------------------ """ fitlist = [] l = len(Z) x2 = __np__.linspace(-1, 1, l) y2 = __np__.linspace(-1, 1, l) [X2,Y2] = __np__.meshgrid(x2,y2) r = __np__.sqrt(X2**2 + Y2**2) u = __np__.arctan2(Y2, X2) for i in range(n): C = [0]*i+[1]+[0]*(37-i-1) ZF = __interferometer__.__zernikepolar__(C,r,u) for i in range(l): for j in range(l): if x2[i]**2+y2[j]**2>1: ZF[i][j]=0 a = sum(sum(Z*ZF))*2*2/l/l/__np__.pi fitlist.append(round(a,3)) l1 = len(fitlist) fitlist = fitlist+[0]*(37-l1) Z_new = Z - __interferometer__.__zernikepolar__(fitlist,r,u) for i in range(l): for j in range(l): if x2[i]**2+y2[j]**2>1: Z_new[i][j]=0 #plot bar chart of zernike if barchart == True: fitlist1 = fitlist[0:n] index = __np__.arange(n) fig = __plt__.figure(figsize=(9, 6), dpi=80) xticklist = [] width = 0.6 for i in index: xticklist.append('Z'+str(i+1)) barfigure = __plt__.bar(index, fitlist1, width,color = '#2E9AFE',edgecolor = '#2E9AFE') __plt__.xticks( index+width/2, xticklist ) __plt__.xlabel('Zernike Polynomials',fontsize=18) __plt__.ylabel('Coefficient',fontsize=18) __plt__.title('Fitting Zernike Polynomials Coefficient',fontsize=18) __plt__.show() else: pass if remain3D == True: fig = __plt__.figure(figsize=(12, 8), dpi=80) ax = fig.gca(projection='3d') surf = ax.plot_surface(X2, Y2, Z_new, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha = 0.6) v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-v, v) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) cset = ax.contourf(X2, Y2, Z_new, zdir='z', offset=-v, cmap=__cm__.RdYlGn) fig.colorbar(surf, shrink=1, aspect=30) __plt__.title('Remaining Aberration',fontsize=18) p2v = round(__tools__.peak2valley(Z_new),5) rms1 = round(__tools__.rms(Z_new),5) label_new = "P-V: "+str(p2v)+"\n"+"RMS: "+str(rms1) ax.text2D(0.02, 0.1,label_new, transform=ax.transAxes) __plt__.show() else: pass if remain2D == True: fig = __plt__.figure(figsize=(9, 6), dpi=80) ax = fig.gca() im = __plt__.pcolormesh(X2, Y2, Z_new, cmap=__cm__.RdYlGn) __plt__.colorbar() __plt__.title('Remaining Aberration',fontsize=18) ax.set_aspect('equal', 'datalim') __plt__.show() else: pass if interferogram == True: zernike_coefficient = Coefficient(fitlist) __interferometer__.twyman_green(zernike_coefficient) else: pass if removepiston == True: fitlist[0] = 0 else: pass C = Coefficient(fitlist) #output zernike Coefficient class __tools__.zernikeprint(fitlist) return fitlist,C
def lens_param_to_psf(): import numpy as __np__ from numpy import sqrt as __sqrt__ from numpy import cos as __cos__ from numpy import sin as __sin__ import matplotlib.pyplot as __plt__ from matplotlib import cm as __cm__ from matplotlib.ticker import LinearLocator as __LinearLocator__ from matplotlib.ticker import FormatStrFormatter as __FormatStrFormatter__ from mpl_toolkits.mplot3d import Axes3D from numpy.fft import fftshift as __fftshift__ from numpy.fft import ifftshift as __ifftshift__ from numpy.fft import fft2 as __fft2__ def __apershow__(obj): obj = -abs(obj) __plt__.imshow(obj) __plt__.set_cmap('Greys') __plt__.show() l1 = 100 # Generate test surface matrix from a detector x = __np__.linspace(-1, 1, l1) y = __np__.linspace(-1, 1, l1) [X, Y] = __np__.meshgrid(x, y) r = __sqrt__(X**2 + Y**2) Z = __sqrt__(14) * (8 * X**4 - 8 * X**2 * r**2 + r**4) * (6 * r**2 - 5) for i in range(len(Z)): for j in range(len(Z)): if x[i]**2 + y[j]**2 > 1: Z[i][j] = 0 fig = __plt__.figure(1) ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha=0.6) v = max(abs(Z.max()), abs(Z.min())) ax.set_zlim(-v * 5, v * 5) cset = ax.contourf(X, Y, Z, zdir='z', offset=-v * 5, cmap=__cm__.RdYlGn) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) fig.colorbar(surf, shrink=1, aspect=30) __plt__.show() d = 800 A = __np__.zeros([d, d]) A[d // 2 - 49:d // 2 + 51, d // 2 - 49:d // 2 + 51] = Z __plt__.imshow(A) __plt__.show() abbe = __np__.exp(1j * 2 * __np__.pi * A) for i in range(len(abbe)): for j in range(len(abbe)): if abbe[i][j] == 1: abbe[i][j] = 0 fig = __plt__.figure(2) AP = abs(__fftshift__(__fft2__(__fftshift__(abbe))))**2 AP = AP / AP.max() __plt__.imshow(AP) __plt__.show()
r = __sqrt__(X**2+Y**2) Z = __sqrt__(14)*(8*X**4-8*X**2*r**2+r**4)*(6*r**2-5) for i in range(len(Z)): for j in range(len(Z)): if x[i]**2+y[j]**2>1: Z[i][j]=0 fig = __plt__.figure(1) ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=__cm__.RdYlGn, linewidth=0, antialiased=False, alpha = 0.6) v = max(abs(Z.max()),abs(Z.min())) ax.set_zlim(-v*5, v*5) cset = ax.contourf(X, Y, Z, zdir='z', offset=-v*5, cmap=__cm__.RdYlGn) ax.zaxis.set_major_locator(__LinearLocator__(10)) ax.zaxis.set_major_formatter(__FormatStrFormatter__('%.02f')) fig.colorbar(surf, shrink=1, aspect=30) __plt__.show() d = 400 A = __np__.zeros([d,d]) A[d/2-49:d/2+51,d/2-49:d/2+51] = Z __plt__.imshow(A) __plt__.show() abbe = __np__.exp(1j*2*__np__.pi*A) for i in range(len(abbe)): for j in range(len(abbe)): if abbe[i][j]==1: abbe[i][j]=0