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 1:27AM

    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

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • edited 1:27AM

    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 1:27AM

    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

    Did you like my answer? Feel free to Buy Me A Coffee :)

  • edited 1:27AM

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

  • edited 1:27AM

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

    Did you like my answer? Feel free to Buy Me A Coffee :)

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games