其他
Psychopy | 第4期:实验数据的收集与处理
from psychopy import core, gui #导入
info = {'observer':'01_xxx','gender':['m','f'],'age':18}
dlg = gui.DlgFromDict(info, title='flanker',
order=['observer','gender','age']) #设置gui
if dlg.OK: #单击 OK 后的操作
pass #占位符
else:
core.quit()
import time
date = time.strftime("_20%y_%m_%d_%H%M", time.localtime()) #获取当前时间
file = 'data/'+info['observer']+'_'+info['gender']+
'_'+str(info['age'])+date #文档名(含路径)
with open("%s.csv"%(file),'a') as D: #写入表头
D.write('flanker'+','+'center'+','+'RT'+','+'accuracy'+'\n')
from psychopy import event
for frame in range(fix_times): #注视点呈现
fix.draw()
time0 = Win.flip()
print(time0)
while True:
[s.draw() for s in stims] #刺激呈现
time1 = Win.flip()
if len(event.getKeys(['left','right'])) > 0: break
RT = time1 - time0 #计算反应时
from psychopy import event
b=0 #设置变量 b 的起始值用于后面控制呈现中止
while True: #呈现刺激
[s.draw() for s in stims]
time1 = Win.flip()
for key in event.getKeys(): #收集按键
if key in ['left', 'right']:
RT = round((time1-time0)*1000) #计算反应时
b=1
break #跳出收集按键的 for 循环
if b==1:
b=0 #b设置回默认
break #跳出呈现的 while 循环
if (stims[2].flipHoriz==True and key=='right') or\
(stims[2].flipHoriz==False and key=='left'): #判断按键是否正确
acc=1
else:
acc=0
with open("%s.csv"%(file),'a') as D:
D.write(trial[0]+','+trial[1]+','+str(RT)+','+str(acc)+'\n')
# -*- coding: utf-8 -*-
"""
The demo of flanker
@author: A Mei
"""
from psychopy import visual, event, gui, core
import time, random #导入
#设置gui
info = {'observer':'01_xxx','gender':['m','f'],'age':18}
dlg = gui.DlgFromDict(info, title='flanker',
order=['observer','gender','age'])
if dlg.OK:
pass
else:
core.quit()
#设置数据文档
date = time.strftime("_20%y_%m_%d_%H%M", time.localtime()) #设置文件
file = 'data/'+info['observer']+'_'+info['gender']+'_'+str(info['age'])+date
with open("%s.csv"%(file),'a') as D:
D.write('flanker'+','+'center'+','+'RT'+','+'accuracy'+'\n')
#设置窗口、注视点、结束语
Win = visual.Window((1024,768), color=(128,128,128), fullscr=False,
units='pix',colorSpace='rgb255')
fix = visual.TextStim(Win, text='+', color='black', height=50,bold=True)
endPrompt = visual.TextStim(Win, text='实验结束,谢谢!',
color='black', height=60)
#设置各部分呈现时间
Rate = 60
Dura = 1000/Rate
fix_Dura = 300
blank_Dura = 500
fix_times = int(round(fix_Dura/Dura))
blank_times = int(round(blank_Dura/Dura))
#设置自变量
var = []
for flanker in ['left', 'right']:
for center in ['same','diff']:
var.append([flanker, center])
random.shuffle(var)
#设置刺激位置
sites = []
for site in range(5):
sites.append((-100+50*site,0))
#开始循环
a = 0
b = 0
for trial in var:
a+=1
#设置是否翻转
stims = []
for stim in range(5):
if (trial[0] == 'left' and trial[1] == 'diff' and stim == 2) or\
(trial[0] == 'right' and trial[1] == 'diff' and stim != 2) or\
(trial[0] == 'right' and trial[1] == 'same'):
Horiz = True
else:
Horiz = False
#定义箭头
arr = visual.TextStim(Win, text='←', color='black', height=50,
pos=(-200+100*stim,0),flipHoriz=Horiz,
bold=True)
stims.append(arr)
#呈现注视点
for frame in range(fix_times):
fix.draw()
time0 = Win.flip()
#呈现刺激并收集反应时
while True:
[s.draw() for s in stims]
time1 = Win.flip()
for key in event.getKeys():
if key in ['left', 'right']:
RT = round((time1-time0)*1000)
b=1
break
if b==1:
b=0
break
#判断被试反应是否正确
if (stims[2].flipHoriz==True and key=='right') or\
(stims[2].flipHoriz==False and key=='left'):
acc=1
else:
acc=0
#呈现空屏
for frame in range(blank_times):
Win.flip()
#写入数据
with open("%s.csv"%(file),'a') as D:
D.write(trial[0]+','+trial[1]+','+str(RT)+','+str(acc)+'\n')
#呈现结束语
if a==len(var):
while True:
endPrompt.draw()
Win.flip()
if len(event.getKeys()) > 0: break
Win.close()
(点击左右滑动可看完整代码)