【问题标题】:Mouse selection of images with Psychopy使用 Psychopy 鼠标选择图像
【发布时间】:2017-10-02 18:54:17
【问题描述】:

我在 Psychopy 中创建了两个刺激(红色和绿色矩形)作为刺激。另外,我已经启用了四个方向的鼠标移动。使用 Psychopy 中的函数 [mouse.getPressed()] 来选择刺激,我遇到了一些问题。 基本上,我希望鼠标向四个方向移动,当鼠标到达红色/绿色矩形刺激时,我需要选择该刺激并将其颜色更改为蓝色。

任何人都可以调查这个问题并帮助我解决同样的问题吗?

这是我的代码:

from psychopy import visual, core, event 
import numpy as np


# Create a window.
# For configuring and debugging the code turn off full screen.
fullscr = False
win = visual.Window(
    [1200,1000],
    monitor="testMonitor",
    units="deg",
    fullscr=fullscr
    )

#cursor = visual.Circle(win, radius=0.2)

#cursor = visual.CustomMouse(win,
#    leftLimit=-10, topLimit=10, rightLimit=10, bottomLimit=-10,
#    showLimitBox=True, clickOnUp=True)

pos_zero = (0, 0)
cursor = visual.Rect(
        win=win, 
        size=400,
        pos=pos_zero,
        opacity=0
        )

mouse = event.Mouse(visible=True)


# Sinusoidal control version.
freq_one = 0.5
freq_two = 1.5
# Colors of the rectangles.
#color_zero='black'
color_one = 'red'
color_two = 'green'
# Positions of the rectanges.
pos_one = (-10, 0)
pos_two = (10, 0)


start = core.getTime()
cnt = 0
cursor.pos = mouse.getPos()
print cursor.pos
while cnt<600:
    second = core.getTime() - start

    sin_val_one = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_one))
    sin_val_two = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_two))

#while not mouse.getPressed()[0]:
    # Do something if mouse moved

    for key in event.getKeys():
        if key == 'escape': 
           core.quit()
        elif key == "right":
           cursor.pos = cursor.pos + (2,0)
        elif key =="left":
           cursor.pos = cursor.pos - (2,0) 
        elif key =="up":
           cursor.pos = cursor.pos + (0,2) 
        elif key =="down":
           cursor.pos = cursor.pos - (0,2) 


        #if cursor.pos == pos_one:
        #   mouse.getpressed(rect_one)
        #elif cursor.pos == pos_two:
        #   mouse.getpressed(rect_two)

    mouse.setPos(cursor.pos)  
    mouse.lastPos = cursor.pos  

    rect_one = visual.Rect(
        win=win,
        fillColor=color_one,
        lineColor=color_one, 
        size=15,
        pos=pos_one,
        opacity=sin_val_one
        )
    rect_two = visual.Rect(
        win=win,
        fillColor=color_two,
        lineColor=color_two, 
        size=15,
        pos=pos_two,
        opacity=sin_val_two
        )

#images = [rect_one, rect_two]

#for image in images:
#    if mouse.isPressedIn(image):
#        pressed_shape = shape
#            
#    pressed_image.fillColor = 'blue'
#    pressed_image.draw()
#    print pressed_image.name

    rect_one.draw()
    rect_two.draw()
    cursor.draw()
    win.flip()
    cnt += 1

win.close()

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 您想在光标重叠 (cursor_object.overlaps(rect_one)) 或鼠标按下 (mouse.isPressedIn(rect_one)) 时更改颜色吗?还有一个更笼统的问题:为什么要用键盘来控制鼠标?如果受试者永远不会使用鼠标,那么只需使用键盘移动图像左右即可。
  • 我希望鼠标到达图像时颜色会发生变化。它可以是任何一个。我不知道重叠功能,所以我想到了使用mouse.isPressed。我计划将它与大脑信号采集板集成,信号将用于鼠标移动。但就目前而言,我正在制作一个原型来测试键盘功能。我希望在光标到达那里时选择图像。该模块用于与更大的模块集成。谢谢。

标签: python-2.7 mouseevent psychopy


【解决方案1】:

我做了一些更改,并在下面发布了更新的代码。它明显更短更简单。

  • 使用鼠标删除所有内容。由于您只希望在按键上移动视觉效果,因此无需调用鼠标 - 尤其是当您实际上不希望主体使用鼠标时...

  • 创建一次彩色形状,然后在运行时更新颜色。这明显更快,即避免丢帧。

  • 更改了光标大小。那是 400 度!

  • 删除了矩形周围的线,以便在重叠时只需要更改 fillColor

  • 其他一些简化。

代码如下:

from psychopy import visual, core, event 
import numpy as np

# Settings
fullscr = False
directions = {'left': (-2,0), 'right': (2,0), 'up': (0, 2), 'down': (0, -2)}
freq_one = 0.5  # # Sinusoidal control
freq_two = 1.5

# Create a window.
win = visual.Window([1200,1000], monitor="testMonitor", units="deg", fullscr=fullscr)

# The two rectangles
rect_one = visual.Rect(win=win, fillColor='red', lineColor=None, size=15, pos=(-10, 0))
rect_two = visual.Rect(win=win, fillColor='green', lineColor=None, size=15, pos=(10, 0))

# Cursor
cursor = visual.Circle(win, fillColor='white', radius=0.2)

# Run five trials
for trial in range(5):
    # Set to initial condition
    rect_one.fillColor = 'red'
    rect_two.fillColor = 'green'
    cursor.pos = (0, 0)

    # Start task
    start = core.getTime()
    for frame in range(600):
        # Set rectangle opacities
        second = core.getTime() - start
        rect_one.opacity = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_one))
        rect_two.opacity = 0.5+0.5*np.sin(2 * np.pi * second * float(freq_two))

        # Show it
        rect_one.draw()
        rect_two.draw()
        cursor.draw()
        win.flip()

        # Get keyboard responses and change color if there is overlap
        for key in event.getKeys():
            if key == 'escape': 
               core.quit()
            elif key in directions.keys():  # if this is a key corresponding to a direction
               cursor.pos += directions[key]   # Lookup the pos change in "directions" and add it to current position
               if cursor.overlaps(rect_one):
                   rect_one.fillColor = 'blue'
               if cursor.overlaps(rect_two):
                   rect_two.fillColor = 'blue'

【讨论】:

  • 非常感谢。代码现在完美运行。 :) 我也可以问你一个小疑问。假设如果我想继续实验,(即在选择图像并将其蓝色变为初始状态之后)我应该删除 core.getTime() 函数并替换为 while 循环吗?
  • 我更新了代码,对所有内容进行了循环,以便运行多次试验。一开始,它会将内容设置为初始设置。
  • 如果它解决了您的问题,请将此回复标记为已接受的答案(也许还可以投票)。
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
相关资源
最近更新 更多