forked from danielruttley/slm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zernike_fit.py
155 lines (134 loc) · 5.05 KB
/
zernike_fit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
import pandas as pd
import holograms as hg
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
center = (288,227)
radius = 210
ones = np.ones((512,512))
ones = hg.apertures.circ(ones,center,radius)
num_pixels = np.count_nonzero(ones)
zernike = hg.zernike(0,0,1,center,radius,wrap_phase=False)
correction = hg.misc.load(r"Z:\Tweezer\Code\Python 3.7\slm\phase_correction.png")
shifted_correction = np.where(correction>0.5,correction-1,correction)
cmap='twilight_shifted'
#cmap='coolwarm'
fig,ax1 = plt.subplots(1,1)
fig.set_dpi(300)
pcm = ax1.imshow(zernike,cmap=cmap,interpolation='nearest',vmin=-1,vmax=1)
ax1.axis('off')
fig.tight_layout()
cbar = fig.colorbar(pcm,ax=ax1,ticks=[0,0.5,1],label='phase')
cbar.ax.set_yticklabels(['0','$\pi$',r'$2\pi$'])
plt.show()
fig,ax1 = plt.subplots(1,1)
fig.set_dpi(300)
pcm = ax1.imshow(correction,cmap=cmap,interpolation='nearest',vmin=0,vmax=1)
ax1.axis('off')
fig.tight_layout()
cbar = fig.colorbar(pcm,ax=ax1,ticks=[0,0.5,1],label='phase')
cbar.ax.set_yticklabels(['0','$\pi$','$2\pi$'])
plt.show()
cmap='twilight_r'
fig,ax1 = plt.subplots(1,1)
fig.set_dpi(300)
pcm = ax1.imshow(shifted_correction,cmap=cmap,interpolation='nearest',vmin=-0.5,vmax=0.5)
ax1.axis('off')
fig.tight_layout()
cbar = fig.colorbar(pcm,ax=ax1,ticks=[-0.5,0,0.5],label='phase')
cbar.ax.set_yticklabels(['$-\pi$','0','$\pi$'])
plt.show()
cmap='coolwarm'
fig,ax1 = plt.subplots(1,1)
fig.set_dpi(300)
pcm = ax1.imshow(zernike*shifted_correction,cmap=cmap,interpolation='nearest')
ax1.axis('off')
fig.tight_layout()
cbar = fig.colorbar(pcm,ax=ax1)
plt.show()
#%%
reconstructed = np.zeros_like(shifted_correction)
#shifted_correction = hg.zernike(2,2,0.25,center,radius,wrap_phase=False)
results_df = pd.DataFrame()
plot_strs = []
coeffs = []
fit_coeffs = [0,0,0,0,0.063,-0.048,0,0,0,-0.02,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
std = []
for radial in range(7):
for azimuthal in np.arange(-radial,radial+2,2):
results_row = pd.DataFrame()
results_row.loc[0,'zernike_radial'] = radial
results_row.loc[0,'zernike_azimuthal'] = azimuthal
zernike = hg.zernike(radial,azimuthal,1,center,radius,wrap_phase=False)
contrib = shifted_correction*zernike
coeff = np.sum(shifted_correction*zernike)/np.sum(zernike**2)
reconstructed += hg.zernike(radial,azimuthal,coeff,center,radius,wrap_phase=False)
print(radial,azimuthal,coeff)
plot_strs.append('({},{})'.format(radial,azimuthal))
coeffs.append(coeff)
std.append(np.std(reconstructed-shifted_correction))
results_row.loc[0,'fit_amp'] = coeff
results_df = results_df.append(results_row)
results_df.to_csv(r'Z:\Tweezer\Code\Python 3.7\slm\images\2021\May\25\zernike_measurements\phase_image_fit.csv',index=False)
cmap='twilight_r'
#reconstructed *= np.max(shifted_correction)/np.max(reconstructed)
fig,axs = plt.subplots(1,3)
(ax0,ax1,ax2) = axs
fig.set_dpi(300)
pcm = ax0.imshow(shifted_correction,cmap=cmap,interpolation='nearest',vmin=-0.5,vmax=0.5)
ax0.axis('off')
ax0.set_title('measured')
pcm = ax1.imshow(reconstructed,cmap=cmap,interpolation='nearest',vmin=-0.5,vmax=0.5)
ax1.axis('off')
pcm = ax2.imshow(reconstructed-shifted_correction,cmap=cmap,interpolation='nearest',vmin=-0.5,vmax=0.5)
ax1.set_title(r'fit $n\leq 10$')
ax2.axis('off')
ax2.set_title('fit $-$ measured')
fig.tight_layout()
cbar = fig.colorbar(pcm,ax=axs,ticks=[-0.5,0,0.5],label='phase',shrink=0.6**2)
cbar.ax.set_yticklabels(['$-\pi$','0','$\pi$'])
fig.suptitle('Fitting phase map with Zernike polynomials',y=0.8)
plt.show()
x_pos = [i for i, _ in enumerate(plot_strs)]
plt.bar(x_pos, coeffs)
plt.xlabel("Zernike polynomial (radial,azimuthal) index")
plt.ylabel("coefficient")
plt.title("Zernike polynomial decomposition")
plt.xticks(x_pos, plot_strs)
plt.xticks(rotation=90)
plt.show()
x = np.arange(len(plot_strs)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, coeffs, width, label='phase map decomposition')
rects2 = ax.bar(x + width/2, fit_coeffs, width, label='Zernike fitting')
plt.xlabel("Zernike polynomial (radial,azimuthal) index")
plt.ylabel("coefficient")
plt.title("Zernike polynomial decomposition")
ax.set_xticks(x)
ax.set_xticklabels(plot_strs)
ax.legend()
# ax.bar_label(rects1, padding=3)
# ax.bar_label(rects2, padding=3)
plt.xticks(rotation=90)
fig.tight_layout()
plt.show()
x_pos = [i for i, _ in enumerate(plot_strs)]
plt.scatter(x_pos, std)
plt.xlabel("most recent Zernike polynomial (radial,azimuthal) index")
plt.ylabel("standard deviation of fit $-$ measured")
plt.title("Zernike polynomial error")
plt.xticks(x_pos, plot_strs)
plt.xticks(rotation=90)
plt.show()
# cmap='twilight_r'
# #reconstructed *= np.max(shifted_correction)/np.max(reconstructed)
# fig,ax1 = plt.subplots(1,1)
# fig.set_dpi(300)
# pcm = ax1.imshow(shifted_correction,cmap=cmap,interpolation='nearest')#,vmin=-0.5,vmax=0.5)
# ax1.axis('off')
# fig.tight_layout()
# cbar = fig.colorbar(pcm,ax=ax1)#,ticks=[-0.5,0,0.5],label='phase')
# plt.show()