2D data

Contents

2D data#

In spm1d a 1D observation is a 1D (Q,) array, where Q usually represents the number of 1D domain points. However, spm1d’s permutation procedures also support analyses of nD continua ℹ️, including registered 2D images. For 2D images Q represents the total number of pixels, and flattened 2D images can be submitted as (Q,) arrays to spm1d procedures.

ℹ️ Support for nD continua
  • spm1d’s ability to handle nD continua has only been verified for relatively small samples of 2D data.

  • Only permutation (e.g. spm.inference(0.05, method='perm')) is suitable for nD continuum analysis in spm1d.

  • nD data must be registered before analysis; spm1d does not support nD registration.

  • spm1d is not designed to handle large samples and/or high-resolution nD data; large datasets may encounter memory limits or other numerical problems.

  • Use at your own risk.

Permutation inference calculates the critical threshold zc from the distribution of the test statistic maximum across all domain points, and this procedure depends neither on the order of the Q points nor on the actual dimensionality that Q embodies. Thus zc, h0reject, p_zmax and point-level p-values p_point are valid for flattened nD data, for all n>0. Cluster-level permutation results are valid only for 1D data where the Q points are sorted in order across the domain.

Example#

The dataset below is from Pataky (2012) and contains 20 plantar pressure images from one subject with 10 trials of each of forward- and backward-leaning during quiet standing. Each image is a (60,32) array of mean pressures (kPa) that were averaged over a 5 s recording. The images were registered as described in Oliveira et al. (2010).


pressure2d.npy

import numpy as np
import spm1d

Y = np.load( 'pressure2d.npy' )   # (20,60,32) array
../_images/data2d_means_light.png ../_images/data2d_means_dark.png

The figure above suggests different mean distributions for the two tasks. To analyze these data in spm1d, the data must first be flattened into a (Q,) array, here a (1920,) array. Pixels with zero pressure in all images have zero variance, so these pixels should be removed before testing by defining a boolean mask i as follows:

y   = Y.reshape( 20, -1 ) # (20,1920) flattened images
eps = np.finfo( float ).eps
i   = (y[:10].std( axis=0 ) > eps) & (y[10:].std( axis=0 ) > eps)   # (1920,) boolean mask:  non-zero variance in both tasks

Tests can then be conducted on the masked data:

np.random.seed( 0 )
ti = spm1d.stats.ttest2( y[:10, i], y[10:, i] ).inference( 0.05, method='perm', nperm=10000 )
print( ti.zc )    # 3.728

The number of permutations must be large enough for zc to be numerically stable. See permutation inference for use guidelines.

Visualizing results requires that the test statistic is returned to its original 2D shape:

z    = np.zeros( y.shape[1] )
z[i] = ti.z
Z    = z.reshape( Y.shape[1:] )                   # (60,32) t-value image
Zi   = np.where( np.abs( Z ) > ti.zc, Z, 0 )      # thresholded image
../_images/data2d_results_light.png ../_images/data2d_results_dark.png

The maximum test statistic value exceeds zc so the null hypothesis is rejected. The results suggest that pressure is greater under the heel when leaning backward and greater under the forefoot when leaning forward.