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
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):
lineSplit=line.split()
for i in range(3):
m[frame][marker][i]=float(lineSplit[i])

myWin = visual.Window(monitor='testMonitor',units='deg')

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.show()
#expInfo = myDlg.data
#dataFile=openDataFile(expInfo[0])
dataFile=openDataFile("xxxx")

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, mask = "circle", size = 4)
fixation.setAutoDraw(True)

vars = {"initialtime": [30],
"stimulustime": [30],
"orientation": [-45,0,45],
"location": [-1,1]}
stimList = createList(vars)
trials = data.TrialHandler(stimList, 10)

nDone = 0
for thisTrial in trials:
timeresponse = None
frame = 1
win.setRecordFrameIntervals(True)

while timeresponse == None:
if frame > thisTrial["initialtime"] and frame <= (thisTrial["initialtime"] + thisTrial["stimulustime"]):
sti.setPos([thisTrial["location"] * 4, 0])
sti.setOri(thisTrial["orientation"])
sti.draw()

flip_time = win.flip()
if frame == 1:
time_start = flip_time
frame +=1
timeresponse = getResponse(["left", "right"])

win.flip()
win.setRecordFrameIntervals(False)
nDone+=1

if nDone==1:
print >>dataFile, "trial",
for name in thisTrial.keys():
print>>dataFile, name,
print >>dataFile, "rt", "response"

print >>dataFile, nDone,
for value in thisTrial.values():
print>>dataFile, value,
print>>dataFile, timeresponse[0] - time_start, timeresponse[1]

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])
myWin.close()