Randomly selecting 10 images from a list of 40 without repetition across blocks

I’m very new to Psychopy with no previous experience in coding.
I’m trying to create an experiment with 8 blocks - 4 asking one question (ID blocks) and 4 asking another (Likert blocks), alternating between the two questions for each successive block.
Each block needs to contain 10 images randomly selected from a list of 40, but it should exclude images already selected by previous blocks of the same type (i.e. the 10 images used in the first ID block shouldn’t be selected in the second ID block).

I can’t figure out how to display the images with the logic I want.

With the help of AI, I tried using a code component and writing this in the ‘Begin Experiment’ tab:

import pandas as pd
import random

# Read the Excel file
image_list = pd.read_excel('images.xlsx')['image_files'].tolist()

# Ensure the image list is of expected length
if len(image_list) != 40:
    raise ValueError("The image list must contain exactly 40 images.")

# Create 4 blocks of 10 unique images each
blocks = []
remaining_images = image_list.copy()

for i in range(4):
    block_images = random.sample(remaining_images, 10)
    blocks.append(block_images)
    remaining_images = [img for img in remaining_images if img not in block_images]

# Flatten the blocks list to iterate through images in sequence
all_images = sum(blocks, [])

and this in the ‘Begin Routine’ tab:

# Determine the trial number
trial_num = self.thisN if self.thisN is not None else 0  # Get the current trial number

# Set the current image
if trial_num < len(all_images):
    current_image = all_images[trial_num]
else:
    current_image = None  # Safety check

and inserting an image component referring to $current_image

However I’m getting this error:

 image.setImage(current_image)
UnboundLocalError: local variable 'current_image' referenced before assignment
################ Experiment ended with exit code 1 [pid:22613] #################

Can anyone help me solve this? I’ve been trying to get AI to help me with this but I think it’s only creating a bigger mess.

Thanks everyone.

If I was doing this I would have a loop pointing to images.xlsx and then set the question based on the value of trials.thisN (possibly from a shuffled list).

However, to address your error

UnboundLocalError: local variable ‘current_image’ referenced before assignment

Is your image component set to update each repeat or constant?

My feeling is that AI is good at generating Python code but doesn’t know how to work with PsychoPy Builder components.

thank you for the reply.
the image component is set to update each repeat.
i agree about the limits of AI of not knowing how to work with psychopy.

what would you do to get this to work? the order needs to be:
ID Block #1: randomly select 10/40
Likert Block #1: randomly select 10/40
ID Block #2: randomly select 10/30 leftover images
Likert Block #2: randomly select 10/30 leftover images
ID Block #3: randomly select 10/20 leftover images
Likert Block #3: randomly select 10/20 leftover images
ID Block #4: select 10 leftover images (shown at random order)
Likert Block #4: select 10 leftover images (shown at random order)

Hello

I hope that I understood your procedure correctly. The following toy-experiment presents stimuli in blocks of 10 for an ID- and a Likert-part. Uncomment the shuffle-command in the component setIdx - codeSet - Begin Experiment to get a randomized distribution.

The program reads in an Excel-file and presents stim either in sequential or random order.

Best wishes Jens

4Blocks.psyexp (23.2 KB)
4Blocks.xlsx (10.0 KB)