2016-12-06 92 views
1

我想製作一個腳本,用於測量參與者按下輸入鍵或空格鍵的速度,只有當他們從聲音文件中聽到2/30聲音時才按下輸入鍵或空格鍵。 所以有時用戶不必按任何東西,腳本仍然移動到下一個聲音文件。我該怎麼做呢?我現在所擁有的是這樣的:(而不是聲音文件,我有文字大氣壓):Python腳本不工作?:播放聲音,測量反應時間

# Grounding of Words Experiment # 

#Import libraries 
import re 
import glob 
from psychopy import sound, visual, event, data, core, gui # imports a module for visual presentation and one for controlling events like key presses 


# ID, age, gender box display 
myDlg = gui.Dlg(title="Experiment") #, pos=(400,400) 
myDlg.addField('ID:') 
myDlg.addField('Age:') 
myDlg.addField('Gender:', choices = ['Female', 'Male']) 
myDlg.show()#you have to call show() for a Dlg 
if myDlg.OK: 
    ID = myDlg.data[0] 
    Age = myDlg.data[1] 
    Gender = myDlg.data[2] 
else: 
    core.quit() 

trial=0 

#Creates the outfile, that will be the file containing our data, the name of the file, as saved on the computer is the filename 
out_file="Grounding_experiment_results.csv" 
#Creates the header for the data 
header="trial,ID,Gender,Age,Word,rt,SpaceKlik\n" 
#opens the outfile in writemode 
with open(out_file,"w") as f: 
    f.write(header)#writes the header in the outfile 


# define window 
win = visual.Window(fullscr=True) # defines a window using default values (= gray screen, fullscr=False, etc) 

# Instruction box display 

def instruct(txt): 
    instructions = visual.TextStim(win, text=txt, height = 0.05) # create an instruction text 
    instructions.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented 
    win.flip() # flip the screen to reveal the stimulus 
    event.waitKeys() # wait for any key press 

instruct(''' 
Welcome to the experiment! 

You will be hearing different words. 
Whenever you hear the word "Klik" and "Kast" please press the left mouse button. 
Whenever you hear any other word - do nothing. 
Try to be as fast and accurate as possible. 
Please put on the headphones. 
The experiment will take 5 minutes. 

Press any key to start the experiment''') 

# Play sound 

# Function that makes up a trial 

trial(word): 
    global trial 
    trial += 1 
    if word in ["Klik", "Press", "Throw"]: 
     condition = "press" 
    else : 
     condition = "no_press" 
    event.clearEvents() 
    for frame in range(90): 
     text = visual.TextStim(win, text=word, height = 0.05) 
     text.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented 
     time_start=win.flip() 
    try: 
     key, time_key=event.getKeys(keyList=['space', 'escape'], timeStamped = True)[0] # wait for any key press 
    except IndexError: 
     key = "0" 
     rt = "NA" 
    else: 
     if key=='escape': 
       core.quit() 
     rt = time_key - time_start 
    if key == "space" and condition=="press": 
     accuracy = 1 
    elif key == "0" and condition=="no_press": 
     accuracy = 1 
    else: 
     accuracy = 0 
    with open(out_file,"a") as f: 
     f.write("{},{},{},{},{},{},{},{}\n".format(trial,ID,Gender,Age,word,accuracy,rt,SpaceKlik)) 

# s = sound.Sound('sound.wav') 
# s.play() 

# Register space bar press or mouse click 

# Measure reaction time 

# Check to see if answer is correct to sound - certain sound files are "klik". Others "kast", "løb", "sko" and so on 

# Write csv logfile with coloumns: "ID", "Gender", "Word", "Correct/incorrect", "Reaction time", "Space/click" 

我將在年底全部PsychoPy運行。預先感謝您的協助。

+0

作爲一般提示,請避免將'trial'作爲全局變量。只需在'trial()'函數之外遞增它並將其作爲第二個參數傳遞給該函數。同樣,每次運行指令功能時都不要創建文本激勵(這是一個耗時的操作)。創建一次,並將文本刺激傳遞給函數,以及新的文本內容。在您的試用函數中,這是一個更大的問題,在每個幀上都會重新實例化文本刺激。這非常耗時且可能導致計時問題。 –

回答

0

您的縮進存在問題,因此並非所有內容都在適當級別的試用功能中運行。您可能會放棄異常處理,以更直接地檢查是否已做出響應。然後利用for循環的else:子句來處理循環完成但未按下任何鍵時發生的情況。這避免了必須處理任何給定幀上發生的無響應的邏輯(如果響應很快就會發生,這可能不代表任何意義)。

像這樣的東西一般僞代碼:

# define window 
win = visual.Window(fullscr=True) 

# function to draw instructions (probably overkill unless run more than once) 
def instruct(instructions = 'xxx'): 
    # as you have above 

# function to run each trial 
def trial(number = -999, sound_name = 'A'): 

    sound = Sound.sound(sound_name) 

    if sound_name in ["Klik.wav", "Press.wav", "Throw.wav"]: 
     condition = "press" 
    else: 
     condition = "no_press" 

    event.clearEvents() 

    sound.play() 

    for frame in range(90): 

     time_start = win.flip() 

     keys = event.getKeys(keyList=['space', 'escape'], timeStamped = True) 
     if keys: # if a non-empty list returned: 
      key, time_key = keys[0] 
      rt = time_key - time_start 

      sound.stop() # ready to start next trial immediately 

      if key == 'escape': 
       core.quit() 

      if condition == "press": 
       return {'accuracy':1, 'rt':rt} 
      else: 
       return {'accuracy':0, 'rt':rt} 

    else: # the loop ended without a key press 
     if condition == "press": 
      return {'accuracy':0, 'rt':'NA'} 
     else: 
      return {'accuracy':1, 'rt':'NA'} 

#### 
# run the experiment: 
#### 

#### 
# show the instructions: 
#### 
instruct('some instructions') 

#### 
# run the trials: 
#### 
for trial_num in range(10): 

    # run each trial and get the results. 
    # I'm not sure where you're getting your sound values 
    # but they could be indexed from a list using the trial number: 
    result = trial(number=trial_num, sound_name=sound_names[trial_num]) 

    # then save the values from the returned dictionary to a file 
0

好的,好的 - 謝謝!現在我繼續嘗試開發你的腳本。我已經添加了一個試用列表,現在我不太確定該做什麼。我的腳本如下所示:

# Grounding of Words Experiment # 

# -*- coding: utf-8 -*- 

#Import libraries 
import re 
import glob 
from psychopy import sound, visual, event, data, core, gui # imports a module for visual presentation and one for controlling events like key presses 
import ppc 

# ID, age, gender box display 
myDlg = gui.Dlg(title="Experiment") #, pos=(400,400) 
myDlg.addField('ID:') 
myDlg.addField('Age:') 
myDlg.addField('Gender:', choices = ['Female', 'Male']) 
myDlg.show()#you have to call show() for a Dlg 
if myDlg.OK: 
    ID = myDlg.data[0] 
    Age = myDlg.data[1] 
    Gender = myDlg.data[2] 
else: 
    core.quit() 

# define window 
win = visual.Window(fullscr=True) 


# function to draw instructions (probably overkill unless run more than once) 
def instruct(txt): 
    instructions = visual.TextStim(win, text=txt, height = 0.05) # create an instruction text 
    instructions.draw() # draw the text stimulus in a "hidden screen" so that it is ready to be presented 
    win.flip() # flip the screen to reveal the stimulus 
    event.waitKeys() # wait for any key press 
    # as you have above 


# function to run each trial 
def trial(number = -999, sound_name = 'A'): 

    sound = Sound.sound() 

    if sound_name in ["dog-howling.wav"]: 
     condition = "press" 
    else: 
     condition = "no_press" 

    event.clearEvents() 

    sound.play(sound_name) 

    for frame in range(90): 

     time_start = win.flip() 

     keys = event.getKeys(keyList=['space', 'escape'], timeStamped = True) 
     if keys: # if a non-empty list returned: 
      key, time_key = keys[0] 
      rt = time_key - time_start 

      sound.stop() # ready to start next trial immediately 

      if key == 'escape': 
       core.quit() 

      if condition == "press": 
       return {'accuracy':1, 'rt':rt} 
      else: 
       return {'accuracy':0, 'rt':rt} 

    else: # the loop ended without a key press 
     if condition == "press": 
      return {'accuracy':0, 'rt':'NA'} 
     else: 
      return {'accuracy':1, 'rt':'NA'} 
#### 
# define triallist 

trial_list = [] 
conditions = ["klik", "notklik"] 
sounds = ["dog-howling.wav", "sound2.wav", "sound3.wav"] 
for condition in conditions: 
    for sound in sounds: 
     # Add a dictionary for every trial 
      trial_list += [{ 
       'ID': ID, 
       'age': AGE, 
       'gender': GENDER, 
       'condition': condition, 
       'sounds': sound, 
       'rating': '', 
       'rt': '' 
        }] 

# Randomize order 
trial_list = sample(trial_list, len(trial_list)) 

# Add trial numbers 
for i, trial in enumerate(trial_list): 
    trial['no'] = i + 1 # start at 1 

# write file 

#### 
# run the experiment: 
#### 

#### 
# show the instructions: 
#### 
instruct('''Welcome to the experiment! 

You will be hearing different words. 
Whenever you hear the word "Klik" and "Kast" please press the left mouse button. 
Whenever you hear any other word - do nothing. 
Try to be as fast and accurate as possible. 
Please put on the headphones. 
The experiment will take 5 minutes. 

Press any key to start the experiment''') 

#### 
# run the trials: 
#### 
for trial_num in range(10): 

    # run each trial and get the results. 
    # I'm not sure where you're getting your sound values 
    # but they could be indexed from a list using the trial number: 
    result = trial(number=trial_num, sound_name=sound_names[trial_num]) 

    # then save the values from the returned dictionary to a file+ 

#Creates the outfile, that will be the file containing our data, the name of the file, as saved on the computer is the filename 
#out_file="Grounding_experiment_results.csv" 
#Creates the header for the data 
#header="trial,ID,Gender,Age,Word,rt,SpaceKlik\n" 
#opens the outfile in writemode 
#with open(out_file,"w") as f: 
# f.write(header)#writes the header in the outfile 
+0

你真的需要查看'TrialHandler'類。將您的試用清單提供給它,並自動處理跟蹤試用號碼,保存數據等。 PsychoPy的編碼器視圖中的演示菜單下有幾個示例。 –