odf_inversion

nrxrdct.texture.odf_inversion

ODF inversion from pole-figure data (WIMV method).

Inverts one or more pole figures (from :mod:nrxrdct.texture.odf) into a discretised orientation distribution function (ODF) on a grid of Bunge Euler angles, using the WIMV algorithm (Matthies & Vinel, 1982): an iterative, multiplicative correction scheme that is the standard choice in texture analysis whenever pole-figure coverage is incomplete, because it degrades gracefully over uncovered regions and — unlike a literal generalised spherical-harmonic fit — guarantees a non-negative ODF by construction.

This is the natural next step after single-rotation-axis pole-figure extraction: a fixed vertical rotation axis with no tilt only ever sweeps a curve on the pole sphere per hkl (see the coverage caveat in :mod:nrxrdct.texture.odf), so pole-figure coverage here is inherently incomplete — the reason WIMV rather than the harmonic method is used.

Orientation convention

Orientations are represented as rotation matrices mapping a crystal-frame vector to the sample frame, v_sample = R @ v_crystal, parametrised by Bunge-style Euler angles (phi1, Phi, phi2) via

R(phi1, Phi, phi2) = Rz(phi1) @ Rx(Phi) @ Rz(phi2)

This is one of several conventions in use across texture software (Bunge, Roe, Kocks differ, and even "Bunge" implementations disagree on axis order and active/passive sense) — verify against your reference before comparing Euler angles quantitatively with another package.

Crystal directions passed to :func:compute_odf must already be unit vectors in an orthonormal crystal Cartesian frame (this module does not parse lattice parameters); for a cubic crystal, [h, k, l] normalised is sufficient, otherwise apply your crystal's B-matrix first.

compute_odf

compute_odf(pole_figures: Dict[str, Tuple[ndarray, ndarray, ndarray]], crystal_directions: Dict[str, ndarray], step_deg: float = 10.0, n_iter: int = 10, fold_hemisphere: bool = False, chunk_size: int = 4096) -> dict

Invert pole-figure data into a discretised ODF via WIMV.

Parameters:
  • pole_figures (dict) –

    Maps hkl label to (alpha_deg, beta_deg, intensity), each a 1-D array of equal length — measured pole-figure points, e.g. built from :func:nrxrdct.texture.odf.pole_figure_coordinates applied to a merged pole-figure sinogram. Intensities should be non-negative (background-subtracted, as from :func:extract_ring_intensity, may need clipping at zero first).

  • crystal_directions (dict) –

    Maps the same hkl labels to unit vector(s) in an orthonormal crystal Cartesian frame — shape (3,) for a single direction, or (M, 3) to include symmetry-equivalent directions of the same {hkl} family (the pole figure is then treated as covering all M equivalents, standard practice for a {hkl} pole figure).

  • step_deg (float, default: 10.0 ) –

    Euler-angle grid spacing passed to :func:orientation_grid (default 10.0). Each pole-figure data point is assigned (via :func:_build_kernel_matrix's hard nearest-cell binning) to its single nearest grid orientation, so this is now the only angular-resolution knob — there is no separate smoothing width.

  • n_iter (int, default: 10 ) –

    Number of WIMV iterations (default 10).

  • fold_hemisphere (bool, default: False ) –

    If True, map every pole direction with alpha > 90 to its antipodal upper-hemisphere equivalent before fitting (Friedel's law), matching how :func:nrxrdct.texture.texture_plotting.plot_pole_figure displays data. Default is False — folding independently per hkl is mathematically equivalent to conflating orientation R with its point-inversion -R across every hkl simultaneously, and WIMV's positivity constraint only sometimes breaks that tie: confirmed by testing to occasionally converge to the ~180°-misoriented "ghost" solution instead of the true orientation, for both single- and multi-hkl fits. Only enable this if you specifically need folded display-style output and have separately verified it doesn't corrupt your fit.

  • chunk_size (int, default: 4096 ) –

    Forwarded to :func:_build_kernel_matrix — bounds peak memory by processing this many pole-figure data points per batch instead of matching all of them against the grid at once; does not affect the fitted result (default 4096).

Returns:
  • dict( dict ) –

    With keys 'euler_deg' (grid, shape (N,3)), 'R' (grid rotation matrices, shape (N,3,3)), 'cell_weight' (shape (N,)), 'f' (fitted ODF value per grid cell, shape (N,), normalised so sum(f * cell_weight) / sum(cell_weight) == 1), and 'rp_history' (list of per-iteration relative pole-figure error, for convergence checking).

Source code in src/nrxrdct/texture/odf_inversion.py
def compute_odf(
    pole_figures: Dict[str, Tuple[np.ndarray, np.ndarray, np.ndarray]],
    crystal_directions: Dict[str, np.ndarray],
    step_deg: float = 10.0,
    n_iter: int = 10,
    fold_hemisphere: bool = False,
    chunk_size: int = 4096,
) -> dict:
    """
    Invert pole-figure data into a discretised ODF via WIMV.

    Args:
        pole_figures (dict): Maps hkl label to ``(alpha_deg, beta_deg, intensity)``,
            each a 1-D array of equal length — measured pole-figure points, e.g.
            built from :func:`nrxrdct.texture.odf.pole_figure_coordinates` applied
            to a merged pole-figure sinogram. Intensities should be non-negative
            (background-subtracted, as from :func:`extract_ring_intensity`, may
            need clipping at zero first).
        crystal_directions (dict): Maps the same hkl labels to unit vector(s) in
            an orthonormal crystal Cartesian frame — shape ``(3,)`` for a single
            direction, or ``(M, 3)`` to include symmetry-equivalent directions of
            the same ``{hkl}`` family (the pole figure is then treated as
            covering all M equivalents, standard practice for a ``{hkl}``
            pole figure).
        step_deg (float, optional): Euler-angle grid spacing passed to
            :func:`orientation_grid` (default 10.0). Each pole-figure data
            point is assigned (via :func:`_build_kernel_matrix`'s hard
            nearest-cell binning) to its single nearest grid orientation, so
            this is now the only angular-resolution knob — there is no
            separate smoothing width.
        n_iter (int, optional): Number of WIMV iterations (default 10).
        fold_hemisphere (bool, optional): If ``True``, map every pole direction
            with ``alpha > 90`` to its antipodal upper-hemisphere equivalent
            before fitting (Friedel's law), matching how
            :func:`nrxrdct.texture.texture_plotting.plot_pole_figure` displays
            data. **Default is False** — folding independently per hkl is
            mathematically equivalent to conflating orientation ``R`` with its
            point-inversion ``-R`` across every hkl simultaneously, and WIMV's
            positivity constraint only sometimes breaks that tie: confirmed by
            testing to occasionally converge to the ~180°-misoriented "ghost"
            solution instead of the true orientation, for both single- and
            multi-hkl fits. Only enable this if you specifically need folded
            display-style output and have separately verified it doesn't
            corrupt your fit.
        chunk_size (int, optional): Forwarded to :func:`_build_kernel_matrix` —
            bounds peak memory by processing this many pole-figure data points
            per batch instead of matching all of them against the grid at
            once; does not affect the fitted result (default 4096).

    Returns:
        dict: With keys ``'euler_deg'`` (grid, shape ``(N,3)``), ``'R'`` (grid
            rotation matrices, shape ``(N,3,3)``), ``'cell_weight'`` (shape
            ``(N,)``), ``'f'`` (fitted ODF value per grid cell, shape ``(N,)``,
            normalised so ``sum(f * cell_weight) / sum(cell_weight) == 1``),
            and ``'rp_history'`` (list of per-iteration relative pole-figure
            error, for convergence checking).
    """
    if set(pole_figures) != set(crystal_directions):
        raise ValueError(
            f"pole_figures and crystal_directions must cover the same hkl labels; "
            f"got {sorted(pole_figures)} vs {sorted(crystal_directions)}"
        )

    euler_deg, R, cell_weight = orientation_grid(step_deg)
    N = euler_deg.shape[0]

    data_xyz_by_hkl = {}
    grid_variants_by_hkl = {}
    measured = {}
    for hkl, (alpha_deg, beta_deg, intensity) in pole_figures.items():
        alpha_f, beta_f = np.asarray(alpha_deg), np.asarray(beta_deg)
        if fold_hemisphere:
            alpha_f, beta_f = _fold_upper_hemisphere(alpha_f, beta_f)
        data_xyz = _alpha_beta_to_xyz(alpha_f, beta_f)

        h = np.atleast_2d(crystal_directions[hkl]).astype(np.float64)
        h = h / np.linalg.norm(h, axis=1, keepdims=True)
        grid_xyz_variants = np.einsum("nij,mj->mni", R, h)  # (M, N, 3)
        if fold_hemisphere:
            alpha_g = np.rad2deg(np.arccos(np.clip(grid_xyz_variants[..., 2], -1.0, 1.0)))
            beta_g = np.rad2deg(np.arctan2(grid_xyz_variants[..., 1], grid_xyz_variants[..., 0])) % 360.0
            alpha_gf, beta_gf = _fold_upper_hemisphere(alpha_g, beta_g)
            grid_xyz_variants = _alpha_beta_to_xyz(alpha_gf, beta_gf)

        data_xyz_by_hkl[hkl] = data_xyz
        grid_variants_by_hkl[hkl] = grid_xyz_variants
        measured[hkl] = np.asarray(intensity, dtype=np.float64)

    # Nearest-cell binning (a single k=1 KDTree query per hkl, internally
    # thread-parallel via workers=-1) is cheap enough that splitting hkls
    # across processes no longer pays for its own overhead — verified up to
    # 500k data points / 186k grid cells / 48-fold symmetry, where per-process
    # spawn and array-pickling cost matched or exceeded the serial runtime.
    kernels = {
        hkl: _build_kernel_matrix(data_xyz_by_hkl[hkl], grid_variants_by_hkl[hkl], chunk_size=chunk_size)
        for hkl in pole_figures
    }

    f = np.ones(N, dtype=np.float64)
    rp_history = []

    for it in range(n_iter):
        log_ratio_num = np.zeros(N)
        weight_den = np.zeros(N)
        sse, ssm = 0.0, 0.0

        for hkl, K in kernels.items():
            predicted = K @ f
            predicted_safe = np.where(predicted > 0, predicted, np.nan)
            ratio = measured[hkl] / predicted_safe
            valid = np.isfinite(ratio) & (ratio > 0)

            log_ratio_num += K[valid].T @ np.log(ratio[valid])
            weight_den += np.asarray(K[valid].sum(axis=0)).ravel()

            sse += float(np.nansum((predicted - measured[hkl]) ** 2))
            ssm += float(np.sum(measured[hkl] ** 2))

        correction = np.ones(N)
        has_support = weight_den > 0
        correction[has_support] = np.exp(log_ratio_num[has_support] / weight_den[has_support])
        f = f * correction

        rp = float(np.sqrt(sse / ssm)) if ssm > 0 else float("nan")
        rp_history.append(rp)
        print(f"  WIMV iter {it+1}/{n_iter}: Rp = {rp:.4f}")

    norm = np.sum(f * cell_weight) / np.sum(cell_weight)
    if norm > 0:
        f = f / norm

    return {
        "euler_deg": euler_deg,
        "R": R,
        "cell_weight": cell_weight,
        "f": f,
        "rp_history": rp_history,
    }

load_pole_figures

load_pole_figures(pole_figure_file: Path, hkl_labels: Sequence[str], n_rot: int, line_index: Optional[int] = None, translation_motor: str = 'dty', rotation_motor: str = 'rot', chi_offset_deg: float = 0.0, chi_sign: float = 1.0, clip_negative: bool = True) -> Dict[str, Tuple[np.ndarray, np.ndarray, np.ndarray]]

Build the pole_figures dict :func:compute_odf expects, directly from a merged pole-figure HDF5 file.

For each hkl, assembles the pole-figure sinogram (:func:~nrxrdct.texture.odf.assemble_pole_figure_sinogram), reads its stored 2theta window, and converts it to flat pole-figure coordinates (:func:~nrxrdct.texture.odf.sinogram_to_pole_figure) — the glue between a file written by :func:~nrxrdct.texture.odf.assemble_pole_figure_data / :mod:nrxrdct.texture.slurm_pole_figures and :func:compute_odf.

Parameters:
  • pole_figure_file (Path) –

    HDF5 file written by :func:~nrxrdct.texture.odf.assemble_pole_figure_data or :mod:nrxrdct.texture.slurm_pole_figures's merge.

  • hkl_labels (sequence of str) –

    Ring identifiers to load, e.g. ["111", "200"].

  • n_rot (int) –

    Number of rotation steps, forwarded to :func:~nrxrdct.texture.odf.assemble_pole_figure_sinogram.

  • line_index (int, default: None ) –

    Translation line to use; None (default) averages over all lines into one bulk pole figure for the whole scanned cross-section. Pass an explicit index for a per-line bulk pole figure instead.

  • translation_motor (str, default: 'dty' ) –

    Passed through to :func:~nrxrdct.texture.odf.assemble_pole_figure_sinogram (default "dty").

  • rotation_motor (str, default: 'rot' ) –

    Passed through to :func:~nrxrdct.texture.odf.assemble_pole_figure_sinogram (default "rot").

  • chi_offset_deg (float, default: 0.0 ) –

    Forwarded to :func:~nrxrdct.texture.odf.pole_figure_coordinates — verify against your beamline before trusting results (default 0.0).

  • chi_sign (float, default: 1.0 ) –

    Forwarded likewise (default 1.0).

  • clip_negative (bool, default: True ) –

    Clip negative intensities to zero (default True). Background-subtracted ring intensities can go slightly negative from noise; :func:compute_odf assumes non-negative measurements.

Returns:
  • dict( Dict[str, Tuple[ndarray, ndarray, ndarray]] ) –

    {hkl_label: (alpha_deg, beta_deg, intensity)}, ready to pass directly as :func:compute_odf's pole_figures argument.

Source code in src/nrxrdct/texture/odf_inversion.py
def load_pole_figures(
    pole_figure_file: Path,
    hkl_labels: Sequence[str],
    n_rot: int,
    line_index: Optional[int] = None,
    translation_motor: str = "dty",
    rotation_motor: str = "rot",
    chi_offset_deg: float = 0.0,
    chi_sign: float = 1.0,
    clip_negative: bool = True,
) -> Dict[str, Tuple[np.ndarray, np.ndarray, np.ndarray]]:
    """
    Build the ``pole_figures`` dict :func:`compute_odf` expects, directly from
    a merged pole-figure HDF5 file.

    For each hkl, assembles the pole-figure sinogram
    (:func:`~nrxrdct.texture.odf.assemble_pole_figure_sinogram`), reads its
    stored 2theta window, and converts it to flat pole-figure coordinates
    (:func:`~nrxrdct.texture.odf.sinogram_to_pole_figure`) — the glue between
    a file written by :func:`~nrxrdct.texture.odf.assemble_pole_figure_data` /
    :mod:`nrxrdct.texture.slurm_pole_figures` and :func:`compute_odf`.

    Args:
        pole_figure_file (Path): HDF5 file written by
            :func:`~nrxrdct.texture.odf.assemble_pole_figure_data` or
            :mod:`nrxrdct.texture.slurm_pole_figures`'s ``merge``.
        hkl_labels (sequence of str): Ring identifiers to load, e.g. ``["111", "200"]``.
        n_rot (int): Number of rotation steps, forwarded to
            :func:`~nrxrdct.texture.odf.assemble_pole_figure_sinogram`.
        line_index (int, optional): Translation line to use; ``None`` (default)
            averages over all lines into one bulk pole figure for the whole
            scanned cross-section. Pass an explicit index for a per-line bulk
            pole figure instead.
        translation_motor (str, optional): Passed through to
            :func:`~nrxrdct.texture.odf.assemble_pole_figure_sinogram` (default ``"dty"``).
        rotation_motor (str, optional): Passed through to
            :func:`~nrxrdct.texture.odf.assemble_pole_figure_sinogram` (default ``"rot"``).
        chi_offset_deg (float, optional): Forwarded to
            :func:`~nrxrdct.texture.odf.pole_figure_coordinates` — verify against
            your beamline before trusting results (default 0.0).
        chi_sign (float, optional): Forwarded likewise (default 1.0).
        clip_negative (bool, optional): Clip negative intensities to zero
            (default ``True``). Background-subtracted ring intensities can go
            slightly negative from noise; :func:`compute_odf` assumes
            non-negative measurements.

    Returns:
        dict: ``{hkl_label: (alpha_deg, beta_deg, intensity)}``, ready to pass
            directly as :func:`compute_odf`'s ``pole_figures`` argument.
    """
    pole_figures = {}
    for hkl in hkl_labels:
        sino, azimuthal, _, rot_deg = assemble_pole_figure_sinogram(
            pole_figure_file, hkl, n_rot, translation_motor, rotation_motor
        )
        with h5py.File(pole_figure_file, "r") as hin:
            tth_deg = float(np.mean(hin[f"pole_figures/{hkl}/tth_range"][:]))

        alpha, beta, intensity = sinogram_to_pole_figure(
            sino, azimuthal, rot_deg, tth_deg,
            line_index=line_index, chi_offset_deg=chi_offset_deg, chi_sign=chi_sign,
        )
        if clip_negative:
            intensity = np.clip(intensity, 0.0, None)
        pole_figures[hkl] = (alpha, beta, intensity)

    return pole_figures

misorientation_angle_deg

misorientation_angle_deg(R1: ndarray, R2: ndarray) -> float

Angle (degrees) of the relative rotation between two orientation matrices.

Parameters:
  • R1 (ndarray) –

    Rotation matrix, shape (3, 3).

  • R2 (ndarray) –

    Rotation matrix, shape (3, 3).

Returns:
  • float( float ) –

    Misorientation angle in degrees, in [0, 180].

Source code in src/nrxrdct/texture/odf_inversion.py
def misorientation_angle_deg(R1: np.ndarray, R2: np.ndarray) -> float:
    """
    Angle (degrees) of the relative rotation between two orientation matrices.

    Args:
        R1 (np.ndarray): Rotation matrix, shape ``(3, 3)``.
        R2 (np.ndarray): Rotation matrix, shape ``(3, 3)``.

    Returns:
        float: Misorientation angle in degrees, in ``[0, 180]``.
    """
    dR = R1.T @ R2
    cos_angle = np.clip((np.trace(dR) - 1.0) / 2.0, -1.0, 1.0)
    return float(np.rad2deg(np.arccos(cos_angle)))

orientation_grid

orientation_grid(step_deg: float = 10.0) -> Tuple[np.ndarray, np.ndarray, np.ndarray]

Build a regular grid of orientations in Bunge Euler space.

Parameters:
  • step_deg (float, default: 10.0 ) –

    Grid spacing in degrees along each Euler angle (default 10.0). Cell count scales as (360/step)^2 * (180/step), so halving step_deg multiplies the grid size (and run time) by ~8.

Returns:
  • euler_deg( ndarray ) –

    Grid orientations as (phi1, Phi, phi2) in degrees, shape (N, 3).

  • R( ndarray ) –

    Rotation matrices for each grid orientation, mapping crystal-frame to sample-frame vectors, shape (N, 3, 3).

  • cell_weight( ndarray ) –

    Relative SO(3) (Haar-measure) volume of each cell, proportional to sin(Phi), shape (N,). Use these as weights when averaging or normalising the ODF over the grid.

Source code in src/nrxrdct/texture/odf_inversion.py
def orientation_grid(step_deg: float = 10.0) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    """
    Build a regular grid of orientations in Bunge Euler space.

    Args:
        step_deg (float, optional): Grid spacing in degrees along each Euler
            angle (default 10.0). Cell count scales as ``(360/step)^2 * (180/step)``,
            so halving *step_deg* multiplies the grid size (and run time) by ~8.

    Returns:
        euler_deg (np.ndarray): Grid orientations as ``(phi1, Phi, phi2)`` in
            degrees, shape ``(N, 3)``.
        R (np.ndarray): Rotation matrices for each grid orientation, mapping
            crystal-frame to sample-frame vectors, shape ``(N, 3, 3)``.
        cell_weight (np.ndarray): Relative SO(3) (Haar-measure) volume of each
            cell, proportional to ``sin(Phi)``, shape ``(N,)``. Use these as
            weights when averaging or normalising the ODF over the grid.
    """
    phi1 = np.arange(0.0, 360.0, step_deg) + step_deg / 2
    Phi = np.arange(0.0, 180.0, step_deg) + step_deg / 2
    phi2 = np.arange(0.0, 360.0, step_deg) + step_deg / 2

    P1, PH, P2 = np.meshgrid(phi1, Phi, phi2, indexing="ij")
    euler_deg = np.stack([P1.ravel(), PH.ravel(), P2.ravel()], axis=1)
    cell_weight = np.sin(np.deg2rad(euler_deg[:, 1]))

    R = _euler_to_matrix(euler_deg)
    return euler_deg, R, cell_weight

recalculate_pole_figure

recalculate_pole_figure(odf_result: dict, crystal_direction: ndarray, alpha_deg: ndarray, beta_deg: ndarray, fold_hemisphere: bool = False) -> np.ndarray

Forward-project the fitted ODF back onto pole-figure coordinates, for comparison against the original measured pole figure (quality check).

Parameters:
  • odf_result (dict) –

    Output of :func:compute_odf.

  • crystal_direction (ndarray) –

    Unit vector(s) in the crystal frame, shape (3,) or (M, 3) (same convention as in :func:compute_odf).

  • alpha_deg (ndarray) –

    Polar angles (degrees) at which to evaluate.

  • beta_deg (ndarray) –

    Azimuthal angles (degrees) at which to evaluate.

  • fold_hemisphere (bool, default: False ) –

    Must match the value passed to :func:compute_odf when odf_result was produced — see the warning in that function's docstring (default False).

Returns:
  • ndarray –

    np.ndarray: Recalculated pole-figure intensity at each query point, same shape as alpha_deg.

Source code in src/nrxrdct/texture/odf_inversion.py
def recalculate_pole_figure(
    odf_result: dict,
    crystal_direction: np.ndarray,
    alpha_deg: np.ndarray,
    beta_deg: np.ndarray,
    fold_hemisphere: bool = False,
) -> np.ndarray:
    """
    Forward-project the fitted ODF back onto pole-figure coordinates, for
    comparison against the original measured pole figure (quality check).

    Args:
        odf_result (dict): Output of :func:`compute_odf`.
        crystal_direction (np.ndarray): Unit vector(s) in the crystal frame,
            shape ``(3,)`` or ``(M, 3)`` (same convention as in :func:`compute_odf`).
        alpha_deg (np.ndarray): Polar angles (degrees) at which to evaluate.
        beta_deg (np.ndarray): Azimuthal angles (degrees) at which to evaluate.
        fold_hemisphere (bool, optional): Must match the value passed to
            :func:`compute_odf` when *odf_result* was produced — see the
            warning in that function's docstring (default ``False``).

    Returns:
        np.ndarray: Recalculated pole-figure intensity at each query point,
            same shape as *alpha_deg*.
    """
    R = odf_result["R"]
    f = odf_result["f"]

    h = np.atleast_2d(crystal_direction).astype(np.float64)
    h = h / np.linalg.norm(h, axis=1, keepdims=True)
    grid_xyz_variants = np.einsum("nij,mj->mni", R, h)
    alpha_f, beta_f = np.asarray(alpha_deg), np.asarray(beta_deg)
    if fold_hemisphere:
        alpha_g = np.rad2deg(np.arccos(np.clip(grid_xyz_variants[..., 2], -1.0, 1.0)))
        beta_g = np.rad2deg(np.arctan2(grid_xyz_variants[..., 1], grid_xyz_variants[..., 0])) % 360.0
        alpha_gf, beta_gf = _fold_upper_hemisphere(alpha_g, beta_g)
        grid_xyz_variants = _alpha_beta_to_xyz(alpha_gf, beta_gf)
        alpha_f, beta_f = _fold_upper_hemisphere(alpha_f, beta_f)

    query_xyz = _alpha_beta_to_xyz(alpha_f, beta_f)

    K = _build_kernel_matrix(query_xyz, grid_xyz_variants)
    return K @ f