我试着为一个实验模拟一些事件的发生和预测。我有两个预测器(圆和正方形)。刺激(“事件”)需要1秒,ISI(刺激间隔)为8秒。我还对这两种与基线的对比感兴趣(圆圈与基线;正方形与基线)。最后,我尝试运行我定义的函数(simulate_data_fixed,n=420是一个固定的参数)1000次,在每次迭代中,我希望最后计算一个效率分数,并将效率分数存储在一个列表中
def simulate_data_fixed_ISI(N=420):
dg_hrf = glover_hrf(tr=1, oversampling=1)
# Create indices in regularly spaced intervals (9 seconds, i.e. 1 sec stim + 8 ISI)
stim_onsets = np.arange(10, N - 15, 9)
stimcodes = np.repeat([1, 2], stim_onsets.size / 2) # create codes for two conditions
np.random.shuffle(stimcodes) # random shuffle
stim = np.zeros((N, 1))
c = np.array([[0, 1, 0], [0, 0, 1]])
# Fill stim array with codes at onsets
for i, stim_onset in enumerate(stim_onsets):
stim[stim_onset] = 1 if stimcodes[i] == 1 else 2
stims_A = (stim == 1).astype(int)
stims_B = (stim == 2).astype(int)
reg_A = np.convolve(stims_A.squeeze(), dg_hrf)[:N]
reg_B = np.convolve(stims_B.squeeze(), dg_hrf)[:N]
X = np.hstack((np.ones((reg_B.size, 1)), reg_A[:, np.newaxis], reg_B[:, np.newaxis]))
dvars = [(c[i, :].dot(np.linalg.inv(X.T.dot(X))).dot(c[i, :].T))
for i in range(c.shape[0])]
eff = c.shape[0] / np.sum(dvars)
return eff
但是,我希望将整个块运行1000次,并将“eff”存储在一个数组中,等等。因此,稍后我希望将它们显示为直方图。ı怎么能做到这一点
你的问题似乎可以归结为:
我猜“我定义的函数”就是你问题中的
simulate_data_fixed_ISI
然后,您可以使用基本for循环简单地运行1000次,并将结果添加到列表中:
现在
eff_results
包含1000个条目,每个条目都是对函数simulate_data_fixed_ISI
的调用如果我没弄错的话,你应该可以跑了
正如@theonlygusti所阐明的,这一行
EFF
运行函数simulate_data_fixed_ISI()
1000次,并将每个返回放入数组EFF
测试
相关问题 更多 >
编程相关推荐