1

new to the scene. I've created a code for a Visual Search task with a randomized number of Distractors (1/5/9) and a Target that may or may not appear (50% chance).

The coordinates for the placements of both Distractors and Target are randomized, but naturally that on its own doesn't prevent overlap, and both Distractors and Target keep clashing with each other.

Is there any way to prevent it?

I've considered trying to add each new random set of positions to a list and somehow make sure the list doesn't allow duplicates/close numbers (if duplicate/close then randomize again), but no idea how to implement.

Code provided: (D = Distractor, T = Target):

for i in range(20):

    ClosedD_num = random.choice([1, 5, 9])
    ClosedD_list = []

    for i in range(ClosedD_num):
        x_pos = random.uniform(-0.5, 0.5)
        y_pos = random.uniform(-0.5, 0.5)

        ClosedD = visual.ImageStim(
            win=window,
            name='ClosedD',
            image='images/closed_d.png', mask=None, anchor='center',
            ori=randint(1, 360), pos=(x_pos, y_pos), size=(0.1, 0.1),
            color=[1, 1, 1], colorSpace='rgb', opacity=None,
            flipHoriz=False, flipVert=False,
            texRes=128.0, interpolate=True, depth=-3.0)
        ClosedD_list.append(ClosedD)

        for ClosedD in ClosedD_list:
            ClosedD.setAutoDraw(True)

    r_pos = random.uniform(-0.5, 0.5)
    s_pos = random.uniform(-0.5, 0.5)

    ClosedT = visual.ImageStim(
        win=window,
        name='ClosedT',
        image='images/closed_t.png', mask=None, anchor='center',
        ori=randint(1, 360), pos=(r_pos, s_pos), size=(0.1, 0.1),
        color=[1, 1, 1], colorSpace='rgb', opacity=None,
        flipHoriz=False, flipVert=False,
        texRes=128.0, interpolate=True, depth=-3.0)

    ClosedT_present = random.choice([0, 1])
    if ClosedT_present == 0:
        ClosedT.setAutoDraw(False)
    else:
        ClosedT.setAutoDraw(True)
    window.flip()

P.S can provide further information if needed

Markovka
  • 11
  • 2

1 Answers1

0

To avoid overlap in many stimuli you basically need to constrain the randomness. I'd recommend you take a different approach of creating a grid of coordinates, with 1 element per grid position, and then applying a random jitter to the positions of the elements that is smaller than the size of your imaginary boxes. This will still give an appearance of random positions but means a) no overlaps and b) a more even distribution with no clumps of elements

Jon
  • 1,198
  • 7
  • 8
  • I'd also recommend you take a look at the ElementArray stimulus as this is much more efficient at drawing a large set of identical objects (although you can vary posisions, sizes, orientations etc) – Jon May 02 '23 at 08:53