predict module

img2fmri python package for predicting group-level fMRI responses to individual images. Tutorial and analysis found at: https://github.com/dpmlab/img2fmri Author: Maxwell Bennett - mbb2176@columbia.edu

predict.average_subjects(image_dir, input_dir='temp/mni_s', output_dir='.', roi_list=['EarlyVis', 'OPA', 'LOC', 'RSC', 'PPA'])

Averages each BOLD5000 subject’s brain volume in MNI space into one volume. Averages by z-scoring each volume, summing those volumes, and then z-scoring the sum. Saves averaged volumes in the output directory. Only saves voxels within mask generated by utils.get_subj_overlap(roi_list), which is the union of all subjects’ ROI masks in MNI space.

Parameters:
  • image_dir (str) – Path to directory of images, with filenames to be used for output.

  • input_dir (str) – Path to directory of subjects’ predicted responses to average. Defaults to ‘temp/mni_s’.

  • output_dir (str) – Path to output directory where group-avg responses are saved. Defaults to ‘.’, which saves the files in the current directory.

  • roi_list (list) – List of voxel ROIs to predict. Defaults to [‘EarlyVis’, ‘OPA’, ‘LOC’, ‘RSC’, ‘PPA’].

predict.generate_activations(input_dir, output_dir='temp/activations', center_crop=False)

Pushes input images through our pretrained resnet18 model and saves the activations. Can be modified to use a different network or layer if desired, by changing ‘model’.

Parameters:
  • input_dir (str) – Relative path to input directory of images to be predicted.

  • output_dir (str) – Relative path to save intermediate files used in prediction. Defaults to ‘temp/activations/’.

  • center_crop (bool) – If True, crops each image to a square, from the center of the image, before processing. If False, images are resized to a square before processing. Defaults to False.

predict.generate_predictions(roi_list=['EarlyVis', 'OPA', 'LOC', 'RSC', 'PPA'], input_dir='temp/activations', output_dir='temp/subj_space')

Pushes each image activation through our regression model and saves an fMRI response in each of our three training subjects’ brain space.

Parameters:
  • roi_list (list) – List of voxel ROIs to predict. Defaults to [‘EarlyVis’, ‘OPA’, ‘LOC’, ‘RSC’, ‘PPA’].

  • input_dir (str) – Relative path to input directory of activations to predict from. Defaults to ‘temp/activations’.

  • output_dir (str) – Relative path to save predicted subj space brain responses. Defaults to ‘temp/subj_space’.

predict.predict(input_data, output_dir='.', roi_list=['EarlyVis', 'OPA', 'LOC', 'RSC', 'PPA'], predict_movie=False, sigma=1, center_crop=False)

Predicts an fMRI response for each input image, and saves it to output.

Predictions for each image are first made for specified ROIs of each subject’s brain, then transformed to MNI space, smoothed, averaged together to form a group-average, and then saved in the output directory.

Parameters:
  • input_data (str) – Path to an input directory of images or to a movie file to be predicted. If supplying a movie file, ‘predict_movie’ must be True.

  • output_dir (str) – Relative path to directory where predicted responses are saved. Defaults to ‘.’, which leads to saving in current directory.

  • predict_movie (bool) – Determines whether to predict response to a movie and save as a 4D brain volume of shape (x,y,z,movie_frames). Defaults to False.

  • roi_list (list) – List of voxel ROIs to predict. Defaults to [‘EarlyVis’, ‘OPA’, ‘LOC’, ‘RSC’, ‘PPA’].

  • sigma (int) – Standard dev of smoothing filter used for ROIs. FWHM≈2.355*sigma. Defaults to 1.

  • center_crop (bool) – If True, crops each image to a square, from the center of the image, before processing. If False, images are resized to a square before processing. Defaults to False.

Raises:

NotADirectoryError – Raises an error if a directory is not supplied as input, if not predicting a movie.

predict.smooth_brains(sigma=1, input_dir='temp/mni', output_dir='temp/mni_s')

Smooths the voxels in MNI space.

Parameters:
  • sigma (int) – Standard deviation of smoothing filter used for each ROI. Defaults to 1. FWHM≈2.355*sigma.

  • input_dir (str) – Path to input directory of mni space responses to smooth. Defaults to ‘temp/mni’.

  • output_dir (str) – Path to save smoothed mni space responses to. Defaults to ‘temp/mni_s’.

predict.transform_to_MNI(input_dir='temp/subj_space', output_dir='temp/mni')

Transforms each subject’s brain volume into MNI space for all input images.

Parameters:
  • input_dir (str) – Path to input directory of responses to transform into MNI space. Defaults to ‘temp/subj_space’.

  • output_dir (str) – Relative path to save predicted responses in mni space. Defaults to ‘temp/mni’.