\(\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}\)
Correlation per voxel, in 2D¶
- For code template see:
correlation_2d_code.py
; - For solution see: Correlation per voxel, in 2D.
In this exercise, we will take each voxel time course in the brain, and calculate a correlation between the task-on / task-off vector and the voxel time course. We then make a new 3D volume that contains correlation values for each voxel.
>>> #: Standard imports
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> import nibabel as nib
Import the events2neural
function from the stimuli.py
module:
>>> #- import events2neural from stimuli module
If you don’t have it already, download the ds114_sub009_t2r1.nii
image. Load it with nibabel.
>>> #- Load the ds114_sub009_t2r1.nii image
>>> #- Get the number of volumes in ds114_sub009_t2r1.nii
The TR (time between scans) is 2.5 seconds.
>>> #: TR
>>> TR = 2.5
Call the events2neural
function to give you a time course that is 1 for
the volumes during the task (thinking of verbs) and 0 for the volumes during
rest.
>>> #- Call events2neural to give on-off values for each volume
Using slicing, drop the first 4 volumes, and the corresponding on-off values:
>>> #- Drop the first 4 volumes, and the first 4 on-off values
>>> #- Calculate the number of voxels (number of elements in one volume)
Reshape the 4D data to a 2D array shape (number of voxels, number of volumes).
>>> #- Reshape 4D array to 2D array n_voxels by n_volumes
>>> #- Make a 1D array of size (n_voxels,) to hold the correlation values
If you finished the Pearson 1D and 2D function exercise exercise, you can use your
pearson_2d
routine for calculating Pearson correlations across a 2D array.
Otherwise, loop over all voxels, calculate the correlation coefficient with
time_course
at this voxel, and fill in the corresponding entry in your 1D
array.
>>> #- Loop over voxels filling in correlation at this voxel
>>> #- Or (much faster) use pearson_2d function
Reshape the correlations 1D array back to a 3D array, using the original 3D shape.
>>> #- Reshape the correlations array back to 3D
Test that your brain-at-a-time correlation image gives the same answer when
you run the correlation on a single voxel time course. Select an example
voxel – say data[42, 32, 19]
– and check that this gives the same
answer as you found for the matching voxel in your correlations 3D array:
>>> #- Check you get the same answer when selecting a voxel time course
>>> #- and running the correlation on that time course. One example voxel
>>> #- could be the voxel at array coordinate [42, 32, 19]
Plot the middle slice (plane) of the third axis from the correlations array. Look for any voxels with a high task correlation in the frontal lobe:
>>> #- Plot the middle slice of the third axis from the correlations array