将函数重复1000次并将每次迭代结果保存在列表中

2024-10-01 07:38:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着为一个实验模拟一些事件的发生和预测。我有两个预测器(圆和正方形)。刺激(“事件”)需要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”存储在一个数组中,等等。因此,稍后我希望将它们显示为直方图。ı怎么能做到这一点


Tags: infornp事件regdotdg基线
2条回答

你的问题似乎可以归结为:

I am trying to run the function that I have defined for 1000, at each iteration I would like to calculate an efficiency score in the end and store the efficiency scores in a list

我猜“我定义的函数”就是你问题中的simulate_data_fixed_ISI

然后,您可以使用基本for循环简单地运行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


eff_results = []
for _ in range(1000):
    efficiency_score = simulate_data_fixed_ISI()
    eff_results.append(efficiency_score)
    

现在eff_results包含1000个条目,每个条目都是对函数simulate_data_fixed_ISI的调用

如果我没弄错的话,你应该可以跑了

EFF = [simulate_data_fixed_ISI() for i in range(1000)] #1000 repeats

正如@theonlygusti所阐明的,这一行EFF运行函数simulate_data_fixed_ISI()1000次,并将每个返回放入数组EFF

测试

import numpy as np
def simulate_data_fixed_ISI(n=1):
    """
    Returns 'n' random numbers
    """
    return np.random.rand(n)

EFF = [simulate_data_fixed_ISI() for i in range(5)]

EFF
#[array([0.19585137]),
# array([0.91692933]),
# array([0.49294667]),
# array([0.79751017]),
# array([0.58294512])]

相关问题 更多 >