Source code for spm1d._results.diagnostics

'''
Results of the assumption tests: sphericity and equal variance.
'''

# Copyright (C) 2026  Todd Pataky

from .. util._serial import AttrDictSerial


[docs] class SphericityResults0D(AttrDictSerial): ''' Mauchly's sphericity test result for 0D data. ''' #: Within-subject effect label (e.g., ``"A"``, ``"A:B"``). effect = None def __init__(self, x2, df, p): #: Test statistic type: ``"X2"`` (chi-squared). self.STAT = 'X2' #: Chi-squared value. self.z = x2 #: Degrees of freedom ``(1, df)``. self.df = (1,df) #: p value. self.p = p @property def dfs(self): '''Degrees of freedom ``df`` as a string such as ``"(1, 21)"``.''' from .. util._str import dflist2str return dflist2str( self.df ) def __repr__(self): from .. util._rpr import DisplayParams dp = DisplayParams( self ) dp.add_default_header() dp.add( 'STAT' ) dp.add( 'z', fmt='%.5f' ) dp.add( 'df', fmt=dp.fmt['dflist2str'] ) dp.add( 'p', fmt=dp.fmt['p2string5'] ) return dp.asstr()
[docs] class SphericityResults1D(AttrDictSerial): ''' Mauchly's sphericity test result for 1D data. ''' #: Within-subject effect label (e.g., ``"A"``, ``"A:B"``). effect = None def __init__(self, x2, df, p_point): #: Test statistic type: ``"X2"`` (chi-squared). self.STAT = 'X2' #: Chi-squared field as a ``(Q,)`` array. self.z = x2 #: Degrees of freedom ``(1, df)``. self.df = (1,df) #: Point-level p values as a ``(Q,)`` array. self.p_point = p_point #: Estimated chi-squared field smoothness (full width at half maximum). self.fwhm = None @property def dfs(self): '''Degrees of freedom ``df`` as a string such as ``"(1, 21)"``.''' from .. util._str import dflist2str return dflist2str( self.df ) def __repr__(self): from .. util._rpr import DisplayParams dp = DisplayParams( self ) dp.add_default_header() dp.add( 'STAT' ) dp.add( 'z', fmt=dp.fmt['array2shortstr'] ) dp.add( 'df', fmt=dp.fmt['dflist2str'] ) dp.add( 'p_point', fmt=dp.fmt['array2shortstr'] ) dp.add( 'p', fmt=dp.fmt['p2string5'] ) if getattr(self, 'fwhm', None) is not None: dp.add( 'fwhm', fmt='%.5f' ) return dp.asstr() @property def p(self): '''Field maximum p value (smallest ``p_point``).''' return self.p_point.min() @property def zmax(self): '''Maximum of the chi-squared field.''' return self.z.max()
[docs] class SphericityResultsList(list, AttrDictSerial): ''' List of sphericity results (one per within-subject effect) accessible by effect name. ''' def __init__(self, results, effects): super().__init__( results ) #: Within-subject effect labels (``"A"``, ``"B"``, ``"A:B"``) in list #: order. self.effects = list( effects ) def __getitem__(self, i): if isinstance(i, str): if i not in self.effects: from .. exceptions import SPM1DKeyError raise SPM1DKeyError( f'"{i}" is not a within-subject effect of this result. Its effects are {self.effects}.' ) return super().__getitem__( self.effects.index(i) ) return super().__getitem__( i ) def __repr__(self): s = f'{self.__class__.__name__} ({len(self)} within-subject effects)\n' for e, r in zip( self.effects, self ): s += f'--- {e} ---\n' + r.__repr__() + '\n' return s @property def p(self): '''List of effect p values in ``effects`` order.''' return [ r.p for r in self ] def _to_dict(self): d = AttrDictSerial._to_dict( self ) d['results'] = [ r._to_dict() for r in self ] return d
[docs] class EqVarResults0D( SphericityResults0D ): ''' Equal-variance test result for 0D data. ''' pass
[docs] class EqVarResults1D( SphericityResults1D ): ''' Equal-variance test result for 1D data. ''' pass