Skip to main content

midas-nf-fitorientation

Differentiable, drop-in replacement for the three NF-HEDM orientation and calibration C executables in MIDAS:

C executable Python equivalent Console script
FitOrientationOMP fit_orientation_run midas-nf-fit-orientation
FitOrientationParameters fit_parameters_run midas-nf-fit-parameters
FitOrientationParametersMultiPoint fit_multipoint_run midas-nf-fit-multipoint

Built on top of midas-diffract, so the forward model is the same one validated to pixel-exact agreement against the C simulators. Orientation refinement uses a vectorised PyTorch Nelder-Mead running every (voxel × winner) fit problem in one batched forward call per NM iteration; calibration refinement (in fit_parameters and fit_multipoint) uses L-BFGS over the soft Gaussian-splat surrogate because Nelder-Mead scales poorly past ~10 DoF.

What changed vs. the C codes

  • Orientation optimiser: NLopt Nelder-Mead → vectorised PyTorch Nelder-Mead (midas_nf_fitorientation.torch_nm). Same hard FracOverlap objective the C uses, but every (voxel, winner) fit is batched into one forward call per NM iteration. Converged simplices are trimmed out of the active set on the fly so each successive iteration is cheaper. ~22× faster than per-winner scipy NM on H100; bit-equivalent answers modulo NM convergence noise.
  • Calibration optimiser (joint orientation + geometry refinement in fit_parameters / fit_multipoint): NLopt LN_NELDERMEAD → PyTorch L-BFGS over a soft Gaussian-splat surrogate with tanh-reparameterised bounds. NM is the wrong tool past ~10 DoF; L-BFGS scales well, at the cost of optimising a slightly smoothed basin floor.
  • Obs volume storage: the 24 GB SpotsInfo.bin is loaded as uint8 (6 GB) by default. The orientation kernel only needs 0/1 values; the L-BFGS calibration paths request float32 explicitly.
  • Bounds: each refined parameter (Eulers, tilts, Lsd, ΔLsd, BC, optionally wedge) is reparameterised as x = x0 + tol * tanh(u), so L-BFGS sees an unbounded variable but the physical parameter cannot leave its tolerance box.
  • Tikhonov regularisation (opt-in, layered on top of tanh): a quadratic prior on the calibration block, useful in the multi-point joint fit where you want to drift only when many voxels' worth of evidence agree.
  • Wedge refinement (opt-in, paramfile key RefineWedge 1): not available in the C code; the model accepts wedge as a calibration DoF.
  • Multi-start global search for the multi-point joint fit: the C code's NM→CRS2→NM ladder is replaced with NumIterations independent L-BFGS attempts, each seeded with a Gaussian perturbation of the previous best within the tanh box. CRS2's true global behaviour is lost; for well-seeded calibration the multi-start gives the same answer in practice.
  • Output files (MicFileBinary, MicFileBinary.AllMatches) match the C pwrite byte layout exactly.

Installation

From the repository root:

pip install ./packages/midas_diffract
pip install ./packages/midas_nf_fitorientation

PyTorch ≥ 2.0 is required; CUDA / MPS is auto-detected.

Notebooks

Worked-example Jupyter notebooks live in notebooks/. They are not shipped with pip install — get them by cloning the MIDAS repository.

CLI usage

The argument signatures mirror the C executables, so existing wrapper scripts swap binaries without changes:

midas-nf-fit-orientation  params.txt blockNr nBlocks nCPUs [--device cuda] [--fp32] [--screen-only]
midas-nf-fit-parameters   params.txt rowNr   [nCPUs]      [--device cuda] [--fp32]
midas-nf-fit-multipoint   params.txt         [nCPUs]      [--device cuda] [--fp32]

Common flags (parsed after the positional args):

  • --device {auto,cpu,cuda} — defaults to auto (CUDA if available).
  • --fp32 — float32 forward (faster on CUDA, less bit-stable than float64 — the package default).
  • --screen-only — stop after Phase 1 and dump screen_cpu.csv (mirrors MIDAS_SCREEN_ONLY=1 in the C code).
  • --verbose — chatty progress.
  • --lbfgs-max-iter N, --lbfgs-max-outer N — L-BFGS step limits.

Python API

import midas_nf_fitorientation as fit

# Replaces FitOrientationOMP
fit.fit_orientation_run(
    paramfile="params.txt", block_nr=0, n_blocks=1, n_cpus=8,
    device="cuda", verbose=True,
)

# Replaces FitOrientationParameters (single-voxel calibration)
result = fit.fit_parameters_run(
    paramfile="params.txt", voxel_idx=42, n_cpus=4, verbose=True,
)
print(result["Lsd"], result["tilts"], result["frac_overlap"])

# Replaces FitOrientationParametersMultiPoint (joint multi-voxel)
result = fit.fit_multipoint_run(paramfile="params.txt", n_cpus=8)

Paramfile keys

Every key consumed by the C executables is recognised, plus a small set of new keys. See midas_nf_fitorientation/params.py for the full schema. The new keys:

Key Default Purpose
RefineWedge 1 off Add wedge to the calibration DoF set
WedgeTol 0.05 0.05° Tanh-box width for wedge
TikhonovCalibration 1.0 0 Global λ for the calibration prior; 0 disables
TikhonovSigmaLsd 100.0 100 µm Prior σ for Lsd
TikhonovSigmaTilts 0.05 0.05° Prior σ for tilts
TikhonovSigmaBC 1.0 1 px Prior σ for beam centres
TikhonovSigmaWedge 0.05 0.05° Prior σ for wedge
GaussianSplatSigmaPx 1.5 auto Override the auto-σ for the soft-overlap kernel

NumIterations (existing) controls the number of multi-start trials in fit_multipoint. Default 1 (single L-BFGS run); set to ≥ 8 for true multi-start.

Status

v0.3.x. The forward path is validated against midas-diffract (pixel-exact vs. the C simulators); the fit drivers have unit-test coverage at the module level. End-to-end agreement against the C MicFileBinary on a real reconstruction dataset is the next milestone.

Licence

BSD-3-Clause.

Metadata

Release files for midas-nf-fitorientation 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for midas-nf-fitorientation 0.11.0
File Size Uploaded Provenance Actions
midas_nf_fitorientation-0.11.0.tar.gz 194.6 kB With provenance Details for midas_nf_fitorientation-0.11.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for midas-nf-fitorientation 0.11.0
File Interpreter ABI Platform Actions
midas_nf_fitorientation-0.11.0-py3-none-any.whl Python 3 none any Details for midas_nf_fitorientation-0.11.0-py3-none-any.whl

Total release size: 334.6 kB

Release files / midas_nf_fitorientation-0.11.0.tar.gz

Download URL midas_nf_fitorientation-0.11.0.tar.gz
Size 194.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a78698a3f834f84f81fe9ce82892a5dc99534ead01f7a75780ace0045670a8e4
BLAKE2b-256 checksum
How to use checksums
171344cf6a0c2eb48f515683bf723b46e2015d34d2a80572d92ab45b85d20ce1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Release files / midas_nf_fitorientation-0.11.0-py3-none-any.whl

Download URL midas_nf_fitorientation-0.11.0-py3-none-any.whl
Size 140.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aeb49693b27ed0e0fa16bd6d3eff6ed7b8bbd2e56e521a90e2d7bc206143c0e0
BLAKE2b-256 checksum
How to use checksums
c80368bc9d9a599b57dd533275aa433d8a79a8440019fba21344e8338f48d382
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Security for midas-nf-fitorientation 0.11.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for midas_nf_fitorientation-0.11.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page