Source code for spm1d._settings

'''
Package settings and warning filters.
'''

# Copyright (C) 2026  Todd Pataky

import warnings
from . import exceptions as _exc


[docs] class SPM1DSettings(): ''' Package settings (``spm1d.settings``). ''' def __init__(self): #: True if procedure timing is printed (``enable_timer``). self.timer_on = False #: True if ``SPM1DUserWarning`` and ``SPM1DDeprecationWarning`` are #: shown (``enable_warnings``). self.warnings_on = True #: True if progress bars are shown (``enable_progress``). self.progress_on = True #: True if SPM12 rules replace spm1d rules #: (``enable_spm12_compatibility``). self.spm12_on = False # Messages already emitted. A warning fires once per process so that # simulation loops do not repeat it. self._warned = set()
[docs] def enable_progress(self, b=True): ''' Toggle progress bars. :Parameters: - *b* --- True (default) to enable, False to disable :Returns: - None :Example: >>> import spm1d >>> spm1d.settings.enable_progress( False ) ''' self.progress_on = b
[docs] def enable_spm12_compatibility(self, b=True): ''' Reproduce SPM12 where spm1d departs from it (off by default). Affects two calculations: - 0D two-sample t: residual-field degrees of freedom trRV**2/trRVRV instead of Welch's degrees of freedom. - 1D covariance estimate: each node is scaled to unit residual variance before pooling. :Parameters: - *b* --- True (default) to enable, False to disable :Returns: - None :Example: >>> import spm1d >>> spm1d.settings.enable_spm12_compatibility() >>> spm1d.settings.enable_spm12_compatibility( False ) ''' self.spm12_on = b
[docs] def enable_timer(self, b=True): ''' Toggle procedure timing printouts. :Parameters: - *b* --- True (default) to enable, False to disable :Returns: - None :Example: >>> import spm1d >>> spm1d.settings.enable_timer() >>> spm1d.settings.enable_timer( False ) ''' self.timer_on = b
[docs] def enable_warnings(self, b=True): ''' Toggle ``SPM1DUserWarning`` and ``SPM1DDeprecationWarning`` (errors are unaffected). :Parameters: - *b* --- True (default) to enable, False to disable :Returns: - None :Example: >>> import spm1d >>> spm1d.settings.enable_warnings( False ) >>> spm1d.settings.enable_warnings() ''' self.warnings_on = b s = 'always' if b else 'ignore' warnings.simplefilter(s, _exc.SPM1DDeprecationWarning) warnings.simplefilter(s, _exc.SPM1DUserWarning)
[docs] def reset_warnings(self): ''' Reset once-per-process warnings. :Returns: - None :Example: >>> import spm1d >>> spm1d.settings.reset_warnings() ''' self._warned.clear()
def _warn_once(self, msg, category, stacklevel=3): ''' Emit a warning once. :Parameters: - *msg* --- warning message - *category* --- warning class - *stacklevel* --- as for ``warnings.warn`` (default 3) :Returns: - True if the warning was emitted, otherwise False :Example: >>> import spm1d >>> from spm1d.exceptions import SPM1DUserWarning >>> spm1d.settings.reset_warnings() >>> print( spm1d.settings._warn_once( 'Check the smoothness.', SPM1DUserWarning ) ) True >>> print( spm1d.settings._warn_once( 'Check the smoothness.', SPM1DUserWarning ) ) False ''' if not self.warnings_on: return False if msg in self._warned: return False self._warned.add( msg ) warnings.warn( msg, category, stacklevel=stacklevel ) return True
settings = SPM1DSettings() def _user_stacklevel(): ''' Stack level of the first frame outside spm1d so that warnings are attributed to the user's code. ''' import sys, os pkg = os.path.dirname( os.path.abspath( __file__ ) ) + os.sep f = sys._getframe( 1 ) n = 1 while f is not None: if not os.path.abspath( f.f_code.co_filename ).startswith( pkg ): return n f = f.f_back n += 1 return 1 # Python's default shows deprecations from __main__ only. warnings.simplefilter( 'default', _exc.SPM1DDeprecationWarning ) # Shown on every call so that silencing it is a deliberate act. warnings.simplefilter( 'always', _exc.SPM1DUncorrWarning )