<jupyter_start><jupyter_text>Preliminary analysis In this notebook, we will explore the data and point out things to look out for, such as the confounds and masks to use. Goals* Perform confound regression* Masking the data using brainmask files<jupyter_code>from packages import *
%matplotlib inline
from collections import Counter
import sklearn
from _1_file_acquisition import get_subject_images
from nilearn.image.image import mean_img<jupyter_output>Importing Jupyter notebook from _1_file_acquisition.ipynb<jupyter_text>Using the functions from the previous notebook, we can load in the images.<jupyter_code>images = get_subject_images(1, DATA_DIR, fmri_prep_reqs=['preproc', 'brainmask', 'confounds'])
PLOTS['stat_map'](mean_img(images['fmriprep'][1][1]['preproc']), images['fmriprep'][1][1]['brainmask'])<jupyter_output><empty_output><jupyter_text>MaskingMost of the voxels in the scan will not be of use to us. To filter them, we will build masks and apply them by doing element-wise matrix multiplications. For this example we will use the brainmask from the same session to discard voxels that are outside the brain. Later on, we will build specific masks for brain regions, gray matter and etc.<jupyter_code>timeseries_bold = images['fmriprep'][1][1]['preproc'].get_fdata()
brainmask = images['fmriprep'][1][1]['brainmask'].get_fdata()
timeseries_bold = timeseries_bold * np.expand_dims(brainmask, -1)<jupyter_output><empty_output><jupyter_text>We can see the effect of the mask by plotting the mean image again.<jupyter_code>PLOTS['stat_map'](mean_img(make_img(timeseries_bold, images['fmriprep'][1][1]['preproc'].affine)), images['fmriprep'][1][1]['brainmask'])<jupyter_output><empty_output><jupyter_text>Next, we will flatten the three-dimensional array for each time-point for the next part.<jupyter_code>timeseries_bold = np.moveaxis(timeseries_bold, -1, 0)
timeseries_bold = timeseries_bold.reshape(194, np.product(timeseries_bold.shape[1:]))
timeseries_bold.shape<jupyter_output><empty_output><jupyter_text>ConfoundsThe fmriprep scans contain confounds tables where movement and rotation confounds are provided. We can use the values for these confounds to decorrelate the timeseries form the effects of these confounds.<jupyter_code>confounds = images['fmriprep'][1][1]['confounds'].replace(np.nan,0)
# add temporal derivatives of motion estimates
motionvars=['X', 'Y', 'Z', 'RotX', 'RotY', 'RotZ']
for v in motionvars:
    confounds['%s_deriv'%v]=0
    confounds['%s_deriv'%v][1:]=confounds[v].iloc[1:].values -confounds[v].iloc[:-1].values

plt.imshow(sklearn.preprocessing.scale(confounds).T,aspect='auto',
           cmap='viridis',interpolation='nearest')
plt.plot(confounds.FramewiseDisplacement)
plt.imshow(sklearn.preprocessing.scale(timeseries_bold).T,aspect='auto',
           cmap='gray',interpolation='nearest')
# confounds with GSR
confounds_to_include = ['CSF', 'WhiteMatter', 'GlobalSignal', 'stdDVARS',
                        'FramewiseDisplacement', 'tCompCor00', 'tCompCor01',
                        'tCompCor02', 'tCompCor03', 'tCompCor04', 'tCompCor05', 
                        'aCompCor00','aCompCor01', 'aCompCor02', 'aCompCor03', 
                        'aCompCor04', 'aCompCor05',
                        'Cosine00', 'Cosine01', 'Cosine02', 'Cosine03', 'Cosine04', 'X', 
                        'Y', 'Z', 'RotX', 'RotY', 'RotZ',
                        'X_deriv', 'Y_deriv', 'Z_deriv', 'RotX_deriv', 'RotY_deriv',
                        'RotZ_deriv']

confounds_gsr=confounds.loc[:, confounds_to_include]
lr=sklearn.linear_model.LinearRegression(fit_intercept=False)

lr.fit(confounds_gsr.values, timeseries_bold)
bolddata_reg_gsr = timeseries_bold - lr.predict(confounds_gsr)
plt.imshow(sklearn.preprocessing.scale(bolddata_reg_gsr).T,aspect='auto',
           cmap='gray',interpolation='nearest')
bolddata_reg_gsr /= np.mean(bolddata_reg_gsr)
bolddata_reg_gsr = np.moveaxis(bolddata_reg_gsr.reshape(194, *images['fmriprep'][1][1]['preproc'].shape[:-1]), 0, -1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12818609255134866, 'fraction_numerical': 0.01756990843850532, 'mean_word_length': 5.6480263157894735, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '479741', 'license': 'MIT', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1323, 'n_words': 290, 'path': 'src/_2_preliminary_analysis.ipynb', 'repo_name': 'kdonbekci/brain-decoder', 'size': '4041'}