12.2. openwfs.algorithms
- class DualReference(*, feedback, slm, phase_patterns, group_mask, amplitude=1.0, phase_steps=4, iterations=2, optimized_reference=None)[source]
Bases:
objectA generic iterative dual reference WFS algorithm, which can use a custom set of basis functions.
This algorithm is adapted from [Tao2017], with the addition of the ability to use custom basis functions and specify the number of iterations.
In this algorithm, the SLM pixels are divided into two groups: A and B, as indicated by the boolean group_mask argument. The algorithm first keeps the pixels in group B fixed, and displays a sequence on patterns on the pixels of group A. It uses these measurements to construct an optimized wavefront that is displayed on the pixels of group A. This process is then repeated for the pixels of group B, now using the optimized wavefront on group A as reference. Optionally, the process can be repeated for a number of iterations, which each iteration using the current correction pattern as a reference. This makes this algorithm suitable for non-linear feedback, such as multi-photon excitation fluorescence [Osnabrugge2019].
This algorithm assumes a phase-only SLM. Hence, the input modes are defined by passing the corresponding phase patterns (in radians) as input argument.
References
[Tao2017]X. Tao, T. Lam, B. Zhu, et al., “Three-dimensional focusing through scattering media using conjugate adaptive optics with remote focusing (CAORF),” Opt. Express 25, 10368–10383 (2017).
[Osnabrugge2019]Gerwin Osnabrugge, Lyubov V. Amitonova, and Ivo M. Vellekoop. “Blind focusing through strongly scattering media using wavefront shaping with nonlinear feedback”, Optics Express, 27(8):11673–11688, 2019. https://opg.optica.org/oe/ abstract.cfm?uri=oe-27-8-1167
- property amplitude: ndarray | None
- property cobasis: tuple[ndarray, ndarray]
The cobasis corresponding to the given basis.
Note: The cobasis is stored in transposed form, with shape = (mode_count, height, width)
- execute(*, capture_intermediate_results=False, progress_bar=DummyProgressBar())[source]
- Executes the blind focusing dual reference algorithm and compute the SLM transmission matrix.
- capture_intermediate_results: When True, measures the feedback from the optimized wavefront after each iteration.
This can be useful to determine how many iterations are needed to converge to an optimal pattern. This data is stored as the ‘intermediate_results’ field in the results
- progress_bar: Optional progress bar object. Following the convention for tqdm progress bars,
this object should have a total attribute and an update() function.
- Returns:
WFSResult –
- An object containing the computed SLM transmission matrix and related data. The amplitude profile
of each mode is assumed to be 1. If a different amplitude profile is desired, this can be obtained by multiplying that amplitude profile with this transmission matrix.
- property gram: tuple[ndarray, ndarray]
The Gram matrix corresponding to the given basis (i.e. phase pattern and amplitude profile).
- property phase_patterns: tuple[ndarray, ndarray]
- class FourierDualReference(*, feedback, slm, slm_shape=(500, 500), phase_steps=4, k_radius=3.2, k_step=1.0, iterations=2, amplitude=1.0, optimized_reference=None)[source]
Bases:
DualReferenceFourier double reference algorithm, based on Mastiani et al. [1].
Improvements over [1]:
The set of plane waves is taken from a disk in k-space instead of a square.
No overlap between the two halves is needed, instead the final stitching step is done using measurements already in the data set.
When only a single target is optimized, the algorithm can be used in an iterative version to increase SNR during the measurement, similar to [2].
[1]: Bahareh Mastiani, Gerwin Osnabrugge, and Ivo M. Vellekoop, “Wavefront shaping for forward scattering,” Opt. Express 30, 37436-37445 (2022)
[2]: X. Tao, T. Lam, B. Zhu, et al., “Three-dimensional focusing through scattering media using conjugate adaptive optics with remote focusing (CAORF),” Opt. Express 25, 10368–10383 (2017).
- property k_radius: float
The maximum radius of the k-space circle.
- property k_step: float
- class SimpleGenetic(feedback, slm, shape=(500, 500), population_size=30, elite_size=5, generations=100, mutation_probability=0.005, generator=None)[source]
Bases:
objectSimple genetic algorithm for wavefront shaping.
This algorithm is included for illustrative purposes. It is based on the algorithm described in [1] and [2].
The algorithm performs the following steps:
Initialize all wavefronts in the population with random phases
For each generation:
2.1. Determine the feedback signal for each wavefront.
- 2.2. Select the ‘elite_size’ best wavefronts to keep. Replace the rest with new wavefronts.
- 2.2.1 If the elite wavefronts are too similar (> 97% identical elements),
randomly generate the new wavefronts.
- 2.2.2 Otherwise, generate new wavefronts by randomly selecting two elite wavefronts
and mixing them randomly (element-wise). Then perform a mutation that replaces a fraction (mutation_probability) of the elements by a new value.
References
- [^1]: Conkey D B, Brown A N, Caravaca-Aguirre A M and Piestun R ‘Genetic algorithm optimization
for focusing through turbid media in noisy environments’ Opt. Express 20 4840–9 (2012).
- [^2]: Benjamin R Anderson et al. ‘A modular GUI-based program for genetic algorithm-based
feedback-assisted wavefront shaping’, J. Phys. Photonics 6 045008 (2024).
- execute(*, progress_bar=DummyProgressBar())[source]
Executes the algorithm. :type progress_bar: :param progress_bar: Optional tqdm-like progress bar for displaying progress
- class StepwiseSequential(feedback, slm, phase_steps=4, n_x=4, n_y=None)[source]
Bases:
objectClass definition for stepwise sequential algorithm for wavefront shaping, as described by Vellekoop [1] [2].
TODO: enable low-res pre-optimization (as in Vellekoop & Mosk 2007) TODO: modulate segments in pupil (circle) only
[1]: Vellekoop, I. M., & Mosk, A. P. (2007). Focusing coherent light through opaque strongly scattering media. Optics Letters, 32(16), 2309-2311. [2]: Ivo M. Vellekoop, “Feedback-based wavefront shaping,” Opt. Express 23, 12189-12206 (2015)
- execute(progress_bar=DummyProgressBar())[source]
Executes the StepwiseSequential algorithm, computing the transmission matrix of the sample
- Returns:
WFSResult – An object containing the computed transmission matrix and statistics.
- property n_x: int
The number of SLM elements in the x direction.
- property n_y: int
The number of SLM elements in the y direction.
- property phase_steps: int
The number of phase steps used for each segment.
- troubleshoot(algorithm, background_feedback, frame_source, shutter, do_frame_capture=True, do_long_stability_test=False, stability_sleep_time_s=0.5, stability_num_of_frames=500, stability_do_save_frames=False, measure_non_modulated_phase_steps=16)[source]
Run a series of basic checks to find common sources of error in a WFS experiment. Quantifies several types of fidelity reduction.
- Parameters:
measure_non_modulated_phase_steps
algorithm – Wavefront Shaping algorithm object, e.g. StepwiseSequential.
background_feedback (
Detector) – Feedback source that determines average background speckle intensity.frame_source (
Detector) – Source object for reading frames, e.g. Camera.shutter – Device object that can block/unblock light source.
do_frame_capture – Boolean. If False, skip frame capture before and after running the WFS algorithm. Also skips computation of corresponding metrics. Also skips stability test.
do_long_stability_test – Boolean. If False, skip long stability test where many frames are captured over a longer period of time.
stability_sleep_time_s – Float. Sleep time in seconds in between capturing frames for long stability test.
stability_num_of_frames – Integer. Number of frames to take in long stability test.
stability_do_save_frames – Boolean. If True, save all recorded frames.
measure_non_modulated_phase_steps – Integer. Number of phase steps for determining non-modulated light.
- Returns:
trouble – WFSTroubleshootResult object containing troubleshoot information.