slurm_reconstruction

nrxrdct.xrdct.slurm_reconstruction

nrxrdct.slurm_reconstruction

Tools for distributing tomographic reconstruction across SLURM HPC clusters.

Python API

from nrxrdct.slurm_reconstruction import build_sinogram, launch_recon

# Step 1 (optional) — assemble sinogram from integrated HDF5
build_sinogram(
    integrated_file = Path("output.h5"),
    sinogram_file   = Path("sinogram.h5"),
    n_rot           = 360,
    n_tth_angles    = 1000,
    n_lines         = 10,
)

# Step 2 — submit reconstruction jobs
launch_recon(
    sinogram_file = Path("sinogram.h5"),
    output_file   = Path("reconstruction.h5"),
    n_jobs        = 8,
    algo          = "SART_CUDA",
    num_iter      = 200,
    gpu           = True,
    conda_env     = "nrxrdct",
)

CLI

nrxrdct-slurm-recon build  --help
nrxrdct-slurm-recon launch --help

build_sinogram

build_sinogram(integrated_file: Path, sinogram_file: Path, n_rot: int, n_tth_angles: int, n_lines: int = 10, overwrite: bool = False) -> Path

Assemble a sinogram from an integrated HDF5 file and save it to disk.

Calls :func:nrxrdct.reconstruction.assemble_sinogram and writes the result to sinogram_file as an HDF5 dataset. If sinogram_file already exists and overwrite is False, the function returns immediately without rebuilding.

The sinogram is stored with axes labelled so that the worker can read individual 2θ slices efficiently:

  • sinogram — shape (n_tth, n_rot, n_lines), dtype float32
  • radial — 2θ axis copied from the integrated file, with unit attribute preserved
  • motors/rot — rotation angles in degrees
  • motors/dty — translation motor positions
Parameters:
  • integrated_file (Path) –

    HDF5 file produced by the integration pipeline (contains integrated/scan_*, integrated/radial, motors/rot, motors/dty).

  • sinogram_file (Path) –

    Output HDF5 file to write (created or overwritten).

  • n_rot (int) –

    Number of rotation frames per scan (sinogram angular dimension).

  • n_tth_angles (int) –

    Number of 2θ bins (spectral dimension).

  • n_lines (int, default: 10 ) –

    Number of translation (dty) lines; passed to :func:~nrxrdct.reconstruction.assemble_sinogram (default 10).

  • overwrite (bool, default: False ) –

    Re-build and overwrite even if sinogram_file already exists (default False).

Returns:
  • Path( Path ) –

    Path to the written sinogram file.

Source code in src/nrxrdct/xrdct/slurm_reconstruction/launch_recon.py
def build_sinogram(
    integrated_file: Path,
    sinogram_file: Path,
    n_rot: int,
    n_tth_angles: int,
    n_lines: int = 10,
    overwrite: bool = False,
) -> Path:
    """
    Assemble a sinogram from an integrated HDF5 file and save it to disk.

    Calls :func:`nrxrdct.reconstruction.assemble_sinogram` and writes the
    result to *sinogram_file* as an HDF5 dataset.  If *sinogram_file* already
    exists and *overwrite* is ``False``, the function returns immediately
    without rebuilding.

    The sinogram is stored with axes labelled so that the worker can read
    individual 2θ slices efficiently:

    - ``sinogram``    — shape ``(n_tth, n_rot, n_lines)``, dtype float32
    - ``radial``      — 2θ axis copied from the integrated file, with ``unit``
      attribute preserved
    - ``motors/rot``  — rotation angles in degrees
    - ``motors/dty``  — translation motor positions

    Args:
        integrated_file (Path): HDF5 file produced by the integration pipeline (contains
            ``integrated/scan_*``, ``integrated/radial``, ``motors/rot``, ``motors/dty``).
        sinogram_file (Path): Output HDF5 file to write (created or overwritten).
        n_rot (int): Number of rotation frames per scan (sinogram angular dimension).
        n_tth_angles (int): Number of 2θ bins (spectral dimension).
        n_lines (int, optional): Number of translation (dty) lines; passed to
            :func:`~nrxrdct.reconstruction.assemble_sinogram` (default 10).
        overwrite (bool, optional): Re-build and overwrite even if *sinogram_file* already exists
            (default ``False``).

    Returns:
        Path: Path to the written sinogram file.
    """
    from nrxrdct.xrdct.sinogram import assemble_sinogram

    sinogram_file = Path(sinogram_file)
    integrated_file = Path(integrated_file)

    if sinogram_file.exists() and not overwrite:
        print(f"✓  Sinogram file already exists, skipping build: {sinogram_file}")
        return sinogram_file

    print("=" * 60)
    print("Building sinogram from integrated file …")
    print("=" * 60)

    # dty is aligned with sino's translation axis (sorted by each scan's own
    # dty attribute, not by its position in the file — see assemble_sinogram).
    sino, dty = assemble_sinogram(integrated_file, n_rot, n_tth_angles, n_lines)
    # sino shape: (n_tth, n_rot, n_lines)

    with h5py.File(integrated_file, "r") as hin:
        radial = hin["integrated/radial"][:]
        radial_unit = hin["integrated/radial"].attrs.get("unit", "2th_deg")
        rot = hin["motors/rot"][:]

    sinogram_file.parent.mkdir(parents=True, exist_ok=True)
    with h5py.File(sinogram_file, "w") as hout:
        ds = hout.create_dataset(
            "sinogram",
            data=sino,
            compression="gzip",
            compression_opts=4,
            chunks=(1, sino.shape[1], sino.shape[2]),
        )
        ds.attrs["axes"] = "tth, rot, dty"

        ds_rad = hout.create_dataset("radial", data=radial)
        ds_rad.attrs["unit"] = radial_unit

        hout["motors/rot"] = rot
        hout["motors/dty"] = dty
        hout["meta/source"] = str(integrated_file)
        hout["meta/n_tth"] = len(radial)
        hout["meta/n_rot"] = n_rot
        hout["meta/n_lines"] = n_lines

    print(f"✓  Sinogram saved: {sinogram_file}  "
          f"shape={sino.shape}  dtype={sino.dtype}")
    return sinogram_file