PsyNet如何基于analyze_recording的analysis结果展示试次自定义反馈
实现方案
1. 配置单试次分析逻辑
首先要为你的试次类实现analyse_response方法,确保每个试次完成后会生成敲击次数的分析数据:
class YourTrial(Trial): # 保留你原有Trial类的其他配置 def analyse_response(self, response, participant): # 从用户提交的响应数据中统计当前试次的敲击次数,根据你实际的响应字段调整 tap_count = len(response.get("tap_events", [])) return { "num_resp_raw": tap_count }
2. 两种方式获取总敲击次数
方式一:直接在performance_check中遍历汇总(无需额外配置)
不需要等待框架生成聚合分析结果,直接遍历当前试次块下的所有试次手动汇总:
def performance_check(self, experiment, participant, participant_trials): n_taps_detected = 0 for trial in participant_trials.trials: if trial.status == "completed" and trial.analysis is not None: n_taps_detected += trial.analysis.get("num_resp_raw", 0) failed = participant_trials.failed return {"score": n_taps_detected, "passed": not failed}
方式二:配置试次块聚合分析(可直接调用participant_trials.analysis)
如果要使用你原来代码里的participant_trials.analysis['num_resp_raw_all']写法,需要给PracticeTrialMaker添加聚合分析方法:
class PracticeTrialMaker(StaticTrialMaker): # 保留你原来的配置 give_end_feedback_passed = True performance_check_type = "performance" performance_check_threshold = 0 end_performance_check_waits = True # 新增聚合分析方法,框架会自动调用生成participant_trials.analysis def aggregate_analysis(self, trials, participant): total_taps = 0 for trial in trials: if trial.status == "completed" and trial.analysis: total_taps += trial.analysis.get("num_resp_raw", 0) return {"num_resp_raw_all": total_taps} # 你原来的反馈页面和性能检查逻辑可以保持不变 def get_end_feedback_passed_page(self, score): how_many_taps = "NA" if score is None else f"{(score):.0f}" return InfoPage( Markup( f"你本次敲击了 <strong>{how_many_taps} 次</strong>。" ), time_estimate=5, ) def performance_check(self, experiment, participant, participant_trials): n_taps_detected = participant_trials.analysis['num_resp_raw_all'] failed = participant_trials.failed return {"score": n_taps_detected, "passed": not failed}
注意事项
你已经配置了end_performance_check_waits = True,该配置会保证框架在所有试次的分析逻辑执行完成后再调用performance_check,不会出现analysis为空的问题。
内容的提问来源于stack exchange,提问作者vanirajendran
相关产品推荐
相关产品推荐

