# Randomization of Image Presentation

**URL:** https://discourse.psychopy.org/t/randomization-of-image-presentation/27441
**Category:** Coding
**Created:** [February 16, 2022, 11:01am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441 "2022-02-16T11:01:57Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 16, 2022, 11:01am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/1 "2022-02-16T11:01:57Z")

</div>

Dear Forum,  
I want to randomise the positions of images on the screen in a grid of 9 potential locations.

The 9 images belong to 3 categories (A,B,C)  
ex. 3 different Pasta images (A1,A2,A3) , 3 different fruit images, 3 different landscapes

I need to randomise 2 things:

1. The position of the images according to their category these 3 images should end up in the same row of the grid

2. Within one category the location of the images needs to be randomised in that row  
ex. within fruit the position of strawberry needs to be randomised between right middle left in the previously selected row

the 9 images are only presented once to the participant. Each trial has 9 different images.

I am really grateful for any help on how to approach this since I am completely new to programming.  
Thank you in advance!

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 16, 2022, 11:37am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/2 "2022-02-16T11:37:04Z")

</div>

Hello,

should each image appear on each position in the run of the experiment?

Best wishes Jens

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 16, 2022, 11:40am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/3 "2022-02-16T11:40:10Z")

</div>

Hello Jens,

Each image is presented only once per participant. Each out of 80 trials has 9 new images  
The image location should be randomised between participants.

best  
Leonie

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 16, 2022, 11:50am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/4 "2022-02-16T11:50:36Z")

</div>

Hello,

sorry, I still don’t get it. There are 9 images which you want to show only once, right? Given that there are 9 positions, this is one trial, right. The other 80 trials do not matter?

Best wishes Jens

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 16, 2022, 11:54am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/5 "2022-02-16T11:54:46Z")

</div>

I am sorry for not being clear. Exactly what you said. The other 80 trials do not matter since the images and categories are new in every trial.

I hope I understood your question correctly

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 16, 2022, 12:39pm UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/6 "2022-02-16T12:39:52Z")

</div>

Hello Leonie,

ok, create a code-component with three x and y-positions for each image you want to show and initialize it:

```auto
pasta1x = 0
pasta2x = 0
pasta3x = 0
pasta1y = 0
pasta2y = 0
pasta3y = 0
fruit1x = 0
fruit2x = 0
fruit3x = 0
fruit1y = 0
fruit2y = 0
fruit3y = 0
land1x = 0
land2x = 0
land3x = 0
land1y = 0
land2y = 0
land3y = 0

xpos = [-.3, 0, .3]
ypos = [-.25, 0, .25]

```

This goes in the _Begin Experiment tab_ of the component. In the _Begin Routine tab_ add the following code:

```auto
shuffle(ypos)
pasta1y = ypos[0]
pasta2y = ypos[0]
pasta3y = ypos[0]
fruit1y = ypos[1]
fruit2y = ypos[1]
fruit3y = ypos[1]
land1y = ypos[2]
land2y = ypos[2]
land3y = ypos[2]

shuffle(xpos)
pasta1x = xpos[0]
pasta2x = xpos[1]
pasta3x = xpos[2]

shuffle(xpos)
fruit1x = xpos[0]
fruit2x = xpos[1]
fruit3x = xpos[2]

shuffle(xpos)
land1x = xpos[0]
land2x = xpos[1]
land3x = xpos[2]

```

I shuffle the x-position three times. This prevent clustering of positions across categories, all images1 are on position 0, all images2 are on position 2 aso…

If you want to save the positions, add the following code to the _End routine tab_:

```auto
thisExp.addData("fruit1x", fruit1x)
thisExp.addData("fruit1y", fruit1y)
thisExp.addData("fruit2x", fruit2x)
thisExp.addData("fruit2x", fruit2y)
thisExp.addData("fruit3x", fruit3x)
thisExp.addData("fruit3y", fruit3y)
thisExp.addData("pasta1x", pasta1x)
thisExp.addData("pasta1y", pasta1y)
thisExp.addData("pasta2x", pasta2x)
thisExp.addData("pasta2y", pasta2y)
thisExp.addData("pasta3x", pasta3x)
thisExp.addData("pasta3x", pasta3y)
thisExp.addData("land1x", land1x)
thisExp.addData("land1y", land1y)
thisExp.addData("land2x", land2x)
thisExp.addData("land2y", land2y)
thisExp.addData("land3x", land3x)
thisExp.addData("land3y", land3y)

```

Now, create nine _image-components_ (fruit1, fruit2, fruit2, pasta1 aso.), one for each image. Change the _location property_ of each image-component (e.g. fruit1; pasta1) to get a unique position (e.g. fruit1x, fruit1y; pasta1x, pasta1y respectively) and set it to _set every repeat_.

![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/2/7241e43c565327029c16117b2ca1bebe3035258b.png)

Best wishes Jens

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 17, 2022, 10:00am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/7 "2022-02-17T10:00:12Z")

</div>

Thank you so much this is extremely helpful!

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 17, 2022, 11:22am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/8 "2022-02-17T11:22:00Z")

</div>

Dear Jens, it works perfectly. I just have a short follow up question.

I also need to randomise between 2 other conditions.  
A) Either one row is one category (like your code)  
B) One column is one category

I am using an extended version of your code at the moment to achieve this.  
What I am missing is: How can I ensure a balanced number between these 2 conditions  
(50% of trials for A, 50% for B) ?

```
if random.random()>0.5:   
    shuffle(ypos)
    A1y = ypos[0]
    A2y = ypos[0]
    A3y = ypos[0]
    B1y = ypos[1]
    B2y = ypos[1]
    B3y = ypos[1]
    C1y = ypos[2]
    C2y = ypos[2]
    C3y = ypos[2]
    
    shuffle(xpos)
    A1x = xpos[0]
    A2x = xpos[1]
    A3x = xpos[2]

    shuffle(xpos)
    B1x = xpos[0]
    B2x = xpos[1]
    B3x = xpos[2]

    shuffle(xpos)
    C1x = xpos[0]
    C2x = xpos[1]
    C3x = xpos[2]
    
else:
    shuffle(xpos)
    A1x = xpos[0]
    A2x = xpos[0]
    A3x = xpos[0]
    B1x = xpos[1]
    B2x = xpos[1]
    B3x = xpos[1]
    C1x = xpos[2]
    C2x = xpos[2]
    C3x = xpos[2]

    shuffle(ypos)
    A1y = ypos[0]
    A2y = ypos[1]
    A3y = ypos[2]

    shuffle(ypos)
    B1y = ypos[0]
    B2y = ypos[1]
    B3y = ypos[2]

    shuffle(ypos)
    C1y = ypos[0]
    C2y = ypos[1]
    C3y = ypos[2]        

# update component parameters for each repeat
B2.setPos((B2x, B2y))
B2.setImage(ImageB2)
B1.setPos((B1x, B1y))
B1.setImage(ImageB1)
B3.setPos((B3x, B3y))
B3.setImage(ImageB3)
A1.setPos((A1x, A1y))
A1.setImage(ImageA1)
A2.setPos((A2x, A2y))
A2.setImage(ImageA2)
A3.setPos((A3x, A3y))
A3.setImage(ImageA3)
C1.setPos((C1x, C1y))
C1.setImage(ImageC1)
C2.setPos((C2x, C2y))
C2.setImage(ImageC2)
C3.setPos((C3x, C3y))
C3.setImage(ImageC3)
```

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 17, 2022, 11:34am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/9 "2022-02-17T11:34:24Z")

</div>

Hello Leonie,

that is within one participant?

Best wishes Jens

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 17, 2022, 11:40am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/10 "2022-02-17T11:40:00Z")

</div>

yes that is within one participant

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 17, 2022, 11:42am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/11 "2022-02-17T11:42:32Z")

</div>

Hello Leonie,

within one block or across two blocks (one column-wise, one row-wise)? Sorry, I need more details.

Best wishes Jens

---

<div class="post-metadata">

### Author: ![LEONIE\_LAMBERTZ](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/leonie_lambertz/32/32848_2.png) [@LEONIE\_LAMBERTZ](https://discourse.psychopy.org/u/LEONIE_LAMBERTZ)
#### Post date: [February 17, 2022, 11:48am UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/12 "2022-02-17T11:48:25Z")

</div>

I will most likely have 4 blocks with 20 trials each.  
Within each block the amount of column-wise trials and row-wise trials should be equal.

Thank you for taking the time!

---

<div class="post-metadata">

### Author: ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)
#### Post date: [February 17, 2022, 12:02pm UTC](https://discourse.psychopy.org/t/randomization-of-image-presentation/27441/13 "2022-02-17T12:02:49Z")

</div>

Hello Leonie,

in the _Begin routine tab_ of the relevant block add the following:

```auto
ranList = [0,1]*10
shuffle(ranList)

if ranList.pop():
    #add here the code for column-wise categories
else:
    #add here the code for row-wise categories

```

Notice that ranList has twenty elements. It will crash if you have more trials per block.

Best wishes Jens  
BTW: it best to open a new topic when a new problem comes up.
