You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替换randrange生成固定方向及间隔≥60°伪随机方向的技术需求

解决刺激方向生成的问题:固定方向+间隔合规的伪随机方向

看起来你需要在Psychopy实验中生成两类刺激方向:三个固定值,以及满足最小间隔要求的伪随机值,原代码的问题在于没有对生成的方向做间隔校验,导致出现重复或间距过近的情况。下面是修改后的完整方案:

关键细节提醒

首先注意一个点:你提到的固定方向包含215°,但原代码里用random.randrange(0, 179)生成0-178的方向,这会导致215°被等价为35°(215-180),和第一个固定方向重复。我假设你的实验允许0-359°的方向范围(更符合实际角度逻辑),如果仍需限制在0-179°,可以参考末尾的调整说明。

修改后的完整代码

from psychopy import visual, event, misc, core, sound, gui, data
from sys import argv
from math import sin, cos
import numpy as np
import numpy.random as rand
import sys, os, datetime as dt
import random
import csv
from psychopy.tools.filetools import fromFile

# 1. 定义固定方向和核心参数
fixed_orientations = [35, 155, 215]  # 要求的三个固定刺激方向
min_angle_spacing = 60  # 伪随机方向的最小间隔要求
all_ori = []  # 存储所有生成的方向,用于后续校验

# 2. 先处理固定方向的刺激(分配给前3个stim)
for s in range(3):
    this_stim = stims[s]
    this_stim.setFillColor(-1)  # 确保刺激填充色为黑色
    
    # 计算刺激的坐标位置
    if vis == 'left':
        current_x, current_y = eccen * np.cos(left_polar[s]), eccen * np.sin(left_polar[s])
    elif vis == 'right':
        current_x, current_y = eccen * np.cos(right_polar[s]), eccen * np.sin(right_polar[s])
    
    this_stim.setPos([current_x, current_y])
    this_orient = fixed_orientations[s]
    all_ori.append(this_orient)
    this_stim.setOri(this_orient)
    this_stim.draw()

# 3. 定义生成合规伪随机方向的辅助函数
def generate_valid_random_ori(existing_oris, min_spacing):
    while True:
        # 生成0-359范围内的候选方向
        candidate_ori = random.randint(0, 359)
        # 检查与所有已有方向的环形最小间隔
        is_valid = True
        for ori in existing_oris:
            angle_diff = abs(candidate_ori - ori)
            # 环形角度的最小间隔:取顺时针或逆时针的较小值(比如350°和10°的间隔是20°而非340°)
            min_diff = min(angle_diff, 360 - angle_diff)
            if min_diff < min_spacing:
                is_valid = False
                break
        if is_valid:
            return candidate_ori

# 4. 处理剩余的伪随机刺激
for s in range(3, len(stims)):
    this_stim = stims[s]
    this_stim.setFillColor(-1)
    
    # 计算刺激的坐标位置
    if vis == 'left':
        current_x, current_y = eccen * np.cos(left_polar[s]), eccen * np.sin(left_polar[s])
    elif vis == 'right':
        current_x, current_y = eccen * np.cos(right_polar[s]), eccen * np.sin(right_polar[s])
    
    this_stim.setPos([current_x, current_y])
    # 生成符合间隔要求的伪随机方向
    this_orient = generate_valid_random_ori(all_ori, min_angle_spacing)
    all_ori.append(this_orient)
    this_stim.setOri(this_orient)
    print(all_ori)  # 打印验证方向是否符合要求
    this_stim.draw()

方案核心优势

  • 固定方向优先分配:先将三个固定方向绑定到前3个刺激,确保它们必出现在实验中
  • 环形间隔精准校验:考虑角度的环形特性,避免出现“350°和10°间距算340°”这类错误判断,确保所有方向间的最小间隔严格≥60°
  • 循环验证生成逻辑:通过不断生成候选值并校验,直到找到符合要求的方向,彻底解决原代码中重复、间距过近的问题

若需限制方向在0-179°的调整方案

  1. 调整固定方向:215°在0-179°范围内等价于35°,会和第一个固定方向重复,建议改为[35, 95, 155](三个方向间隔均为60°,符合要求)
  2. 修改辅助函数:
    • 生成范围改为random.randint(0, 179)
    • 间隔计算改为min(angle_diff, 180 - angle_diff)

内容的提问来源于stack exchange,提问作者Mary Jackie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:06:06