Source code for spm1d.plot._val


'''
Plots of numerical validation results.
'''

# Copyright (C) 2026  Todd Pataky

import numpy as np


def _hist(ax, x, xc, p, valtype):
    ax.hist(x, bins=21, density=True, color='0.7', ec='0.5')
    ax.axvline(xc, color='r', ls='--')
    ax.text(0.03, 0.92, 'p = %0.5f'%p, transform=ax.transAxes, color='r' )
    ax.set_xlabel( f'{valtype}-value' )
    ax.set_ylabel('Density')


def _per_contrast(xc, m):
    #  One critical value (alpha) for valtype="p" and one per contrast for
    #  valtype="z".
    return np.broadcast_to( np.atleast_1d( np.asarray(xc, dtype=float) ), (m,) )


[docs] def plot_validation(results, show=True): ''' Histogram of single-contrast validation values. :Parameters: - *results* --- validator **SimulationResults** - *show* --- if True (default), call ``matplotlib.pyplot.show`` :Returns: - Axes (new figure) :Example: >>> import spm1d >>> validator = spm1d.util._val.val_ttest( 8, niter=200, progress_bar=False ) >>> ax = spm1d.plot.plot_validation( validator.results ) ''' import matplotlib.pyplot as plt fig = plt.figure( figsize=(6,4) ) ax = fig.add_subplot(1, 1, 1) _hist( ax, results.x, results.xc, results.p, results.valtype ) if show: plt.show() return ax
[docs] def plot_validation_multi(results, show=True): ''' One histogram per contrast for a multi-contrast validation. :Parameters: - *results* --- validator **SimulationResultsMultiContrast** - *show* --- if True (default), call ``matplotlib.pyplot.show`` :Returns: - Array of axes (one per panel of a new figure) :Example: >>> import spm1d >>> validator = spm1d.util._val.val_anova2( [[5, 5], [5, 5]], [[1, 1], [1, 1]], niter=200, progress_bar=False ) >>> axs = spm1d.plot.plot_validation_multi( validator.results ) ''' import matplotlib.pyplot as plt from math import ceil n = int( ceil( results.m**0.5 ) ) fig,axs = plt.subplots( n, n, figsize=(10,7) ) for ax,x,xc,p in zip(axs.ravel(), results.x.T, _per_contrast(results.xc, results.m), results.p): _hist( ax, x, xc, p, results.valtype ) if show: plt.show() return axs