Skip to content

Add MID-XFEL loader, geometry and pipeline integration#111

Draft
Abd-zak wants to merge 10 commits into
clatlan:devfrom
Abd-zak:xfel_io
Draft

Add MID-XFEL loader, geometry and pipeline integration#111
Abd-zak wants to merge 10 commits into
clatlan:devfrom
Abd-zak:xfel_io

Conversation

@Abd-zak

@Abd-zak Abd-zak commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces an initial implementation of support for BCDI datasets acquired at the European XFEL MID instrument.

The implementation follows the existing cdiutils beamline architecture and provides a new XFEL loader together with the required geometry and pipeline integration to enable data loading and preprocessing through the standard cdiutils workflow.


Changes

New XFEL loader

Added:

src/cdiutils/io/xfel.py

providing:

  • detector data loading from DAMNIT variables
  • motor position loading
  • photon energy loading
  • detector calibration parameter loading
  • compatibility with the standard Loader interface

The implementation is currently based on MID proposal p010402 and uses the EXtra-data / DAMNIT ecosystem for accessing processed experimental data.


Loader registration

Registered XFEL support in:

src/cdiutils/io/__init__.py
src/cdiutils/io/loader.py

allowing:

Loader.from_setup("xfel", ...)

and:

from cdiutils.io import XFELLoader

Geometry support

Added XFEL geometry definition in:

src/cdiutils/geometry.py

using the geometry currently employed in the XFEL reciprocal-space mapping workflow.

The geometry includes:

  • sample rotation axes
  • detector rotation axes
  • detector orientation
  • beam direction
  • sample surface normal

allowing direct integration with SpaceConverter and the standard cdiutils preprocessing pipeline.


AGIPD detector support

Added AGIPD detector support in:

src/cdiutils/io/loader.py

including detector recognition and mask generation through the existing cdiutils infrastructure.

Detector calibration parameters are automatically loaded, including:

  • sample-detector distance (SDD)
  • detector center coordinates
  • AGIPD pixel size (200 µm)

NaN handling

The processed detector data provided through DAMNIT may contain NaN values.

To ensure compatibility with the preprocessing pipeline and voxel-centering routines, NaN values are automatically replaced by zero during detector data loading.


Example notebook

Added:

examples/bcdi_pipeline_example_xfel.ipynb

demonstrating:

  • XFEL data loading
  • validation against the existing XFEL analysis workflow
  • integration with the BCDI preprocessing pipeline

Validation

The loader outputs were compared directly against the reference XFEL analysis notebook.

Detector data

Reference shape: (151, 1325, 1196)
Loader shape:    (151, 1325, 1196)

Same shape: True
All close: True
Max abs diff: 0.0

Motor positions

Position same shape: True
Position all close: True
Max angle diff: 0.0

Experimental parameters

The following quantities were verified against the reference workflow:

  • photon energy
  • sample-detector distance (SDD)
  • theta
  • chi
  • phi
  • two-theta

All values were found to be identical.


Current status

The implementation has been validated for:

  • detector data loading
  • motor position loading
  • detector calibration loading
  • geometry initialization
  • integration with the cdiutils preprocessing pipeline

Validation on reconstructed BCDI datasets and full reconstruction workflows is currently ongoing and will be performed on upcoming XFEL experimental data.

@Abd-zak Abd-zak changed the title Add XFEL loader, geometry and pipeline integration Add MID-XFEL loader, geometry and pipeline integration Jun 1, 2026
@Abd-zak Abd-zak self-assigned this Jun 1, 2026
Abd-zak added 9 commits June 1, 2026 12:52
Add European XFEL loading support based on DAMNIT processed variables and EXtra-data aliases.

This updates XFELLoader to:
- load detector images from DAMNIT variables
- reduce pulse-resolved data using mean, sum, or no reduction
- replace NaN/inf detector values with zeros before downstream processing
- load run metadata through EXtra-data RunDirectory and alias files
- read rocking-angle positions from the DAMNIT xarray position coordinate
- load detector distance from the sdd alias
- load photon energy from the energy-kev alias and convert keV to eV
- infer the scanned motor when no explicit angle array is provided
- keep a fallback scan-position detector for train-resolved motor values

The implementation was validated against the MID notebook reference for:
- detector data shape and values
- rocking-angle positions
- detector distance
- photon energy.
The XFEL loader was simplified by removing the dedicated
`damnit_dir_name` configuration and using the provided
`experiment_file_path` directly as the DAMNIT database root.

Changes:
- remove `damnit_dir_name` parameter from XFELLoader
- remove associated instance attribute and path construction logic
- simplify DAMNIT database access by using:
    Damnit(experiment_file_path)
- move DAMNIT import to module level alongside EXtra-data imports
- simplify loader initialization and configuration interface
- update safe-loader documentation strings
- preserve existing EXtra-data RunDirectory support
- preserve alias-file handling through `extra-data-aliases.yml`
- preserve pulse reduction workflow (mean/sum/None)
- preserve rocking-angle detection and geometry handling
- preserve detector calibration, energy loading, and motor loading APIs
- preserve NaN/Inf sanitization of detector data through
  `np.nan_to_num`

Motivation:
The previous implementation required a separate DAMNIT
subdirectory configuration although the loader already operates
from a DAMNIT database location. Removing this extra parameter
reduces configuration complexity and makes the XFEL loader API
closer to the actual XFEL workflow.

Notes:
- detector images continue to be loaded from DAMNIT variables
- NaN values present in raw detector data are still converted
  to zeros before downstream processing
- geometry mapping verification remains pending
- testing on reconstructed datasets will be performed separately
- move DAMNIT and EXtra-data from optional dependencies to core dependencies
- remove the dedicated xfel extra
- simplify dependency management for XFEL support
- ensure XFEL packages are available in standard cdiutils installations
- avoid import failures when XFEL modules are imported
- remove duplicate XFEL dependency definitions from optional extras
@Abd-zak Abd-zak marked this pull request as draft June 6, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant