自变量
呈现3个字母,中间为A或者B的话,被试需要反应f,中间为X或Y的话,被试需要反应j。
3个字母中间有两种情况:AB或XY
3个字母两边有同类或异类两种情况:AB两边是AB为同类,为XY为异类;XY则反之
3个字母两边的颜色:红、绿、蓝
注视点呈现时间:400ms、500ms、600ms
因变量
被试的反应时和判断正确率
实验设计
480次trail
注视点呈现400ms、500ms和600ms分别160次
红、绿、蓝分别160次
中间为A的情况120次,其中60次两边为B(同类),30次两边为X(异类),30次两边为Y(异类)
中间为B、X和Y的情况依此构造
需要记录的结果
被试ID、实验序号、注视点呈现时间、呈现的字母、两边字母的颜色、正确的反应按键、实际的反应按键、反应时、是否反应正确。
程序思路
首先准备实验刺激,生成有16个项目的trailTypes,和颜色组合成48种,翻10倍,再和注视点时间进行搭配。之后写函数来实现具体程序。阅读程序时,从“这是程序真正开始的地方”开始阅读会比较容易,之后再具体看程序。
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 10 12:40:30 2016
@author: zbg
"""
from psychopy.visual import Window, ImageStim, TextStim
from psychopy import core, event, gui, clock
import random
#准备trail和注视点时间
trailTypes = ['BAB', 'BAB', 'XAX', 'YAY',
'ABA', 'ABA', 'XBX', 'YBY',
'YXY', 'YXY', 'AXA', 'BXB',
'XYX', 'XYX', 'AYA', 'BYB']
colors = [(-1,-1,1), (-1,1,-1), (1,-1,-1)]
trails = []
"""
trails = [(timeCross, trailType, color, correctKey), ...]
"""
timeCrosses = [.4] * 160 + [.5] * 160 + [.6] * 160
for i in range(10):
for tt in trailTypes:
for c in colors:
tc = tt[1]
correctKey = 0
if tc in "AB":
correctKey = 'f'
elif tc in "XY":
correctKey = 'j'
trails.append((timeCrosses.pop(), tt, c, correctKey))
#到这里,trails包含16 * 3 * 10 = 480次试验
random.shuffle(trails)
#存放结果的地方
results = []
"""
results = [(按键, 反应时, 是否正确), ...]
"""
#程序使用的各种函数
def GetSubject():
"""
返回被试的id
"""
myDlg = gui.Dlg(title="Subject Information")
myDlg.addField(u'被试ID:')
myDlg.show()
if myDlg.OK:
thisInfo = myDlg.data
else:
exit(0)
return thisInfo[0]
def ShowIntro(win):
introduce =u"""
啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦
按[空格键]继续
"""
t =TextStim(win, introduce ,pos=(0,-0.0))
t.draw()
win.flip()
keys=[]
while 'space' not in keys:
keys=event.getKeys()
def ShowBlank(win):
win.flip()
clk = clock.CountdownTimer(0.5)
while clk.getTime() > 0:
pass
def ShowCross(win, time):
t =TextStim(win, '+' ,pos=(0,0), height = 55, units = "pix")
t.draw()
win.flip()
clk = clock.CountdownTimer(time) #非常精确的计时器
while clk.getTime() > 0:
pass
return
def ShowTrailAndGetKey(win, text, color):
"""
返回(按键, 反应时(s))
情况分别有:
('j', 反应时)
('f', 反应时)
('0', 1.2) (超时)
"""
tLeft = TextStim(win, text[0], pos=(-70, 0), color = color, height = 55, units = "pix")
tRight = TextStim(win, text[2], pos=(+70, 0), color = color, height = 55, units = "pix")
tCenter = TextStim(win, text[1], pos=( 0, 0), color = (1,1,1), height = 55, units = "pix")
tLeft.draw()
tRight.draw()
tCenter.draw()
event.clearEvents()
win.flip()
clk = clock.CountdownTimer(1.2)
while clk.getTime() > 0:
keys = event.getKeys()
if 'j' in keys:
return ('j', 1.2-clk.getTime())
elif 'f' in keys:
return ('f', 1.2-clk.getTime())
elif 'q' in keys:
return ('q', 1.2-clk.getTime())
#超时
return ('0', 1.2)
def ShowIncorrect(win):
t =TextStim(win, u'按键错误' ,pos=(0,0), height = 55, units = "pix")
t.draw()
win.flip()
clk = clock.CountdownTimer(1) #非常精确的计时器
while clk.getTime() > 0:
pass
return
def ShowBreak(win):
introduce =u"""
啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦
按[空格键]继续
"""
t =TextStim(win, introduce ,pos=(0,-0.0))
t.draw()
win.flip()
clk = clock.CountdownTimer(10) #强制休息10秒
while clk.getTime() > 0:
pass
keys=[]
event.clearEvents()
while 'space' not in keys:
keys=event.getKeys()
def ShowEnd(win):
introduce =u"""
啦啦啦啦啦啦啦啦啦啦
啦啦啦啦啦啦啦
按[空格键]退出
"""
t =TextStim(win, introduce ,pos=(0,-0.0))
t.draw()
win.flip()
keys=[]
while 'space' not in keys:
keys=event.getKeys()
def StoreResult(name, N, trails, results):
fp = open(name + '.txt','w')
fp.write("ID\tnum\ttimeCross\ttrailType\tcolor\tcorrectKey\trKey\trTime\tCorrect\n")
def w(x):
fp.write(str(x) + '\t')
def n():
fp.write('\n')
for i in range(N):
(timeCross, trailType, color, correctKey) = trails[i]
(rKey, rTime, Correct) = results[i]
w(name)
w(i+1)
w(timeCross)
w(trailType)
w(color)
w(correctKey)
w(rKey)
w("%.0f" % (rTime * 1000))
w(Correct)
n()
fp.close()
#这是程序真正开始的地方
N = 480
name = GetSubject()
win = Window(fullscr = True, color=(-1,-1,-1))
ShowIntro(win)
for i in range(N):
(timeCross, trailType, color, correctKey) = trails[i]
ShowBlank(win)
ShowCross(win, timeCross)
(rKey, rTime) = ShowTrailAndGetKey(win, trailType, color)
if rKey == 'q':
StoreResult(name, i, trails, results)
exit(0)
if rKey != correctKey:
ShowIncorrect(win)
results.append((rKey, rTime, 0))
else:
results.append((rKey, rTime, 1))
if i % 120 == 119:
ShowBreak(win)
ShowEnd(win)
StoreResult(name, N, trails, results)
win.close()
psychopy 定做实验程序 https://item.taobao.com/item.htm?spm=a230r.1.14.6.Q6E2OW&id=530690095131&ns=1&abbucket=15#detail
来源:oschina
链接:https://my.oschina.net/u/1387924/blog/666105