<jupyter_start><jupyter_text>1P and 2P resolution estimation<jupyter_code>#import pylab
import matplotlib.pyplot as plt
import numpy as np
#plt.style.use('dark_background')
%pylab inline<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>1P Formulas In the lateral direction:$$ \Delta x,y = \frac{\lambda}{2\cdot \text{NA}}  $$And in the axial direction:$$ \Delta z = \frac{2\cdot \lambda}{\text{NA}^2} $$Third formula:$$ \Delta z_{\text{FWHM}}=\frac{0.64 \cdot \lambda}{n-\sqrt{n^2-\text{NA}^2}}$$<jupyter_code>resolution_sp_xy = @(lambda, NA) lambda./(2.*NA);
resolution_sp_z = @(lambda, NA, n) 2.*lambda.*n./(NA.^2);
resolution_sp_z_FWHM = @(lambda, NA, n) 0.64*lambda./(n-(sqrt(n.^2-NA.^2)));
def resolution_sp_xy(wavelength,NA):
    return np.divide(wavelength,2*NA)

def resolution_sp_z(wavelength,NA,n):
    return np.divide(2*wavelength*n,np.power(NA,2))

def resolution_sp_z_FWHM(wavelength, NA, n):
    return np.divide(0.64*wavelength,n-np.sqrt(np.power(n,2)-np.power(NA,2)))
lambda1 = 0.48
NA = 1.4
print(resolution_sp_xy(lambda1,NA))
print(resolution_sp_z(lambda1,NA,n=1.33))<jupyter_output>0.171428571429
0.651428571429<jupyter_text>Plot: 1P xy and z resolution with NA<jupyter_code>NA = np.linspace(0.05,1.5,1000)
wavelength = 0.45
n = 1.33
fig = plt.figure()

titlestring =  'Single-photon xy and z resolution at a wavelength of '+ str(wavelength) + ' µm'

plt.title(titlestring,fontsize=15)
plt.grid(True)

fig.set_size_inches(10, 8)
ax = fig.add_subplot(111)
ax.plot(NA, resolution_sp_xy(wavelength, NA),'r', label='XY resolution')
ax.plot(NA, resolution_sp_z(wavelength, NA,n),'b', label='Z resolution')

legend = ax.legend()
ax.set_yscale('log')
ax.set_xlabel('Numerical aperture (NA)')
ax.set_ylabel('Resolution (µm)')
ax.xaxis.label.set_size(15)
ax.yaxis.label.set_size(15)<jupyter_output><empty_output><jupyter_text>Two-photon resolution formulas In the lateral direction:$$ \Delta x_{FWHM} =  \begin{cases}   2\sqrt{\ln 2}\cdot\frac{0.32\lambda}{\sqrt{2}\text{NA}} & \text{if } \text{NA} < 0,7 \\   2\sqrt{\ln 2}\cdot\frac{0.325\lambda}{\sqrt{2}\text{NA}^{0.91}} & \text{if } \text{NA} > 0,7  \end{cases} $$   And in the axial direction: $$ \Delta z_{FWHM} =2\sqrt{\ln 2}\cdot\frac{0.532\cdot \lambda}{\sqrt{2}}\left(\frac{1}{n-\sqrt{n^2-\text{NA}^2}}\right) $$According to Zipfel 2003: Nonlinear magic: multiphoton microscopy in the biosciences [Paper Link](http://dx.doi.org/10.1038/nbt899)<jupyter_code>% Two photon (according to Zipfel 2003)
resolution_tp_xy_lowNA_FWHM = @(lambda, NA) 0.32*lambda./(sqrt(2).*NA)*2*sqrt(log(2));
resolution_tp_xy_highNA_FWHM = @(lambda, NA) 0.325*lambda./(sqrt(2).*NA.^0.91)*2*sqrt(log(2));
resolution_tp_z_FWHM = @(lambda, NA, n) 0.532*lambda/sqrt(2).*(1./(n-sqrt(n.^2-NA.^2)))*2*sqrt(log(2));
def resolution_tp_xy_lowNA_FWHM(wavelength, NA):
    assert np.min(NA) != 0 or np.min(NA) < 0, 'NA has to be different and larger than 0'
    assert np.min(NA) <= 0.7, 'NA has to be lower or equal than 0.7 for this formula to be used.'
    return np.divide(0.32*wavelength,np.sqrt(2)*NA)*2*np.sqrt(np.log(2))

def resolution_tp_xy_highNA_FWHM(wavelength, NA):
    assert np.min(NA) != 0 or np.min(NA) < 0, 'NA has to be different and larger than 0'
    assert np.min(NA) > 0.7, 'NA has to be larger than 0.7 for this formula to be used.'
    return np.divide(0.325*wavelength,np.sqrt(2)*np.power(NA,0.91))*2*np.sqrt(np.log(2))

def resolution_tp_z_FWHM(wavelength, NA, n):
    assert np.max(NA) <= np.min(n), 'The numerical aperture (NA) has to be smaller or equal than the refractive index n'
    return np.divide(0.532*wavelength,np.sqrt(2))*np.divide(1,n-np.sqrt(np.power(n,2)-np.power(NA,2)))

print(resolution_tp_xy_lowNA_FWHM(0.85,0.28))
print(resolution_tp_xy_highNA_FWHM(0.85,1.4))
print(resolution_tp_z_FWHM(0.85,1.3,1.3))
wavelength1 = 0.85
wavelength2 = 1.027
n = 1.33
# defining two NA arrays so I won't have trouble with the necessary min/max values to avoid assert errors to 
# be thrown, especially ensuring that the NA stays below the refractive index n
NA = np.linspace(0.2,n-0.01,100)
NA1 = np.linspace(0.1,0.7,1000)
NA2 = np.linspace(0.7001,n-0.01,1000)
fig = plt.figure()
# change resolution
titlestring =  'Two-photon xy resolution'

plt.title(titlestring,fontsize=15)
plt.grid(True)

fig.set_size_inches(16, 8)
ax = fig.add_subplot(111)

label1 = str(wavelength1) + ' µm'
ax.plot(NA1, resolution_tp_xy_lowNA_FWHM(wavelength1, NA1),'b', label=label1)
ax.plot(NA2, resolution_tp_xy_highNA_FWHM(wavelength1, NA2),'b')

label2 = str(wavelength2) + ' µm'
ax.plot(NA1, resolution_tp_xy_lowNA_FWHM(wavelength2, NA1),'r', label=label2)
ax.plot(NA2, resolution_tp_xy_highNA_FWHM(wavelength2, NA2),'r')

#ax.plot(NA2, resolution_tp_xy_highNA_FWHM(wavelength, NA,n),'b', label='Z resolution')

legend = ax.legend()
#ax.set_yscale('log')
ax.set_xlabel('Numerical aperture (NA)')
ax.set_ylabel('Resolution (µm)')
ax.xaxis.label.set_size(15)
ax.yaxis.label.set_size(15)
fig = plt.figure()
# change resolution
titlestring =  'Two-photon z-resolution'

plt.title(titlestring,fontsize=15)
plt.grid(True)

fig.set_size_inches(16, 8)
ax = fig.add_subplot(111)

label1 = str(wavelength1) + ' µm'
ax.plot(NA, resolution_tp_z_FWHM(wavelength1, NA,n),'b', label=label1)

label2 = str(wavelength2) + ' µm'
ax.plot(NA, resolution_tp_z_FWHM(wavelength2, NA,n),'r', label=label2)

legend = ax.legend()
#ax.set_yscale('log')
ax.set_xlabel('Numerical aperture (NA)')
ax.set_ylabel('Resolution (µm)')
ax.xaxis.label.set_size(15)
ax.yaxis.label.set_size(15)
fig = plt.figure()
# change resolution
titlestring =  'Two-photon z-resolution'

plt.title(titlestring,fontsize=15)
plt.grid(True)

fig.set_size_inches(16, 8)
ax = fig.add_subplot(111)

label1 = str(wavelength1) + ' µm'
ax.plot(NA, resolution_tp_z_FWHM(wavelength1, NA,n),'b', label=label1)

label2 = str(wavelength2) + ' µm'
ax.plot(NA, resolution_tp_z_FWHM(wavelength2, NA,n),'r', label=label2)

legend = ax.legend()
#ax.set_yscale('log')
ax.set_xlabel('Numerical aperture (NA)')
ax.set_ylabel('Resolution (µm)')
ax.xaxis.label.set_size(15)
ax.yaxis.label.set_size(15)
ax.set_ylim(0,5)
ax.set_xlim(0.4,1.4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17531942422772118, 'fraction_numerical': 0.054989487303897785, 'mean_word_length': 5.932735426008969, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '75038', 'license': 'MIT', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2574, 'n_words': 466, 'path': 'microscope-resolution/1P and 2P resolution estimation.ipynb', 'repo_name': 'ffvoigt/optics-notebooks', 'size': '6183'}