3

Many behavioural experimental designs in psychology/neuroscience require conditional branching (e.g. only proceed to the test phase if a requisite performance level has been reached in an initial practice phase). PsychoPy’s Builder view allows one to generate a Python script to run an experiment using largely graphical controls. But it doesn't seem to have built-in support for conditional branching.

Can skipping a particular routine on a given run be implemented in Builder by using Python snippets in a Code component? Or does it require moving to the full Python Coder environment?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
  • Stack Overflow is a great place to ask questions about specific source code that isn't working, where the code is shared so that everyone can look at it. It is not so good for questions about libraries that are only used by a few people, especially if the developers do not hang out here. Perhaps try PsychoPy's mailing list: https://groups.google.com/forum/#!forum/psychopy-users – Paul May 01 '14 at 23:52
  • I'd also note that Python has 'if' statements, and Python has a datatype called 'dictionary' that could be used to link functions to apply depending on a key variable. I have no idea whether PsychoPy takes advantage of any of this, or expects the end user to be able to program it in. – Paul May 01 '14 at 23:56
  • 1
    @Paul PsychoPy has 7000 registered users: http://www.psychopy.org/usage.php. StackOverflow is a much better system for Q/A and developers will hang out here from now on. Let us know if you have any ideas/comments whether this should be done differently. – Jonas Lindeløv May 02 '14 at 09:24
  • 1
    @Jonas I created a pyschopy tag for you here on SO. You, or others who are interested, should [edit the psychopy tag's wiki](http://stackoverflow.com/edit-tag-wiki/103787) and add some basic info and links to the documentation. This way your group will be able to monitor questions with this tag. – Paul May 05 '14 at 01:27
  • Thanks a lot, Paul! This will make it a lot easier to organize our help on SO. – Jonas Lindeløv May 05 '14 at 09:43

1 Answers1

3

The Coder view in PsychoPy gives you full access to the Python programming language and hence you can implement arbitrarily complex experimental designs.

PsychoPy’s graphical Builder view, meanwhile, emphasises ease of use and simplicity over flexibility. One thing it does not cater for directly is conditional branching. It can, however, be hacked to achieve it indirectly.

Let’s say you have a three-phase experiment: a practice block, followed by two possible experimental blocks, ConditionA or ConditionB. After completing the practice block, high-performing subjects are assigned to conditionA, while low-performing subjects are assigned to conditionB.

To implement this in Builder, create three routines to represent each of the task blocks (Practice, conditionA, and conditionB). Each will also be surrounded by a loop (practice_loop, A_loop, and B_loop, respectively.) Also insert a routine between Practice and conditionA (called, say, assignCondition).

In the assignCondition routine, place a Code component. Assume in this case that a performance score counter was maintained in the Practice routine. We can use this to change the number of repetitions of subsequent routines. That is, by setting the repetition number of a loop to zero, we ensure that the routines inside that loop will not be executed. Hence the number of repetitions of these loops will not be a fixed value, but instead a variable (say, repetitionsA and repetitionsB).

In the "Begin Routine" tab of the assignCondition routine's Code component, put a Python snippet like this:

if performanceScore > 25:
    repetitionsA = 50 # run this routine 50 times
    repetitionsB = 0 # don't run this condition at all
else:
    repetitionsA = 0 # vice versa: don't run this
    repetitionsB = 50 # do run this

A fuller description of this technique is given by Matt Wall in a blog post here (with an fMRI block design as example, in which the order of blocks needs to be variable):

http://computingforpsychologists.wordpress.com/2013/11/12/how-to-hack-conditional-branching-in-the-psychopy-builder/

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28