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: http://forum.cogsci.nl/index.php?p=/discussion/469/solved-selective-sampling-with-a-ratio-help-please. 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
random.shuffle(stimlist)

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!

Comments

  • edited 8:15AM

    Hi,

    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!

    Best,

    Lotje

    Lotje van der Linden - http://www.cogsci.nl
    FACEBOOK

  • edited 8:15AM

    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 (http://forum.cogsci.nl/index.php?p=/discussion/469/solved-selective-sampling-with-a-ratio-help-please#Item_8) I tried both .txt and .csv versions of the file, but it still doesn't work. My code is here:

    http://pastebin.com/qANXqzdJ

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

    Thank you in advance!

  • edited 8:15AM

    Hi,

    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()
    

    instead.

    Does that make sense?

    Best,

    Lotje

    Lotje van der Linden - http://www.cogsci.nl
    FACEBOOK

  • edited 8:15AM

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

  • edited 8:15AM

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

    Lotje van der Linden - http://www.cogsci.nl
    FACEBOOK

Sign In or Register to comment.