psychopy coder模式編寫心理試驗程序 字符程序和記錄反應時

自變量

呈現3個字母,中間爲A或者B的話,被試須要反應f,中間爲X或Y的話,被試須要反應j。app

3個字母中間有兩種狀況:AB或XYdom

3個字母兩邊有同類或異類兩種狀況:AB兩邊是AB爲同類,爲XY爲異類;XY則反之函數

3個字母兩邊的顏色:紅、綠、藍ui

注視點呈現時間:400ms、500ms、600msthis

 

因變量

被試的反應時和判斷正確率spa

 

實驗設計

480次trail設計

注視點呈現400ms、500ms和600ms分別160次code

紅、綠、藍分別160次orm

中間爲A的狀況120次,其中60次兩邊爲B(同類),30次兩邊爲X(異類),30次兩邊爲Y(異類)htm

中間爲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

相關文章
相關標籤/搜索