Source code for spm1d.prob._bonf


'''
Bonferroni-corrected probabilities.
'''

# Copyright (C) 2026  Todd Pataky

import numpy as np
from .. geom import assemble_clusters
from . _general import p_corrected_dirn, z_corrected_dirn, p_point_in_region
from . _results import InferenceResults1D


[docs] def bonf(STAT, z, df, alpha=0.05, dirn=0, circular=False): ''' Bonferroni 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 - *alpha* --- Type I error rate (default 0.05) - *dirn* --- inference direction: 0 (two-tailed), +1 or -1 (one-tailed) - *circular* --- True for circular domains :Returns: - **InferenceResults1D** object :Example: Bonferroni inference on the t field of a one-sample t test. >>> import spm1d >>> import jikudata as jd >>> y = jd.SimulatedPataky2015b().y # (10,101) array >>> t = spm1d.stats.ttest( y, 0 ) >>> results = spm1d.prob.bonf( 'T', t.z, t.df, alpha=0.05, dirn=1 ) >>> print( round( results.zc, 3 ), results.h0reject ) 4.788 False The example datasets require the jiku-data package: ``pip install "spm1d[data]"``. ''' from . _uncorr import zc_uncorrected, p_uncorrected from . _general import p_critical_bonf, p_corrected_bonf _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. n = z.count() if np.ma.is_masked(z) else z.size pc = p_critical_bonf(a, n) zc = zc_uncorrected(STAT, pc, df) _z = z_corrected_dirn(z, dirn) p_point_u = p_uncorrected(STAT, _z, df) p_point = np.array([p_corrected_bonf(p, n) for p in p_point_u]) p_point = p_corrected_dirn(p_point, dirn) p_point = p_point_in_region(p_point, z) p_set = None # circular: a field whose ends meet, so clusters touching both ends are # one cluster. clusters = assemble_clusters(z, zc, dirn=dirn, circular=circular) results = InferenceResults1D( 'bonf', STAT, z, alpha, df, dirn, zc, p_set, clusters, p_point, isparametric=True ) return results