Skip to main content

Refjax - Lightweight jit and grad compatible reflectometry in jax

This package provides lightweight, low-level access to neutron reflectometry and spectroscopic ellipsometry kernels in jax.

The provided kernels are all jit and grad compatible and easy to integrate with your favourite optimisation or uncertainty quantification library for fast parameterisation of stratified reflectometry models. Several examples for popular libraries are available in the examples folder.

Installation

From PyPI:

pip install refjax
# or
uv add refjax

Or from GitHub, pinned to the 0.1.1 release by its tag:

pip install "refjax @ git+https://github.com/MDCHAMP/refjax@v0.1.1"
# or
uv add git+https://github.com/MDCHAMP/refjax --tag v0.1.1

refjax needs Python >= 3.11, jax and jaxlib >= 0.4.26 and numpy >= 2.0. The test suite runs in CI at these minimum versions and at the latest releases.

64-bit precision is on by default

import refjax switches on jax's 64-bit mode (jax.config.update("jax_enable_x64", True)), so refjax computes in float64 and complex128. This setting is global: it applies to all jax code in the same Python process, not only to refjax. To work in 32-bit, switch it off after importing refjax, before creating any arrays: jax.config.update("jax_enable_x64", False). refjax then computes in float32 and complex64; its tests run only in 64-bit.

Neutron reflectometry

Design pattern

Reflectometry models are created by creating reflectivity kernels with refjax.kernel:

kernel = refjax.kernel(model, smear=refjax.smear.no_smear, smear_params=None, jit=True)

Currently implemented models:

  • refjax.model.abeles Abeles matrix formalism for stratified media

Currently implemented smearing kernels:

  • refjax.smear.no_smear No smearing kernel (default)
  • refjax.smear.constant_gaussian Constant gaussian smearing at every Q value (the third argument is the FWHM as a percentage of Q)
  • refjax.smear.pointwise_gaussian Pointwise Gaussian approximation to the true smearing kernel (requires FWHM resolution measurements), by 17th-order Gauss-Hermite quadrature

Once a kernel has been created, it is called with the model parameters and Q (and FWHM values for smeared kernels).

R = kernel(params, Q, FWHM) # fast because jit

Parameters of abeles

For a slab model with n layers between a fronting and a backing medium, params is a dict:

key shape meaning
thick (n,) layer thicknesses, in the length unit of 1/Q (Å for Q in Å⁻¹)
SLD (n + 2,) scattering length densities in 10⁻⁶ Å⁻², fronting first, backing last
rough (n + 1,) interfacial roughnesses, one per interface
scale () multiplies the reflectivity
bkg () constant background added to the reflectivity

Example

import jax.numpy as jnp
import refjax

params = {
    "thick": jnp.array([255.0, 17.0]),
    "SLD": jnp.array([0.0, 6.4, 3.47, 2.07]),
    "rough": jnp.array([5.2, 5.0, 2.07]),
    "scale": 1.0,
    "bkg": 2e-7,
}
Q = jnp.linspace(0.005, 0.3, 200)  # Å⁻¹
FWHM = 0.05 * Q  # resolution FWHM at each Q
kernel = refjax.kernel(refjax.model.abeles, refjax.smear.pointwise_gaussian)
R = kernel(params, Q, FWHM)

examples/01_basic_usage.py walks through this model against the example data in examples/.example_NR_data.csv; the other scripts in examples show parameter identification with optax and numpyro.

Spectroscopic ellipsometry

refjax.model.spec_ellips(params, lam) computes the ellipsometric angles Psi and Delta of a stratified medium (transfer-matrix method) at the wavelengths lam. It is jit and grad compatible and is called directly (not through refjax.kernel).

For n layers between an ambient medium and a substrate, params is a dict:

key shape meaning
nbar (n + 2, lam.shape) complex refractive index n + ik (k >= 0) of each medium, ambient first, substrate last
thick (n,) layer thicknesses, in the same length unit as lam
aoi_rads () angle of incidence in the ambient, in radians

It returns (psi, delta) in degrees, each of shape lam.shape.

Conventions:

  • tan(psi) exp(i delta) = rp / rs in Woollam's convention, as used by J.A. Woollam's instruments and CompleteEASE and set out in H. Fujiwara, Spectroscopic Ellipsometry: Principles and Applications (Wiley, 2007), doi:10.1002/9780470060193: the complex index written n - ik, so refjax's n + ik enters conjugated. delta is wrapped to [0, 360); compare it to data modulo 360.
  • The complex index is n + ik with k >= 0. This is the complex conjugate of Woollam's n - ik, hence the minus sign on the phase of rp / rs in the code. A Woollam-convention index N = n - ik is passed as N.conj().
  • n cos(theta) in each medium follows from Snell's law with the principal square root, which gives Im >= 0: the decaying wave for n + ik.
import jax.numpy as jnp
import numpy as np
import refjax

lam = jnp.linspace(370.0, 1000.0, 50)  # nm
# ambient, film, SiO2, Si substrate (n + ik), each constant in wavelength here
nbar = jnp.array([1.0, 1.58, 1.46, 3.9 + 0.02j])[:, None] * jnp.ones_like(lam)
params = {"nbar": nbar, "thick": jnp.array([17.0, 2.0]), "aoi_rads": np.radians(75.0)}
psi, delta = refjax.model.spec_ellips(params, lam)

examples/06_spectroscopic_ellipsometry.py models and fits a thick polymer film on Si against a measured spectrum in examples/.example_ellips_data.csv (which carries the substrate's indices). test/test_ellips.py checks spec_ellips against an independent numpy implementation and against that spectrum.

Changes in 0.1.1

The first release on PyPI. Over the 0.1.0 tag: the smearing quadrature is computed in float64 (it was float32, an error of about 2e-8 relative in smeared reflectivity), importing refjax no longer starts jax's backend, abeles and spec_ellips have docstrings, and the packaging metadata is updated.

Changes in 0.1.0

spec_ellips now follows the conventions above. delta is wrapped to [0, 360); before 0.1.0 it was unwrapped along the wavelength axis, so it could differ from the 0.1.0 value by multiples of 360. n cos(theta) now comes from the principal square root rather than arcsin of Snell's law. Code written against the earlier spec_ellips should compare delta modulo 360.

Development

uv run --extra test pytest

Citing

If you use refjax in published work, please cite:

Max D. Champneys, Andrew J. Parnell and Stephanie L. Burg, "Towards real-time surrogate-free Bayesian inversion for neutron reflectometry", Communications Materials (2026). doi:10.1038/s43246-026-01341-4

@article{champneys2026refjax,
  author  = {Champneys, Max D. and Parnell, Andrew J. and Burg, Stephanie L.},
  title   = {Towards real-time surrogate-free {B}ayesian inversion for neutron reflectometry},
  journal = {Communications Materials},
  year    = {2026},
  doi     = {10.1038/s43246-026-01341-4},
  url     = {https://doi.org/10.1038/s43246-026-01341-4}
}

Please also state the refjax version you used (e.g. 0.1.1). Citation metadata is in CITATION.cff; GitHub's "Cite this repository" button reads it.

Having trouble? Feature request? Raise an issue on the GitHub issues page.

Metadata

Release files for refjax 0.1.1

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

Source distribution (sdist)

Source distribution for refjax 0.1.1
File Size Uploaded Provenance Actions
refjax-0.1.1.tar.gz 13.1 kB With provenance Details for refjax-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for refjax 0.1.1
File Interpreter ABI Platform Actions
refjax-0.1.1-py3-none-any.whl Python 3 none any Details for refjax-0.1.1-py3-none-any.whl

Total release size: 22.3 kB

Release files / refjax-0.1.1.tar.gz

Download URL refjax-0.1.1.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e48b6b00af0038ff8452fc3ff6b5e8d92378490e5eccf30bf11fcc66eaece002
BLAKE2b-256 checksum
How to use checksums
cc20a21e26616c8de7b4a1674dfabe7e916a0e684bf74a9572f5fb0c052f9f2d
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 15, 2026.

Transparency log

Release files / refjax-0.1.1-py3-none-any.whl

Download URL refjax-0.1.1-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57127877b696a8dae2bc7fc2ab09cfa007d8cd99da5c19bdc00d173e26a7a212
BLAKE2b-256 checksum
How to use checksums
4b644b286c2b3180a190ccaa77314f359ff184920581b19b5118323d058c8829
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 15, 2026.

Transparency log

Security for refjax 0.1.1

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 refjax-0.1.1.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 15, 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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

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