'''
Random field theory (RFT) probabilities.
'''
# Copyright (C) 2026 Todd Pataky
import numpy as np
from . _results import InferenceResults1D
from .. geom import assemble_clusters
from . _general import p_corrected_dirn, z_corrected_dirn
def _clusterlevel_inference(calc, z, zc, fwhm, dirn=0, circular=False):
clusters = assemble_clusters(z, zc, dirn=dirn, circular=circular)
sc = 2 if (dirn==0) else 1 # multiply by 2 if two-tailed inference
for i,c in enumerate( clusters ):
k,u = c.extent / fwhm, abs(c.height)
p = min( 1.0, sc * calc.p.cluster(k, u) )
clusters[i] = c._as_inference_cluster(k, p, metric_type='extent_resels')
return clusters
def _setlevel_inference(calc, zc, clusters, dirn=0):
c = len(clusters)
if c==0:
p = None
else:
k = min([c.metric for c in clusters]) # min extent_resels
sc = 2 if (dirn==0) else 1 # multiply by 2 if two-tailed inference
p = min( 1.0, sc * calc.p.set(c, k, zc) )
return p
[docs]
def rft(STAT, z, df, fwhm, resels, alpha=0.05, circular=False, withBonf=True, dirn=0, zc=None):
'''
Random field theory (RFT) inference on a 1D test statistic field.
:Parameters:
- *STAT* --- test statistic: ``'T'``, ``'F'``, ``'T2'`` or ``'X2'``
- *z* --- (Q,) test statistic field (optionally masked)
- *df* --- degrees of freedom as a two-element tuple
- *fwhm* --- residual smoothness in nodes (for cluster extents only)
- *resels* --- search region resel counts from
:func:`spm1d.geom.resel_counts`
- *alpha* --- Type I error rate (default 0.05)
- *circular* --- True for circular domains
- *withBonf* --- if True (default), cap RFT probabilities at Bonferroni
values
- *dirn* --- inference direction: 0 (two-tailed), +1 or -1 (one-tailed)
- *zc* --- critical height replacing the ``alpha``-derived value
:Returns:
- **InferenceResults1D** object
:Example:
RFT inference on the t field of a one-sample t test using the
smoothness and resel counts the test estimated.
>>> import spm1d
>>> import jikudata as jd
>>> y = jd.SimulatedPataky2015b().y # (10,101) array
>>> t = spm1d.stats.ttest( y, 0 )
>>> results = spm1d.prob.rft( 'T', t.z, t.df, t.fwhm, t.resels, alpha=0.05, dirn=1 )
>>> print( round( results.zc, 3 ), results.h0reject, round( results.p_set, 3 ) )
3.218 True 0.045
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
import rft1d
_stats = ["T", "F", "T2", "X2"]
if STAT not in _stats:
from .. exceptions import SPM1DError
raise SPM1DError( f'Unknown statistic: {STAT}. Must be one of: {_stats}' )
if isinstance(dirn, bool) or (dirn not in (-1, 0, 1)):
from .. exceptions import SPM1DError
raise SPM1DError( f'"dirn" must be -1, 0 or 1, not {dirn!r}.' )
if STAT != 'T':
dirn = 1
a = 0.5 * alpha if (dirn==0) else alpha
# A masked z is a search region. The Bonferroni fallback counts its nodes.
nNodes = z.count() if np.ma.is_masked(z) else z.size
calc = rft1d.prob.RFTCalculatorResels(STAT=STAT, df=df, resels=resels, withBonf=withBonf, nNodes=nNodes)
zc = calc.isf(a) if (zc is None) else float(zc)
clusters = _clusterlevel_inference(calc, z, zc, fwhm, dirn=dirn, circular=circular)
p_set = _setlevel_inference(calc, zc, clusters, dirn=dirn)
# No point-level p values for RFT: the exceedance probability is accurate
# only for small p (see p_zmax).
_z = z_corrected_dirn(z, dirn)
p_zmax = p_corrected_dirn( float( calc.sf( float(_z.max()) ) ), dirn )
results = InferenceResults1D( 'rft', STAT, z, alpha, df, dirn, zc, p_set, clusters, None, isparametric=True, p_zmax=p_zmax )
return results