Metadata-Version: 2.5
Name: structure-factor-v2
Version: 2.2.0
Summary: Structure factor S(k) of point sets in the unit cube: scattering intensity, Kaiser taper, NUFFT, plots
Project-URL: Repository, https://github.com/adecacqueray/structure-factor-v2
Project-URL: Issues, https://github.com/adecacqueray/structure-factor-v2/issues
License: MIT
License-File: LICENSE
Keywords: Kaiser taper,NUFFT,curl noise,finufft,hyperuniformity,point process,power spectrum,scattering intensity,structure factor
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.9
Requires-Dist: finufft
Requires-Dist: matplotlib
Requires-Dist: numpy
Provides-Extra: gpu12
Requires-Dist: cufinufft; extra == 'gpu12'
Requires-Dist: cupy-cuda12x; extra == 'gpu12'
Provides-Extra: gpu13
Requires-Dist: cufinufft; extra == 'gpu13'
Requires-Dist: cupy-cuda13x; extra == 'gpu13'
Provides-Extra: keops
Requires-Dist: pykeops; extra == 'keops'
Provides-Extra: torch
Requires-Dist: torch; extra == 'torch'
Description-Content-Type: text/markdown

# structure-factor-v2

[![PyPI](https://img.shields.io/pypi/v/structure-factor-v2.svg)](https://pypi.org/project/structure-factor-v2/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/adecacqueray/structure-factor-v2/blob/main/LICENSE)
[![GitHub](https://img.shields.io/badge/GitHub-adecacqueray%2Fstructure--factor--v2-181717?logo=github)](https://github.com/adecacqueray/structure-factor-v2)

**Structure factor $S(k)$ of point sets in the unit cube $[0, 1)^D$**, estimated by the
scattering intensity, with fast transforms (finufft / cufinufft) and ready-made log-log plots.

For a point set $(x_j)_{1 \le j \le N} \subset [0, 1)^D$, the structure factor (or power spectrum) is

```math
S(k) = \frac{1}{N} \left| \sum_{j=1}^{N} e^{-2i\pi \langle k, x_j \rangle} \right|^2,
\qquad k \in \mathbb{Z}^D \setminus \{0\}.
```

$S(k)$ measures density fluctuations in Fourier space. A pattern is hyperuniform (blue noise) when
$S(k) \to 0$ as $|k| \to 0$, and hyperuniform of order $\alpha > 0$ when $S(k) \sim |k|^\alpha$.

---

## Installation

```bash
pip install structure-factor-v2                  # CPU: numpy + finufft
# pip install "structure-factor-v2[gpu12]"       # GPU: [gpu12] or [gpu13], cupy + cufinufft
```

---

## Quick start

```python
import structure_factor_v2 as sf
import numpy as np

N, D = 10_000, 2
x = sf.jittered_lattice(N, D)                # (N, D) points in [0, 1)^D, hyperuniform


# ===== Bragg peaks detection: =======
sf.certify_no_bragg(x)


# ===== Full vizualisation ===========
sf.plot(x)                                   # scatter plot
sf.plot_structure_factor(x)                  # log-log S(k), raw values + radial average
```

Built-in samplers:

```python
sf.config.compute.precision = "float32"      # fast, recommanded default. Use float64 for 
                                             # precise plot if S(k) is very small, essentially 
                                             # in the stealthy case or for a pure korobov lattice

sf.plot_structure_factor(np.random.rand(N, D))          # Poisson: S = 1
sf.plot_structure_factor(sf.jittered_lattice(N, D))     # noisy crystal (hyperuniform yet free of Bragg peaks)
sf.plot_structure_factor(sf.curled_lattice(N, D))       # curl noise (iid jitter replaced by a divergence-free perturbation)

sf.config.compute.precision = "float64"
sf.plot_structure_factor(sf.korobov_lattice(N, D))      # pure crystal, stealthy (yet with bragg peaks)
```

![example](example.png)

---

## Normalisation conventions

- `structure-factor-v2` only accepts points in the unit cube $[0, 1)^D$. If your points live in
  another domain, first extract a cubic sub-window, rescale it to $[0, 1)^D$, and use
  `trust_boundary=False` (see below) to compensate for the induced boundary effects.
- To make plots comparable across $N$ and $D$, the x-axis shows $|k| / N^{1/D}$: an abscissa of 1
  corresponds to a wavelength of the order of the mean interparticle distance $N^{-1/D}$.

---

## Two estimators: `trust_boundary`

| `trust_boundary` | When                                                                                     |
|------------------|------------------------------------------------------------------------------------------|
| `True`           | Default: the points compensate for boundary effects by themselves (e.g. they are periodic) |
| `False`          | The points need a procedure to compensate for boundary effects (here, a Kaiser taper)    |
| `"both"`         | Compare the two estimators                                                               |

```python
sf.plot_structure_factor(x, trust_boundary="both")
```

---

## Settings: `sf.config`

To keep the API simple and easy to use, functions expose only the essential parameters as arguments. All other settings can be configured at the module level through `sf.config`, where they are organized by topic.

These include precision, computational backend, NUFFT tolerance, Kaiser parameters, Bragg threshold, plotting options, lattice parameters, and more. Settings defined in `sf.config` apply globally to all subsequent function calls.


```python
# ======== config =====================
sf.config.compute.precision = "float64"   # default "auto": float32 (much faster) iff the input points are float32
sf.config.compute.backend = "torch"       # default "auto", see the table below
sf.config.taper.beta = 5                  # Kaiser parameter, default None: automatic
sf.config.bragg.threshold = 50            # S >= threshold: Bragg peak (default 100)
#and so one...
# ======== reset =======================
sf.config.reset()                         # back to the defaults
```

Sections: `compute`, `nufft`, `direct_sums`, `wave_vectors`, `taper`, `bragg`, `average`,
`plot`, `lattice`, `curl_noise` (each setting is documented in `structure_factor_v2/config.py`).

Backends (`sf.config.compute.backend`), assuming their dependencies are installed:

| `backend`     | method                                   | dimensions          |
|---------------|------------------------------------------|---------------------|
| `"finufft"`   | type-1 non-uniform FFT, CPU              | D <= 6 [^slicing]   |
| `"cufinufft"` | same, GPU                                | D <= 6 [^slicing]   |
| `"keops"`     | direct sums, pykeops (GPU if available)  | any                 |
| `"torch"`     | direct sums, torch (CUDA if available)   | any                 |
| `"numpy"`     | direct sums, numpy                       | any                 |
| `"cupy"`      | direct sums, cupy                        | any                 |

---

## References

- **Korobov lattices**: I. H. Sloan and S. Joe. *Lattice Methods for Multiple Integration.*
  Oxford University Press, 1994.
- **Curl noise jittering**: J. A. Bærentzen, J. R. Frisvad, and J. Martínez. *Curl Noise
  Jittering.* SIGGRAPH Asia 2023 Conference Papers, article 88, 2023.
  [doi:10.1145/3610548.3618163](https://doi.org/10.1145/3610548.3618163),
  code: [jonasmb/curlnoisejittering](https://github.com/jonasmb/curlnoisejittering).
  `curled_lattice` starts from a Korobov lattice instead of their triangular / cubic grids,
  and uses a random Fourier divergence-free field (exactly periodic, any dimension) instead
  of the curl of a Perlin noise; independent implementation.
- **finufft / cufinufft**: A. H. Barnett, J. F. Magland, and L. af Klinteberg.
  *A parallel non-uniform fast Fourier transform library based on an "exponential of
  semicircle" kernel.* SIAM J. Sci. Comput. 41(5), C479–C504, 2019.
  See [finufft](https://github.com/flatironinstitute/finufft).
- **structure-factor**: the original package, of which this is a rewrite,
  [For-a-few-DPPs-more/structure-factor](https://github.com/For-a-few-DPPs-more/structure-factor).

Used by [blue-sampler](https://pypi.org/project/blue-sampler/).

---

## License

MIT

[^slicing]: finufft and cufinufft natively support D <= 3, but D = 4, 5, 6 are handled with a slicing trick.