Howdy, Stranger!

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

Supported by

Perform functions according to coordinates within xy_grid?

edited July 2016 in OpenSesame

Hi there,
I was just wondering if it is possible to use the xy_grid function to draw a grid with different stimuli, according to the coordinates? Can you reference the coordinates somehow?
Essentially, I am trying to produce a grid of letters whose positions are random.
Many thanks,
Joff

Comments

  • edited 1:38AM

    Hi Joff,

    I'm not sure I understand your question correctly, but what you're describing is exactly what the xy_grid() function is for. A simple 3x3 grid with letters at random locations can be created like this:

    import random
    
    # Get a list of 9 letters
    letters = 'ABCDEFGHI'
    # Get a list of 3x3=9 (x, y) coordinates
    # and shuffle the order
    xy_list = xy_grid(n=3, spacing=40)
    random.shuffle(xy_list)
    
    # Now create a canvas with a letter on
    # each location and show it.
    my_canvas = canvas()
    for letter, (x, y) in zip(letters, xy_list):
        my_canvas.text(text=letter, x=x, y=y)
    my_canvas.show()
    

    If you have no experience with Python, you may also want to walk through one of the beginner Python tutorials:

    Cheers!
    Sebastiaan

  • edited 1:38AM

    Hi Sebastian,
    Sorry for the initial vagueness, regardless your advice was very helpful and I think I am very close to achieving what I want to do.

    Rather than these being completely random letters there are some rules. It is a visual search task where a letter F is presented in a grid of distracter letters (E's and t's). The size of the grid will change depending on the performance of the individual and the letters can be oriented as normal, or they can be mirror images.

    import random
    
    # Create canvas called c
    c = canvas(color='green', penwidth=2)
    print('Canvas size is:', c.size)
    
    # Set starting values for column and row variables
    i = 2
    j = 2
    
    # Create empty list with length i*j
    search_list = [None]*(i*j)
    print('Empty search list:', search_list)
    
    # Set lists for distractors and targets
    distractors = ['normal_e', 'mirror_e', 'left_t', 'right_t']
    targets = ['normal_f', 'mirror_f']
    
    # Randomly select distractors to fill search array
    # and then replace one distractor with a random target
    for n in range (i*j):
        search_list[n] = random.choice(distractors)
    
    # Set target variable and randomly select a target F to fill a random position
    # in the array
    var.target = 'target'
    var.target = random.choice(targets)
    search_list[random.randint(0, i*j)] = var.target
    print('The randomised search array is:', search_list)
    
    # Create empty xy_grid called search array
    search_array = xy_grid((i, j), 40)
    
    # Combine the stimuli with the search array !!!!!!!!!!!!
    for z, (x, y) in zip (search_list, search_array)
        if z == 'normal_e':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'mirror_e':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'left_t':
            c.line(x, y, x, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'right_t':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'normal_f':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
        elif z == 'mirror_f':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
    

    I am having a problem with the final loop which draws the letters into their corresponding positions in the grid. I imagine it is just a question of syntax, but I can't see it myself.

    I would greatly appreciate the help of your trained eye!

    Many thanks,
    Joff

  • Hi Joff,

    There are a few things that quite work out in the code you posted.

    1) There is a : missing in the end of the line in which you initiate the for loop
    2) Your usage of random.randint allows indices to be selected that are out of the range of your list. You should use i*j-1 instead
    3) You don't present anything yet. I added two lines in the end, that will actually show your displays (until a key is pressed)

    Here is your (updated) code:

    import random
    
    # Create canvas called c
    c = canvas(color='green', penwidth=2)
    # create also a keyboard
    k = keyboard()
    print('Canvas size is:', c.size)
    
    # Set starting values for column and row variables
    i = 2
    j = 2
    
    # Create empty list with length i*j
    search_list = [None]*(i*j)
    print('Empty search list:', search_list)
    
    # Set lists for distractors and targets
    distractors = ['normal_e', 'mirror_e', 'left_t', 'right_t']
    targets = ['normal_f', 'mirror_f']
    
    # Randomly select distractors to fill search array
    # and then replace one distractor with a random target
    for n in range (i*j):
        search_list[n] = random.choice(distractors)
    
    # Set target variable and randomly select a target F to fill a random position
    # in the array
    var.target = 'target'
    var.target = random.choice(targets)
    search_list[random.randint(0, i*j-1)] = var.target
    print('The randomised search array is:', search_list)
    
    # Create empty xy_grid called search array
    search_array = xy_grid((i, j), 40)
    
    # Combine the stimuli with the search array !!!!!!!!!!!!
    for z, (x, y) in zip (search_list, search_array):
        if z == 'normal_e':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'mirror_e':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
            c.line(x, y+21, x+10, y+21)
        elif z == 'left_t':
            c.line(x, y, x, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'right_t':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y+10, x+10, y+10)
        elif z == 'normal_f':
            c.line(x, y, x, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
        elif z == 'mirror_f':
            c.line(x+11, y, x+11, y+20)
            c.line(x, y, x+10, y)
            c.line(x, y+10, x+10, y+10)
    
    # show canvas   
    c.show()
    k.get_key()
    

    I hope this solved your issue.

    Best,

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.