Press "Enter" to skip to content

Python script for trial randomization

In this post you will find a Python script for randomizing conditions with constraints (i.e., never 2 consecutive trials of X). This script will in a later post be implemented in a cross-modal oddball task created in Psychopy.

I recently started to use Psychopy to build experiments. To build my experiments (mainly oddball tasks) I have, up until I found psychopy, used e-prime. In an oddball task one does not usually present several oddball (typically called “novel” or “deviant”) stimuli in succession. Without getting into detail; I was not able to solve this problem with e-primes scripting language “e-basic” so I made a python script instead. With this script I pre-generated .txt-files that I loaded into e-prime and got my constraints fulfilled.

PsychoPy is based on Python and it felt natural (I know Python better than e-basic), therefore, to program my new experiment in Psychopy and implement my script there instead. That is, the code will randomize trials on the fly for each participant.

The following script is intended to use with TrialHandler in Psychopy (trials = data.TrialHandler (listcreatedbyscript, 1, method = “sequential”). Sequential method will be used because the script already randomizes trials. A further feature of the script is that every oddball is presented an equal number of times for each digit (3 times for each digit in each block). However, it is possible that I will skip using the TrialHandler.

Update: In a new post I have created another randomization script. In the new script I do not use PsychoPy’s  TrialHandler. That is, it is not dependent on using PsychoPy.

Click here for link to the post.

Python script:

The Python script will create a list containing dictionaries. Each dictionary is one Trial and contains the correct button, condition, visual target, block and trial #.
Below are the 10 first dictionaries printed (first block, first 10 trials):

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: