Wilbert's website at SocSci

> Software> PsychoPy Course> 5 Functions

psychopy/05functions.html 2018-04-26

Programming with PsychoPy, 5 Structuring: Writing your own functions

With all knowledge from previous chapters, we can write an experiment, but it as experiments get larger, the lack of structure may become problematic. When programming PsychoPy, it is often the case that a certain sequence of operations, is put together, forming a little coherent part. Such a sequence can have a form like

  • change the text of a stimulus;
  • turn the text into a text stimulus;
  • put the new stimulus on the screen.

When put together, such a part is called a function and it forms the core of Python programming. You can build your whole experiment from these little functions, because they can be accessed anywhere in your Python code, or within another function, via a function call, using the name of the function. This makes your code easily readable and very flexible. A function consists of:

  • the name of the function;
  • optionally: the variables that are sent to the function;
  • the body of the function which contains the statements;
  • optionally: the return statement of the function which send a variable back from the function.

Here is an example of a function:

def showText(window, myText): 
	message = visual.TextStim(window, text=myText)
	message.draw()
	window.flip()

Let us examine the details of this function. The function header starts with the reserved word def (for define) and followed by the function name. After that you can see parentheses containing variables you want to pass to the function, the so-called arguments. Each argument is separated from the next by a comma. A function can also be defined without argument, then the parentheses contain nothing. The example functon above does not contain a return statement.

The body of the function is the indented part and end when the indent stops. All indented statements will be executed when the function is called. Any variable declared here will be treated as local to the function. That means that the variable does not exist outside the function body (the indented text). The function described above can be called in PsychoPy by the following code.

win = visual.Window([400,300], monitor="testMonitor")
showText (win, "Hello World")
It seems like a useless function, but when something has to be printed several times, for instance, when computing a list, it can become very useful. Let's take another function, which can calculate square values of integers (math) returning this value. It is important to ensure that your arguments have the expected type (number, string, list). Otherwise the function will produce strange results or errors. The return statements causes the function to return the desired result.
def calculateSquare(number):
	return number * number

Assignment 5: getInput

  1. Run the following program. What does it do?
  2. #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # There is little experiment if the participant cannot give any input.
    # Here we changed assignment 2 a bit so that it waits for a keys, rather
    # than waiting 5 s. Note that we need to import the event module from
    # PsychoPy to make this work.
    from psychopy import core, visual, event
     
    ## Setup Section
    win = visual.Window([400,300], monitor="testMonitor")
    textString = "Press any key to continue\n"
    message = visual.TextStim(win, text=textString)
    
    ## Experiment Section
    message.draw()
    win.flip()
    c = event.waitKeys() # read a character
    message = visual.TextStim(win, text=textString + c[0])
    message.draw()
    win.flip()
    event.waitKeys()
    
    ## Closing Section
    win.close()
    core.quit()
    
  3. Make a function that prints some text on the screen (for instance a question), records the one character result, and returns it. Make a small program that runs the function.

Continue with the next lesson