Skip to content

Commit

Permalink
Merge branch 'full_vectorial'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/napari_generic_simulator/_widget.py
  • Loading branch information
Meizhu-Liang committed Mar 24, 2023
2 parents 16ebc95 + 86a22f6 commit 5466e13
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 123 deletions.
58 changes: 27 additions & 31 deletions Jupyter_SIMulator.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ To install latest development version :
## Usage

1) Open napari and create the viewer.
2) ![raw](https://github.com/Meizhu-Liang/napari-generic-SIMulator/raw/main/images/hex.avi)


2) Launch the widget in ***Plugin***
Expand Down
16 changes: 11 additions & 5 deletions src/napari_generic_simulator/Illumination.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ def jones_vectors(self, astep):
f_p = self.xp.array(self.polarised_field(phi_S))
self.S[:, i, :] = self.xp.transpose(self.rotation(phi_S, self.theta) @ f_p)

def _ill_test(self, x, y, pstep, astep):
def _ill_obj(self, x, y, pstep, astep):
"""Illumination intensity applied on the object"""
ill = self.xp.sum(self._ill_obj_vec(x, y, pstep, astep), axis=1) # take real part and round to 15 decimals
return ill

def _ill_obj_vec(self, x, y, pstep, astep):
"""Vectorised illumination intensity applied on the object"""
p = [0, pstep * 2 * np.pi / self._phaseStep, pstep * (-4) * np.pi / self._phaseStep]
E = self.xp.zeros((self.npoints, self._n_beams, 3), dtype=self.xp.complex64)
E = self.xp.zeros((self.npoints, self._n_beams, 3), dtype=self.xp.complex64) # exponential terms of field
for i in range(self._n_beams):
phi_E = i * self._beam_a + astep * 2 * np.pi / self._angleStep + self.angle_error[i, astep]
xyz = self.xp.transpose(self.xp.stack([x, y, self.xp.zeros(self.npoints)]))
e = self.xp.exp(-1j * (xyz @ self.rotation(phi_E, self.theta) @ self.xp.array([0, 0, self.k0]) + p[i] +
self.phase_error[i, astep, pstep]))
E[:, i, :] = self.xp.transpose(self.xp.array([e, ] * 3))
F = self.xp.sum(self.S * E, axis=1, dtype=self.xp.complex64)
ill = self.xp.sum(F * self.xp.conjugate(F), axis=1).real # the dot multiplication
F = self.xp.sum(self.S * E, axis=1, dtype=self.xp.complex64) # field of illumination
# to calculate intensity: ill = F @ F_conjugate
ill = (F * self.xp.conjugate(F)).real.round(15) # take real part and round to 15 decimals
return ill


class ConIll(Illumination):
def __init__(self):
self._phaseStep = 3
Expand Down
29 changes: 18 additions & 11 deletions src/napari_generic_simulator/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class Pol(Enum):

class Psf_calc(Enum):
SCALAR = 0
VECTOR_in_development = 1
VEC_FLEXI = 1
VEC_RIGID = 2


class Accel(Enum):
Expand Down Expand Up @@ -250,6 +251,7 @@ def parameters(self):
self.fwhmz = FloatSpinBox(value=3.0, name='spin', label='fwhmz(μm)', min=0.0, max=10.0)
self.random_seed = SpinBox(value=123, name='spin', label='random seed')
self.drift = FloatSpinBox(value=0.0, name='spin', label='drift(nm)', min=0.0, max=1000.0, step=5)
self.defocus = FloatSpinBox(value=0.0, name='spin', label='de-focus(μm)', min=-10.0, max=10, step=5)
self.sph_abb = FloatSpinBox(value=0.0, name='spin', label='spherical(rad)', min=-10.0, max=10, step=0.5)
self.lable = Label(value='aberration')

Expand All @@ -259,7 +261,7 @@ def par_list(self):
self.Psf.value, self.N.value, self.pixel_size.value, self.magnification.value, self.ill_NA.value,
self.det_NA.value, self.n.value, self.ill_wavelength.value, self.det_wavelength.value,
self.zrange.value, self.tpoints.value, self.xdrift.value, self.zdrift.value,
self.fwhmz.value, self.random_seed.value, self.drift.value, self.sph_abb.value]
self.fwhmz.value, self.random_seed.value, self.drift.value, self.defocus.value, self.sph_abb.value]

def set_att(self):
"""Sets attributes in the simulation class. Executed frequently to update the parameters"""
Expand Down Expand Up @@ -299,8 +301,10 @@ def set_att(self):
if self.Acceleration.value == Accel.CUPY:
self.sim.acc = 3

if self.Psf.value == Psf_calc.VECTOR_in_development:
self.sim.psf_calc = 'vector'
if self.Psf.value == Psf_calc.VEC_RIGID:
self.sim.psf_calc = 'vector_rigid'
elif self.Psf.value == Psf_calc.VEC_FLEXI:
self.sim.psf_calc = 'vector_flexible'
elif self.Psf.value == Psf_calc.SCALAR:
self.sim.psf_calc = 'scalar'

Expand All @@ -324,14 +328,16 @@ def set_att(self):
self.sim.fwhmz = self.fwhmz.value
self.sim.drift = self.drift.value
self.sim.random_seed = self.random_seed.value
self.sim.defocus = self.defocus.value
self.sim.sph_abb = self.sph_abb.value
self.used_par_list = [self.SIM_mode.value, self.Polarisation.value, self.Acceleration.value,
self.Psf.value,
self.N.value, self.pixel_size.value, self.magnification.value, self.ill_NA.value,
self.det_NA.value,
self.n.value, self.ill_wavelength.value, self.det_wavelength.value,
self.zrange.value, self.tpoints.value, self.xdrift.value, self.zdrift.value,
self.fwhmz.value, self.random_seed.value, self.drift.value, self.sph_abb.value]
self.fwhmz.value, self.random_seed.value, self.drift.value, self.defocus.value,
self.sph_abb.value]

def start_simulator(self):
"""Starts the raw images generators and create the frequency space"""
Expand Down Expand Up @@ -383,7 +389,8 @@ def show_img(data):
'tpoints': self.tpoints.value, 'xdrift': self.xdrift.value,
'zdrift': self.zdrift.value, 'fwhmz': self.fwhmz.value,
'random seed': self.random_seed.value, 'Brownian': self.drift.value,
'sph_abb': self.sph_abb.value})
'defocus': self.defocus.value, 'sph_abb': self.sph_abb.value
})
current_step = list(self._viewer.dims.current_step)
for dim_idx in [-3, -2, -1]:
current_step[dim_idx] = data.shape[dim_idx] // 2
Expand Down Expand Up @@ -414,7 +421,8 @@ def wrap_widgets(self):
self.pixel_size, self.ill_NA, self.det_NA, self.n,
self.ill_wavelength, self.det_wavelength]),
Container(widgets=[self.magnification, self.zrange, self.tpoints, self.xdrift,
self.zdrift, self.fwhmz, self.random_seed, self.drift, self.sph_abb])], layout='horizontal')
self.zdrift, self.fwhmz, self.random_seed, self.drift, self.defocus,
self.sph_abb])], layout='horizontal')
w_cal = magicgui(self.get_results, call_button='Calculate raw image stack', auto_call=False)

# 'save and print' widgets
Expand Down Expand Up @@ -514,10 +522,9 @@ def on_show_illumination_click():
except Exception as e:
print(str(e))

w_save_and_print = Container(widgets=[Container(widgets=[save_tif_with_tags, print_tif]),
Container(widgets=[show_psf, show_otf, show_illumination])],
layout='horizontal', labels=None)
w_save_and_print = Container(widgets=[save_tif_with_tags, print_tif], layout='horizontal')
w_show = Container(widgets=[show_psf, show_otf, show_illumination], layout='horizontal')
self.messageBox = LineEdit(value='Messages')
self.w = Container(widgets=[w_parameters, magicgui(self.select_layer, call_button='Calculate results'),
w_save_and_print, self.messageBox],
w_save_and_print, w_show, self.messageBox],
labels=None)
Loading

0 comments on commit 5466e13

Please sign in to comment.