Tutorial 1: Generating your first stimulus

A tutorial to get you going with your first stimulus display.

Know your monitor

PsychoPy® has been designed to handle your screen calibrations for you. It is also designed to operate (if possible) in the final experimental units that you like to use e.g. degrees of visual angle.

In order to do this PsychoPy® needs to know a little about your monitor. There is a GUI to help with this (select MonitorCenter from the tools menu of |PsychoPy|IDE or run …site-packages/monitors/MonitorCenter.py).

In the MonitorCenter window you can create a new monitor name, insert values that describe your monitor and run calibrations like gamma corrections. For now you can just stick to the [testMonitor] but give it correct values for your screen size in number of pixels and width in cm.

Now, when you create a window on your monitor you can give it the name ‘testMonitor’ and stimuli will know how they should be scaled appropriately.

Your first stimulus

Building stimuli is extremely easy. All you need to do is create a Window, then some stimuli. Draw those stimuli, then update the window. PsychoPy® has various other useful commands to help with timing too. Here’s an example. Type it into a coder window, save it somewhere and press run.

 1from psychopy import visual, core  # import some libraries from PsychoPy
 2from psychopy.hardware import keyboard
 3
 4#create a window
 5mywin = visual.Window([800,600], monitor="testMonitor", units="deg")
 6
 7#create some stimuli
 8grating = visual.GratingStim(win=mywin, mask="circle", size=3, pos=[-4,0], sf=3)
 9fixation = visual.GratingStim(win=mywin, size=0.5, pos=[0,0], sf=0, rgb=-1)
10
11#create a keyboard component
12kb = keyboard.Keyboard()
13
14#draw the stimuli and update the window
15grating.draw()
16fixation.draw()
17mywin.update()
18
19#pause, so you get a chance to see it!
20core.wait(5.0)

Note

For those new to Python. Did you notice that the grating and the fixation stimuli both call GratingStim but have different arguments? One of the nice features about python is that you can select which arguments to set. GratingStim has over 15 arguments that can be set, but the others just take on default values if they aren’t needed.

That’s a bit easy though. Let’s make the stimulus move, at least! To do that we need to create a loop where we change the phase (or orientation, or position…) of the stimulus and then redraw. Add this code in place of the drawing code above:

for frameN in range(200):
    grating.setPhase(0.05, '+')  # advance phase by 0.05 of a cycle
    grating.draw()
    fixation.draw()
    mywin.update()

That ran for 200 frames (and then waited 5 seconds as well). Maybe it would be nicer to keep updating until the user hits a key instead. That’s easy to add too. In the first line add event to the list of modules you’ll import. Then replace the line:

for frameN in range(200):

with the line:

while True: #this creates a never-ending loop

Then, within the loop (make sure it has the same indentation as the other lines) add the lines:

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

the first line counts how many keys have been pressed since the last frame. If more than zero are found then we break out of the never-ending loop. The second line clears the event buffer and should always be called after you’ve collected the events you want (otherwise it gets full of events that we don’t care about like the mouse moving around etc…).

Your finished script should look something like this:

 1from psychopy import visual, core, event #import some libraries from PsychoPy
 2from psychopy.hardware import keyboard
 3
 4#create a window
 5mywin = visual.Window([800,600],monitor="testMonitor", units="deg")
 6
 7#create some stimuli
 8grating = visual.GratingStim(win=mywin, mask='circle', size=3, pos=[-4,0], sf=3)
 9fixation = visual.GratingStim(win=mywin, size=0.2, pos=[0,0], sf=0, rgb=-1)
10
11#create a keyboard component
12kb = keyboard.Keyboard()
13
14#draw the stimuli and update the window
15while True: #this creates a never-ending loop
16    grating.setPhase(0.05, '+')#advance phase by 0.05 of a cycle
17    grating.draw()
18    fixation.draw()
19    mywin.flip()
20
21    if len(kb.getKeys()) > 0:
22        break
23    event.clearEvents()
24
25#cleanup
26mywin.close()
27core.quit()

There are several more simple scripts like this in the demos menu of the Coder and Builder views and many more to download. If you’re feeling like something bigger then go to Tutorial 2: Measuring a JND using a staircase procedure which will show you how to build an actual experiment.


Back to top