plt.figure(figsize=(15,3)) plt.plot(np.mean(data.data, axis=1), linewidth=3) plt.xlabel('Time', fontsize=18) plt.ylabel('Intensity', fontsize=18) # Notice there is a clear slow drift in the signal that we will need to remove with our high pass filter. # # Now, let's see if there are any spikes in the data that exceed our threshold. What happens if we use a different threshold? # In[37]: spikes = data.find_spikes(global_spike_cutoff=2.5, diff_spike_cutoff=2.5) f, a = plt.subplots(figsize=(15,3)) spikes = Design_Matrix(spikes.iloc[:,1:], sampling_freq=1/tr) spikes.plot(ax = a, linewidth=2) # For this subject, our spike identification procedure only found a single spike. Let's add all of these covariate to our design matrix. # # In this example, we will append each of these additional matrices to our main design matrix. # # **Note**: `.append()` requires that all matrices are a design_matrix with the same sampling frequency. # In[38]:
return onsets_to_dm(onsets, sampling_freq=1/tr, run_length=n_tr) def make_motion_covariates(mc): z_mc = zscore(mc) all_mc = pd.concat([z_mc, z_mc**2, z_mc.diff(), z_mc.diff()**2], axis=1) all_mc.fillna(value=0, inplace=True) return Design_Matrix(all_mc, sampling_freq=1/tr) for sub in layout.get_subjects(scope='derivatives'): data = Brain_Data([x for x in layout.get(subject=sub, scope='derivatives', suffix='bold', extension='nii.gz', return_type='file') if 'denoised' not in x][0]) smoothed = data.smooth(fwhm=fwhm) dm = load_bids_events(layout, sub) covariates = pd.read_csv(layout.get(subject=sub, scope='derivatives', extension='.tsv')[0].path, sep='\t') mc_cov = make_motion_covariates(covariates[['trans_x','trans_y','trans_z','rot_x', 'rot_y', 'rot_z']]) spikes = data.find_spikes(global_spike_cutoff=spike_cutoff, diff_spike_cutoff=spike_cutoff) dm_cov = dm.convolve().add_dct_basis(duration=128).add_poly(order=1, include_lower=True) dm_cov = dm_cov.append(mc_cov, axis=1).append(Design_Matrix(spikes.iloc[:, 1:], sampling_freq=1/tr), axis=1) smoothed.X = dm_cov stats = smoothed.regress() file_name = layout.get(subject=sub, scope='derivatives', suffix='bold', extension='nii.gz', return_type='file')[0] stats['beta'].write(os.path.join(os.path.dirname(file_name), f"sub-{sub}_betas_denoised_{file_name.split('_')[1]}_{file_name.split('_')[2]}_smoothed{fwhm}_{file_name.split('_')[-1]}")) for i, name in enumerate([x[:-3] for x in dm_cov.columns[:10]]): stats['beta'][i].write(os.path.join(os.path.dirname(file_name), f"sub-{sub}_{name}_denoised_{file_name.split('_')[2]}_smoothed{fwhm}_{file_name.split('_')[-1]}")) ``` Now, we are ready to run our first group analyses! Let's load our design matrix to remind ourselves of the various conditions