Thursday, 18 September 2014

Psychopy First lesson


I'm currently working on a way to present Python to the NeuralCode group. Offering a means of creating a stimulus presentation script through Psychopy might get people involved in programming. It follows the principle of offering something that saves time or solves a problem immediately.


Psychopy can be downloaded as a self contained install. Meaning that Psychopy
and the version of Python it needs, along with the additional libraries, are all packaged together. This makes it easy to install and get going.


The teaching materials for Psychopy created by Prof. Gary Lupya are excellent and
I don't think I can do better. So here they are.

Exercise 1 on http://sapir.psych.wisc.edu

import time
import sys
from psychopy import visual, event, core

win = visual.Window([400,400], color="black", units='pix')
square = visual.PatchStim(win, tex="none", mask="none", color="blue", size=[100,100])
square.draw()
win.flip()
core.wait(.5)
sys.exit()

One of the first things you notice about Python code is that it is very 'readable'. I have left a line between the lines for loading libraries and the code for the actual workings of the program.


Starting at the line after the gap, 'win' is defined. It is a window with relevant details. (note the image to the left isn't created at this point, it is only defined - this image  and the one below are just to show you what is being defined)










The next line defines 'square' with details about where it will be displayed and other properties.










The next two lines need some explanation as they deal with properties of video card display. The square is drawn to a buffer and is then displayed on the screen with win.flip( ). This is coherent with how the video card creates the screen in memory and then displays it usually in two halves (top and bottom).

It 'flips' like one of those old mechanical clocks with the numbers in two halves.
It's important to realize that this is the timing bottleneck in any stimulus presentation program. Stimuli can only be presented to the screen in increments of the screen refresh rate. Any mention of timing accuracy greater than the screen refresh rate is not very useful. Response collection is another story.

 The last line waits for half a second or we wouldn't see anything before the program exits.

The next few exercise are about manipulating various properties of the square.

 Useful Psychopy code snippets

The first couple of challenges are pretty simple.
a) How would you increase the display time to 30 seconds?
b) How would you colour the square red?