Source code for spm1d.rng._classic

'''
Classic designs named after the corresponding analysis procedures. All accept
**ANOVA** keyword arguments.
'''

# Copyright (C) 2026  Todd Pataky

from __future__ import annotations

import numpy as np

from . _generators import ANCOVA, ANOVA, Dataset, Regression


[docs] class ANOVA1(ANOVA): ''' One-way between-subject ANOVA. *nsubj* is the number of observations per group. ''' def __init__(self, nlevels=None, nsubj=10, **kwargs): if nlevels is None: nlevels = len(nsubj) if (np.ndim(nsubj) > 0) else 3 # one size per group, otherwise three groups super().__init__((int(nlevels),), rm='', nsubj=nsubj, **kwargs)
class _SplitDataset(Dataset): '''Two-group dataset. Tuple form: (y0, y1).''' def astuple(self): A = self.factors[0] return self.Y[A == 0], self.Y[A == 1] class _OneSampleDataset(Dataset): '''One-group dataset. Tuple form: (y,).''' def astuple(self): return (self.Y,)
[docs] class TTEST(ANOVA): ''' One-sample t test (Hotelling's T2 if *ncomp* > 1). ''' def __init__(self, nsubj=10, **kwargs): super().__init__((1,), rm='', nsubj=nsubj, **kwargs)
[docs] def random(self): ''' Generate one random dataset. :Returns: - A **Dataset** instance :Example: >>> import spm1d >>> g = spm1d.rng.TTEST( nsubj=8, Q=101, fwhm=20, seed=0 ) >>> y, = g.random().astuple() >>> print( y.shape ) (8, 101) ''' d = super().random() return _OneSampleDataset(d.Y, d.factors, SUBJ=d.SUBJ, X=d.X, design=d.design)
[docs] class TTEST2(ANOVA): ''' Two-sample t test (Hotelling's T2 if *ncomp* > 1). ''' def __init__(self, nsubj=10, **kwargs): super().__init__((2,), rm='', nsubj=nsubj, **kwargs)
[docs] def random(self): ''' Generate one random dataset. :Returns: - A **Dataset** instance :Example: >>> import spm1d >>> g = spm1d.rng.TTEST2( nsubj=8, Q=101, fwhm=20, seed=0 ) >>> y0, y1 = g.random().astuple() >>> print( y0.shape, y1.shape ) (8, 101) (8, 101) ''' d = super().random() return _SplitDataset(d.Y, d.factors, SUBJ=d.SUBJ, X=d.X, design=d.design)
[docs] class TTEST_PAIRED(ANOVA): ''' Paired t test (Hotelling's T2 if *ncomp* > 1). ''' def __init__(self, nsubj=10, **kwargs): super().__init__((2,), rm='A', nsubj=nsubj, **kwargs)
[docs] def random(self): ''' Generate one random dataset. :Returns: - A **Dataset** instance :Example: >>> import spm1d >>> g = spm1d.rng.TTEST_PAIRED( nsubj=8, Q=101, fwhm=20, seed=0 ) >>> y0, y1 = g.random().astuple() >>> print( y0.shape, y1.shape ) (8, 101) (8, 101) ''' d = super().random() return _SplitDataset(d.Y, d.factors, SUBJ=d.SUBJ, X=d.X, design=d.design)
[docs] class ANOVA1RM(ANOVA): '''One-way repeated-measures ANOVA. *nsubj* is the number of subjects.''' def __init__(self, nlevels=3, nsubj=10, **kwargs): super().__init__((int(nlevels),), rm='A', nsubj=nsubj, **kwargs)
[docs] class ANOVA2(ANOVA): ''' Two-way between-subject ANOVA. *nsubj* is the number of observations per cell. ''' def __init__(self, nlevels=(2, 3), nsubj=10, **kwargs): super().__init__(nlevels, rm='', nsubj=nsubj, **kwargs)
[docs] class ANOVA2RM(ANOVA): ''' Two-way repeated-measures ANOVA (both factors repeated). ''' def __init__(self, nlevels=(2, 3), nsubj=10, **kwargs): super().__init__(nlevels, rm='AB', nsubj=nsubj, **kwargs)
[docs] class ANOVA2ONERM(ANOVA): '''Two-way ANOVA with repeated measures on the second factor (B).''' def __init__(self, nlevels=(2, 3), nsubj=10, **kwargs): super().__init__(nlevels, rm='B', nsubj=nsubj, **kwargs)
[docs] class ANOVA3(ANOVA): '''Three-way between-subject ANOVA.''' def __init__(self, nlevels=(2, 2, 2), nsubj=10, **kwargs): super().__init__(nlevels, rm='', nsubj=nsubj, **kwargs)
[docs] class ANOVA3RM(ANOVA): ''' Three-way repeated-measures ANOVA (all factors repeated). ''' def __init__(self, nlevels=(2, 2, 2), nsubj=10, **kwargs): super().__init__(nlevels, rm='ABC', nsubj=nsubj, **kwargs)
[docs] class ANOVA3ONERM(ANOVA): '''Three-way ANOVA with repeated measures on the third factor (C).''' def __init__(self, nlevels=(2, 2, 2), nsubj=10, **kwargs): super().__init__(nlevels, rm='C', nsubj=nsubj, **kwargs)
[docs] class ANOVA3TWORM(ANOVA): ''' Three-way ANOVA with repeated measures on the second and third factors (B, C). ''' def __init__(self, nlevels=(2, 2, 2), nsubj=10, **kwargs): super().__init__(nlevels, rm='BC', nsubj=nsubj, **kwargs)
CLASSIC = { 'ttest': TTEST, 'ttest2': TTEST2, 'ttest_paired': TTEST_PAIRED, 'anova1': ANOVA1, 'anova1rm': ANOVA1RM, 'anova2': ANOVA2, 'anova2rm': ANOVA2RM, 'anova2onerm': ANOVA2ONERM, 'anova3': ANOVA3, 'anova3rm': ANOVA3RM, 'anova3onerm': ANOVA3ONERM, 'anova3tworm': ANOVA3TWORM, 'ancova': ANCOVA, 'regress': Regression, }