funcs_correlate

Functions for loading, standardizing, and calculating correlations (including lagged)

M Chow, Oct 2012 machow@princeton.edu
pycorr.funcs_correlate.corcomposite(sub, dsummed, **kwargs)[source]

corsubs convenience function for correlating subject with a composite it is part of

pycorr.funcs_correlate.corsubs(A, B, axis=-1, standardized=False)[source]

Return correlation mat of time series along specified axis.

Parameters:
  • A (ndarray) – array of arbitrary dimension
  • B (ndarray) – array with dim(B) == dim(A)
  • axis (int) – along which to correlate
  • standardized (bool) – specify if sequences are already demeaned, with variance of one
Returns:

Correlation matrix with dim(A), except for the axis used for correlating.

TODO: generalize axis argument

pycorr.funcs_correlate.crosscor(dlist, B=None, standardized=True)[source]

Takes list of subject data, returns matrix of correlation matrices at each voxel.

Parameters:
  • dlist – list of arrays to correlate along last dimension (must be equal shape)
  • B – optionally calculate the cross-correlation matrix with arrays in B
  • standardized – whether all data are standardized (speeds calculation)
pycorr.funcs_correlate.intersubcorr(C_all, excludeself=True)[source]

Returns an array of intersubj correlations. Last two dims must be the cov matrix.

This function uses that the var( sum of R.V.’s ) is the sum of their cov matrix. If entire row / col is NaN, then returns ISC as if that subject was removed (to allow dropping voxels with mean activity < 6000.

Parameters:
  • C_all – ndarray with correlation matrix on last two dims
  • excludeself – remove participant from ISC calculation
pycorr.funcs_correlate.lagcor(A, B, h, axis=-1, standardized=False, offset=0)[source]

Return the corr of A_t+h with B_t. If dim length differs, cut from end.

Parameters: A: n-dim array with time as final dim B: same shape array axis: time axis (passed to corsubs) standardized: already demained, and var == one? (passed to corsubs) offset: is offset number of timepoints passed B

pycorr.funcs_correlate.load_roi(nii, thresh=6000, standardize=True, outlen=None, padding=<function mean at 0x1044e9050>, meantc=False)[source]

Load a nifti file and apply several functions to it.

nii – name of nifti to load or ndarray standardize – return all tcs with mean = 0 and stdev = 1 outlen – specify how long resultant tcs should be, pad if necessary padding – function to apply to ndarray, must take axis=-1 argument meantc – return the meantc over all voxels (before evaluating standardize)

TODO: fix default outlen arg

pycorr.funcs_correlate.roimask(data, roi, filter_func=None, proc_func=None, mean_ts=False)[source]

Mask data using values in roi > 0

Parameters: data – numpy array to be masked roi – array to be checked for nonzero values filter_func – function to further subset roi (e.g. remove time courses with mean < 6000) proc_func – TODO

pycorr.funcs_correlate.shift(A, h, outlen=None, offset=0)[source]

Shifts entire time series by h. If h is negative, pad beginning with NaN

pycorr.funcs_correlate.standardize(A, axis=-1, demean=True, devar=True, inplace=False)[source]

Subtract mean, divide standard deviation (z-scoring).

Parameters:
  • A (ndarray) – data to standardize.
  • axis (int) – axis along which to standardize.
  • demean (bool) – make mean equal to zero.
  • devar (bool) – make variance equal to one.
  • inplace (bool) – operate in-place (and return reference)

Returns: standardized copy or reference of A

Note

NaNs should be taken care of prior to using this function, as they will propogate.

pycorr.funcs_correlate.sum_tc(dlist, nans=True, standardize_subs=False, standardize_out=False, shape=None)[source]

Returns the sum of all timecourses in dlist.

Parameters:
  • dlist – list or generator of arrays to sum
  • nans (bool) – set NaN to 0 when summing
  • standardize_subs (bool) – standardize before summing
  • standardize_out (bool) – standardize output
  • shape (list) – shape (necessary if using generator)

TODO: what is the point of the nans option?

pycorr.funcs_correlate.trim(A, ends=(0, 0), h=None)[source]

Returns Array with last dim trimmed

Parameters:
  • A – n-dim array
  • ends – how much trim from each end, e.g. (10, -10)
  • h – specify how much to trim from one side. Overrides that argument in ends.

TODO: h is a weird option (and you never use it)