Search code examples
pythonpsychopy

PsychoPy simulate Focus Of Expansion using Random Dot Kinematogram (RDK)


I need to simulate a focus of expansion using PsychoPy's RDK functionality.

I have the following code so far. However this only created a RDK that moves in a certain direction.

from psychopy import visual, event, core

win = visual.Window([1000,1000], rgb=(255,255,255), fullscr=False)
fixSpot = visual.GratingStim(win,tex=None, mask="gauss", size=(0.05,0.05),color='black')


rdk = visual.DotStim(win, units='', nDots=1000, coherence=1.0, 
                    fieldPos=(0,0), 
                    fieldSize=(1,1), 
                    fieldShape='sqr', dotSize=6.0, 
                    dotLife=150, dir=0, speed=0.01, 
                    rgb=None, color=(0,0,0), 
                    colorSpace='rgb255', opacity=1.0, 
                    contrast=1.0, depth=0, element=None, 
                    signalDots='different', 
                    noiseDots='direction', name='', 
                    autoLog=True)

stop = False

while stop == False:    

    fixSpot.draw()
    rdk.draw()

    win.flip()

    if event.getKeys("a"):
        win.close()
        stop = True

I need to create an RDK where the dots move away from a specific position in the window.

i.e. Focus of Expansion using RDK

I tried changing parameters however I cant mimic the desired functionality. I also looked through and searched the psychopy documentation, however there was no mention of 'focus of expansion'.

Is there any way to do this using PsychoPy? If not, what is the best alternative?


Solution

  • Fun question. A way to do it is:

    from psychopy import visual
    win = visual.Window()
    stim = visual.DotStim(win, nDots=50, dotLife=60, speed=0)  # a non-moving DotStim
    
    for frame in range(100):
        stim._dotsXY *= 1.02  # accelerating X-Y expansion
        #stim.dotsXY *= stim.dotsXY * [1.02, 1.05]  # faster acceleration in y-direction
        stim.draw()
        win.flip()
    

    This goes "behind the scenes" and manipulates the internal attribute called visual.DotStim._dotsXY. It is just a 2 x nDots numpy array like this:

    print stim._dotsXY  # look at coordinates
    [[ 0.02306344 -0.33223609]
     [ 0.30596334 -0.0300994 ]
     [-0.10165172 -0.08354835]
     [ 0.21854653 -0.07456332]
     [-0.39262477 -0.21594382]
    ...etc
    

    ... on which you do all sorts of operations. I can't quite figure out how to do constant-speed expansion in a neat way.