# Have my stimuli appear across the entirety of the screen, not just a smaller location

**URL:** <https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937>\
**Category:** Coding\
**Tags:** randomization\
**Created:** [July 8, 2021, 10:40am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937 "2021-07-08T10:40:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [July 8, 2021, 10:40am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/1 "2021-07-08T10:40:34Z")

</div>

Hello everyone,

I am making good progress with my visual search task and I have managed to create two sets of random distractors of Red and Green Letter T’s, however due to the code I have implemented, rather than use the entire screen, the distractors only appear in a small subset in the center of the screen

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/f/8f1030fc23528afea7fc92ca1a685eb9fd63c0fe.png)

As you can see from the image the items are located relatively closely together and they can overlap at times.

This is my code (see below) I used to create my stimuli. I know why it’s happening, because I use Norm units (I find norm units the easiest to understand) So the random function is picking a number between 0-1 and then subtracting 0.5 in order to balance the display, I have tried messing around with other values but then my stimuli are no longer centered. Would it be more optimal to switch to pixels, and if so, what would my random function become?

Also would it be more efficient to use an element arraystim to create a screen of various targets and a singular distractor?

T\_textlargeRed =  
T\_textlargeGreen=

targetPos=(random()-0.5, random()-0.5)

for x in range(10):  
T\_textR = visual.TextStim(win=win, name=‘T\_textR’,  
text=‘T’,  
font=‘Open Sans’,  
**pos=(random()-0.5, random()-0.5)**, height=0.1, wrapWidth=None, ori=0.0,  
color=‘red’, colorSpace=‘rgb’, opacity=None,  
units = ‘norm’,  
languageStyle=‘LTR’,  
depth=0.0)

```
    T_textlargeRed.append(T_textR)

```

print(T\_textlargeRed)

for letter in T\_textlargeRed:  
letter.setAutoDraw(True)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [July 8, 2021, 8:36pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/2 "2021-07-08T20:36:21Z")

</div>

Did you know that norm units go from -1 to +1 in each dimension?

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [July 9, 2021, 12:36pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/3 "2021-07-09T12:36:16Z")

</div>

Hi, thanks for the assistance again! Yes, I read about the types spacial units yesterday to refresh my memory. random() is between 0 and 1, with -0.5 added to x and y to create a spread that is not skewed towards left/right of the monitor. I just realised I never tested just random() without any minus-05

Maybe I have misunderstood, but if random() selects a random number 0-1, then I need to increase the number I minus from this to create a better spread, say -1 in order to account for norm ranging from between -1 to 1

In my readings yesterday I actually wondered whether random()-0.is the optimal way to randomize my stimuli, rather randomrange() maybe better, as I no longer need to perform a calculation on the outcome of random(), but rather set the range between -1 and 1 then have some step size that would create enough random permutations, do you think this would make more sense?

Kind regards,

Rob

---

<div class="post-metadata">

**Author:** ![Mel499](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mel499/32/6581_2.png) [@Mel499](https://discourse.psychopy.org/u/Mel499)\
**Post date:** [September 9, 2022, 9:29am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/4 "2022-09-09T09:29:46Z")

</div>

Hi, I encontered the same problem while programming a visual search task. In order to avoid overlaps, I checked the positions with np.allclose() (also tried np.isclose() and math.isclose()):

```auto
for i in range(1,n_items):
    x_pos = np.random.uniform(-0.3, 0.3)
    y_pos = np.random.uniform(-0.3, 0.3)
    x_comp = np.allclose(x_pos, x_locations[i-1], rtol = 0.01)
    y_comp = np.allclose(y_pos, y_locations[i-1], rtol = 0.01)
    while x_comp == TRUE:
        x_pos = np.random.uniform(-0.3, 0.3)
    while y_comp == TRUE:
        y_pos = np.random.uniform(-0.3, 0.3)
    x_locations.append(x_pos)
    y_locations.append(y_pos)

```

Still, I get overlapped items sometimes and after 5-15 repetitions, the experiment crashes.

So I was wondering if you found a solution for your problem about the overlapped items in visual search that may work better?

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [September 9, 2022, 10:08am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/5 "2022-09-09T10:08:07Z")

</div>

Hey Mel,

The best solution I found was adding an offset to either the X or Y co-ordinates. Are you running your experiment online?

**Still, I get overlapped items sometimes and after 5-15 repetitions, the experiment crashes.** - as in stimuli being created?

Kind regards,

Rob

---

<div class="post-metadata">

**Author:** ![ajus](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@ajus](https://discourse.psychopy.org/u/ajus)\
**Post date:** [September 9, 2022, 11:55am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/6 "2022-09-09T11:55:42Z")

</div>

Hi @Mel499,

I’m not a Python pro, but looking at your code I don’t think it does what you want it to do. Firstly, you only compare item `i` and item `i-1` in terms of being close. So there is nothing in the code preventing, for example, item `i` and item `i-2` from overlapping. Second, you only compare `x` and `y` coordinates separately. Third, once `x_comp` or `y_comp` are `True` (you write `TRUE` so this may also be a problem) you go into an infinite `while` loop, because inside the loop those values can’t change and the loop will therefore never break. I suspect this is why the experiment crashes at some point.

Maybe, this is more suitable for you:

```auto
for i in range(1,n_items):
    x_pos = np.random.uniform(-0.3, 0.3)
    y_pos = np.random.uniform(-0.3, 0.3)

    x_close = np.isclose(x_pos, x_locations, rtol = 0.01)
    y_close = np.isclose(y_pos, y_locations, rtol = 0.01)

    overlap = np.any(np.logical_and(x_close, y_close))
    
    while overlap == True:
        x_pos = np.random.uniform(-0.3, 0.3)
        y_pos = np.random.uniform(-0.3, 0.3)

        x_close = np.isclose(x_pos, x_locations, rtol = 0.01)
        y_close = np.isclose(y_pos, y_locations, rtol = 0.01)

        overlap = np.any(np.logical_and(x_close, y_close))
    
    x_locations.append(x_pos)
    y_locations.append(y_pos)

```

If you still get to much overlap with this code, you can increase `rtol` to make the definition of “close” more liberal or specify an absolute tolerance (may be easier to control) by including the argument `atol` instead of `rtol`. Now that I think of it `rtol = .01 ` is probably way too low to prevent overlap.

---

<div class="post-metadata">

**Author:** ![Mel499](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mel499/32/6581_2.png) [@Mel499](https://discourse.psychopy.org/u/Mel499)\
**Post date:** [September 12, 2022, 1:32pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/7 "2022-09-12T13:32:34Z")

</div>

Hi @ajus ,

thank you so much for your suggestion! This was indeed the problem, with your help, the code works just fine right now. In the end, I used atol instead of rtol, because the items were still overlapping.

---

<div class="post-metadata">

**Author:** ![mbistr16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mbistr16](https://discourse.psychopy.org/u/mbistr16)\
**Post date:** [November 22, 2022, 11:15am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/8 "2022-11-22T11:15:21Z")

</div>

**OS** (e.g. Win10):  
macOS Ventura13.0

**PsychoPy version** (e.g. 1.84.x):  
PsychoPy v2022.2.4

Hello everyone,

I am trying to do a very similar thing as the one being discussed in this thread.  
I am very new to psychopy and was hoping that someone in this forum can detect where I go wrong.

I am making a visual search task that consist of a target and three different distractors. Psychopy draw these via my input file. On top of that I have added a code component so that the distractors get drawn 16 times each for a total of 48 distractors. This works fine. But of course overlapping is an issue.  
I have attempted to implement the code presented by ajus in this thread.

My code is as follows:

n\_distractors=16

distractors=

for i in range(n\_distractors):  
distractor = visual.TextStim(win=win, name=‘distractor’,  
text=(text),  
font=‘Open Sans’,  
pos=(random()-0.5,random()-0.5), height=0.1, wrapWidth=None, ori=0.0,  
color=(text\_color), colorSpace=‘rgb’, opacity=None,  
languageStyle=‘LTR’,  
depth=0.0);

```
distractors.append(distractor)    

```

for i in range(n\_distractors):  
distractor = visual.TextStim(win=win, name=‘distractor’,  
text=(text\_two),  
font=‘Open Sans’,  
pos=(random()-0.5,random()-0.5), height=0.1, wrapWidth=None, ori=0.0,  
color=(text\_two\_color), colorSpace=‘rgb’, opacity=None,  
languageStyle=‘LTR’,  
depth=0.0);

```
distractors.append(distractor)    

```

for i in range(n\_distractors):  
distractor = visual.TextStim(win=win, name=‘distractor’,  
text=(text\_three),  
font=‘Open Sans’,  
pos=(random()-0.5,random()-0.5), height=0.1, wrapWidth=None, ori=0.0,  
color=(text\_three\_color), colorSpace=‘rgb’, opacity=None,  
languageStyle=‘LTR’,  
depth=0.0);

```
distractors.append(distractor)

```

for distractor in distractors:  
distractor.setAutoDraw(True)

for i in range(1,n\_distractors):  
x\_pos=np.random.uniform(-0.3,0.3)  
y\_pos=np.random.uniform(-0.3,0.3)

```
x_close=np.isclose(x_pos, x_locations, atol=0.01)
y_close=np.isclose(y_pos, y_locations, atol=0.01)
    
overlap=np.any(np.logical_and(x_close, y_close))
    
while overlap==True:
    x_pos=np.random.uniform(-0.3,0.3)
    y_pos=np.random.uniform(-0.3,0.3)
        
    x_close=np.isclose(x_pos, x_locations, atol=0.01)
    y_close=np.isclose(y_pos, y_locations, atol=0.01)
        
    overlap=np.any(np.logical_and(x_close, y_close))
        
x_locations.append(x_pos)
y_locations.append(y_pos)

```

When I try to run, the experiment close with this message:

```
x_close=np.isclose(x_pos, x_locations, atol=0.01)

```

NameError: name ‘x\_locations’ is not defined  
################# Experiment ended with exit code 1 [pid:1195] #################

If anyone could help me with how I might go about fixing this, it would be much, much appreciated.

Best regards, Mathilde

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [November 22, 2022, 11:40am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/9 "2022-11-22T11:40:30Z")

</div>

Hey Mathilde

I think the issue is ‘x\_locations in not defined, is that it isn’t defined before you need it, or Psychopy can’t understand what this is referring too. Are x\_locations defined prior to your while loop?

Kind regards,

Rob

---

<div class="post-metadata">

**Author:** ![mbistr16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mbistr16](https://discourse.psychopy.org/u/mbistr16)\
**Post date:** [November 22, 2022, 12:03pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/10 "2022-11-22T12:03:51Z")

</div>

Hey Rob,  
Thanks for your reply!  
It’s very likely the issue.  
Would you happen to know how I define x\_locations?  
In the part of the code that is related to the text stimuli, under position maybe or something completely different?

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [November 22, 2022, 4:35pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/11 "2022-11-22T16:35:06Z")

</div>

Hey no worries!

I would create an empty array at the beginning of the routine.

```auto
x_locations = []

```

See if that helps?

Kind regards,

Rob

---

<div class="post-metadata">

**Author:** ![mbistr16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mbistr16](https://discourse.psychopy.org/u/mbistr16)\
**Post date:** [November 22, 2022, 6:33pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/12 "2022-11-22T18:33:54Z")

</div>

Yea it did! In the sense that the experiment is now running and not closing with an error.  
However, all the distractors are now in the same position, looking like a big clump.  
Do you have an idea what could be causing that?

Best regards, Mathilde

---

<div class="post-metadata">

**Author:** ![rh4818](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rh4818](https://discourse.psychopy.org/u/rh4818)\
**Post date:** [November 22, 2022, 7:42pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/13 "2022-11-22T19:42:34Z")

</div>

Amazing! hmm, This sounds similar to an issue I had ages ago.

When you say in the same position, I assume it’s plotting them all on the same position, so you have the target, then 48 Distractors on top of each other?

I have a feeling what it’s doing now, is making some random positions, then applying it to each distractor, which is not what we want, we want random positions per distractor.

---

<div class="post-metadata">

**Author:** ![mbistr16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mbistr16](https://discourse.psychopy.org/u/mbistr16)\
**Post date:** [November 22, 2022, 8:40pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/14 "2022-11-22T20:40:27Z")

</div>

Yes that was exactly what it was doing.  
And yes, I just realized that I had removed ‘pos=(random()-0.5,random()-0.5)’ at some point and then forgotten about it… I have put it back, and it seems that the distractors are being distributed better than before I used the code, with less overlap than before.  
I still get some overlap though between distractors.  
I Wonder if distances can be made bigger still using this code? Thanks again!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [November 23, 2022, 9:24am UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/15 "2022-11-23T09:24:58Z")

</div>

What I do is create a grid of possible points, shuffle them and then add a little jitter to create random locations without overlap.

e.g.

Begin Experiment

```auto
maxX = .4
maxY = .35
nX = 10
nY = 8
positions = []
for Idx in range(nX):
    for Jdx in range(nY):
        positions.append([Idx/(nX-1)*maxX*2-maxX,Jdx/(nY-1)*maxY*2-maxY])

jitter = .05
maxElements = 24
elements = []

for Idx in range(maxElements):
    elements.append(visual.TextStim(win=win, 
    name='element'+str(Idx),
    text=' ',
    font='Arial',
    pos=(0,0),
    height=0.06, wrapWidth=None, ori=0, 
    color="white"))

```

Begin Routine

```auto
shuffle(positions)
elements[0].text = Target
elements[0].setPos([positions[0][0]+random()*jitter-random()*jitter, positions[0][1]+random()*jitter-random()*jitter])
elements[0].setAutoDraw(True)
for Idx in range(1,nDistractors):
    elements[Idx].text = Distractors
    elements[Idx].setPos([positions[Idx][0]+random()*jitter-random()*jitter, positions[Idx][1]+random()*jitter-random()*jitter])
    elements[Idx].setAutoDraw(True)

```

This comes from a visual search demo that it appears I haven’t yet uploaded to my Online Demos. tTraceTest8 uses something similar but (I think) avoids any points being directly in line horizontally or vertically.

---

<div class="post-metadata">

**Author:** ![mbistr16](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@mbistr16](https://discourse.psychopy.org/u/mbistr16)\
**Post date:** [November 26, 2022, 2:37pm UTC](https://discourse.psychopy.org/t/have-my-stimuli-appear-across-the-entirety-of-the-screen-not-just-a-smaller-location/23937/16 "2022-11-26T14:37:38Z")

</div>

In the end I did something quite simple - I set the y-coordinate to always be the same and the x-axis to be random, which was a good enough solution to the problem in this case.  
Thank you for taking the time and replying to my question, much appreciated.
