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
5 changes: 4 additions & 1 deletion docs/scripts/binary_holograms_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
pg.set_fields(algo, input_field, target_field)
# the order to filter, as given by the algorithm, in um from the center of the intermediate plane
y, x = np.array(algo.order_frequency()) * regions.pixel_sizes_in_um(algo.intermediate_pixel_sizes)
mask = ((y - MASK_HALF_SIZE, y + MASK_HALF_SIZE), (x - MASK_HALF_SIZE, x + MASK_HALF_SIZE))
center_y, center_x = np.array(algo.shape) * regions.pixel_sizes_in_um(algo.intermediate_pixel_sizes) / 2
# in the frame of the extension: the origin is the corner of the field
mask = ((center_y + y - MASK_HALF_SIZE, center_y + y + MASK_HALF_SIZE),
(center_x + x - MASK_HALF_SIZE, center_x + x + MASK_HALF_SIZE))
pg.set_mask(app, ApplyMaskTo.INTERMEDIATE, mask, 'elliptical')
pg.run_algorithm(algo)
output = algo.output_field
Expand Down
12 changes: 7 additions & 5 deletions docs/src/user_guide/algorithms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ shaping for the MRAF algorithm using different initial phase type and mixing par

.. note::
The metrics (NRMSE, Fidelity, Efficiency) are **automatically saved** in the ``.h5beam`` file under
``/RawData/Algorithm/``. See :ref:`h5beam-structure` for details.
``/RawData/Shaping/Final/``. See :ref:`h5beam-structure` for details.

Algorithms provided by other packages
-------------------------------------
Expand Down Expand Up @@ -291,19 +291,21 @@ cells one after the other, editing the settings on the way:
* *Snap*: ``pg.snap`` runs one step of the algorithm without any mask and plots the intermediate plane of 4f setups
(intensity in log scale), to see where the mask has to be placed;
* *Masks*: set ``APPLY_TARGET_MASK`` or ``APPLY_INTERMEDIATE_MASK`` to ``True`` to apply a mask (algorithms such as
*Binary Threshold*, *Parallel Lee* and *Superpixel* need the one of the intermediate plane), its region being given in um around the center of the field as the slices shown by the field loader and the extension, along y then along x, that can be pasted as is (``(slice(-100, 100, None), slice(-50, 50, None))``, as text or not; ``((-100, 100), (-50, 50))`` is accepted too) (``None``: the default region
*Binary Threshold*, *Parallel Lee* and *Superpixel* need the one of the intermediate plane), its region being given in um as the slices shown by the extension, in its frame (the origin is the corner of the field), along y then along x, that can be pasted as is (``(slice(1073, 5365, None), slice(1073, 5365, None))``, as text or not; ``((1073, 5365), (1073, 5365))`` is accepted too) (``None``: the default region
of the extension, centered and covering 2/3 of the effective area of the SLM in the plane, printed to be edited). The cell
draws the outline of the intermediate mask on the plot of the snap, which is updated (not created again): run it
again until the mask is right.

The *Run* cell can save the output of each iteration: ``SAVE_FIELDS`` (``True`` for all the fields, the amplitude and
phase of the output and modulator planes and the intensity of the intermediate one, or the names of some of them such
as ``('modulator/phase',)``) and ``SAVE_METRICS``, both off by default. The format is the one of the continuous saving
of the extension: a group ``/RawData/ShapingXXX`` per run, holding the data enlarged along a ``step`` axis, with the
settings of the run, saved as in the extension: the name of the algorithm, the number of iterations, the initial phase
as ``('modulator/phase',)``) and ``SAVE_METRICS``, both off by default. ``SAVE_FINAL`` saves once, at the end of the run, all the fields and the metrics of the last iteration, as the *Save* action of the extension does, whatever is saved at each iteration. The format is the one of the continuous saving
of the extension: a group ``/RawData/ShapingXXX`` per run, with the settings of the run, holding one sub group for each kind of data: ``Fields`` (the fields saved at each iteration) and ``Metrics`` (the metrics), each enlarged along its own ``step`` navigation axis, ``Final`` (the fields saved once at the end, without navigation axis, as in the extension's *Save*, whose main group is the same ``Shaping`` group, without index) and the ``Input`` and ``Target`` groups (the fields as used by the algorithm, with the settings of their loaders, as the *Input* and *Target* groups of the extension's *Save*, readable with the same function). The sub groups of the data per iteration and ``Final`` are only created if requested. The settings of the run are saved
as in the extension: the name of the algorithm, the number of iterations, the initial phase
and its parameters, the masks (applied or not, type and slices) and the parameters of the algorithm. ``SAVE_FILE`` gives the file (created if needed, the runs being added to it); with ``None``
a file is created in the PyMoDAQ data folder at the first saved run and holds the next ones. Its path is printed (``algo.saved_h5file``).

The script ``scripts/plot_convergence.py`` plots the convergence of the saved metrics (one panel per metric against the iteration) from the ``Metrics`` group of a saved run.

During the run of an iterative algorithm, the index of the iteration and the metrics (fitness and efficiency) are printed after each iteration (``verbose=False`` in ``pg.run_algorithm`` to silence it).

The last cells run the algorithm, plot an overview as in the extension, then the output field and the intermediate
Expand Down
144 changes: 121 additions & 23 deletions docs/src/user_guide/data_management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,102 @@ The ``.h5field`` file has the following hierarchy:

The ``.h5beam`` file has the following hierarchy:

.. code-block:: text

/
├── [Global Attributes] # Metadata (date, version, etc.)
│
└── RawData/ (GROUP)
├── @settings: <binary> # **Global configuration** (XML)
│ # Includes: wavelength, setup_type (2f/4f/no_lens),
│ # SLM settings, default loaders, etc.
│
├── Shaping/ (GROUP) # **One group for the whole session** (no index)
│ ├── @settings: <binary> # Algorithm name, run settings and algorithm settings (XML)
│ │
│ ├── Final/ (GROUP) # Algorithm results (last iteration)
│ │ ├── Data0D/CH00/Data00 # **NRMSE** (Normalized Root Mean Square Error)
│ │ │ Data01 # **Efficiency** (beam shaping efficiency)
│ │ └── Data2D/ (GROUP)
│ │ ├── CH00/ # **Output Field Amplitude** (Axis00, Axis01: coordinates)
│ │ ├── CH01/ # **Output Field Phase**
│ │ ├── CH02/ # **Modulator Amplitude** (sent to SLM)
│ │ └── CH03/ # **Modulator Phase** (sent to SLM)
│ │
│ ├── Input/ (GROUP) # **Input Field** (source beam)
│ │ ├── @settings: <binary> # Loader app and loader settings (XML)
│ │ ├── Axis00, Axis01 # Coordinates (m)
│ │ ├── Data00 (CARRAY) # Amplitude
│ │ └── Data01 (CARRAY) # Phase
│ │
│ ├── Target/ (GROUP) # **Target Field** (desired output), same content as Input
│ │
│ └── SLM/ (GROUP) # **SLM Phases**
│ └── Data2D/ (GROUP) # Phases by channel
│ ├── CH00/ # **Algorithm Phase** (to be applied to SLM), e.g. (540, 960)
│ ├── CH01/ # **Quadratic Phase** (curvature correction)
│ ├── CH02/ # **Linear Phase** (tilt correction)
│ └── CH03/ # **Zernike Phase** (aberration correction)
│
└── Logger (VLARRAY) # Log messages (optional)

.. list-table:: **Key Attributes in .h5field**
:header-rows: 1
:widths: 30 20 50
:class: longtable

* - **Location**
- **Attribute**
- **Description**
* - ``/``
- ``date``, ``time``
- File creation timestamp (format: ``YYYY-MM-DD``, ``HH:MM:SS.microseconds``).
* - ``/``
- ``pymodaq_version``, ``pymodaq_data_version``
- PyMoDAQ and pymodaq_data versions used to create the file.
* - ``/RawData/Field/Axis00``
- ``scaling``
- **Pixel size in meters** (e.g., ``6.494e-06`` for 6.494 µm/pixel).
* - ``/RawData/Field/Axis00``
- ``units``
- Physical unit for the axis (``"m"`` for meters).
* - ``/RawData/Field/Data00``
- ``label``
- **Data type** (``"Amplitude"`` or ``"Phase"``).
* - ``/RawData/Field/Data00``
- ``shape``
- Dimensions of the 2D array (e.g., ``(1024, 1024)``).
* - ``/RawData/Field``
- ``settings``
- **Serialized XML configuration** of the Field Loader (includes loader type, parameters, masking, etc.).

.. note::
- The **amplitude** and **phase** are stored as **separate datasets** (``Data00`` and ``Data01``).
- The **physical coordinates** (in meters) are defined by ``Axis00`` (X) and ``Axis01`` (Y).
- The ``scaling`` attribute of each axis gives the **pixel pitch** in meters.

---

.. _h5beam-format:

.h5beam Format
~~~~~~~~~~~~~~

**Purpose**: Store a **complete beam shaping session**, including:
- Input and Target fields.
- SLM phases (algorithm, quadratic, linear, Zernike).
- Algorithm results (output field, modulator field, metrics).
- Global configuration (wavelength, setup type, etc.).

**Generated by**: Beam Shaping Extension.

.. _h5beam-structure:

**HDF5 Structure**
""""""""""""""""""

The ``.h5beam`` file has the following hierarchy:

.. code-block:: text

/
Expand Down Expand Up @@ -182,43 +278,45 @@ The ``.h5beam`` file has the following hierarchy:
* - **Group**
- **Purpose**
- **Key Data**
* - ``/RawData/Input``
* - ``/RawData/Shaping/Input``
- Source beam field
- Amplitude + Phase (2D arrays)
* - ``/RawData/Target``
* - ``/RawData/Shaping/Target``
- Desired output field
- Amplitude + Phase (2D arrays)
* - ``/RawData/Algorithm/Data0D/CH00``
* - ``/RawData/Shaping/Final/Data0D/CH00``
- **Performance metrics**
- NRMSE (``Data00``), Efficiency (``Data01``)
* - ``/RawData/Algorithm/Data2D/CH00``
* - ``/RawData/Shaping/Final/Data2D/CH00``
- **Output field amplitude**
- 2D array (same size as Input/Target)
* - ``/RawData/Algorithm/Data2D/CH01``
* - ``/RawData/Shaping/Final/Data2D/CH01``
- **Output field phase**
- 2D array (radians)
* - ``/RawData/Algorithm/Data2D/CH02``
* - ``/RawData/Shaping/Final/Data2D/CH02``
- **Modulator amplitude**
- 2D array (sent to SLM)
* - ``/RawData/Algorithm/Data2D/CH03``
* - ``/RawData/Shaping/Final/Data2D/CH03``
- **Modulator phase**
- 2D array (radians)
* - ``/RawData/SLM/Data2D/CH00``
* - ``/RawData/Shaping/SLM/Data2D/CH00``
- **Algorithm phase for SLM**
- 2D array (shape matches SLM resolution, e.g., 540x960)
* - ``/RawData/SLM/Data2D/CH01``
* - ``/RawData/Shaping/SLM/Data2D/CH01``
- **Quadratic phase correction**
- 2D array
* - ``/RawData/SLM/Data2D/CH02``
* - ``/RawData/Shaping/SLM/Data2D/CH02``
- **Linear phase correction**
- 2D array
* - ``/RawData/SLM/Data2D/CH03``
* - ``/RawData/Shaping/SLM/Data2D/CH03``
- **Zernike phase correction**
- 2D array

.. note::
- The **SLM phases** (``/RawData/SLM/Data2D/``) are **downsampled** to match the SLM resolution (e.g., 960x540 for PLUTO, 1024x768 for LC2012).
- The **Algorithm/Data2D** results are at the **same resolution as the Input/Target fields** (e.g., 1024x1024).
- The **SLM phases** (``/RawData/Shaping/SLM/Data2D/``) are **downsampled** to match the SLM resolution (e.g., 960x540 for PLUTO, 1024x768 for LC2012).
- The **Final/Data2D** results are at the **same resolution as the Input/Target fields** (e.g., 1024x1024).
- Files written before this layout (groups ``/RawData/Algorithm``, ``Input``, ``Target`` and ``SLM`` directly under ``/RawData``) are still loaded by the extension.
- Runs of the headless playground (``pg.run_algorithm``) use the same layout, in indexed groups ``/RawData/Shaping000``, ``Shaping001``... with in addition the ``Fields`` and ``Metrics`` groups holding the data saved at each iteration (see :ref:`sec-algorithms-playground`), the SLM group being left out.
- The ``@settings`` attributes contain **serialized XML configurations** (can be deserialized with ``pymodaq_utils``).


Expand All @@ -235,7 +333,7 @@ You can **read `.h5beam` and `.h5field` files programmatically** using `pymodaq_
# Load data from a .h5beam file
with DataLoader("my_file.h5beam") as loader:
# Access SLM phases
phase_data = loader.load_data("/RawData/SLM/Data2D/CH00") # return a DataWithAxes object
phase_data = loader.load_data("/RawData/Shaping/SLM/Data2D/CH00") # return a DataWithAxes object

# Access metadata
print(phase_data.extra_attributes)
Expand Down Expand Up @@ -333,30 +431,30 @@ HDF5 Attributes Reference
* - **Group**
- **Purpose**
- **Key Data**
* - ``/RawData/Algorithm/Data0D/CH00``
* - ``/RawData/Shaping/Final/Data0D/CH00``
- Performance metrics
- NRMSE (``Data00``), Efficiency (``Data01``)
* - ``/RawData/Algorithm/Data2D/CH00``
* - ``/RawData/Shaping/Final/Data2D/CH00``
- Output field amplitude
- 2D array (same size as Input/Target)
* - ``/RawData/Algorithm/Data2D/CH01``
* - ``/RawData/Shaping/Final/Data2D/CH01``
- Output field phase
- 2D array (radians)
* - ``/RawData/Algorithm/Data2D/CH02``
* - ``/RawData/Shaping/Final/Data2D/CH02``
- Modulator amplitude
- 2D array (sent to SLM)
* - ``/RawData/Algorithm/Data2D/CH03``
* - ``/RawData/Shaping/Final/Data2D/CH03``
- Modulator phase
- 2D array (radians)
* - ``/RawData/SLM/Data2D/CH00``
* - ``/RawData/Shaping/SLM/Data2D/CH00``
- Algorithm phase for SLM
- 2D array (SLM resolution, e.g., 540x960)
* - ``/RawData/SLM/Data2D/CH01``
* - ``/RawData/Shaping/SLM/Data2D/CH01``
- Quadratic phase
- 2D array (curvature correction)
* - ``/RawData/SLM/Data2D/CH02``
* - ``/RawData/Shaping/SLM/Data2D/CH02``
- Linear phase
- 2D array (tilt correction)
* - ``/RawData/SLM/Data2D/CH03``
* - ``/RawData/Shaping/SLM/Data2D/CH03``
- Zernike phase
- 2D array (aberration correction)
Loading
Loading