Howdy, Stranger!

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

Supported by

Defining variables in inline python script

edited December 2022 in OpenSesame

Hi everyone, I am just starting with OpenSesame, and I have an issue which is pretty straightforward but to which I could not find a solution in the forum.

I am trying to define two lists in an inline script, and appending numbers and symbols from the "number" and "symbol" variables defined through a sequential loop. My aim is to then randomize the symbol-number pairings across different experiment runs, maintaining consistency within the individual run.

I am using the following code, which is the format I would use in psychopy:

# This runs in the 'Prepare' block
numbers_list = []
symbols_list = []

# This runs in the 'Run' block
numbers_list.append(var.number)
symbols_list.append(var.symbol)

However, I get the following error: "The variable 'numbers_list' does not exist."

I have tried defining them with the syntax "var.numbers_list = []" but I get the same error.

Any advice is greatly appreciated.

Comments

  • edited December 2022

    So, after some trial and error, I was able to define the variables using this syntax:

    import numpy as np
    
    exp.set('numbers_list', list(np.arange(1,10,1)))
    exp.set('symbols_list', [])
    

    However, after running a loop containing the following inline code, the symbol list still shows up as empty.

    var.symbols_list.append(var.symbol)
    
    


  • Hi,

    if you fill your loop in the run phase, you need to use a feedback item to present it, otherwise it will only show its state that it had before the run phase started. See this for more details: https://osdoc.cogsci.nl/3.3/manual/prepare-run/#sketchpad-and-feedback-items


    Hope this helps,

    Eduard

    Buy Me A Coffee

  • Thank you for your reply, Eduard. I will try to explain more clearly what I am attempting to do, as being pretty new to OpenSesame, I am not even sure that it is the right process.

    Basically, I am trying to recreate a digital version of the Symbol-Digits Modalities Test, in which participants need to press the number (between 1-9) corresponding to a symbol displayed randomly at the center of the screen, based on symbol-number pairings displayed in a legend at the top of the screen. Additionally, I need to randomize the symbol-number pairings across different test trials, to allow for repeated testing.

    I was able to do this in psychopy by loading my symbols at the beginning of the experiment with a random loop, which appended each symbol to a list variable. I then zipped my symbol and numbers lists into a dictionary, which I used to display the legend and to determine correct responses for each stimulus presentation.

    My issue atm is that I seem to be able to create the lists through a "loader" loop

    but then I am not able to access individual numbers or symbols in a subsequent "experimental" loop via normal python indexing (e.g. using var.symbols__list[0]in a sketchpad script or [numbers_list[0]] in a sketchpad textline element).

    It is very much likely that the whole process is wrong, or that I am making some rookie mistake, but I was unable to find specific info for my use case.

  • Hi @michedini ,


    Thanks for your interest in OpenSesame and welcome to the forum. I would advise you to have a look at our tutorials to get a grasp on how OpenSesame works, and especially how you can build a typical experimental hierarchy with block and trial levels.


    To achieve your goal, I think you should:


    • Define your dictionary at the very beginning of your experiment (i.e. at the top level of the hierarchy), for example with an inline_script item with the following Python code in the Prepare tab:
    import random
    
    # Define two lists:
    l_digits= [1,2,3,4,5,6,7,8,9]
    l_symbols = ["A","B","C","D","E","F","G","H","I"]
    
    # Shuffle:
    random.shuffle(l_digits)
    random.shuffle(l_symbols)
    
    # Convert to dictionary with numbers as keys and symbols as values:
    dictionary = {l_digits[i]: l_symbols[i] for i in range(len(l_digits))}
    


    • Next, at the trial level, determine the values of the variables that belong to the current trial. Use the .var notation to store the variables for future use in the GUI. The Python script would look something like this (make sure to put it in the Prepare tab again):
    # NOTE that the trial_number variable is defined in the blockloop:
    var.trial_symbol = dictionary[var.trial_digit]
    
    # NOTE also that the correct response is identical to the trial digit
    var.correct_response = var.trial_digit
    
    
    • Your overview area should now look something like this:


    • Finally, you can show the current trial values in a sketchpad item.



    I attached a working example (including instructions and feedback, in case you were interested in that, too).


    Just let us know if you have any further questions.


    Cheers,


    Lotje



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

Sign In or Register to comment.