Howdy, Stranger!

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

Supported by

[solved] Equal number of trials when randomising

edited December 2013 in OpenSesame

Hi all,

I'm running a simple experiment that requires people to read words off the screen. The aim is to test perception thresholds for different font colours. I have 255 words and 15 different font colours and I want each word to be displayed in a randomly chosen colour. Therefore, for each colour I would like to have 17 words randomly picked from the list.

To do so, I created a block with a list of words (255) that runs a sequence with 15 sketchpads, each using different font colour. Before the sketchpads there is a short script I wrote to create a variable [n] that has value of a random integer (1,15). If [n] = number of sketchpad, the sketchpad gets run.

Unfortunately, this method results in different frequencies of calling each sketchpad, so I'm not getting a nice 17 words per each sketchpad.

I bet there is a better solution to this, but I'm not very fluent with OS.

I would really appreciate your help!

Thanks,

Jarek

Comments

  • edited December 2013

    Hi Jarek,

    To make sure that every sketchpad is ran exactly 17 times, you could use the built-in Python function pop(), which draws one item from a list without replacement. This way, you can keep popping sketchpad numbers until the list is empty (i.e. until every sketchpad is ran 17 times).

    To achieve this, firstly append an inline_script item to the beginning of your block sequence where you define a list containing the sketchpad numbers for all trials in a given block (i.e., seventeen 1's, seventeen 2's, etc., until 15):

    # import the module random:
    import random
    
    # create the list:
    l = range(1,16)*17
    
    # Shuffle the list:
    random.shuffle(l)
    

    Next, change the code in the inline_script in your trial_sequence where you define the value of the variable n:

    # Draw one item from the list:
    n = l.pop()
    
    # Set the variable for future use in the GUI:
    exp.set('n', n)
    

    Does this help?

    Best,

    Lotje

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

  • edited 10:36PM

    Hi Lotje,

    This looks great and works great too, thank you very much!

    Just to make sure I understand what's going on:
    does this little bit

    l = range(1,16)*17

    create 17 lists of 15 elements? If so, in case I would like to run 3 blocks of 255, could I just change it to

    l = range(1,16)*51

    Would this be also correct?

    Best,

    Jarek

  • edited December 2013

    Hi Jarek,

    Good to hear that it works.

    Does this bit (..) create 17 lists of 15 elements?

    No, it creates one list containing 255 items. Seventeen 1's, seventeen 2's, etc.

    Actually, the corresponding piece of code might be easier to understand when it is rewritten to:

    # Create list containing the integer 1 to (but not including) 16:
    # See also:
    #http://docs.python.org/2/library/functions.html#range">http://docs.python.org/2/library/functions.html#range
    l = range(1,16)
    
    # Print the list to the debug window:
    print l
    

    This will return

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    

    in the debug window.

    # Make the list seventeen times as long, such that every item occurs seventeen times instead of once:
    l  = l * 17
    
    # And again, print the resulting list to the debug window:
    print l
    

    This will return:

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    

    And

    print len(l)
    

    will print '

    255
    

    i.e. the length of the list.

    As you see, printing information to the debug window can be really helpful when using Python inline coding.

    image

    If so, in case I would like to run 3 blocks of 255, could I just change it to l = range(1,16)*51?

    Yes, that would work. But note that after randomising the resulting list, you will not be sure that every sketchpad occurs equally often within a given block (but only that it occurs equally often across the 3 blocks). That's why I advised to (re)set the list at the beginning of every block sequence (in which case all sketchpads do occur equally often within a given block).

    Does this make sense? Please let us know if you have any further questions.

    Cheers,

    Lotje

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

  • edited 10:36PM

    HI Lotje,

    Yes, that makes perfect sense. In my case I want to have equal numbers across all trials, so I can go for one big list.

    Thanks again for helping me,

    Jarek

Sign In or Register to comment.