How to make sure target appear on 9 kinds possible position

What did you try to make it work?:

in my trial,there are 1 target,and 11 distract1,11 distract2.I want they randomly placed on the display based on an invisible 6 by 6 grid occupying an area of 25 cm × 15 cm on the center of screens, with a total of 25 possible locations. So there are totally 9 kinds position(distance to the center point). Jitter of up to 25 pixels allowed the position of the stimuli to vary slightly inside each grid position.

Begin experiment tab:

x_pos=[-12.5,-7.5,-2.5,2.5.7.5,12.5]

y_pos=[-7.5,-4.5,-1.5,1.5,4.5,7.5]

Begin Routine tab:

shuffle(x_pos)

shuffle(y_pos)

I use Polygon define target and distract.

target position:(x_pos[0],y_pos[0])(set every repeat)

distract position:(x_pos[1],y_pos[1]),(x_pos[2],y_pos[2])…

What are you trying to achieve?:

How to make sure target appear on 9 kinds possible position,in different trial.

How to Jitter of up to 25 pixels?

Hi @Hl_Z ,

How to make sure target appear on 9 kinds possible position,in different trial.

At a glance, what you are trying to make the stimuli appear randomly looks reasonable. Is there a specific error or problem that you are having in your experiment? For example, is the experiment crashing with an error message? Or, are the stimuli appearing in the wrong place?

How to Jitter of up to 25 pixels?

Assuming everything else is working, I think you can use some random number generators for this. If your positions are in centimetres, then your jitter should also be in centimetres. For example, maybe 0.5cm is close to 25 pixels, so you could use something like the following in begin routine:

 #get a random number from -5 to +5 (number of mm to jitter by), convert to cm by dividing by 10
jittery= float(randint(-5,6))/10
jitterx= float(randint(-5,6))/10

and then set your positions to x_pos[index]+jitterx, y_pos[index]+jittery

I haven’t tested this code so the syntax might be a little off, but you get the idea.

-shabkr

1 Like

I’m trying to work out why you say 25 possible locations in a 6x6 grid and not 36.

For your “9 kinds position” I take it you are counting positions are equivalent if they are the same distance. If you want these 9 distances to appear equally often across trials you will need to start by defining them – e.g.

positionKinds = [[[0,0],[0,5],[5,0],[5,5]],[[1,0],[1,5],[4,0],[4,5]],...]

Assuming positionKinds has a length of 9 you could then use code like:

thisKind = trials.thisN % 9
if thisKind == 0:
     shuffle(positionKinds)
shuffle(positionKinds[thisKind])
targetPosition = [x_pos[positionKinds[thisKind][0][0]]+jitterx,y_pos[positionKinds[thisKind][0][1]]+jittery]

For the distractors the easiest method might be to have a second set of x_pos and y_pos coordinates which you do shuffle. You could avoid having the target in the same position as a distractor by using the final coordinate if there’s a match, e.g.

for Idx in range(11):
     jittery= float(randint(-5,6))/10
     jitterx= float(randint(-5,6))/10
     if x_pos1[Idx] != x_pos[positionKinds[thisKind][0][0] or y_pos1[Idx] != y_pos[positionKinds[thisKind][0][1]:
          distract1Pos[Idx] = [x_pos1[Idx]+jitterx,y_pos1[Idx]+jittery]
     else:
          distract1Pos[Idx] = [x_pos1[-1]+jitterx,y_pos1[-1]+jittery]
for Idx in range(11):
     jittery= float(randint(-5,6))/10
     jitterx= float(randint(-5,6))/10
     if x_pos1[Idx+11] != x_pos[positionKinds[thisKind][0][0] or y_pos1[Idx+11] != y_pos[positionKinds[thisKind][0][1]:
          distract2Pos[Idx] = [x_pos1[Idx+11]+jitterx,y_pos1[Idx+11]+jittery]
     else:
          distract2Pos[Idx] = [x_pos1[-1]+jitterx,y_pos1[-1]+jittery]


1 Like

Hello!I’m sorry I make a mistake in this post,actually there are 36 possible position.And I use your code,my program can run.Thank you very much!

Besides,there are n kinds different color of target ,and I control target color by Excel.How should I do make sure every color appear on 9 kinds position?I think copy and paste 9 times in excel cannot workout.

Looking forward to your early reply!

That sounds like you need a list that’s 9 x nColours long.

Begin Experiment

positionKinds = [[[0,0],[0,5],[5,0],[5,5]],[[1,0],[1,5],[4,0],[4,5]],...]
distCols = []
for Idx in range(9):
     for Jdx in ['red','green','blue']
          distCols.append([Idx,Jdx]

Begin Routine

calc1 = trials.thisN % len(distCols)
if calc1 == 0:
     shuffle(distCols)
thisKind = distCol[calc1][0]
thisCol = distCol[calc1][1]
shuffle(positionKinds[thisKind])
targetPosition = [x_pos[positionKinds[thisKind][0][0]]+jitterx,y_pos[positionKinds[thisKind][0][1]]+jittery]
1 Like

Thank you! Your code helped a lot. I just changed a few details to achieve what I want. And I think this forum is very friendly to beginners.:rose:

1 Like

Thank you. Everything is fine now.