'''
Test statistic results: ``SPM0D`` and ``SPM1D``.
'''
# Copyright (C) 2026 Todd Pataky
import warnings
from . _base import _SPM0DBase, _SPM1DBase
[docs]
class SPM0D(_SPM0DBase):
'''
Statistical parametric map (SPM) object.
**Main attributes**
``fn_name``
name of the ``spm1d.stats`` function
``STAT``
test statistic type: ``'T'``, ``'F'``, ``'T2'`` or ``'X2'``
``z``
test statistic value
``df``
degrees of freedom; when a covariance model is estimated these are the
Satterthwaite effective degrees of freedom.
``dim``
data dimensionality (0)
``residuals``
model residuals
**Model attributes**
``design``
experimental design object
``fit``
fitted model object
``contrast``
contrast vector or matrix
**ANOVA and regression attributes**
``name``
effect name (ANOVA only)
``ss``, ``sse``, ``ms``, ``mse``
(**F statistics only**) effect and error sums of squares and mean
squares
``r``
(**regress only**) correlation coefficient
**Indicator attributes**
``isanova``, ``isregress``, ``ismultivariate``, ``ismultigroup``, ``iswilks``, ``isinference``
'''
[docs]
def inference(self, alpha=0.05, method='param', **kwargs):
'''
Statistical inference on a 0D test statistic.
:Parameters:
- *alpha* --- Type I error rate (default 0.05)
- *method* --- ``'param'`` (parametric, default) or ``'perm'``
(permutation)
- *dirn* --- inference direction: 0 (two-tailed), +1 or -1 (one-tailed)
- *nperm* --- (**method='perm' only**) number of permutations. Default:
all unique permutations if at most
10000 and 10000 random permutations otherwise. -1: all unique
permutations.
- *two_tailed* --- DEPRECATED. Use ``dirn=0`` and ``dirn=1`` for True
and False, respectively.
- *iterations* --- DEPRECATED (use ``nperm``)
- *force_iterations* --- DEPRECATED (use ``nperm``)
:Returns:
- **SPMi0D** object
:Example:
>>> spmi = spm1d.stats.ttest2( yA, yB ).inference( 0.05 )
'''
# Checked first: the parser's message for an invalid method lists the
# valid values without saying why "rft" is absent.
from .. prob._methods import check_method
check_method( method, 0 )
# 0D has no search region.
self._check_statistic_defined( None )
from .. stats._argparsers import InferenceArgumentParser0D
parser = InferenceArgumentParser0D(self.STAT, method)
parser.parse( alpha, **kwargs )
# parse() rewrites the deprecated aliases into parser.kwargs, not into
# the caller's dict.
kwargs = parser.kwargs
if (self.STAT=='T') and ('dirn' not in kwargs):
kwargs.update( {'dirn':0} )
if method == 'param':
from .. prob import param
dfa = self.df
# The post hoc lists pass the critical value as "_zc". prob/ knows
# it as "zc".
pkwargs = { ('zc' if k == '_zc' else k): v for k, v in kwargs.items() }
iresults = param(self.STAT, self.z, dfa, alpha=alpha, **pkwargs)
elif method == 'perm':
from .. prob._perm import perm
from . _base import check_perm_applicable, perm_args
QQ = check_perm_applicable( self )
iresults = perm(self.STAT, self.z, alpha=alpha, testname=self._testname, args=perm_args(self), effect=self._perm_effect, QQ=QQ, **kwargs)
return self._as_spmi(iresults, kwargs)
[docs]
class SPM1D(_SPM1DBase):
'''
Statistical parametric map (SPM) object.
**Main attributes**
``fn_name``
name of the ``spm1d.stats`` function
``STAT``
test statistic type. It is ``'T'`` (t), ``'F'``, ``'T2'`` (Hotelling's
T²) or ``'X2'`` (χ²)
``z``
test statistic continuum as a ``(Q,)`` array
``df``
degrees of freedom. For a t statistic the first value is always 1 and
the second value is the error degrees of freedom. When a covariance
model is estimated these are the Satterthwaite effective degrees of
freedom. They are then generally not whole numbers.
``dim``
data dimensionality (1)
``residuals``
model residuals as a ``(J,Q)`` array. The smoothness (1D) and normality
tests are computed from them
**Model attributes**
``design``
experimental design including the design matrix, factors and contrasts
``fit``
fitted model including parameter estimates (``fit.b``), residuals
(``fit.e``), sums of squares and ``fit.wilks`` for multivariate
procedures
``contrast``
the contrast vector or matrix that defines the tested effect
**ANOVA and regression attributes**
``name``
effect name (e.g., ``'Main A'``) for ANOVA effects, otherwise ``None``
``ss``, ``sse``, ``ms``, ``mse``
(**F statistics only**) effect and error sums of squares and mean
squares
``r``
(**regress only**) correlation coefficient
**Indicator attributes**
``isanova``, ``isregress``, ``ismultivariate``, ``ismultigroup``, ``iswilks``, ``isinference``
``True`` for ANOVA procedures, regression, multivariate procedures,
designs with more than one group, Wilks' Λ statistics (reported as
Rao's F) and inference objects respectively
**1D attributes**
``Q``
number of domain nodes
``roi``
region of interest as a boolean ``(Q,)`` array if present, otherwise
``None`` for the whole domain
``fwhm``
estimated smoothness expressed as a full width at half maximum (FWHM).
It is estimated from the model residuals
``lkc``
Lipschitz–Killing curvature. This is an alternative expression of the
smoothness and domain size
``resels``
resolution element counts. These are the number of separate domain
segments and the domain length divided by the FWHM
'''
[docs]
def inference(self, alpha=0.05, method='rft', roi=None, **kwargs):
'''
Statistical inference on a 1D test statistic.
:Parameters:
- *alpha* --- Type I error rate (default 0.05)
- *method* --- inference method: ``'rft'`` (default), ``'perm'``,
``'fdr'``, ``'bonf'`` or ``'uncorr'``
- *roi* --- boolean region of interest array (default None: whole
domain)
- *dirn* --- inference direction: 0 (two-tailed), +1 or -1 (one-tailed)
- *nperm* --- (**method='perm' only**) number of permutations. Default:
all unique permutations if at most
10000 and 10000 random permutations otherwise. -1: all unique
permutations.
- *circular* --- True for circular domains
- *withBonf* --- (**method='rft' only**) if True (default), cap RFT
probabilities at Bonferroni values
- *cluster_metric* --- (**method='perm' only**) ``'MaxClusterExtent'``
(default), ``'MaxClusterHeight'`` or ``'MaxClusterIntegral'``
- *approach* --- (**method='fdr' only**) ``'bh'`` (Benjamini-Hochberg,
default) or ``'by'`` (Benjamini-Yekutieli)
- *two_tailed* --- DEPRECATED. Use ``dirn=0`` and ``dirn=1`` for True
and False, respectively.
- *iterations* --- DEPRECATED (use ``nperm``)
- *force_iterations* --- DEPRECATED (use ``nperm``)
:Returns:
- **SPMi1D** object
:Example:
>>> spmi = spm1d.stats.ttest2( yA, yB ).inference( 0.05 )
>>> spmi = spm1d.stats.ttest2( yA, yB ).inference( 0.05, method='perm', nperm=1000, dirn=1 )
'''
from .. prob._methods import check_method, check_alpha, check_kwargs
check_method( method, 1 )
check_alpha( alpha )
kwargs = check_kwargs( method, kwargs, STAT=self.STAT )
if (self.STAT == 'T') and ('dirn' not in kwargs):
kwargs['dirn'] = 0
# The region reaches prob/ as a masked z plus the region-local
# smoothness. No prob/ function takes roi.
_roi = self._resolve_roi( roi )
# A constant node matters only where a p value is computed, so the
# check waits for the region.
self._check_statistic_defined( _roi )
z, sm = self._roi_view( _roi )
# roi is recorded so that _reexec reproduces it.
ikwargs = dict( kwargs )
if (roi is not None) and (_roi is not None):
ikwargs['roi'] = _roi
# The post hoc lists pass the critical height as "_zc". prob/ knows it
# as "zc".
pkwargs = { ('zc' if k == '_zc' else k): v for k, v in kwargs.items() }
if method == 'rft':
if self._isnormality:
# The K2 field is not a chi-square field in the tail, so it
# has its own EC density.
from .. stats._diag.ec import rft_k2
iresults = rft_k2(z, self.df, self.design.J, sm.fwhm,
sm.resels, alpha=alpha, **pkwargs)
else:
from .. prob import rft
iresults = rft(self.STAT, z, self.df, sm.fwhm, sm.resels, alpha=alpha, **pkwargs)
elif method == 'perm':
from .. prob._perm import perm
# perm takes the region as a parameter so that every permuted field
# is masked.
from . _base import check_perm_applicable, perm_args
QQ = check_perm_applicable( self )
iresults = perm(self.STAT, z, alpha=alpha, testname=self._testname, args=perm_args(self), dim=1, roi=_roi, effect=self._perm_effect, QQ=QQ, **kwargs)
elif method == 'fdr':
from .. prob import fdr
iresults = fdr(self.STAT, z, self.df, alpha=alpha, **kwargs)
elif method == 'bonf':
from .. prob import bonf
iresults = bonf(self.STAT, z, self.df, alpha=alpha, **kwargs)
elif method == 'uncorr':
from .. prob import uncorr
from .. exceptions import SPM1DUncorrWarning
from .. _settings import _user_stacklevel
# warns on every call (see SPM1DUncorrWarning)
warnings.warn(
'method="uncorr" yields uncorrected probabilities which ignore the domain-level '
'multiple comparisons problem and which are not valid for domain-level analysis. '
'These uncorrected probabilities are provided in spm1d as a baseline for comparison '
'with the other methods. To force-allow access to these probabilities without a '
'warning use: \n\nwarnings.filterwarnings("ignore", category=spm1d.exceptions.SPM1DUncorrWarning).',
SPM1DUncorrWarning, stacklevel=_user_stacklevel() )
iresults = uncorr(self.STAT, z, self.df, alpha=alpha, **kwargs)
spmi = self._as_spmi(iresults, ikwargs)
if _roi is not None:
# fwhm, lkc and resels are measured over the region.
spmi._sm = sm
spmi.roi = _roi
spmi._teststat.z = z
return spmi