I create the stimuli using psychopy

Repetitive stimulation

To generate repetitive stimulation, which is sometimes used in psychophysics, I created the function \(repite\).

The demo bellow displays a stimulus that alternates between three colors. Every color is displayed for 10 frames. In the argument ‘start’, I indicate where I want the sequence to start. In this case, I want the onset of the red color to be displayed in the frame 13.

from psychopy import *

def repite(frame,duration,numberStates=2,start=1):
    startP=numberStates*duration-start%(numberStates*duration)
    counter=(frame+startP)%(numberStates*duration)
    for i in range(numberStates):
        if counter>=i*duration and counter<(i+1)*duration:
            flag=i+1
    return flag


myWin = visual.Window(monitor='testMonitor')
stimulus = visual.PatchStim(myWin,tex='None')

frame=1
while frame<60:
    flag=repite(frame,duration=10,numberStates=3,start=13)
    if flag==1:
        color='red'
    if flag==2:
        color='green'
    if flag==3:
        color='blue'
    stimulus.setColor(color)
    stimulus.draw()
    print frame,color
    myWin.flip()
    frame+=1
core.quit()

Point-light walker

In the demo bellow the coordinates of each point are in the file walker.txt that should be included in the same folder that the psychopy file.

from psychopy import *
from numpy import *
from random import *

### function to escape the program
def esc():
    for keys in event.getKeys():
        if keys in ['escape','q']:
            core.quit()

framesFile = open('walker.txt','r') #open the file that contains the coordinates of each point

### reading the first line of the file; contains the general information of the animation
firstLine=framesFile.readline()
firstLineSplit=firstLine.split()
numberFrames=int(firstLineSplit[0])
numberMarkers=int(firstLineSplit[1])
numberHz=int(firstLineSplit[2])

### defining the dimensions of the main structure data
m=zeros((numberFrames,numberMarkers,3))

### creating the matrix that contain the coordinates of each point
for frame in range(numberFrames):
    for marker in range(numberMarkers):
        line=framesFile.readline()
        lineSplit=line.split()
        for i in range(3):
            m[frame][marker][i]=float(lineSplit[i])
            
myWin = visual.Window(monitor='testMonitor',units='deg')
sti = visual.PatchStim(myWin,tex='None',mask='circle',size=.2)

size=0.02
frame=1
while True:
    for marker in range(numberMarkers):
        x=size*m[frame%numberFrames][marker][0]
        y=size*m[frame%numberFrames][marker][2]
        sti.setPos([x,y])
        sti.draw()
    sti.draw()
    myWin.flip()
    frame+=1
    esc()
core.quit()

Template to create experiment

Below a template that I used to run experiments using the method of constant stimuli. The relevant information needs to be included only once in the list \(vars\). It uses some helper functions that I created grouped in the module psychopyHelp.py that it could be placed in the same folder as the template.

from psychopyHelp import *

myDlg = gui.Dlg(title="Setting experiment")
myDlg.addField('Observer:','dl')
myDlg.show()
expInfo = myDlg.data
dataFile=openDataFile(expInfo[0])

win = visual.Window(monitor='testMonitor',allowGUI=False,units='deg')
fixation=visual.GratingStim(win, tex=None, mask='circle', color=-1,size=.5)
sti=visual.GratingStim(win, tex='sin', mask='circle', color=1,size=4)
fixation.setAutoDraw(True)

vars={'orientation': [-45,0,45], 'location' :[-1,1]}
stimList = createList(vars)
trials = data.TrialHandler(stimList,1)

nDone=0
for thisTrial in trials:
    win.setRecordFrameIntervals(True)
    for frame in range(60):
        sti.setPos([thisTrial['location']*4,0])
        sti.setOri(thisTrial['orientation'])
        sti.draw()
        win.flip()
    
    win.flip()
    win.setRecordFrameIntervals(False)
    response=getResponse(['left','right'])
    
    nDone+=1
    if nDone==1:
        print >>dataFile, 'trial', 
        for name in thisTrial.keys():
            print>>dataFile,name,
        print >>dataFile, 'response'

    print >>dataFile, nDone,
    for value in thisTrial.values():
        print>>dataFile, value,
    print>>dataFile, response
    
dataFile.close()
win.close()
core.quit()

Create simple animations

With the code below I create a sequence of png’s that I attach using the free software TSBSimpleAnimator (direct download).

from psychopy import visual

myWin = visual.Window([200,200])
grating=visual.GratingStim(myWin, mask='gauss', color=1,size=1,sf=3,texRes=512)
for i in range(60):
    grating.draw()
    grating.setPhase(.1,'+')
    myWin.getMovieFrame(buffer='back')
    myWin.flip()

#save the movie in the format of your choice
myWin.saveMovieFrames('frame.png')
#myWin.saveMovieFrames('frame.tif') #much like png files (but used more on win32)
#myWin.saveMovieFrames('frame.jpg') #lossy, but highly compressed images
#myWin.saveMovieFrames('myMovie.gif')# for better results, make your gif in gimp
#myWin.saveMovieFrames('myMovie.mpg')#only on win32 so far (requires pymedia)
myWin.close()