Exemple #1
0
def simulate_timeseries(n_tr=200, n_trial=5, amplitude=1, tr=1, sigma=0.05):
    y = np.zeros(n_tr)
    y[np.arange(20, n_tr, int(n_tr / n_trial))] = amplitude

    hrf = glover_hrf(tr, oversampling=1)
    y = np.convolve(y, hrf, mode='same')
    epsilon = sigma * np.random.randn(n_tr)
    y = y + epsilon
    return y
def convolve_hrf(design_matrix, tr):
    ### BEGIN SOLUTION
    design_matrix_new = design_matrix.copy()
    hrf = glover_hrf(tr, oversampling=1)
    labels = design_matrix.columns
    for label in labels:
        design_matrix_new[label] = np.convolve(
            design_matrix_new[label].to_numpy(), hrf, mode='same')
    return design_matrix_new
a[1].set_ylabel('Intensity', fontsize=18)
a[1].set_title('Signal convolved with boxcar kernel', fontsize=18)
a[1].set_xlabel('Time', fontsize=18)

# Now what happens if we switch out the boxcar kernel for something with a more interesting shape, say a hemodynamic response function?
#
# Here we will use a double gamma hemodynamic function (HRF) developed by Gary Glover.
#
# **Note**: If you haven't install nltools yet run `!pip install nltools`.  You may need to restart your jupyter kernel as well.

# In[17]:

from nltools.external import glover_hrf

tr = 2
hrf = glover_hrf(tr, oversampling=20)
plt.plot(hrf, linewidth=2, color='red')
plt.ylabel('Intensity', fontsize=18)
plt.xlabel('Time', fontsize=18)
plt.title('Hemodynamic Response Function', fontsize=18)

# For this example, we oversampled the function to make it more smooth.  In practice we will want to make sure that the kernel is the correct shape given our sampling resolution.  Be sure to se the oversampling to 1.  Notice how the function looks more jagged now?

# In[18]:

hrf = glover_hrf(tr, oversampling=1)
plt.plot(hrf, linewidth=2, color='red')
plt.ylabel('Intensity', fontsize=18)
plt.xlabel('Time', fontsize=18)
plt.title('Hemodynamic Response Function', fontsize=18)
Exemple #4
0
 def convolve_hrf(A, tr):
     t = glover_hrf(tr, oversampling=1)
     V = A.copy()
     for c in A.columns:
         V[c] = np.convolve(V[c].to_numpy(), t, mode='same')
     return V