Howdy, Stranger!

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

Supported by

[open] Use .csv in lieu of loop "spreadsheet"

edited April 2014 in OpenSesame

Hi,

I've seen several responses which do "nearly" what I want so please excuse me for not being able to work it out for myself:

I'd like to use a .csv (or other text file) as a "drop-in replacement" for the variable data in a "loop" (so that I can edit some complex materials externally). Following this discussion it seems easy to read stuff in and transfer it to experimental variables, but if I understand the example correctly it had six items hard-coded in?

What I'd like to do is generalise the solution into a loop, so that I can read in an arbitrary number of items into "setcycle" variables without having to write repetitive and error-prone code. And for some reason, a sensible solution is eluding me... Any pointers hugely appreciated!

--MC

Comments

  • edited May 2014

    I had to do something not too dissimilar recently, which I've detailed here.

    For your case, I would try adjusting the code at the bottom of the page to something along the lines of this (although I haven't tested this in OpenSesame yet):

    import csv
    # Read the csv file into a list, with one entry per row
    with open('stimuli_file.csv', 'rb') as stimfile:
        reader = csv.reader(stimfile, delimiter=',')
        contents = [row for row in reader]
    
    # Make a list of dict items, with the column names from the 
    # top of the csv file as keys, and the subsequent columns
    # as values.
    col_names = contents[0]
    stimuli = [dict(zip(col_names, row)) for row in contents[1:]]
    
    # Transform these stimuli to OpenSesame variables.
    # To illustrate, imagine the first two variables are "probe" and "condition"
    for i in range(len(stimuli)):
        trial = stimuli[i]
        exp.set("probe_%i" % i, trial["probe"])
        exp.set("condition_%i" % i, trial["condition"])
        exp.set("other_variable_%i" % i, trial["other_variable"])
    # ...sets [probe_0] to the first row in the "probe" column of your csv, etc, up
    # to [probe_N], where N is the number of trials minus 1.
    

    You can then use the example loop object in the link to use these loaded variables.

    Edit: Be aware that any numerical variables will be saved as strings using this method. OpenSesame might deal with this gracefully and you won't need to worry, but otherwise you may need to use the python int() function:


    for i in range(len(stimuli)): trial = stimuli[i] exp.set("numeric_variable_%" % i, int(trial["numeric_variable"]))

    Again, without testing this, I just don't know.

  • edited 5:30PM

    Hi, this looks great, thanks Eoin! I'll have a play and post here when done.

    --MC

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