def test_time_shift():
    # Intialize values for class data. 
    TR = 2.5
    tr_times = np.arange(0, 30, TR)
    hrf_at_trs = np.array([hrf_single(x) for x in tr_times])
    
    # Load class data.
    n_vols = 173
    neural_prediction = events2neural(pathtoclassdata+'ds114_sub009_t2r1_cond.txt', TR, n_vols)

    # Get np.convolve time course. 
    convolved = np.convolve(neural_prediction, hrf_at_trs) 
    N = len(neural_prediction)  # N == n_vols == 173

    # Compare shifted time courses by hand and using function.
    actual_shifted = convolved[5:(5+N)]
    exp_convolved2, exp_shifted = time_shift(convolved, neural_prediction, 5)
    assert_almost_equal(actual_shifted, exp_shifted)
Exemple #2
0
def test_time_shift():
    # Intialize values for class data. 
    TR = 2.5
    tr_times = np.arange(0, 30, TR)
    hrf_at_trs = np.array([hrf_single(x) for x in tr_times])
    
    # Load class data.
    n_vols = 173
    neural_prediction = events2neural(pathtoclassdata+'ds114_sub009_t2r1_cond.txt', TR, n_vols)

    # Get np.convolve time course. 
    convolved = np.convolve(neural_prediction, hrf_at_trs) 
    N = len(neural_prediction)  # N == n_vols == 173

    # Compare shifted time courses by hand and using function.
    actual_shifted = convolved[5:(5+N)]
    exp_convolved2, exp_shifted = time_shift(convolved, neural_prediction, 5)
    assert_almost_equal(actual_shifted, exp_shifted)
cond3=np.loadtxt(condition_location+"cond003.txt")

# Initialize needed values
TR = 2
tr_times = np.arange(0, 30, TR)
hrf_at_trs = np.array([hrf_single(x) for x in tr_times])
n_vols=data.shape[-1]

# Creating the .txt file for the events2neural function
cond_all=np.row_stack((cond1,cond2,cond3))
cond_all=sorted(cond_all,key= lambda x:x[0])
np.savetxt(condition_location+"cond_all.txt",cond_all)

# Event stimuli
neural_prediction = events2neural(condition_location+"cond_all.txt",TR,n_vols)

# Convolved time course using np.convolve
convolved = np.convolve(neural_prediction, hrf_at_trs) 

# Get the back-shifted time course. 
convolved2, shifted = time_shift(convolved, neural_prediction, TR)

# Compare before and after shifting.
plt.plot(neural_prediction)
plt.plot(convolved2)
plt.plot(shifted)
plt.savefig(location_of_images + "shifted.png")

print("Shifted time course more closely matches stimuli.")

n_vols = data.shape[-1]

# Creating the .txt file for the events2neural function
cond_all = np.row_stack((cond1, cond2, cond3))
cond_all = sorted(cond_all, key=lambda x: x[0])
np.savetxt(condition_location + "cond_all.txt", cond_all)

# Event stimuli
neural_prediction = events2neural(condition_location + "cond_all.txt", TR,
                                  n_vols)

# Convolved time course using np.convolve
convolved = np.convolve(neural_prediction, hrf_at_trs)

# Get the back-shifted time course.
convolved2, shifted = time_shift(convolved, neural_prediction, TR)

# Compare before and after shifting.
plt.plot(neural_prediction)
plt.plot(convolved2)
plt.plot(shifted)
plt.savefig(location_of_images + "shifted.png")

print("Shifted time course more closely matches stimuli.")

#######################
# Ben's improvements: #
#######################

# using the cond_all from above
cond_all = np.array(cond_all)[:, 0]