Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS_BEAM_SHAPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ close to the template so that it can be updated); this file is what is specific
| `algorithms/factory.py` | `AlgorithmFactory`, `register_algorithms()` and the entry point group `pymodaq_plugins_beam_shaping.algorithms` for third party algorithms. |
| `algorithms/algorithms/` | The **algorithms**, one module per family (see below). |
| `algorithms/ini_phases/`, `stoppings/`, `loss.py` | Initial phases, stopping criteria and loss functions used by iterative algorithms. |
| `algorithms/compensation.py`, `compensations/` | Compensation of the input beam profile (factory like the initial phases, chosen in the AlgoApp settings); an algorithm sets `INPUT_COMPENSATION` to the `Compensations` (StrEnum, add a member for a new compensation) member of its default compensation (None: none) and calls `compensate_target`. |
| `algorithms/compensation.py`, `compensations/` | Compensation of the input beam profile (factory like the initial phases, chosen in the AlgoApp settings); an algorithm sets `INPUT_COMPENSATION` to the `Compensations` (StrEnum, add a member for a new compensation) member of its default compensation (None: none) and calls `compensate_target`. Input phase (issue #98): modulator field = input field x SLM field, so `set_phase_in_modulator_plane` takes the SLM phase and adds `input_phase`; a back-propagated phase goes through `slm_phase_from_modulator_phase`; algorithms with `ALGOTYPE` not AMPLITUDE encode `target_phase()` (minus the input phase when the `Compensate input phase` checkbox is on). |
| `algorithms/algorithm_app.py` | `AlgoApp`: the widget running an algorithm, owning the masks, the shaper and the fields. |
| `algorithms/utils.py` | Enums (`AlgoStatus`, `AlgoType`, `ApplyMaskTo`, `LensSetup`). |
| `utilities/shaper.py` | `Shaper` and `ModulatorType` (phase, amplitude, both), defined in `pymodaq_plugins_utils`; `get_default_shaper()` reads the config. |
Expand Down
45 changes: 45 additions & 0 deletions docs/src/_images/fields_and_compensations.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 95 additions & 11 deletions docs/src/user_guide/algorithm_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ by encoding the complex field on a checkerboard (:cite:t:`mendoza-yero_encoding_
*Settings*:

* ``Mask block size (pxls)`` (default 5): size of the blocks of the checkerboard.
* The input beam is compensated (see *Input compensation* below): the amplitude of the target is divided by the one of
* The input beam is compensated (see *Compensations of the input beam* below): the amplitude of the target is divided by the one of
the beam before the angles of the checkerboard are computed.
* ``amplitude divider`` (default 1).

Expand Down Expand Up @@ -97,16 +97,16 @@ in the review but not available.
* ``Binarize`` (default checked): the pattern is binarized, as a DMD can only switch its pixels on or off. Unchecked,
the grey levels of the pattern are kept (the grey off-axis hologram, an ideal modulation not feasible on a DMD, to
see what the binarization costs).
* The compensation of the input beam is not a setting of the algorithm: see *Input compensation* below.
* The compensation of the input beam is not a setting of the algorithm: see *Compensations of the input beam* below.
* ``Remove carrier`` (default checked), ``Carrier from`` and ``Carrier factor``: see below.
* ``Order position``: a read-only text, updated with the periods, giving the position of the order to filter, and the
slices of a mask around it that can be copied to the slices of the *Intermediate Masking*.

What is sent to the modulator is the *SLM field* (``slm_field`` of the algorithm, ``slm`` data): the amplitude pattern
(0 and 1 for a DMD, 1 for a phase-only modulator) and the phase (0 for an amplitude-only modulator), without the
corrections that the extension adds when sending. It is not the *modulator field*, the field just after the modulator:
this one is the input beam times the SLM field, and is the one used to compute the output. The fields of the
algorithm hold ``slm/phase`` for every type of modulator and ``slm/amplitude`` when an amplitude is sent.
this one is the input beam times the SLM field (input phase included), and is the one used to compute the output. See
:ref:`sec-algorithm-fields`.

The two first orders of the grating carry opposite phases, and this is physical: the order at ``+ν`` carries the phase
``-φ`` of the target, the one at ``-ν`` (the order (-1, 0) of the review) carries ``+φ``. Select the order that carries
Expand Down Expand Up @@ -247,13 +247,77 @@ Minimize and TorchOptim
trust-ncg, trust-exact or trust-krylov.


Input compensation
------------------
.. _sec-algorithm-fields:

Fields and planes
-----------------

An algorithm works on the fields of the planes of the setup. The modulator multiplies the field of the input beam by
what it is sent: the **modulator field** is the input field times the **SLM field**, so its amplitude is the input
amplitude times the amplitude pattern and **its phase is the input phase plus the SLM phase**. The SLM field never
contains the input phase. The output is always computed from the modulator field, so it shows the effect of the
input beam, phase included, whatever the algorithm does with it.

.. list-table:: **Fields of an algorithm** (full names of the data plotted and saved)
:header-rows: 1
:widths: 25 20 55

* - **Field**
- **Data**
- **Description**
* - Input field
- *Input* group of a saved run
- The beam arriving on the modulator (amplitude and phase), from the input loader.
* - Target field
- *Target* group of a saved run
- What is asked in the output plane (amplitude and phase), from the target loader, normalised to the power of the
input.
* - SLM field
- ``slm/phase``, ``slm/amplitude``
- What is sent to the modulator (``slm_field``): the phase computed by the algorithm (wrapped, quantised, with
crosstalk, including the initial phase) and the amplitude pattern (0 and 1 for a DMD, 1 for a phase-only
modulator, only plotted when an amplitude is sent). Without the input phase and without the corrections that the
extension adds when sending.
* - Modulator field
- ``modulator/amplitude``, ``modulator/phase``
- The field just after the modulator (``modulator_field``): input field times SLM field. Its phase is the input
phase plus the SLM phase (the input phase alone for an amplitude modulator, whose SLM phase is 0).
* - Intermediate field
- ``intermediate`` (intensity)
- 4f setups: the Fourier plane of the first lens, filtered by the intermediate mask (``intermediate_field``).
* - Output field
- ``output/amplitude``, ``output/phase``
- The field in the target plane (``output_field``), propagated from the modulator field.
* - Unreachable region
- ``unreachable``
- Where the amplitude compensation cannot give the target (see below), only when there is one.
* - Metrics
- ``metrics``
- The fitness of the output against the target and the efficiency.

The algorithms see the true input field, phase included. An iterative algorithm that propagates back to the modulator
plane (Gerchberg-Saxton, Projections) gets the phase of the modulator field and removes the input phase to get the SLM
phase (``slm_phase_from_modulator_phase``). The initial phase is an SLM phase: before the first iteration the
modulator field is the input field times the initial phase.

.. figure:: /_images/fields_and_compensations.svg
:alt: fields and compensations

The planes and fields of an algorithm, and the two compensations of the input beam.


Compensations of the input beam
-------------------------------

The group ``Input Compensation`` of the settings of the algorithm app holds two independent corrections. It is shown
for the algorithms that can use at least one of them, and each one is shown only where it applies.

Amplitude
~~~~~~~~~

The algorithms that compute the pattern of the modulator from the target (*Parallel Lee*, *Superpixel* and the
checkerboard *OL2014*) give, without compensation, a target multiplied by the amplitude profile of the input beam. The
group ``Input Compensation`` of the settings of the algorithm app (shown for these algorithms only, like the initial
phase for the algorithms that have one) chooses what is done:
``Compensation`` list chooses what is done:

* ``NoCompensation``: the output is the target times the profile of the beam.
* ``DivideByInput`` (default): the amplitude to encode is the target divided by the input amplitude, scaled so that no
Expand All @@ -271,6 +335,26 @@ extension. It is in the ``unreachable`` data of the algorithm and its fraction o

A compensation is a class of the package ``algorithms/compensations``, registered with
``CompensationFactory.register_compensation()``, with its own ``params``, like an initial phase: another type (for
instance for a 2f setup) is added by writing one more module. An algorithm that uses it sets ``INPUT_COMPENSATION`` to the name of its
default compensation (``'DivideByInput'`` for the algorithms above, ``None`` for an algorithm that does not use one: the
group is then hidden) and calls ``self.compensate_target(amplitude)``.
instance for a 2f setup) is added by writing one more module and adding its name to the ``Compensations`` enum. An
algorithm that uses it sets ``INPUT_COMPENSATION`` to the member of ``Compensations`` of its default compensation
(``Compensations.DivideByInput`` for the algorithms above, ``None`` for an algorithm that does not use one) and calls
``self.compensate_target(amplitude)``.

Phase
~~~~~

``Compensate input phase`` (default checked) removes the input phase from the target phase: the phase encoded is
``target phase - input phase`` (``target_phase()`` of the algorithm), so that the modulator field, and so the output,
has the target phase. Unchecked, the target phase is encoded as given and the output carries the input phase on top
of it.

It is shown for the algorithms that control the output phase, those whose ``ALGOTYPE`` is not ``AMPLITUDE``
(``can_compensate_input_phase``): *Direct*, *Binary Threshold*, *Parallel Lee*, *Superpixel* and *OL2014*. The
algorithms that shape the intensity only (``ALGOTYPE = AMPLITUDE``: Gerchberg-Saxton, Projections, the torch based
ones) have no target phase to correct; they still see the input phase, so that the intensity they compute is the one of
the setup. In a script: ``set_compensate_input_phase(app, False)`` of ``headless_algorithms``.

.. note::

Before this version the input phase was dropped from the modulator field: the results change for any input beam
whose phase is not flat. Loading an input with a flat phase gives the previous results.
44 changes: 42 additions & 2 deletions src/pymodaq_plugins_beam_shaping/algorithms/algo_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
plugin_config = Config()



def wrap_phase(phase: np.ndarray) -> np.ndarray:
""" The phase brought back to [-pi, pi)"""
return (np.asarray(phase) + np.pi) % (2 * np.pi) - np.pi

class AlgoParameterManager(ParameterManager):
settings_name = 'algo_settings'

Expand Down Expand Up @@ -248,6 +253,41 @@ def set_amplitude_in_modulator_plane(self, amplitude: np.ndarray):
phase=self._slm_phase_or_zeros(pattern.shape),
pixel_sizes=self._input_field.pixels_sizes)
self._modulator_field.amplitude = self._input_field.amplitude * pattern
self._modulator_field.phase = wrap_phase(self.input_phase + self._slm_field.phase)

@property
def input_phase(self) -> np.ndarray:
""" The phase of the input beam on the modulator (zeros if the input does not have the shape of the
modulator field)"""
phase = getattr(self._input_field, 'phase', None)
if phase is None or phase.shape != tuple(self._modulator_field.shape):
return np.zeros(self._modulator_field.shape)
return phase

def slm_phase_from_modulator_phase(self, phase: np.ndarray) -> np.ndarray:
""" The SLM phase giving this phase in the modulator plane: the input phase removed (the phase of a field
back-propagated to the modulator plane is the phase of the modulator field, input phase included)"""
return wrap_phase(phase - self.input_phase)

@property
def can_compensate_input_phase(self) -> bool:
""" The algorithm controls the output phase (ALGOTYPE not AMPLITUDE): it can compensate the input phase in
the target phase"""
return self.ALGOTYPE != AlgoType.AMPLITUDE

@property
def compensate_input_phase(self) -> bool:
""" The target phase is corrected for the input phase (Compensate input phase in the app, on by default),
only for the algorithms that can do it"""
return self.can_compensate_input_phase and bool(getattr(self.parent_app, 'compensate_input_phase', True))

def target_phase(self) -> np.ndarray:
""" The phase to encode for the target: the target phase minus the input phase if the input phase is
compensated (the modulator field then has the target phase), the target phase otherwise"""
phase = self._target_field.phase
if self.compensate_input_phase and phase.shape == tuple(self._modulator_field.shape):
return phase - self.input_phase
return phase

def _slm_phase_or_zeros(self, shape) -> np.ndarray:
return self._slm_field.phase.copy() if self._slm_field.phase.shape == tuple(shape) else np.zeros(shape)
Expand Down Expand Up @@ -333,7 +373,7 @@ def set_phase_in_modulator_plane(self, phase: np.ndarray, induced_amplitude: np.
if self.crosstalk.apply:
phase = gaussian_filter(phase, self.crosstalk.value)

self._modulator_field.phase = phase
self._modulator_field.phase = wrap_phase(self.input_phase + phase) # input field times SLM field
self._slm_field = Field('slm', amplitude=self._slm_field.amplitude if
self._slm_field.amplitude.shape == phase.shape else np.ones(phase.shape),
phase=phase, pixel_sizes=self._input_field.pixels_sizes)
Expand Down Expand Up @@ -579,7 +619,7 @@ def back_propagate_amplitude(self, amplitude: np.ndarray):
given amplitude, propagate back to the modulator plane and set the phase obtained as the modulator one"""
corrected = Field(amplitude=amplitude, phase=self._output_field.phase,
pixel_sizes=self._output_field.pixels_sizes)
self.set_phase_in_modulator_plane(self.compute_backward_fft(corrected).phase)
self.set_phase_in_modulator_plane(self.slm_phase_from_modulator_phase(self.compute_backward_fft(corrected).phase))

def value_changed(self, param: Parameter):
if self.RECOMPUTE_ON_CHANGE:
Expand Down
Loading
Loading