'''
Smooth random field generation. Smoothness may be specified either as a FWHM or
as an LKC:
.. math::
\\mathrm{LKC} = \\sqrt{4\\log 2}\\, \\frac{Q-1}{\\mathrm{FWHM}}
'''
# Copyright (C) 2026 Todd Pataky
from __future__ import annotations
from math import log, sqrt
import numpy as np
from . import _errors
import rft1d
SQRT4LOG2 = sqrt(4.0 * log(2.0))
[docs]
def lkc2fwhm(lkc, Q):
'''
Convert Lipschitz-Killing curvature to FWHM for a continuum of *Q* nodes
(same as :func:`spm1d.geom.lkc2fwhm`).
:Parameters:
- *lkc* --- Lipschitz-Killing curvature (positive)
- *Q* --- number of continuum nodes
:Returns:
- FWHM in nodes (float)
:Example:
>>> import spm1d
>>> print( round( spm1d.rng.lkc2fwhm( 5, 101 ), 3 ) )
33.302
'''
lkc = float(lkc)
if lkc <= 0:
raise _errors.SPM1DRngError(f'lkc must be positive; the given value is {lkc}.')
return SQRT4LOG2 * (int(Q) - 1) / lkc
[docs]
def fwhm2lkc(fwhm, Q):
'''
Convert FWHM to Lipschitz-Killing curvature for a continuum of *Q* nodes
(same as :func:`spm1d.geom.fwhm2lkc`).
:Parameters:
- *fwhm* --- smoothness in nodes (positive)
- *Q* --- number of continuum nodes
:Returns:
- Lipschitz-Killing curvature (float)
:Example:
>>> import spm1d
>>> print( round( spm1d.rng.fwhm2lkc( 20, 101 ), 3 ) )
8.326
'''
fwhm = float(fwhm)
if fwhm <= 0:
raise _errors.SPM1DRngError(f'fwhm must be positive; the given value is {fwhm}.')
return SQRT4LOG2 * (int(Q) - 1) / fwhm
def resolve_smoothness(Q, fwhm=None, lkc=None):
'''
Resolve the smoothness of a continuum given either a FWHM or an LKC.
:Parameters:
- *Q* --- number of continuum nodes (1 for 0D data)
- *fwhm* --- full-width at half-maximum in node units
- *lkc* --- Lipschitz-Killing curvature
:Returns:
- (fwhm, lkc). Both are ``None`` for 0D data.
'''
if int(Q) == 1:
if (fwhm is not None) or (lkc is not None):
raise _errors.SPM1DRngError('Smoothness (fwhm or lkc) is meaningless for 0D data (Q=1).')
return None, None
if (fwhm is not None) and (lkc is not None):
raise _errors.SPM1DRngError('Specify smoothness as either "fwhm" or "lkc", not both.')
if (fwhm is None) and (lkc is None):
raise _errors.SPM1DRngError('1D data require a smoothness: specify either "fwhm" or "lkc".')
if lkc is not None:
fwhm = lkc2fwhm(lkc, Q)
fwhm = float(fwhm)
if fwhm <= 0:
raise _errors.SPM1DRngError(f'fwhm must be positive; the given value is {fwhm}.')
return fwhm, fwhm2lkc(fwhm, Q)
[docs]
def estimate_fwhm(Y):
'''
Estimate FWHM of a set of continua.
:Parameters:
- *Y* --- (J,Q) array or (J,Q,I) array of vector-valued continua
:Returns:
- Estimated FWHM (float).
:Example:
Smoothness of ten simulated responses from Pataky et al. (2015).
>>> import spm1d
>>> import jikudata as jd
>>> y = jd.SimulatedPataky2015a().y # (10,101) array
>>> print( round( spm1d.rng.estimate_fwhm( y ), 2 ) )
26.92
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
Y = np.asarray(Y, dtype=float)
R = Y - Y.mean(axis=0)
# Imported here so that the generators stay independent of the statistics
# they validate.
from .. geom import _smoothness as _geom
if R.ndim == 3:
return float( _geom.estimate_fwhm_mv(R, method='taylor2008') )
return float( _geom.estimate_fwhm(R, method='taylor2007') )
[docs]
def estimate_lkc(Y):
'''
Estimate Lipschitz-Killing curvature of a set of continua
(``estimate_fwhm`` converted with ``fwhm2lkc``).
:Parameters:
- *Y* --- (J,Q) array or (J,Q,I) array of vector-valued continua
:Returns:
- Estimated LKC (float).
:Example:
Lipschitz-Killing curvature of ten simulated responses from Pataky et
al. (2015).
>>> import spm1d
>>> import jikudata as jd
>>> y = jd.SimulatedPataky2015a().y # (10,101) array
>>> print( round( spm1d.rng.estimate_lkc( y ), 3 ) )
6.186
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
return fwhm2lkc(estimate_fwhm(Y), np.shape(Y)[1])
def white_noise(J, Q, fwhm=None, rng=None, pad=True):
'''
Generate *J* independent unit-variance noise responses.
:Parameters:
- *J* --- number of responses
- *Q* --- number of continuum nodes (1 for 0D data)
- *fwhm* --- (**1D data only**) smoothness in node units
- *rng* --- a **numpy.random.Generator** if present, otherwise ``None``
- *pad* --- pad the continuum before smoothing to avoid edge effects
:Returns:
- (J,Q) array. Rows: smooth Gaussian fields (1D) or single standard normal
values (0D).
'''
J, Q = int(J), int(Q)
if Q == 1:
e = (rng.standard_normal(J) if rng is not None else np.random.randn(J))
return e.reshape(J, 1)
# rft1d uses numpy's global random state. It is therefore seeded from rng
# for reproducibility
if rng is not None:
seed = int(rng.integers(0, 2 ** 31 - 1))
state = np.random.get_state()
np.random.seed(seed)
try:
Y = rft1d.random.randn1d(J, Q, fwhm, pad=pad)
finally:
np.random.set_state(state)
else:
Y = rft1d.random.randn1d(J, Q, fwhm, pad=pad)
return np.asarray(Y, dtype=float).reshape(J, Q)