如何将对视觉刺激的反应时间保存到csv文件中

时间:2019-03-26 15:28:42

标签: python csv psychopy

我是一个非常新的人,并且对Python /编码缺乏经验。我正在用python Psychopy创建Stroop任务。我创建了一个功能代码来呈现所需的视觉刺激,我只需要弄清楚如何保存参与者对每种刺激的反应时间。我尝试了一些在网上找到的代码,它给了我与实验开始有关的反应时间,而不是每个刺激刺激的表现(这就是我想要的)。在这方面的任何帮助将不胜感激。另外,我还需要在csv文件中记录试验编号,文本的颜色,颜色词的名称,参与者使用的颜色以及它们是否正确。在这方面的任何帮助都将超出我最初的问题,而且非常有帮助! :)

filename = 'stroop3data.csv'

f = open(filename, 'w')
csvfile = DictWriter(f, fieldnames = ['RT'])
csvfile.writeheader()

congruent_pairs = 15 * [('red', 'red'), ('blue', 'blue'), ('green',      'green'), ('yellow', 'yellow')]
incongruent_pairs = 5 * [('red', 'blue'), ('red', 'green'), ('red',   'yellow'), ('blue', 'red'), ('blue', 'green'), ('blue', 'yellow'), ('green', 'red'), ('green', 'blue'), ('green', 'yellow'), ('yellow', 'red'), ('yellow', 'blue'), ('yellow', 'green')]

pairs = congruent_pairs + incongruent_pairs



shuffle(congruent_pairs)
shuffle(incongruent_pairs)
shuffle(pairs)

answer_keys = {'f': 'red', 'g': 'blue', 'h': 'green', 'j': 'yellow'}

rts = []



win = visual.Window([1024, 768], fullscr = False, \
                allowGUI=True, units="pix", color = (-1, -1, -1))

stim = visual.TextStim(win)

instructionText = visual.TextStim(win, "Thank you for choosing to   participate in this experiment. You will be presented with different names of colours. In some trials the colour of the word will match the name of the colour, in some the colour of the word \
                              will be different to the name of the colour. Your task is to identify the colour of the word, ignoring the word itself. Coloured stickers on the keyboard in front of you correspond to the key you should press to indicate your response.\
                              \
                              \
                              PRESS 'ENTER' TO CONTINUE", color=(1,   1, 1))
instructionText.draw()

win.flip()

event.waitKeys(keyList=['return'])

readyText = visual.TextStim(win, "Ready?", color=(1, 1, 1))

readyText.draw()


win.flip()


event.waitKeys(keyList=['return'])



for pair in pairs:



    stim.text = pair[0]
    stim.color = pair[1]



    stim.draw()
    event.clearEvents()

    displaytime = win.flip()





    keys, rt = event.waitKeys(keyList=answer_keys.keys(), timeStamped=True)[0] 
    key, keytime = keys [0]
    rts.append(keytime - displaytime)




    score = answer_keys[key] == pair[1]#means that the correct answer corresponds to the colour of the word



    stim.text = 'CORRECT!' if score else 'INCORRECT'
    stim.text += '\nRT=%i ms' %(rt*1000)
    stim.draw()
    win.flip()
    event.waitKeys()

    csvfile.writerow({'RT': rts})

我已经尝试在保存密钥和响应时间的部分中更改其他名称。我尝试过的所有组合都会导致许多不同的错误...例如:'float对象不可迭代','tuple对象不可迭代'-第93行(关键时间-显示时间),该行上方的参数不足,等等。

我希望将反应时间以秒为单位保存到csv文件中。

0 个答案:

没有答案