OpenSesame中如何在sequence内随机混合两个loop的刺激?
解决OpenSesame中两类刺激随机混合呈现的问题
核心方案:合并刺激源为单一随机Loop
OpenSesame默认会按顺序执行嵌套的多个Loop,要实现两类刺激随机混合,最直接稳妥的方式是把两类刺激合并到一个数据源,用变量区分类型,再通过单一随机Loop控制呈现。
步骤1:合并刺激列表
- 把目标句(带Trigger)和填充句(无Trigger)的信息整理到同一个CSV文件,新增一列
stim_type,用target/filler标记类型。 - 针对目标句,额外添加
trigger_indices(需要触发的单词位置,比如0,2代表第1、3个词)和trigger_codes(对应触发码,比如101,103),填充句的这两列留空即可。CSV示例:sentence,stim_type,trigger_indices,trigger_codes "The cat sat on mat",target,1,102 "A dog runs fast",filler,,
步骤2:设置主随机Loop
- 添加一个Loop元件,导入合并后的CSV文件,将Order设置为
Random,确保所有试次随机抽取。
步骤3:构建试次Sequence(逐词呈现+Trigger控制)
在Loop内嵌套一个Sequence,用来处理单句的逐词呈现和Trigger逻辑:
- 添加
inline_script元件(放在Sequence开头),编写Python代码处理逐词和Trigger:# 获取当前试次的刺激信息 sentence = self.get('sentence') stim_type = self.get('stim_type') trigger_indices = self.get('trigger_indices') trigger_codes = self.get('trigger_codes') # 拆分句子为单词列表 words = sentence.split() # 预处理Trigger信息(仅目标句) trigger_dict = {} if stim_type == 'target' and trigger_indices: indices = [int(i) for i in trigger_indices.split(',')] codes = [int(c) for c in trigger_codes.split(',')] trigger_dict = dict(zip(indices, codes)) # 遍历每个单词,依次呈现并触发(如果需要) for idx, word in enumerate(words): # 更新当前要呈现的单词 self.experiment.set('current_word', word) # 运行text_display元件呈现单词(提前创建好名为word_display的text元件,内容设为[current_word]) self.experiment.items['word_display'].run() # 检查是否需要发送Trigger if idx in trigger_dict: # 发送触发码,根据你的硬件调整(比如并行口/USB设备) self.experiment.send_trigger(trigger_dict[idx]) # 添加单词间的延迟(比如500ms) self.clock.sleep(500) - 提前创建一个
text_display元件(命名为word_display),内容设置为[current_word],用来显示单个单词。
替代方案:动态生成打乱的试次列表
如果不想合并CSV,可以用Python脚本在实验启动时读取两个刺激文件,合并后打乱,再赋值给主Loop:
- 在实验最开头添加
inline_script元件,写入以下代码:import pandas as pd import random # 读取两个刺激文件(替换为你的文件路径) target_stims = pd.read_csv('target_sentences.csv').to_dict('records') filler_stims = pd.read_csv('filler_sentences.csv').to_dict('records') # 合并并随机打乱 all_stims = target_stims + filler_stims random.shuffle(all_stims) # 将打乱后的试次列表赋值给主Loop(替换为你的Loop名称) self.experiment.items['main_loop'].items = all_stims - 主Loop不需要导入CSV,直接设置为循环
len(all_stims)次,后续的Sequence逻辑和核心方案一致。
注意事项
- Trigger发送需确保硬件连接正常,OpenSesame的
send_trigger()方法支持多数常用设备,部分设备可能需要安装对应插件(比如并行口插件)。 - 逐词呈现的延迟可以根据实验需求调整,也可以用
keyboard_response元件让被试按键后进入下一个词。
内容的提问来源于stack exchange,提问作者Pablo Bernabeu
相关产品推荐
相关产品推荐

