Miscellaneous#

Field geometry#

spm1d.geom contains functions for calculating geometrical properties of 1D fields including smoothness and morphological labeling (bwlabel).

from spm1d.geom import estimate_fwhm, estimate_fwhm_mv

fwhm    = estimate_fwhm( r )        # r:  (J,Q) array of univariate residuals
fwhm_mv = estimate_fwhm_mv( rmv )   # rmv:  (J,Q,I) array of multivariate residuals
estimate_fwhm(r, method='taylor2007', roi=None, df=None)[source]#

Estimate the FWHM (smoothness) of univariate residuals.

Methods:

  • "taylor2007" (default): Lipschitz-Killing curvature estimator of Taylor and Worsley (2007).

  • "barnes2013": same estimator in the notation of Barnes et al. (2013).

  • "kiebel1999": standardized-residual estimator of Kiebel et al. (1999) Eqn. 16.

  • "spm1d-v04": v0.4 estimator for reproducing v0.4 results.

Parameters:

  • r — (J,Q) array of residuals with J observations and Q nodes

  • method — "taylor2007" (default), "barnes2013", "kiebel1999" or "spm1d-v04"

  • roi — (“spm1d-v04” only) (Q,) boolean search region array (default None: whole domain)

  • df — (“kiebel1999” only) error degrees of freedom (default None: J - 1)

Returns:

  • FWHM in nodes (float)

Example:

Smoothness of the residuals of a one-sample t test on ten simulated responses from Pataky et al. (2015).

>>> import spm1d
>>> import jikudata as jd
>>> y = jd.SimulatedPataky2015b().y  # (10,101) array
>>> t = spm1d.stats.ttest( y, 0 )
>>> print( round( spm1d.geom.estimate_fwhm( t.residuals ), 3 ) )
26.915

The example datasets require the jiku-data package: pip install "spm1d[data]".

Note:

  • Taylor JE, Worsley KJ (2007). Detecting sparse signals in random fields, with an application to brain mapping. Journal of the American Statistical Association 102:913-928.

  • Barnes GR, Ridgway GR, Flandin G, Woolrich M, Friston K (2013). Set-level threshold-free tests on the intrinsic volumes of SPMs. NeuroImage 68:133-140.

  • Kiebel SJ, Poline JB, Friston KJ, Holmes AP, Worsley KJ (1999). Robust smoothness estimation in statistical parametric maps using standardized residuals from the general linear model. NeuroImage 10:756-766. https://doi.org/10.1006/nimg.1999.0508

estimate_fwhm_mv(r, method='taylor2008')[source]#

Estimate the FWHM (smoothness) of multivariate residuals.

Parameters:

  • r — (J,Q,I) residual array

  • method — "taylor2008" (default) or "spm1d-v04"

Returns:

  • FWHM in nodes (float)

Example:

Smoothness of the residuals of a one-sample Hotelling’s T2 test on the Dorn et al. (2012) dataset.

>>> import spm1d
>>> import jikudata as jd
>>> y = jd.Dorn2012().y  # (8,100,3) array
>>> h = spm1d.stats.hotellings( y )
>>> print( round( spm1d.geom.estimate_fwhm_mv( h.residuals ), 3 ) )
13.599

The example datasets require the jiku-data package: pip install "spm1d[data]".

resel_counts(r, fwhm=1, element_based=False, roi=None)[source]#

Resolution element (resel) counts of a search domain.

Parameters:

  • r — (J,Q) residual array (NaN: nodes without observations) or (Q,) boolean mask (True: excluded nodes)

  • fwhm — residual smoothness in nodes (default 1)

  • element_based — if True, count inter-node elements instead of nodes (default False)

  • roi — (Q,) boolean search region array (default None: whole domain)

Returns:

  • resels0 — search domain Euler characteristic

  • resels1 — search domain length in FWHM units

Example:

Resel counts of the residuals of a one-sample t test on ten simulated responses from Pataky et al. (2015) for the whole domain and for a region of interest.

>>> import numpy as np
>>> import spm1d
>>> import jikudata as jd
>>> y = jd.SimulatedPataky2015b().y  # (10,101) array
>>> t = spm1d.stats.ttest( y, 0 )
>>> r0, r1 = spm1d.geom.resel_counts( t.residuals, fwhm=t.fwhm )
>>> print( r0, round( r1, 3 ) )
1 3.715
>>> roi = np.zeros( 101, dtype=bool )
>>> roi[20:60] = True
>>> r0, r1 = spm1d.geom.resel_counts( t.residuals, fwhm=t.fwhm, roi=roi )
>>> print( r0, round( r1, 3 ) )
1 1.449

The example datasets require the jiku-data package: pip install "spm1d[data]".

resel_counts_mv(r, fwhm=1, element_based=False, roi=None)[source]#

Resolution element (resel) counts of a search domain from multivariate residuals.

Parameters:

  • r — (J,Q,I) residual array (nodes with all-zero residuals are excluded)

  • fwhm — residual smoothness in nodes (default 1)

  • element_based — if True, count inter-node elements instead of nodes (default False)

  • roi — (Q,) boolean search region array (default None: whole domain)

Returns:

  • resels0 — search domain Euler characteristic

  • resels1 — search domain length in FWHM units

Example:

Resel counts of the residuals of a one-sample Hotelling’s T2 test on the Dorn et al. (2012) dataset.

>>> import spm1d
>>> import jikudata as jd
>>> y = jd.Dorn2012().y  # (8,100,3) array
>>> h = spm1d.stats.hotellings( y )
>>> r0, r1 = spm1d.geom.resel_counts_mv( h.residuals, fwhm=h.fwhm )
>>> print( r0, round( r1, 3 ) )
1 7.28

The example datasets require the jiku-data package: pip install "spm1d[data]".

fwhm2lkc(fwhm, Q)[source]#

Lipschitz-Killing curvature (LKC) of a 1D field of a given smoothness.

Parameters:

  • fwhm — field smoothness in nodes

  • Q — number of field nodes

Returns:

  • Field LKC

Example:
>>> import spm1d
>>> print( round( spm1d.geom.fwhm2lkc( 20, 101 ), 3 ) )
8.326
lkc2fwhm(lkc, Q)[source]#

Smoothness of a 1D field from its Lipschitz-Killing curvature (LKC).

Parameters:

  • lkc — field LKC

  • Q — number of field nodes

Returns:

  • Field FWHM in nodes

Example:
>>> import spm1d
>>> print( round( spm1d.geom.lkc2fwhm( 5, 101 ), 3 ) )
33.302
lkc2resels(lkc, d=1)[source]#

Resel count of a field from its Lipschitz-Killing curvature (LKC).

Barnes et al. (2013) Eqn. 9: resels = (4 ln 2)^(-d/2) LKC.

Parameters:

  • lkc — field LKC

  • d — field dimensionality (default 1)

Returns:

  • Field resel count

Example:
>>> import spm1d
>>> print( round( spm1d.geom.lkc2resels( 8.0 ), 3 ) )
4.804
resels2lkc(resels, d=1)[source]#

Lipschitz-Killing curvature (LKC) of a field from its resel count.

Inverse of lkc2resels().

Parameters:

  • resels — field resel count

  • d — field dimensionality (default 1)

Returns:

  • Field LKC

Example:
>>> import spm1d
>>> print( round( spm1d.geom.resels2lkc( 5.0 ), 3 ) )
8.326
bwlabel(b, merge_wrapped=False)[source]#

Label clusters of a 1D binary field.

Equivalent to scipy.ndimage.label() (float labels).

Parameters:

  • b — (Q,) boolean array

  • merge_wrapped — if True, merge clusters touching both domain ends (default False)

Returns:

  • L — (Q,) float array: 0 outside clusters and labels 1, 2, … inside

  • n — number of clusters

Example:
>>> import numpy as np
>>> import spm1d
>>> b    = np.array( [1, 1, 0, 0, 1, 0, 1, 1], dtype=bool )
>>> L, n = spm1d.geom.bwlabel( b )
>>> L
array([1., 1., 0., 0., 2., 0., 3., 3.])
>>> n
3
>>> L, n = spm1d.geom.bwlabel( b, merge_wrapped=True )
>>> L
array([1., 1., 0., 0., 2., 0., 1., 1.])
>>> n
2
assemble_clusters(z, zc, dirn=0, circular=False)[source]#

Suprathreshold clusters of a test statistic field.

Parameters:

  • z — (Q,) test statistic field

  • zc — critical threshold

  • dirn — direction: 0 (two-tailed, default), +1 or -1

  • circular — True for circular domains

Returns:

  • ClusterList sorted by position

Example:
>>> clusters = spm1d.geom.assemble_clusters( z, 3.5, dirn=0 )

Other utilities#

These functions are in the spm1d.util module.

from spm1d.util import smooth, isequal
smooth(Y, fwhm=5.0)[source]#

Smooth a set of 1D continua with a Gaussian kernel.

Uses scipy.ndimage.gaussian_filter1d with kernel width specified by fwhm.

Parameters:

  • Y — (J,Q) numpy array

  • fwhm — Gaussian kernel full-width at half-maximum.

Returns:

  • (J,Q) numpy array

Example:

>>> Y0  = np.random.rand(5, 101)
>>> Y   = spm1d.util.smooth(Y0, fwhm=10.0)

Note

A Gaussian kernel’s fwhm is related to its standard deviation (sd) as follows:

>>> fwhm = sd * np.sqrt( 8 * np.log(2) )
isequal(self, other, attrs, attrs_approx=None, verbose=False)[source]#

Equality of two objects on named attributes.

Parameters:

  • self — object

  • other — other object

  • attrs — list of attribute names compared exactly

  • attrs_approx — list of attribute names compared approximately (relative tolerance 1e-6 and absolute tolerance 1e-12). Default None

  • verbose — if True, print the first unequal attribute name (default False)

Returns:

  • True if the objects are equal, otherwise False

Example:
>>> import spm1d
>>> import jikudata as jd
>>> y  = jd.SimulatedPataky2015b().y  # (10,101) array
>>> t0 = spm1d.stats.ttest( y, 0 )
>>> t1 = spm1d.stats.ttest( y, 0 )
>>> print( spm1d.util.isequal( t0, t1, ['STAT', 'df'], ['z'] ) )
True

The example datasets require the jiku-data package: pip install "spm1d[data]".