Skip to main content

Free-Form SAS Inversion for SAXS/SANS

Tests

A package for the free-form inversion of Small Angle Scattering (SAS) problems arising from X-ray and Neutron sources.

This is a much improved algorithm and implementation over that proposed in the paper: https://journals.iucr.org/j/issues/2022/04/00/jl5041/index.html

Installation

pip install ffsi

Computations run on the GPU automatically when CuPy is installed, and on the CPU with NumPy otherwise. To use an NVIDIA GPU, install the CuPy package matching your CUDA version (shown by nvidia-smi), e.g. pip install cupy-cuda13x for CUDA 13 or pip install cupy-cuda12x for CUDA 12.

Quick Start

Invert 1D SANS or SAXS data for a free-form distribution of sphere radii:

import numpy as np
from ffsi.api import invert

# three columns: q (1/A), I(q) and its standard deviation dI(q)
q, intensity, intensity_std = np.loadtxt("data.txt", unpack=True)

result = invert(
    "sphere", q, intensity, intensity_std,
    grids={"r": (400, 800, 1000)},  # radius bins: (min, max, number of bins) in A
    sld=1.0, sld_solvent=0.0,       # scattering length densities in 1e-6/A^2
    sigma=5.0,                      # smoothing weight (None for no smoothing)
)

r = result.distribution("r")
print(f"volume fraction {result.scale:.3g}, chi2 {result.chi2:.3g}")
print(f"most probable radius {r.grid[np.argmax(r.weights)]:.0f} A")

The other models are "cylinder" and "ellipsoid" (1D), and "cylinder2d" and "ellipsoid2d" (2D, with q given as the (qx, qy) axes of the detector grid); grids takes one entry per model parameter (r for spheres; l and r for cylinders; rp and re for ellipsoids; plus the orientation angles theta and phi, in radians, for the 2D models). See help(invert) for resolution smearing and the full list of options. Solver options such as the iteration limit can be passed to invert with user_options={'maxit': ...}. If the solver stops without converging, GALAHAD issues a RuntimeWarning; Python shows this only once per session by default, so call warnings.simplefilter("always", RuntimeWarning) to see it for every fit.

SAS Inversion

Small Angle Scattering is used to probe and analyse the nanoscale structure of materials. An excellent introduction to (fixed-form) SAS inversion can be found in: https://journals.iucr.org/j/issues/2021/06/00/gj5274/gj5274.pdf

Free-Form SAS Inversion

Free-form SAS inversion seeks to determine the distributions of structural properties of nanoparticles (e.g. radius, length), but crucially (unlike conventional SAS inversion) does not make any assumptions about the forms these distributions take (hence free-form).

A Small Angle Scattering experiment records the intensity, i.e. squared amplitude, $I(q)$ of the scattered wave as a function of the scattering vector $q$. The scattering vector $q$ is itself a function of the small angle $\theta$ and is given by

q = \frac{4 \pi}{\lambda} \sin\left(\frac{\theta}{2}\right)

where $\lambda$ is the wavelength of the radiation source. For small angles, $\sin(\theta) \approx \theta$ and the scattering vector reduces to

q \approx \frac{2 \pi}{\lambda}\theta

i.e. essentially the small angle normalised by the wavelength of the radiation.

Form Factors (aka Green's Functions)

The form factor $F(q)$ describes the intensity $I(q)$ of scattering from a single nanoparticle over a full range of q and constitutes its SAS fingerprint. For example, the form factor $F(q)$ for a sphere of radius $r$ is given via $j_1(z)$ the spherical Bessel function of the first kind as

F(q) = \left[ V(r)\Delta\rho \dfrac{3j_1(qr)}{qr} \right]^2

where $V$ is the sphere volume and $\Delta\rho$ the scattering length density difference.

This repository currently contains the form factors for:

More form-factors will be added as the project progresses (as the code in this repository is vectorized and GPU accelerated, we are unable to directly use the form factors from SasView). They nevertheless reproduce the intensities of SasView's model library, sasmodels (version 1.1, bundled with SasView 6.2), to rounding error, including its adaptive Gauss-Legendre orientation averaging for the 1D cylinder and ellipsoid, and are tested against it.

Polydispersity

In a real SAS experiment, we are likely to see a population of nanoparticles that possess size and/or orientational distributions, this is called polydispersity. The resultant intensity $I(q)$ is then averaged over the distributions. For example, for spheres with different radii,

I(q) = \int F(q,r) w(r) dr

where $w(r)$ is the distribution over the radius.

Running the Tests

pip install -e . pytest sasmodels
pytest tests

The comparisons against SasView's sasmodels are skipped if it is not installed.

Metadata

Release files for ffsi 0.2.2

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

Source distribution (sdist)

Source distribution for ffsi 0.2.2
File Size Uploaded Provenance Actions
ffsi-0.2.2.tar.gz 97.2 kB With provenance Details for ffsi-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 194.4 kB

Release files / ffsi-0.2.2.tar.gz

Download URL ffsi-0.2.2.tar.gz
Size 97.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8254471efc7034a3146a3d4d5020f981306dfd2906fc4651efe30d505a618e7c
BLAKE2b-256 checksum
How to use checksums
630a485d2b4515a0a7bd6e92c2357615c0ce1722a39a7c5dfbff97b1ad612d33
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 Dec 1, 2026.

Transparency log

Release files / ffsi-0.2.2-py3-none-any.whl

Download URL ffsi-0.2.2-py3-none-any.whl
Size 97.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4e181904b2e1f2adbf0d29c91decfcf10900ff609e0aed8088cdbd22e4e184f
BLAKE2b-256 checksum
How to use checksums
8b2892705c888e4107d2fa83eac65bc0900e039615a3dc43fe8fc6aa41d599dd
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 Dec 1, 2026.

Transparency log

Security for ffsi 0.2.2

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 ffsi-0.2.2.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 Dec 1, 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