brainiak package

Brain Imaging Analysis Kit.

Submodules

brainiak.image module

Generic image functionality.

class brainiak.image.ConditionSpec

Bases: numpy.ndarray

One-hot representation of conditions across epochs and TRs.

The shape is (n_conditions, n_epochs, n_trs).

class brainiak.image.SingleConditionSpec

Bases: brainiak.image.ConditionSpec

ConditionSpec with a single condition applicable to an epoch.

extract_labels() → numpy.ndarray

Extract condition labels.

Returns:The condition label of each epoch.
Return type:np.ndarray
brainiak.image.mask_image(image: nibabel.spatialimages.SpatialImage, mask: numpy.ndarray, data_type: type = None) → numpy.ndarray

Mask image after optionally casting its type.

Parameters:
  • image – Image to mask. Can include time as the last dimension.
  • mask – Mask to apply. Must have the same shape as the image data.
  • data_type – Type to cast image to.
Returns:

Masked image.

Return type:

np.ndarray

Raises:

ValueError – Image data and masks have different shapes.

brainiak.image.multimask_images(images: typing.Iterable[nibabel.spatialimages.SpatialImage], masks: typing.Sequence[numpy.ndarray], image_type: type = None) → typing.Iterable[typing.Sequence[numpy.ndarray]]

Mask images with multiple masks.

Parameters:
  • images – Images to mask.
  • masks – Masks to apply.
  • image_type – Type to cast images to.
Yields:

Sequence[np.ndarray] – For each mask, a masked image.

brainiak.io module

I/O functionality.

brainiak.io.load_boolean_mask(path: typing.Union[str, pathlib.Path], predicate: typing.Callable[[numpy.ndarray], numpy.ndarray] = None) → numpy.ndarray

Load boolean nibabel.SpatialImage mask.

Parameters:
  • path – Mask path.
  • predicate – Callable used to create boolean values, e.g. a threshold function lambda x: x > 50.
Returns:

Boolean array corresponding to mask.

Return type:

np.ndarray

brainiak.io.load_images_from_dir(in_dir: typing.Union[str, pathlib.Path], suffix: str = 'nii.gz') → typing.Iterable[nibabel.spatialimages.SpatialImage]

Load images from directory.

For efficiency, returns an iterator, not a sequence, so the results cannot be accessed by indexing.

For every new iteration through the images, load_images_from_dir must be called again.

Parameters:
  • in_dir – Path to directory.
  • suffix – Only load images with names that end like this.
Yields:

SpatialImage – Image.

brainiak.io.load_labels(path: typing.Union[str, pathlib.Path]) → typing.List[brainiak.image.SingleConditionSpec]

Load labels files.

Parameters:path – Path of labels file.
Returns:List of SingleConditionSpec stored in labels file.
Return type:List[SingleConditionSpec]
brainiak.io.save_as_nifti_file(data: numpy.ndarray, affine: numpy.ndarray, path: typing.Union[str, pathlib.Path]) → None

Create a Nifti file and save it.

Parameters:
  • data – Brain data.
  • affine – Affine of the image, usually inherited from an existing image.
  • path – Output filename.

brainiak.isfc module

Intersubject analyses (ISC/ISFC)

Functions for computing intersubject correlation (ISC) and intersubject functional correlation (ISFC), and utility functions for loading subject data files and masks

Paper references: ISC: Hasson, U., Nir, Y., Levy, I., Fuhrmann, G. & Malach, R. Intersubject synchronization of cortical activity during natural vision. Science 303, 1634–1640 (2004).

ISFC: Simony E, Honey CJ, Chen J, Lositsky O, Yeshurun Y, Wiesel A, Hasson U (2016) Dynamic reconfiguration of the default mode network during narrative comprehension. Nat Commun 7.

brainiak.isfc.isc(D, collapse_subj=True)

Intersubject correlation

For each voxel, computes the correlation of each subject’s timecourse with the mean of all other subjects’ timecourses. By default the result is averaged across subjects, unless collapse_subj is set to False.

Parameters:
  • D (voxel by time by subject ndarray) – fMRI data for which to compute ISC
  • collapse_subj (bool, default:True) – Whether to average across subjects before returning result
Returns:

ISC – pearson correlation for each voxel, across subjects

Return type:

voxel ndarray (or voxel by subject ndarray, if collapse_subj=False)

brainiak.isfc.isfc(D, collapse_subj=True)

Intersubject functional correlation

Computes the correlation between the timecoure of each voxel in each subject with the average of all other subjects’ timecourses in all voxels. By default the result is averaged across subjects, unless collapse_subj is set to False.

Uses the high performance compute_correlation routine from fcma.util

Parameters:
  • D (voxel by time by subject ndarray) – fMRI data for which to compute ISFC
  • collapse_subj (bool, default:True) – Whether to average across subjects before returning result
Returns:

ISFC – (or voxel by voxel by subject ndarray, if collapse_subj=False) pearson correlation between all pairs of voxels, across subjects

Return type:

voxel by voxel ndarray