Metadata-Version: 2.4
Name: reflectosim
Version: 0.1.0
Summary: X-ray reflectivity forward physics and synthetic data generation
Keywords: x-ray reflectivity,XRR,simulation,synthetic data,machine learning
Author: Isaac Yong
Author-email: Isaac Yong <isaacyong@naver.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Typing :: Typed
Requires-Dist: numpy>=2.0
Requires-Dist: numba>=0.61
Requires-Dist: scipy>=1.13
Requires-Dist: periodictable>=2.1.0
Requires-Dist: h5py>=3.10 ; extra == 'hdf5'
Requires-Dist: torch>=2.5 ; extra == 'torch'
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/NaNIsaac/ReflectoSim
Project-URL: Repository, https://github.com/NaNIsaac/ReflectoSim
Project-URL: Issues, https://github.com/NaNIsaac/ReflectoSim/issues
Project-URL: Changelog, https://github.com/NaNIsaac/ReflectoSim/blob/main/CHANGELOG.md
Provides-Extra: hdf5
Provides-Extra: torch
Description-Content-Type: text/markdown

# ReflectoSim

[![Tests](https://github.com/NaNIsaac/ReflectoSim/actions/workflows/ci.yml/badge.svg)](https://github.com/NaNIsaac/ReflectoSim/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)

<img src="https://raw.githubusercontent.com/NaNIsaac/ReflectoSim/main/docs/media/curves.png" width="720" alt="Four curves drawn from examples/lab_films.toml: the reflectivity before noise and the Poisson counts">

Realistic synthetic X-ray reflectivity (XRR) data for machine learning: priors over layer stacks and
instruments, counting noise, and a dataset format that records how to regenerate it.

It is for anyone training or testing an inverse model for XRR who needs simulated curves that look
like measured ones: random multilayers with roughness tied to thickness and a minimum contrast
between neighbours, a native oxide placed where you say, Gaussian resolution, footprint, Kα₂, 2θ
offset, background and Poisson counts. The package holds all the forward physics (Parratt
recursion with Névot-Croce roughness, the resolution integral, a differentiable torch forward) and
the generation on top of it. [ReflectoFit](https://github.com/NaNIsaac/ReflectoFit), the fitting
engine, depends on it, so a network trained on these curves and a fit of the same measurement use
the same physics.

Status: early development.

## Visual overview

![ReflectoSim prior-to-dataset pipeline](docs/media/pipeline.svg)

The **example prior bounds** below are configurable input ranges, not empirical distributions. Coupled thickness/roughness and SLD-contrast constraints can alter the accepted distributions.

![Parameter bounds from examples/lab_films.toml](docs/media/prior-ranges.svg)

The XRR curves at the top of this README are generated by the simulator; solid curves show the pre-noise signal and dots show normalized Poisson counts. For a detailed interpretation, the layer-order diagram, units, reproducibility and caveats, see the [visual guide](docs/visual-guide.md).

## Install

Not on PyPI yet. From a clone, with [uv](https://docs.astral.sh/uv/):

```sh
git clone https://github.com/NaNIsaac/ReflectoSim
cd ReflectoSim
uv sync
```

## First run

Simulate 1000 curves of one to three films on SiO₂ on Si from the example prior, then summarise the
file:

<!-- test -->
```sh
uv run reflectosim sim --prior examples/lab_films.toml --n 1000 --seed 20261024,1 -o train.npz
uv run reflectosim describe train.npz
```

`train.npz` holds `q`, `counts`, `reflectivity` (before noise), `counts_scale` and one array per
parameter (`layer0.thickness`, `oxide.sld`, `tth_offset`, ...; NaN where a curve has fewer layers).
Its metadata holds the priors, the seed and the versions; `reflectosim.priors.from_metadata` turns it
back into priors and the same seed gives the same file. Use a `.h5` name for HDF5 (`uv sync --extra
hdf5`).

## Priors

A prior is a TOML file; `examples/lab_films.toml` shows every field. The native oxide position has no
default: `oxide = "under"` puts it on the substrate, `"over"` on top of the films, `"none"` leaves it
out. The two orders give curves that differ by tens of percent, so every prior states its choice.

## Python

```python
import numpy as np
from reflectosim import InstrumentPrior, StackPrior, simulate, tth_to_q

stack = StackPrior(
    n_layers=(1, 3),
    thickness=(20, 250),
    sld=(5, 80),
    roughness=(2, 10),
    min_contrast=4.0,
    oxide="none",
)
instrument = InstrumentPrior(
    sigma_q=(5e-4, 3e-3), tth_offset=(-0.01, 0.01), log_background=(-8.5, -7.0), log_counts=(6, 8)
)
batch = simulate(
    stack, instrument, tth_to_q(np.linspace(0.05, 8, 500)), n=10_000, seed=(20261024, 1)
)
batch.counts, batch.reflectivity, batch.params["layer0.thickness"]
```

## Development

```sh
uv run pytest
```

## License

MIT
