Howdy, Stranger!

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

Supported by

[solved] Reading in stimuli list

edited July 2013 in OpenSesame

Hello - I have a simple .txt file that contains a list of words that are also the names of associated .wav files and .bmp files. For example, the list contains "cat", "dog", etc., and in the same directory I have cat.wav, cat.bmp, dog.wav, dog.bmp, etc.

At different parts in my experiment I want to read in the list, shuffle it, and then choose one item to either (1) display the word in a sketchpad, (2) play the associated wav file in sampler, or (3) display the associated bmp in sketchpad. I've been trying to use this discussion as a model: Right now, before my sequence I have an inline script like the following:

path_list = exp.get_file("list1.txt")

import numpy as np
stimlist = np.loadtxt(path_list, dtype = str)

import random

global stimlist

word = stimlist.pop()

exp.set("word", word)

And then in the following step I have a sketchpad ("my_sketchpad") in which I'm trying to display [word]. But every time I run it, I get the error message: "Error: Runtime error Description: Variable 'word' is not set in item 'my_sketchpad'."

All the relevant files, including the experiment file itself, are in the same directory, and so I'm really banging my ahead against the wall here. Can anyone please tell me why it says the variable is not set? I tried putting in "global word" at the end and that didn't help either. Thank you in advance!


  • edited 12:57PM


    A first thing that comes to mind is that you put the above Python code in the Run phase tab of your inline_script item, whereas you should put it in the Prepare phase tab. Could that be it?

    Due to OpenSesame's prepare-run strategy, in-an-inline_script-defined variables are only available in the prepare phase of other items (such as your sketchpad) when they are delared in the Prepare phase tab.

    For more information, please see:

    If putting the code in the Prepare phase tab of your inline_script item doesn't solve the problem, please let us know!



  • edited 12:57PM

    Yes that was it! I knew it would be something simple like that... Thank you so much. But now I'm getting a new error, this time related to pop(): AttributeError: 'numpy.ndarray' object has no attribute 'pop'

    I suspect this has something to do with the fact that my item 'stimlist' is not being treated as a list? From my understanding, word = stimilist.pop() should work as long as stimlist is actually a list, right? The file that I'm reading in, list1.txt is just a simple text file with no header and is just a list of words with each word on a separate line.

    After seeing this discussion ( I tried both .txt and .csv versions of the file, but it still doesn't work. My code is here:

    (I think this is the full code, anyway. I got it from General properties > Script editor.)

    Thank you in advance!

  • edited 12:57PM


    Glad to hear that putting the inline code to the Prepare phase tab helped! :)

    About popping items from your stimlist:

    I suspect this has something to do with the fact that my item 'stimlist' is not being treated as a list?

    That's right! The np.loadtxt() function reads in your text file as a numpy array data structure, and not as a list. And indeed, the pop() function only works for list data types. To be able to use the pop() after all, you can simply convert your numpy array to a list like so:

    stimlist = np.loadtxt(path_list, dtype = str)
    stimlist = list(stimlist)

    By the way, by default pop() returns the last item from a list. If you want an item from a different position to be returned, you have to give the desired position as a parameter when you call the function. As a consequence, this

    word = stimlist.pop(1)

    will pop an item from position 1 in the list, and since Python starts counting at 0, this equates to the second item in the list. Therefore, you'll run in to trouble if only one item is left (because position 1 will be empty then).

    So, I'd advise you to simply use

    word = stimlist.pop()


    Does that make sense?



  • edited 12:57PM

    Yes, that fixed it! Everything works perfectly now, thank you~

  • edited 12:57PM

    Great! :)
    Good luck with your experiment and let us know if you have any further questions!

Sign In or Register to comment.