How to move BufferImageStim object in PsychoPy

If you have to write a lot of stimuli, it will take time to draw. Since GL works behind the scenes of Psycho Pai, you can save a lot of drawing time by using display list. BufferImageStim is a wrapper for the display list, so you can use this. Even if you change pos before drawing, it will not be reflected, so you can not move it.

It can be used by calling the GL function directly in the following workaround.

BufferImageStim is useful for fast rendering, but at the moment pos has no effect, so you cannot move the stimulus.

Here is workaround.

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

# need these lines to use GL functions
import pyglet
pyglet.options['debug_gl'] = False
GL = pyglet.gl

#create a window
mywin = visual.Window([800,600],monitor="testMonitor", units="pix")

N = 800
xs = np.random.rand(N)*800-400
ys = np.random.rand(N)*600-300
rs = np.random.rand(N)*10

slist = [visual.Circle(win=mywin, radius=r, pos=(x,y)) for x,y,r in zip(xs,ys,rs)]
stim = visual.BufferImageStim(mywin, stim=slist)

t0 = core.getTime()
while True: #this creates a never-ending loop
    nw = core.getTime()
    
    cx = np.sin(-nw*np.pi)

    # stim.draw() # position doesn't change
    
    stim._selectWindow(stim.win)

    GL.glPushMatrix()
    GL.glTranslated(cx, 0, 0) # NOTE: gl coordinate. by default, [-1 to 1, -1 to -1](probably)

    GL.glScalef(stim.thisScale[0] * (1,-1)[stim.flipHoriz],
                stim.thisScale[1] * (1,-1)[stim.flipVert], 1.0)
    GL.glColor4f(stim.desiredRGB[0], stim.desiredRGB[1], stim.desiredRGB[2], stim.opacity)
    GL.glCallList(stim._listID)
    GL.glPopMatrix()

    mywin.flip()

    if len(event.getKeys())>0: break
    event.clearEvents()

#cleanup
mywin.close()
core.quit()

Let's compare the version without BufferImageStim

Here is non-BufferImageStim version. Pretty slow.

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

#create a window
mywin = visual.Window([800,600],monitor="testMonitor", units="pix")

N = 800
xs = np.random.rand(N)*800-400
ys = np.random.rand(N)*600-300
rs = np.random.rand(N)*10

slist = [visual.Circle(win=mywin, radius=r) for x,y,r in zip(xs,ys,rs)]

t0 = core.getTime()
while True: #this creates a never-ending loop
    nw = core.getTime()
    
    cx = np.sin(-nw*np.pi)*400

    for i in range(0, N):
        slist[i].pos = (xs[i]+cx, ys[i])
        slist[i].draw()
        
    mywin.flip()

    if len(event.getKeys())>0: break
    event.clearEvents()

#cleanup
mywin.close()
core.quit()

Enjoy!!

Recommended Posts

How to move BufferImageStim object in PsychoPy
How to develop in Python
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to write soberly in pandas
How to use SQLite in Python
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to log in to Docker + NGINX
How to know the internal structure of an object in Python
How to use calculated columns in CASTable
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
How to convert csv to tsv in CLI
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
How to work with BigQuery in Python
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe
How to check opencv version in python
How to enable SSL (TLS) in Apache
How to use Anaconda interpreter in PyCharm
How to specify non-check target in Flake8
How to handle consecutive values in MySQL
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to do Server-Sent Events in Django
How to implement Scroll View in pythonista 1
How to convert DateTimeField format in Django
How to use Map in Android ViewPager
How to display Hello world in python
How to read CSV files in Pandas
How to change editor color in PyCharm
How to write this process in Perl?
How to use is and == in Python
How to write Ruby to_s in Python
How to view images in Django's Admin
How to draw OpenCV images in Pygame
How to revive cells in iPython notebook
How to make a model for object detection using YOLO in 3 hours
How to deal with memory leaks in matplotlib.pyplot
How to use the C library in Python
How to receive command line arguments in Python
How to clear tuples in a list (Python)
How to keep track of work in Powershell
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
How to implement Discord Slash Command in Python