psychopy.org | Reference | Downloads | Github

Random positions

OS: MAC 10.11.6
PsychoPy version : v1.85.4
What are you trying to achieve?: I’m trying to do the trail making test. I want circle from 1 to 10 and random positions in the screen.

To manage that, I decide to use semi random positions. That’s my code to obtain a list of random position

import pandas as pd
from random import shuffle

positions = ['(-.9, -.9)', '(-.9, -.65)', '(-.9, -.4)', '(-.9, -.15)', '(-.9, .1)', '(-.9, .35)', '(-.9, .6)', '(-.9, .85)',
             '(-.65, -.9)', '(-.65, -.65)', '(-.65, -.4)', '(-.65, -.15)', '(-.65, .1)', '(-.65, .35)', '(-.65, .6)', '(-.65, .85)',
             '(-.4, -.9)', '(-.4, -.65)', '(-.4, -.4)', '(-.4, -.15)', '(-.4, .1)', '(-.4, .35)', '(-.4, .6)', '(-.4, .85)',
             '(-.15, -.9)', '(-.15, -.65)', '(-.15, -.4)', '(-.15, -.15)', '(-.15, .1)', '(-.15, .35)', '(-.15, .6)','(-.15, .85)',
             '(.1, -.9)', '(.1, -.65)', '(.1, -.4)', '(.1, -.15)', '(.1, .1)', '(.1, .35)', '(.1, .6)', '(.1, .85)',
             '(.35, -.9)', '(.35, -.65)', '(.35, -.4)', '(.35, -.15)', '(.35, .1)', '(.35, .35)', '(.35, .6)',
             '(.35, .85)','(.6, -.9)', '(.6, -.65)', '(.6, -.4)', '(.6, -.15)', '(.6, .1)', '(.6, .35)', '(.6, .6)', '(.6, .85)',
             '(.85, -.9)', '(.85, -.65)', '(.85, -.4)', '(.85, -.15)', '(.85, .1)', '(.85, .35)', '(.85, .6)', '(.85, .85)'] #list of all the position possible in the screen with size (0.2, 0.2)

random.sample(positions, len(positions))

my_list_positions = random.sample(positions, 10) #select 10 unique position from the list of all possible possitions

circle = ['circle_1.jpeg', 'circle_2.jpeg', 'circle_3.jpeg', 'circle_4.jpeg', 'circle_5.jpeg',
          'circle_6.jpeg', 'circle_7.jpeg', 'circle_8.jpeg', 'circle_9.jpeg', 'circle_10.jpeg'] #image for the test

shuffle(circle)

df = pd.DataFrame(my_list_positions, columns=["positions"]) #create a dataframe
df['circle'] = circle #add the circles to a random position
df.to_csv('conditions.csv', index=False) #export in a csv file 

The first thing I tried was to loop in the conditions file, but then I realize that I want all my circle at the same time. So I added 10 different image in my trial instead of 1, the 10 images are the 10 different circle and for all the image I would gave it a different position.

The problem is that I always get an error that “positions” cannot be find.

Builder :

I tried with $positions, positions, [positions], [$positions], [positions$], [positions]$

Thanks!!

  • The position field is set to be constant. Therefore PsychoPy expects this variable to be created at the beginning of the experiment. If your code runs later (e.g. at the beginning of a routine), then that variable won’t exist at the start of the experiment. If you want the positions field to be updated on every trial (i.e. every report of the loop), then specify that instead.
  • you just want one of the positions from your list for each stimulus, so specify that by its index (beginning at 0), e.g. my_list_positions[0] through my_list_positions[9] for the respective stimuli.
1 Like

I change my mind. Finally, I decide to use the index without passing by a condition file.
My code :

positions = [[-.9, -.9], [-.9, -.65], [-.9, -.4], [-.9, -.15], [-.9, .1], [-.9, .35], [-.9, .6],[-.9, .85],
             [-.65, -.9], [-.65, -.65], [-.65, -.4], [-.65, -.15], [-.65, .1], [-.65, .35], [-.65, .6], [-.65, .85],
             [-.4, -.9], [-.4, -.65], [-.4, -.4], [-.4, -.15], [-.4, .1], [-.4, .35], [-.4, .6], [-.4, .85],
             [-.15, -.9], [-.15, -.65], [-.15, -.4], [-.15, -.15], [-.15, .1], [-.15, .35], [-.15, .6], [-.15, .85],
             [.1, -.9], [.1, -.65], [.1, -.4], [.1, -.15], [.1, .1], [.1, .35], [.1, .6], [.1, .85],
             [.35, -.9], [.35, -.65], [.35, -.4], [.35, -.15], [.35, .1], [.35, .35], [.35, .6], [.35, .85],
             [.6, -.9], [.6, -.65], [.6, -.4], [.6, -.15], [.6, .1], [.6, .35], [.6, .6], [.6, .85],
             [.85, -.9], [.85, -.65], [.85, -.4], [.85, -.15], [.85, .1], [.85, .35], [.85, .6], [.85, .85]]

shuffle(positions) 

I gave each image an index position

And then in each frame

 if mouse.isPressedIn(cercle_1):
    thisExp.addData('correct', 1)
    thisExp.addData('RT', t)
    continueRoutine = False

elif mouse.isPressedIn(cercle_2):
    thisExp.addData('correct', 0)

elif mouse.isPressedIn(cercle_3):
    thisExp.addData('correct', 0)

To be easier to create, I will use only 3 circles at this time and add the others when it will be working.

Problem : I want to change the image if cercle_2 or cercle_3 is pressed. I tried to add red_2 as an image component and then code that now replace cercle_2 by red_2, but I’m not able to elaborate something

Thanks!!