Howdy, Stranger!

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

Supported by

[solved] Setting cycle variables from inline Python Script

edited August 2012 in OpenSesame

Hello,

how can achieve from within a python inline script the same thing as "setcycle 0 var1 val1" ?
( I have all my cases in a .csv file that I would like to load. So, I need to run the command above from within an inline script)

thanks
guido

Comments

  • edited 7:41PM

    Dear Guido,

    I think the simplest way to achieve your goal is, in fact, via the interface rather than an inline script. This is because you can easily paste the content of your .csv file into a loop item in the following way:

    • Create a loop item and add the names of your independent variables (e.g. 'var1') by clicking on the 'Add variable' button.
    • Set the number of cycles to the same number of cases in your .csv file. Now, the number of cells in the loop item will be equal to the number of cells in your .csv file.
    • Open your .csv file in a spreadsheet reader and copy all cases (but not the column headers, if you have them).
    • And then simply paste everything in the loop item.

    If you prefer to set your values via an inline script after all, please let me know and I will give you an example of this.

    Best wishes,

    Lotje

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

  • edited 7:41PM

    Update:

    On a general note I would like to add that communicating about variables between the GUI and Python inline code works very easily and as follows:

    • To get an in a loop defined variable in an inline_script, use:
        self.get("var")
        # Note that the to-be-retrieved variable name is a string.
        # The returned value can be a string, float, or int 
        # (the most appropriate is selected). 
    • To set an in an inline_script defined variable for future use in other items, such as a sketchpad or, importantly, the logger item, use:
        self.experiment.set("var", value)
        # Note that the to-be-set variable name is a string, and the value can be a 
        # string or numeric.

    See also the documentation on experiment functions.

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

  • edited 7:41PM

    Thanks for the replies.
    I followed advice 1: prepared the experimental block in excell, built a column of setcycle num var1 val1 (using INDEX()), copied it into a .txt document, then copied into the script for the block, and adjusted the number of cycles. Once used to it, its is quite fast and flexible.

  • edited 7:41PM

    I would be grateful for a code example of how to do this inline.

  • edited 7:41PM

    I think I've found a way. Proof of concept code (inline script just before block_loop):

    top    = 192.5
    bottom = -192.5
    left   = 'left_button'
    right  = 'right_button'
    
    word_pos  = (bottom, bottom, bottom, bottom, top, top, top, top)
    pair_pos  = (top, top, top, top, bottom, bottom, bottom, bottom)
    probe     = (1,1,2,2,1,1,2,2)
    probe_pos = (bottom, top, bottom, top, bottom, top, bottom, top)
    correct   = (left, left, right, right, left, left, right, right)
    
    nwords = {
        0: (['Despair', 'Parting'],['Wound', 'Attic'],['Battle','Choose']),
        1: (['Suspicious', 'Discussion'],['Stinking', 'Receiver'])
        }
    string = ""
    for i in range(0,8):
        pair_num = 1
        string += "setcycle %d word_pos \"%s\"\n" % (i,word_pos[i])
        string += "setcycle %d word \"%s\"\n" % (i,nwords[pair_num][0][0])
        string += "setcycle %d probe \"%d\"\n" % (i,probe[i])
        string += "setcycle %d pair_pos \"%s\"\n" % (i,pair_pos[i])
        string += "setcycle %d pair \"%s\"\n" % (i,nwords[pair_num][0][1])
        string += "setcycle %d probe_pos \"%s\"\n" % (i,probe_pos[i])
        string += "setcycle %d correct \"%s\"\n" % (i,correct[i])
        string += "run trial_sequence\n"
    
    bl = exp.items["block_loop"]
    bl.from_string(string)
    bl.cycles = 8
    
Sign In or Register to comment.