.. vim: ft=rst ##################### Basic linear modeling ##################### * For code template see: :download:`multi_model_code.py`; * For solution see: :doc:`multi_model_solution`. In this exercise we will run a simple regression on all voxels in a 4D FMRI image :download:`ds114_sub009_t2r1.nii`: .. nbplot:: >>> #: Import some standard librares >>> import numpy as np >>> # Print to 4 DP >>> np.set_printoptions(precision=4) >>> import numpy.linalg as npl >>> import matplotlib.pyplot as plt >>> # Set default imshow parameters >>> plt.rcParams['image.interpolation'] = 'nearest' >>> plt.rcParams['image.cmap'] = 'gray' .. nbplot:: >>> #: Load the image as an image object >>> import nibabel as nib >>> img = nib.load('ds114_sub009_t2r1.nii') .. nbplot:: >>> #: Load the image data as an array >>> # Drop the first 4 3D volumes from the array >>> # (We already saw that these were abnormal) >>> data = img.get_data()[..., 4:] We make the design matrix from the convolved regressor from :doc:`convolution_background`: .. nbplot:: >>> #- Load the pre-written convolved time course >>> #- Knock off the first four elements .. nbplot:: >>> #- Compile the design matrix >>> #- First column is convolved regressor >>> #- Second column all ones >>> #- Hint: investigate "aspect" keyword to ``plt.imshow`` for a nice >>> #- looking image. .. nbplot:: >>> #- Reshape the 4D data to voxel by time 2D >>> #- Transpose to give time by voxel 2D >>> #- Calculate the pseudoinverse of the design >>> #- Apply to time by voxel array to get betas .. nbplot:: >>> #- Transpose betas to give voxels by 2 array >>> #- Reshape into 4D array, with same 3D shape as original data, >>> #- last dimension length 2 .. nbplot:: >>> #- Show the middle slice from the first beta volume .. nbplot:: >>> #- Show the middle slice from the second beta volume