Spaces:
Sleeping
Sleeping
Fix numeration
Browse files- app.py +65 -40
- fraunhofer_test.py +53 -61
- utils.py +5 -0
- zern_generator.py +126 -75
app.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from fraunhofer_test import generate_diff
|
| 4 |
-
|
|
|
|
| 5 |
css = """
|
| 6 |
.container {
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
}
|
| 9 |
"""
|
| 10 |
|
|
@@ -22,6 +27,9 @@ def get_zern_from_file(file):
|
|
| 22 |
return tuple(coeffs)
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
theme = gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)
|
| 26 |
with gr.Blocks(theme=theme, css=css) as demo:
|
| 27 |
with gr.Row():
|
|
@@ -29,55 +37,72 @@ with gr.Blocks(theme=theme, css=css) as demo:
|
|
| 29 |
|
| 30 |
with gr.Column(scale=1):
|
| 31 |
# file_inp = gr.File(label="Загрузка коэффициентов из файла", height="2em")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# file_inp.upload(get_zern_from_file, inputs=[file_inp], outputs=[*sliders_container.children])
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
@gr.render(inputs=[num_zernikes], triggers=[num_zernikes.change])
|
| 42 |
-
def render_count(count):
|
| 43 |
-
global zerns
|
| 44 |
-
zerns = {}
|
| 45 |
-
sliders_container.children.clear()
|
| 46 |
-
count = min(count, 100)
|
| 47 |
-
for i in range(count):
|
| 48 |
-
sld = gr.Slider(key=i, label=f"Zernike {i}", minimum=-100, interactive=True,
|
| 49 |
-
step=0.01, value=0)
|
| 50 |
-
|
| 51 |
-
sld.change(update_zerns, inputs=[gr.Number(value=i, visible=False), sld])
|
| 52 |
-
sliders_container.add_child(sld)
|
| 53 |
-
|
| 54 |
-
return gr.update()
|
| 55 |
-
|
| 56 |
with gr.Column(scale=5):
|
| 57 |
-
with gr.Tab(label="
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
with gr.Tab(label="Batch"):
|
| 62 |
-
plot_batch = gr.Plot(label="Тестовые экраны (NOLL scheme)")
|
| 63 |
-
with gr.Tab(label="Diff"):
|
| 64 |
-
plot_diff = gr.Plot(label="Разница в фокусе")
|
| 65 |
-
|
| 66 |
-
with gr.Row():
|
| 67 |
btn = gr.Button("Рассчитать")
|
|
|
|
|
|
|
| 68 |
btn_batch = gr.Button("Рассчитать тестовые экраны")
|
|
|
|
|
|
|
| 69 |
btn_diff = gr.Button("Рассчитать разницу в фокусе")
|
| 70 |
|
| 71 |
-
|
| 72 |
def on_button_click(resolution):
|
| 73 |
-
return generate_zern_wavefront_fig(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def on_diff_button_click(defocus_amount):
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
btn.click(on_button_click, inputs=[resolution_num], outputs=[plot_2d, plot_3d])
|
| 80 |
-
btn_batch.click(generate_batch, inputs=[resolution_num], outputs=[plot_batch])
|
|
|
|
| 81 |
btn_diff.click(on_diff_button_click, inputs=[defocus_amt], outputs=[plot_diff])
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
#from fraunhofer_test import generate_diff
|
| 4 |
+
from zern_generator import Zernike
|
| 5 |
+
from fraunhofer_test import Fraunhofer
|
| 6 |
css = """
|
| 7 |
.container {
|
| 8 |
+
# height: 50vh;
|
| 9 |
+
# width: 100%;
|
| 10 |
+
# overflow-x: auto !important;
|
| 11 |
+
# overflow-y: auto !important;
|
| 12 |
+
# scrollbar-width: thin !important;
|
| 13 |
}
|
| 14 |
"""
|
| 15 |
|
|
|
|
| 27 |
return tuple(coeffs)
|
| 28 |
|
| 29 |
|
| 30 |
+
zernike = Zernike()
|
| 31 |
+
fraunhofer = Fraunhofer(zernike)
|
| 32 |
+
|
| 33 |
theme = gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.pink)
|
| 34 |
with gr.Blocks(theme=theme, css=css) as demo:
|
| 35 |
with gr.Row():
|
|
|
|
| 37 |
|
| 38 |
with gr.Column(scale=1):
|
| 39 |
# file_inp = gr.File(label="Загрузка коэффициентов из файла", height="2em")
|
| 40 |
+
with gr.Tab(label="Генератор"):
|
| 41 |
+
|
| 42 |
+
defocus_amt = gr.Number(label="Величина дефокуса", value=2, interactive=True)
|
| 43 |
+
num_zernikes = gr.Number(label="Количество коэффициентов Цернике", value=2, interactive=True)
|
| 44 |
+
sliders_container = gr.Column(elem_classes=["container"])
|
| 45 |
+
|
| 46 |
+
@gr.render(inputs=[num_zernikes], triggers=[num_zernikes.change])
|
| 47 |
+
def render_count(count):
|
| 48 |
+
global zerns
|
| 49 |
+
zerns = {}
|
| 50 |
+
sliders_container.children.clear()
|
| 51 |
+
count = min(count, 100)
|
| 52 |
+
for i in range(count):
|
| 53 |
+
term_name = f" - {zernike.term[i]}" if i < len(zernike.term) else ""
|
| 54 |
+
sld = gr.Slider(key=i, label=f"Zernike {i+1}{term_name}", minimum=-100, interactive=True,
|
| 55 |
+
step=0.01, value=0)
|
| 56 |
+
|
| 57 |
+
sld.change(update_zerns, inputs=[gr.Number(value=i, visible=False), sld])
|
| 58 |
+
sliders_container.add_child(sld)
|
| 59 |
+
|
| 60 |
+
return gr.update()
|
| 61 |
+
|
| 62 |
+
with gr.Tab(label="Настройки"):
|
| 63 |
+
resolution_num = gr.Number(value=256, label="Разрешение")
|
| 64 |
|
| 65 |
# file_inp.upload(get_zern_from_file, inputs=[file_inp], outputs=[*sliders_container.children])
|
| 66 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Column(scale=5):
|
| 68 |
+
with gr.Tab(label="Экран"):
|
| 69 |
+
with gr.Row():
|
| 70 |
+
plot_2d = gr.Plot()
|
| 71 |
+
plot_3d = gr.Plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
btn = gr.Button("Рассчитать")
|
| 73 |
+
with gr.Tab(label="Тестовые экраны"):
|
| 74 |
+
plot_batch = gr.Plot(label="Тестовые экраны (ANSI scheme)")
|
| 75 |
btn_batch = gr.Button("Рассчитать тестовые экраны")
|
| 76 |
+
with gr.Tab(label="Разница у фокуса"):
|
| 77 |
+
plot_diff = gr.Plot(label="Разница в фокусе")
|
| 78 |
btn_diff = gr.Button("Рассчитать разницу в фокусе")
|
| 79 |
|
|
|
|
| 80 |
def on_button_click(resolution):
|
| 81 |
+
return zernike.generate_zern_wavefront_fig(*[zerns[key] for key in sorted(zerns)])
|
| 82 |
+
|
| 83 |
+
def on_settings_click(resolution):
|
| 84 |
+
print(resolution)
|
| 85 |
+
zernike.set_image_params(npix=resolution)
|
| 86 |
|
| 87 |
def on_diff_button_click(defocus_amount):
|
| 88 |
+
pl, mn, diff = fraunhofer.generate_diff(defocus_amount, *[zerns[key] for key in sorted(zerns)])
|
| 89 |
+
|
| 90 |
+
fig, axs = plt.subplots(1, 3, figsize = (20, 10))
|
| 91 |
+
axs[0].imshow(pl, cmap='grey')
|
| 92 |
+
axs[1].imshow(mn, cmap='grey')
|
| 93 |
+
axs[2].imshow(diff, cmap='grey')
|
| 94 |
+
axs[0].axis('off')
|
| 95 |
+
axs[0].set_title(f"+{defocus_amount}")
|
| 96 |
+
axs[1].axis('off')
|
| 97 |
+
axs[1].set_title(f"-{defocus_amount}")
|
| 98 |
+
axs[2].axis('off')
|
| 99 |
+
axs[2].set_title(f"Difference")
|
| 100 |
+
return fig
|
| 101 |
|
| 102 |
|
| 103 |
btn.click(on_button_click, inputs=[resolution_num], outputs=[plot_2d, plot_3d])
|
| 104 |
+
btn_batch.click(zernike.generate_batch, inputs=[resolution_num], outputs=[plot_batch])
|
| 105 |
+
resolution_num.change(on_settings_click, inputs=[resolution_num])
|
| 106 |
btn_diff.click(on_diff_button_click, inputs=[defocus_amt], outputs=[plot_diff])
|
| 107 |
|
| 108 |
demo.launch()
|
fraunhofer_test.py
CHANGED
|
@@ -2,73 +2,65 @@ import matplotlib.pyplot as plt
|
|
| 2 |
import numpy as np
|
| 3 |
import math
|
| 4 |
from scipy.fft import fft2, fftshift, ifftshift
|
| 5 |
-
from zern_generator import
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
span = (-(10 ** -2), 10 ** -2)
|
| 11 |
|
| 12 |
-
z = 100 * 10 ** -3 # Расстояние
|
| 13 |
-
wavelength = 500 * 10 ** -9 # Длина волны
|
| 14 |
-
pixel_scale = 250 * 10 ** -4 # Размер пикселя
|
| 15 |
-
npix = 2**9 # Размер сетки
|
| 16 |
-
k = 2*math.pi/wavelength
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def circle(xv, yv, radius, resolution):
|
| 38 |
-
dist_from_center = np.sqrt(xv**2 + yv**2)
|
| 39 |
-
mask = dist_from_center <= radius
|
| 40 |
-
wf = np.zeros((resolution, resolution), dtype=np.complex64)
|
| 41 |
-
wf[mask] = 1 + 0j
|
| 42 |
-
return wf
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def generate_diff(defocus_amount, *zernikes):
|
| 48 |
-
zernikes = list(zernikes)
|
| 49 |
-
if len(zernikes) < 5:
|
| 50 |
-
zernikes += [0] * (5-len(zernikes))
|
| 51 |
-
fig, axs = plt.subplots(1, 3, figsize = (30, 15))
|
| 52 |
|
| 53 |
-
ddx = np.linspace(*span, npix)
|
| 54 |
-
ddy = np.linspace(*span, npix)
|
| 55 |
-
xv, yv = np.meshgrid(ddx, ddy)
|
| 56 |
-
wf = circle(xv, yv, radius, npix)
|
| 57 |
-
zernikes[4] += defocus_amount
|
| 58 |
-
f_plus = generate_zernike_screen(xv, yv, zernikes, norm_radius=radius)
|
| 59 |
-
zernikes[4] -= 2*defocus_amount
|
| 60 |
-
f_minus = generate_zernike_screen(xv, yv, zernikes, norm_radius=radius)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
axs[0].imshow(focal_image_plus, cmap='grey', extent=(*span, *span))
|
| 67 |
-
axs[1].imshow(focal_image_minus, cmap='grey', extent=(*span, *span))
|
| 68 |
-
axs[2].imshow((focal_image_plus-focal_image_minus), cmap='grey', extent=(*span, *span))
|
| 69 |
-
axs[0].axis('off')
|
| 70 |
-
axs[1].axis('off')
|
| 71 |
-
axs[2].axis('off')
|
| 72 |
-
fig.show()
|
| 73 |
-
return fig
|
| 74 |
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import math
|
| 4 |
from scipy.fft import fft2, fftshift, ifftshift
|
| 5 |
+
from zern_generator import Zernike
|
| 6 |
|
| 7 |
|
| 8 |
+
def denoise(screen: np.ndarray):
|
| 9 |
+
return np.where(np.abs(screen) < 1e-4, 0, screen)
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
class Fraunhofer:
|
| 13 |
+
def __init__(self, zernike: Zernike):
|
| 14 |
+
self.focal_distance = 100 * 10 ** -3
|
| 15 |
+
self.distance = 100 * 10 ** -3
|
| 16 |
+
self.radius = 5 * 10 ** -3
|
| 17 |
+
self.span = (-(10 ** -2), 10 ** -2)
|
| 18 |
+
self.wavelength = 500 * 10 ** -9
|
| 19 |
+
self.k = 2*math.pi/self.wavelength
|
| 20 |
+
|
| 21 |
+
self.zernike = zernike
|
| 22 |
+
|
| 23 |
+
def _denoise(screen: np.ndarray):
|
| 24 |
+
return np.where(np.abs(screen) < 1e-4, 0, screen)
|
| 25 |
+
|
| 26 |
+
def get_focal_point(self,input_screen):
|
| 27 |
+
phase_mul = ((np.exp(1j*self.k*self.distance) * np.exp(1j*self.k*(self.zernike.xv**2 + self.zernike.yv**2)/(2*self.distance)))/(1j*self.wavelength*self.distance))
|
| 28 |
+
U_fraun = fft2(ifftshift(input_screen), overwrite_x=True)
|
| 29 |
+
U_fraun = fftshift(U_fraun * phase_mul)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
I_fraun = np.abs(U_fraun)**2
|
| 33 |
+
return I_fraun / np.max(I_fraun)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def generate_diff(self, defocus_amount, *zernikes):
|
| 37 |
+
zernikes = list(zernikes)
|
| 38 |
+
if len(zernikes) < 5:
|
| 39 |
+
zernikes += [0] * (5-len(zernikes))
|
| 40 |
+
fig, axs = plt.subplots(1, 3, figsize = (20, 15))
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
wf = self.zernike.generate_zern_screen([1], self.radius)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
f_plus = self.zernike.generate_zern_screen(self.zernike.add_defocus(defocus_amount,zernikes), norm_radius=self.radius)
|
| 47 |
+
f_minus = self.zernike.generate_zern_screen(self.zernike.add_defocus(-defocus_amount,zernikes), norm_radius=self.radius)
|
| 48 |
+
|
| 49 |
+
focal_image_plus = denoise(self.get_focal_point(wf*np.exp(f_plus * 1j)))
|
| 50 |
+
focal_image_minus = denoise(self.get_focal_point(wf*np.exp(f_minus * 1j)))
|
| 51 |
+
|
| 52 |
+
return focal_image_plus, focal_image_minus, focal_image_plus-focal_image_minus
|
| 53 |
+
axs[0].imshow(focal_image_plus, cmap='grey')
|
| 54 |
+
axs[1].imshow(focal_image_minus, cmap='grey')
|
| 55 |
+
diff = (focal_image_plus-focal_image_minus)
|
| 56 |
+
#diff /= np.max(diff)
|
| 57 |
|
| 58 |
+
axs[2].imshow(diff - np.min(diff), cmap='grey')
|
| 59 |
+
axs[0].axis('off')
|
| 60 |
+
axs[1].axis('off')
|
| 61 |
+
axs[2].axis('off')
|
| 62 |
+
fig.show()
|
| 63 |
+
return fig
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
|
utils.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
def circle_mask(xv, yv, radius):
|
| 4 |
+
dist_from_center = np.sqrt(xv**2 + yv**2)
|
| 5 |
+
return dist_from_center <= radius
|
zern_generator.py
CHANGED
|
@@ -3,84 +3,135 @@ import math
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
| 5 |
from zernike import RZern
|
|
|
|
| 6 |
|
| 7 |
SAMPLE_SIZE = 256
|
| 8 |
span = (-1, 1)
|
| 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 |
third = 0
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import numpy as np
|
| 5 |
from zernike import RZern
|
| 6 |
+
import utils
|
| 7 |
|
| 8 |
SAMPLE_SIZE = 256
|
| 9 |
span = (-1, 1)
|
| 10 |
|
| 11 |
|
| 12 |
+
|
| 13 |
+
class Zernike:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.set_image_params()
|
| 16 |
+
self.term = [
|
| 17 |
+
"PISTON",
|
| 18 |
+
"V-TILT",
|
| 19 |
+
"H-TILT",
|
| 20 |
+
"O-PR-ASTIGMATISM",
|
| 21 |
+
"DEFOCUS",
|
| 22 |
+
"V-PR-ASTIGMATISM",
|
| 23 |
+
"V-TREFOIL",
|
| 24 |
+
"V-PR-COMA",
|
| 25 |
+
"H-PR-COMA",
|
| 26 |
+
"O-TREFOIL",
|
| 27 |
+
"O-QUADRAFOIL",
|
| 28 |
+
"O-SC-ASTIGMATISM",
|
| 29 |
+
"P-SPHERICAL",
|
| 30 |
+
"V-SC-ASTIGMATISM",
|
| 31 |
+
"V-QUADRAFOIL",
|
| 32 |
+
|
| 33 |
+
# "V-SC-COMA",
|
| 34 |
+
# "H-SC-COMA",
|
| 35 |
+
# "V-SC-TREFOIL",
|
| 36 |
+
# "O-SC-TREFOIL",
|
| 37 |
+
# "O-PENTAFOIL",
|
| 38 |
+
# "V-PENTAFOIL"
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
def set_image_params(self, span = (-(10 ** -2), 10 ** -2), npix = 2**9):
|
| 42 |
+
self.span = span
|
| 43 |
+
self.npix = npix
|
| 44 |
+
ddx = np.linspace(*self.span, self.npix)
|
| 45 |
+
ddy = np.linspace(*self.span, self.npix)
|
| 46 |
+
self.xv, self.yv = np.meshgrid(ddx, ddy)
|
| 47 |
+
|
| 48 |
+
def generate_zern_screen(self, zerns, norm_radius = 1):
|
| 49 |
+
zerns = self._osa_to_noll_list(zerns)
|
| 50 |
+
num_zerns = len(zerns)
|
| 51 |
+
mask = utils.circle_mask(self.xv, self.yv, norm_radius)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
degree = math.ceil((-3 + math.sqrt(8 * num_zerns + 1)) / 2)
|
| 55 |
+
zerns = list(zerns) + [0] * int((((degree + 1) * (degree + 2) / 2) - len(zerns)))
|
| 56 |
+
cart = RZern(degree)
|
| 57 |
+
cart.make_cart_grid(self.xv/norm_radius, self.yv/norm_radius)
|
| 58 |
+
screen = np.array(cart.eval_grid(np.array(zerns), matrix=True))
|
| 59 |
+
screen[mask == 0] = 0
|
| 60 |
+
screen[np.isnan(screen)] = 0
|
| 61 |
+
return screen
|
| 62 |
+
|
| 63 |
+
def generate_zern_wavefront_fig(self, *zerns):
|
| 64 |
+
Phi = self.generate_zern_screen(zerns)
|
| 65 |
+
# Phi[np.isnan(Phi)] = 0
|
| 66 |
+
fig_2d, ax_2d = plt.subplots()
|
| 67 |
+
fig_3d, ax_3d = plt.subplots(subplot_kw={"projection": "3d"})
|
| 68 |
+
a = ax_2d.imshow(Phi, extent=(*span, *span), cmap='gray')
|
| 69 |
+
ax_3d.plot_surface(self.xv, self.yv, Phi)
|
| 70 |
+
fig_2d.colorbar(a)
|
| 71 |
+
plt.show()
|
| 72 |
+
return fig_2d, fig_3d
|
| 73 |
+
|
| 74 |
+
def add_defocus(self, defocus_value, zernikes):
|
| 75 |
+
zernikes = list(zernikes)
|
| 76 |
+
if len(zernikes) < 5:
|
| 77 |
+
zernikes += [0] * (5-len(zernikes))
|
| 78 |
+
zernikes[4] += defocus_value
|
| 79 |
+
return zernikes
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def generate_batch(self, resolution):
|
| 83 |
+
#term = ["PISTON","V-TILT","H-TILT","DEFOCUS","O-PR-ASTIGMATISM","V-PR-ASTIGMATISM","V-PR-COMA","H-PR-COMA","V-TREFOIL","O-TREFOIL","P-SPHERICAL","O-SC-ASTIGMATISM","V-SC-ASTIGMATISM","V-QUADRAFOIL","O-QUADRAFOIL","V-SC-COMA","H-SC-COMA","V-SC-TREFOIL","O-SC-TREFOIL","O-PENTAFOIL","V-PENTAFOIL"]
|
| 84 |
+
fig = plt.figure()
|
| 85 |
+
fig.set_figwidth(30)
|
| 86 |
+
fig.set_figheight(20)
|
| 87 |
+
zern_index = 1
|
| 88 |
+
for i in range(5):
|
| 89 |
+
for j in range(6):
|
| 90 |
+
if j > i:
|
| 91 |
+
continue
|
| 92 |
+
|
| 93 |
+
zern = [0] * (zern_index - 1) + [1]
|
| 94 |
+
ax = plt.subplot2grid((5, 6), (i, j))
|
| 95 |
+
Phi = self.generate_zern_screen(zern, norm_radius=self.span[1]/2)
|
| 96 |
+
ax.set_title(f" \n Zernike {zern_index-1} — {self.term[zern_index-1]} ")
|
| 97 |
+
ax.imshow(Phi, extent=(*span, *span), cmap='gray')
|
| 98 |
+
|
| 99 |
+
zern_index += 1
|
| 100 |
+
plt.show()
|
| 101 |
+
return fig
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def _osa_to_mn(self, idx):
|
| 105 |
+
n = math.ceil((-3 + math.sqrt((9 + 8 * idx))) / 2)
|
| 106 |
+
m = 2 * idx - n * (n + 2)
|
| 107 |
+
return m, n
|
| 108 |
+
|
| 109 |
+
def _mn_to_noll(self, m, n):
|
| 110 |
+
first = (n * (n + 1)) // 2
|
| 111 |
+
second = abs(m)
|
| 112 |
third = 0
|
| 113 |
+
# I wanted to simplify the logic but it seems to work like a colander
|
| 114 |
+
if m > 0 and n % 4 in [0, 1]:
|
| 115 |
+
third = 0
|
| 116 |
+
elif m < 0 and n % 4 in [2, 3]:
|
| 117 |
+
third = 0
|
| 118 |
+
elif m >= 0 and n % 4 in [2, 3]:
|
| 119 |
+
third = 1
|
| 120 |
+
elif m <= 0 and n % 4 in [0, 1]:
|
| 121 |
+
third = 1
|
| 122 |
+
j = first + second + third
|
| 123 |
+
return j
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def _osa_to_noll(self, idx):
|
| 127 |
+
return self._mn_to_noll(*self._osa_to_mn(idx))
|
| 128 |
+
|
| 129 |
+
def _osa_to_noll_list(self, zernikes):
|
| 130 |
+
indexed_zerns = sorted(map(lambda x: (self._osa_to_noll(x[0]), x[1]), enumerate(zernikes)), key=lambda x: x[0])
|
| 131 |
+
used_indeces = [x for x, _ in indexed_zerns]
|
| 132 |
+
mn, mx = min(used_indeces), max(used_indeces)
|
| 133 |
+
missing_indeces = set(range(mn, mx+1)) - set(used_indeces)
|
| 134 |
+
return [x[1] for x in list(sorted(indexed_zerns + [(x, 0) for x in missing_indeces]))]
|
| 135 |
+
|
| 136 |
+
term = ["PISTON","V-TILT","H-TILT","DEFOCUS","O-PR-ASTIGMATISM","V-PR-ASTIGMATISM","V-PR-COMA","H-PR-COMA","V-TREFOIL","O-TREFOIL","P-SPHERICAL","O-SC-ASTIGMATISM","V-SC-ASTIGMATISM","V-QUADRAFOIL","O-QUADRAFOIL","V-SC-COMA","H-SC-COMA","V-SC-TREFOIL","O-SC-TREFOIL","O-PENTAFOIL","V-PENTAFOIL"]
|
| 137 |
+
print(Zernike()._osa_to_noll_list(list(range(0,15))))
|