psychopy.org | Reference | Downloads | Github

Randomization and making association with keyboard press

Dear all,

I want to randomize the location of three text on the screen (textA, textB, textC) in three different positions. I also have a keyboard named Choice with allowed keys of ‘left’,‘down’, and ‘right’ to choose the text stimulus accordingly.

As Michael helped me, I use the following codes to shuffle the position of my text:

positions=[[0,-0.1],[-0.3,-0.1],[-0.6,-0.1]]
shuffle(positions)

And in order to associate the key press with the randomized text, I used the following codes. I created a dictionary which maps keyboard responses to their corresponding x coordinate:

key_to_x = {‘1’:-0.6, ‘2’:-0.3, ‘3’:0}

Then store the x coordinate of each stimulus:

thisExp.addData(‘textA_x’, textA.pos[0])
thisExp.addData(‘textB_x’, textB.pos[0])
thisExp.addData(‘textC_x’, textC.pos[0])

And then indicate the x coordinate of the chosen stimulus:

thisExp.addData(‘chosen_x’, key_to_x[Choice.keys[0])

And this is what I got from the data file:
Untitled

As you can see in the chosen_x column, the first element is a weird number that I don’t know how is it here! It is not all the story. the x coordinate of the chosen text is not exactly what I chose. For example, in the second row, I chose text B by pressing the ‘down’ button, but in the chosen_x column, I got the coordinate of text A.

I would appreciate any comments.

No, that is just the cell to the left (in column N) overflowing because there is actually no value in column O in that row.

This makes me suspect that your code is not running at the right time, as the values aren’t corresponding to what they should be on a given trial (i.e. no value at all on the first trial, and incorrect values on subsequent ones). Check when your code runs, relative to the variables and responses it needs to use (e.g. should it be at the end of routine, rather than at the beginning, and so on).

Dear Michael,

Thank you for your answer. Changing the location of my codes to the End Routine fix the problem. However, the essential problem still exists.

Untitled

Look at my data output. the chosen_x columns just tells us the x coordinate of the selected text and it does not tell us what is that text. For example, I chose text B in the first two trials (look at my key press and the corresponding position of text B in first two trials), however I got two different chosen_x for each.

I think the problem is related to this line of code:

key_to_x = {‘left’:-0.6, ‘down’:-0.3, ‘right’:0}

Regarding to this line, our determining line

thisExp.addData(‘chosen_x’, key_to_x[Choice.keys[0])

look at each key press and add the corresponding value, without any care where the selected text located.

I hope I could explain clear enough.

This code is doing exactly what it should: look at columns M and O: their values correspond exactly as per your dictionary key_to_x.

A computer will only do exactly what you tell it to do. Nowhere do you have any code to extract the selected text.

Correct, because stimulus B was in a different location on each trial. The code is doing exactly what you ask of it.

Perhaps you want to keep the stimuli in fixed locations but instead shuffle your text values across them?

1 Like

Dear Michael,

I think I solved my problem. I put these line of codes End Routine:

if textA.pos [0] == 0 and Choice.keys [0]== ‘right’: chose= ‘A’
if textA.pos [0] == -0.3 and Choice.keys [0]== ‘down’:chose= ‘A’
if textA.pos [0] == -0.6 and Choice.keys [0]== ‘left’: chose= ‘A’

if textB.pos [0] == 0 and Choice.keys [0]== ‘right’: chose= ‘B’
if textB.pos [0] == -0.3 and Choice.keys [0]== ‘down’: chose= ‘B’
if textB.pos [0] == -0.6 and Choice.keys [0]== ‘left’: chose= ‘B’

if textC.pos [0] == 0 and Choice.keys [0]== ‘right’: chose= ‘C’
if textC.pos [0] == -0.3 and Choice.keys [0]== ‘down’: chose= ‘C’
if textC.pos [0] == -0.6 and Choice.keys [0]== ‘left’: chose= ‘C’

thisExp.addData(‘chosenOne’, chose)

And this is my data file:

Untitled

I tested it several times and it worked nicely. ChosenOne columns tells me which text I chose randomly each time.
Do you think this code is doing good to tell us what we want?