- 注册时间
- 2016-8-2
- 阅读权限
- 10
- 最后登录
- 2018-8-24
- 在线时间
- 21 小时
- 贡献值
- 0
- 积分
- 81
- 分享
- 0
- 好友
- 10
- 记录
- 0
- 日志
- 0
- 相册
- 2
- UID
- 45974
 
  - 水晶
- 350
- 心级
- 81
- 精华
- 1
- 主题
- 3
- 帖子
- 19
|
空里流霜 发表于 2016-8-3 05:29 
支持,顺便提一下我原来写的一个示例程序,请各位滴油不吝批评指正用PsychoPy编写实验程序的示例(爱荷华赌 ...
代码写得不错,这个任务很复杂,代码已经比较精简了。几点建议,1) 可以创建一个draw card的函数,这样代码可以更精简一点;2) trial control建议先定义一个trial list,像E-prime那样,这样随机起来非常方便。我在这里贴一段简要的示例代码,主要演示一下整个实验的structure,希望能有所启示。
# Simon effect
# import libraries
import sys, random, os, time
from psychopy import visual, core, event
from sys import maxint
##from ctypes import windll
### routine to send event triggers to EEG
##p = windll.inpout32
##def send_code(code):
##? ? p.Out32(0x378, code)
##? ? time.sleep(0.006)
##? ? p.Out32(0x378, 0)
##? ? time.sleep(0.01)
def send_code(code): pass
# get subject info
def get_subj_info():
? ? subj_id = raw_input('Subject ID: ')
? ? subj_name = raw_input('Subject Name: ')
? ? subj_gender = raw_input('Subject Gender (m/f): ')
? ? subj_age = raw_input('Subject Age: ')
? ? return [subj_id, subj_name, subj_gender, subj_age]
subj = get_subj_info()
# create a window
win = visual.Window((1024, 768), units='pix', fullscr=False)
# some constants
left = (-200, 0)
right = (200, 0)
red = (1.0, -1.0, -1.0)
blue = (-1.0, -1.0, 1.0)
black = (-1.0,-1.0,-1.0)
white = (1.0,1.0,1.0)
random.seed = 1000
# trial list
trials = [['L', 'R', 'z', 1],
? ?? ?? ? ['L', 'B', '/', 2],
? ?? ?? ? ['R', 'R', 'z', 3],
? ?? ?? ? ['R', 'B', '/', 4]]
text_msg = visual.TextStim(win, text='message')
tar_stim = visual.GratingStim(win, tex='None', mask='circle', size=60.0)
# run a single trial
def run_trial(pars, data_file, subj_info):
? ? """ pars should be list that specifies the
? ? location and color of the stimulus, as well as
? ? the correct key response, such as ['R', 'B', '/']"""
? ? l, c, cor_resp, trig = pars
? ? if l == 'R': loc = right
? ? if l == 'L': loc = left
? ? if c == 'R': col = red
? ? if c == 'B': col = blue
? ? # present the fixation
? ? text_msg.setText('+')
? ? text_msg.draw()
? ? win.flip()
? ? core.wait(0.750)
? ? # present the target
? ? tar_stim.setColor(col)
? ? tar_stim.setPos(loc)
? ? tar_stim.draw()
? ? win.flip()
? ? send_code(trig)
? ? tar_resp = event.waitKeys(1500, ['z', 'slash'],timeStamped=True)
? ? print tar_resp
? ? # write data to file
? ? trial_data = subj_info + pars + list(tar_resp[0])
? ? trial_data = map(str, trial_data)
? ? trial_data = ','.join(trial_data) + '\n'
? ? data_file.write(trial_data)
? ?
? ? win.setColor(black)
? ? win.flip()
? ? core.wait(0.500)
#### real experiment starts here
# open a data file
if not os.path.exists('data_file'): os.mkdir('data_file')
d_file = open('data_file/' + '_'.join(subj[:2]) +'.csv', 'w')
# show the instructions
text_msg.setText('Press Z to RED, press / to BLUE. Press to start')
text_msg.draw()
win.flip()
event.waitKeys(maxint, keyList=['space'])
test_trials = trials[:]*2
random.shuffle(test_trials)
for t in test_trials:
? ? run_trial(t, d_file, subj)
# close data file
d_file.close()
# exit
sys.exit() |
-
总评分:?水晶 + 41?
?查看全部评分
|