Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

How to use .txt file to assign order to stimuli presentation?


I am a beginner at Python. I am trying to create an experiment with a training/priming phase, and then a forced choice testing phase.

For the training phase, I have a .txt file that contains a "template" (a list of 700+ items) for the ordering of my stimuli. During the training phase, the participants will see a sequence of eight (8) randomised words individually, but over 700 times within a fixed order as laid out by the template.

The template is kind of like so:

What I need to do is randomise the word list and reassign the corresponding template items for each participant. For example, with the words orange, green, and blue; participant 1 one would have [a0] = orange, [a1] = green, [a2] = blue, participant 2 would have [a0] = blue, [a1] = orange, and [a2] = green.

So far in the prepare phase of an inline_script I have to import my .txt file:
import numpy as np
nonword_train = np.loadtxt(exp.get_file("training_template.txt"), dtype=str)

I apologise if this is not very clear. I would appreciate any help as I am quite lost.


  • Hi,

    I am not sure whether I entirely understand what you want to accomplish. So you have eight different words in total, and present them in a random order for 700 times in total. Is this correct? Or what information do you get from the template?

    Generally, if you want to control stuff happening across participants, you can use the variable var.subject_nr. So here a little example how to go about that:

    # this code assigns three different orders of stimuli over all subjects
    # the '%' is the modulo operator and returns the rest of a division. e.g. 1%3 returns 1, 2%3 returns 2, and 3%3 returns 0
    if var.subject_nr%3 == 0:
        stim_order == ['red','blue','green']
    elif var.subject_nr%3 == 1: 
        stim_order == ['red','green','blue']
    elif var.subject_nr%3 == 2: 
        stim_order == ['blue','green','red']
    # depending on the participant number another order of items is being used. 

    Btw. If you are new to Python, I can only recommend that you spend some extra time now to learn Python before you proceed programming experiments. Even if it slows you down now a little, you will see that you'll be much quicker and better solving problems in the future. Here a link to some resources.


  • edited January 24

    Hi Eduard,

    Thank you so much for taking the time to respond. I will definitely have a look at the Python resources.

    The .txt template is the order in which the stimuli should be presented in the training phase. It provides a fixed order for the presentation of 700 trials. So I have eight different words, that should be presented in the fixed order as defined by the .txt file. Here is a segment of the .txt file: So e.g. [a0] will always = apple for participant 1, but banana for participant 2. [b1] = watermelon for participant 1, but kumquat for participant 2. I hope this clarifies things a little.

    Is there a way for me to import/load the .txtfile to fix the order of presentation, but randomise the stimuli for each participant? Additionally, would I be able to carry over the values of [a0], [a1], [b0] etc, for each participant into the testing phase (e.g. have [a0] be banana for participant 1 in both the training phase and the testing phase) ?

  • I think probably the easiest would be to define the mapping in the beginning of the experiment, and then replace the items. For example:

    import random
    # load the text file
    # as I dont have the text file I just create an example list
    example_txt = ["[a0]","[a1]","[b0]"]*20
    # define how the txt content maps on words, depending on participant number
    if var.subject_nr%3 == 0:
        mapping = {'[a0]':'apple','[a1]':'banana', '[b0]':'peach'}
    elif var.subject_nr%3 == 1:
        mapping = {'[a1]':'apple','[a0]':'banana', '[b0]':'peach'}
    # make a new list and add one by one the mapped labels to it
    replaced_list = []
    for item in example_txt:

    Hope this helps.

Sign In or Register to comment.