By using this colab to download the brain dataset (i.e., data including fMRI, images, labels), you agree to the following terms:
# @title Installations
!pip install nilearn --quiet
!pip install decord --quiet
|████████████████████████████████| 9.6 MB 5.7 MB/s
|████████████████████████████████| 38.1 MB 80.1 MB/s
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
albumentations 0.1.12 requires imgaug<0.2.7,>=0.2.5, but you have imgaug 0.2.9 which is incompatible.
|████████████████████████████████| 13.6 MB 121 kB/s
# Imports
import os
import cv2
import glob
import time
import torch
import random
import urllib
import pickle
import argparse
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
from PIL import Image
from tqdm import tqdm
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA, IncrementalPCA
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
from torch.autograd import Variable as V
from torchvision import transforms as trn
from nilearn import surface
from nilearn import datasets
from nilearn import plotting
from decord import cpu
from decord import VideoReader
fsaverage = datasets.fetch_surf_fsaverage()
Get the dropbox link to download dataset by filling this google form.
Important: You MUST set dropbox_link
parameter dl=1
, e.g., https://www.dropbox.com/myurl/participants_data.zip?dl=1
# @title Enter the dropbox link and run the cell
dropbox_link = '' # @param {type:"string"}
# @title Run the cell to download the data
import requests, zipfile, io
# Use the dropbox link to download the data
if dropbox_link:
fname1 = 'participants_data_v2021'
fname2 = 'AlgonautsVideos268_All_30fpsmax'
if not os.path.exists(fname1) or not os.path.exists(fname2):
print('Data downloading...')
r = requests.get(dropbox_link)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall()
print('Data download is completed.')
else:
print('Data are already downloaded.')
url = 'https://github.com/Neural-Dynamics-of-Visual-Cognition-FUB/Algonauts2021_devkit/raw/main/example.nii'
fname = 'example.nii'
if not os.path.exists(fname):
r = requests.get(url, allow_redirects=True)
with open(fname, 'wb') as fh:
fh.write(r.content)
else:
print(f"{fname} file is already downloaded.")
else:
print('You need to submit the form and get the dropbox link')
Data downloading... Data download is completed.
The Algonauts dataset provides human brain responses to a set of 1,102 3-s long video clips of everyday events. The brain responses are measured with functional magnetic resonance imaging (fMRI). fMRI is a widely used brain imaging technique with high spatial resolution that measures blood flow changes associated with neural responses.
The training set consists of 1,000 video clips and the associated brain responses. The brain responses are provided here in two tracks corresponding to two independent tracks in the Algonauts challenge.
In the first track, brain responses provided are from selected voxels across the whole brain showing reliable responses to videos. The figure below shows the reliability values of different voxels in the brain.
In the second track, brain responses provided are from a set of specific regions of interest (ROIs) known to play a key role in visual perception. These ROIs start in early and mid-level visual cortex (V1, V2, V3, and V4) and extend into higher-level cortex that responds preferentially to all objects or particular categories (Body- EBA; Face - FFA, STS; Object - LOC; Scene - PPA). In the figure below we show the masks of the above mentioned ROIs for an example subject.
The dataset contains 1,000 3-second videos + fMRI human brain data of 10 subjects in response to viewing videos from this set.
The ROI data is provided for 9 ROIs of the visual brain (V1, V2, V3, V4, LOC, EBA, FFA, STS, PPA) in a Pickle file (e.g. V1.pkl) that contains a num_videos x num_repetitions x num_voxels matrix. For each ROI, we selected voxels that showed significant split-half reliability.
The whole brain data is provided for selected voxels across the whole brain showing reliable responses to videos in a Pickle file (e.g. WB.pkl) that contains a num_videos x num_repetitions x num_voxels matrix.
In this section, we demonstrate how to load fMRI data for a given ROI.
# @title Utility functions for data loading
def save_dict(di_, filename_):
with open(filename_, 'wb') as f:
pickle.dump(di_, f)
def load_dict(filename_):
with open(filename_, 'rb') as f:
u = pickle._Unpickler(f)
u.encoding = 'latin1'
ret_di = u.load()
# print(p)
# ret_di = pickle.load(f)
return ret_di
def visualize_activity(vid_id, sub):
# Setting up the paths for whole brain data
fmri_dir = './participants_data_v2021'
track = "full_track"
# get the right track directory depending on whole brain/ROI choice
track_dir = os.path.join(fmri_dir, track)
# get the selected subject's directory
sub_fmri_dir = os.path.join(track_dir, sub)
# result directory to store nifti file
results_dir = '/content/'
# mapping the data to voxels and storing in a nifti file
fmri_train_all,voxel_mask = get_fmri(sub_fmri_dir, "WB")
visual_mask_3D = np.zeros((78, 93, 71))
visual_mask_3D[voxel_mask==1]= fmri_train_all[vid_id, :]
brain_mask = './example.nii'
nii_save_path = os.path.join(results_dir, 'vid_activity.nii')
saveasnii(brain_mask, nii_save_path, visual_mask_3D)
# visualizing saved nifti file
plotting.plot_glass_brain(nii_save_path,
title='fMRI response',plot_abs=False,
display_mode='lyr',colorbar=True)
def get_fmri(fmri_dir, ROI):
"""This function loads fMRI data into a numpy array for to a given ROI.
Parameters
----------
fmri_dir : str
path to fMRI data.
ROI : str
name of ROI.
Returns
-------
np.array
matrix of dimensions #train_vids x #repetitions x #voxels
containing fMRI responses to train videos of a given ROI
"""
# Loading ROI data
ROI_file = os.path.join(fmri_dir, ROI + ".pkl")
ROI_data = load_dict(ROI_file)
# averaging ROI data across repetitions
ROI_data_train = np.mean(ROI_data["train"], axis=1)
if ROI == "WB":
voxel_mask = ROI_data['voxel_mask']
return ROI_data_train, voxel_mask
return ROI_data_train
def saveasnii(brain_mask, nii_save_path, nii_data):
img = nib.load(brain_mask)
nii_img = nib.Nifti1Image(nii_data, img.affine, img.header)
nib.save(nii_img, nii_save_path)
# @title Loading fMRI data and inspecting dimensions
# Select Subject
sub = 'sub04' #@param ["sub01","sub02","sub03","sub04","sub05","sub06","sub07","sub08","sub09","sub10"]
# Select ROI
ROI = 'V1' #@param ["WB", "V1", "V2","V3", "V4", "LOC", "EBA", "FFA","STS", "PPA"]
######## fMRI data loader wrapper code ###################################
fmri_dir = './participants_data_v2021'
if ROI == "WB": # Loading whole brain data
track = "full_track" # stored in full_track directory
else: # Loading ROI data
track = "mini_track" # stored in mini_track directory
# get the right track directory depending on whole brain/ROI choice
track_dir = os.path.join(fmri_dir, track)
# get the selected subject's directory
sub_fmri_dir = os.path.join(track_dir, sub)
# Load the fMRI data for the selected subject and ROI
if track == "full_track":
fmri_train_all,_ = get_fmri(sub_fmri_dir,ROI)
else:
fmri_train_all = get_fmri(sub_fmri_dir,ROI)
######## fMRI data loader wrapper code ###################################
# Visualize the fMRI responses in a heatmap
f, ax = plt.subplots(figsize=(12, 5))
ax.set(xlabel="Voxel", ylabel="Stimulus")
heatmap = ax.imshow(fmri_train_all, aspect="auto", cmap='jet', vmin=-1, vmax=1)
f.colorbar(heatmap, shrink=.5, label="Response amplitude (Z)")
f.tight_layout()