Python脚本不起作用?:播放声音,测量反应时间
我想制作一个脚本,当参与者听到声音文件中2/30的声音时,它测量参与者按enter键或空格键的速度。 因此,有时用户不必按任何键,脚本仍会转到下一个声音文件。我该怎么做?我现在拥有的是:(不是声音文件,而是文本自动取款机。):Python脚本不起作用?:播放声音,测量反应时间,python,loops,audio,measure,psychopy,Python,Loops,Audio,Measure,Psychopy,我想制作一个脚本,当参与者听到声音文件中2/30的声音时,它测量参与者按enter键或空格键的速度。 因此,有时用户不必按任何键,脚本仍会转到下一个声音文件。我该怎么做?我现在拥有的是:(不是声音文件,而是文本自动取款机。): 我最终都会疯掉的。提前感谢您的帮助 缩进存在问题,因此并非所有内容都在适当级别的试用功能中运行。您可能可以删除异常处理,以便更直接地检查是否做出了响应。然后使用for循环的else:子句来处理循环完成但未按下任何键时发生的情况。这避免了必须处理在任何给定帧上发生的非响应的
我最终都会疯掉的。提前感谢您的帮助 缩进存在问题,因此并非所有内容都在适当级别的试用功能中运行。您可能可以删除异常处理,以便更直接地检查是否做出了响应。然后使用
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
缩进存在问题,因此并非所有内容都在适当级别的试用功能中运行。您可能可以删除异常处理,以便更直接地检查是否做出了响应。然后使用
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
好的,很好-谢谢你!现在我继续尝试开发您的脚本。我已经添加了一份试用名单,现在我不太确定该怎么办。我的脚本如下所示:
# 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
好的,很好-谢谢你!现在我继续尝试开发您的脚本。我已经添加了一份试用名单,现在我不太确定该怎么办。我的脚本如下所示:
# 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
作为一般提示,避免将
trial
作为全局变量。只需在trial()
函数之外增加它,并将其作为第二个参数传递给函数。同样,不要每次运行指令函数时都创建文本刺激(这是一个耗时的操作)。创建一次,并将文本刺激和新文本内容一起传递给函数。这是试用函数中一个更大的问题,文本刺激在每一帧上都被重新实例化。这是非常耗时的,可能会导致计时问题。作为一般提示,请避免将trial
作为全局变量。只需在trial()
函数之外增加它,并将其作为第二个参数传递给函数。同样,不要每次运行指令函数时都创建文本刺激(这是一个耗时的操作)。创建一次,并将文本刺激和新文本内容一起传递给函数。这是试用函数中一个更大的问题,文本刺激在每一帧上都被重新实例化。这是非常耗时的,可能会导致计时问题。您确实需要查看TrialHandler
类。将您的试用列表输入到该列表中,它将自动处理跟踪试用编号、保存数据等操作。在PsychoPy的Coder视图中的Demos菜单下有几个示例。您确实需要查看TrialHandler
类。将您的试用列表输入到该列表中,它将自动处理跟踪试用编号、保存数据等操作。在PsychoPy的Coder视图中的Demos菜单下有几个示例。