编码多个列表和循环

时间:2017-06-07 15:30:18

标签: python list loops psychopy

对于我的实验,我正在创建一个简单的反应时间测试。它将有4个街区,每个街区有6个试验。我已经设法在屏幕上呈现了一个块的第一次试验,虽然是练习轮(然而刺激确实出现了)。但是,我不确定是否应该创建一些列表(即为不同的试验保留不同的刺激),或者我是否应该改变我现有循环的特征。我的代码如下:

from psychopy import visual, core, event #import some libraries from PsychoPy
import psychopy.event

#Create the code for saving the data at some point


#create a window
mywin = visual.Window([1920,1080], monitor="testMonitor", units="deg")
mywin.update()

# create the six stimuli:
Target = visual.TextStim(mywin, text = "text default")
text_stim_list = [] # a list to hold them
Stim_text = ["Berlin", "Paris", "London", "Nice", "Vienna", "Charming"] # 
their content

#the positions of the stimuli
positions = [
 (-10, 10),
 (10, 10),
 (-10, -10),
 (10, -10),
 (-1, -10),
 (1, 10),
 ]

 #Initial message to participants about the study 
 message1 = visual.TextStim(mywin, text = "You have been captured by the 
 plotters. As a test to see if you have information regarding the event, an 
on screen test will be adminstered. Press Spacebar when ready")
message1.draw()
mywin.update()

#this will wait for a button press confirmation from p's to continue
response = event.waitKeys(keyList = ['space',], timeStamped = True)

#Initial message to participants about the study 
message1 = visual.TextStim(mywin, text = "On the next screen, you will be 
presented with a practice round of stimuli. Press Q if any of the cities are 
familiar, or press P if you do not recognise the cities")
message1.draw()
mywin.update()

#this will wait for a button press confirmation from p's to continue
response = event.waitKeys(keyList = ['space',], timeStamped = True)

#Loop for drawing the stimulus 
#code for the keyboard response
keys = psychopy.event.waitKeys(keyList=["q", "p"])

#Practice round-should it be stopped by a button press?

for frameN in range(7*60):
# draw each stim *on each frame*
for i in range(len(Stim_text)):
    Target.setPos(positions[i])
    Target.setText(Stim_text[i])
    Target.draw()
# now flip the window (after all stimuli drawn)
mywin.flip()

 #Initial message to participants about the study 
message1 = visual.TextStim(mywin, text = "On the next screen, the cities 
which 
our intelligence tells us are at risk will be presented. Press Q if any are 
familiar, press P if not blah blah blah.. press space when ready")
message1.draw()
mywin.update()
response = event.waitKeys(keyList = ['space',], timeStamped = True)#wait for 
subjects to state they are ready

#Cities block
#First trial 

print Stim_text
del Stim_text[1,2]
print Stim_text

我相当肯定它会涉及更改循环的属性 - 否则就有这么多列表没有意义。

1 个答案:

答案 0 :(得分:0)

一般来说,我会做这样的事情:

# Run the code inside this loop four times
for block in range(4):
    for i, position in enumerate(positions):
        # Prepare stimulus
        Target.pos = position
        Target.text = Stim_text[i]

        # Show it 
        Target.draw()
        win.flip()

        # Collect response and score it
        key = event.waitKeys(keyList=['q', 'p'])[0]  # Just get the first response key
        # Save response here somehow.

正如Mike在评论中指出的那样,请查看psychopy.data.TrialHandler,它可以为您做很多事情,包括保存数据。此外,无需为每条消息生成新的visual.TextStim。只需使用所需文本更新现有文本即可。