Picking random number of stims from a stim list excel sheet

Hello,

i am creating an attentional blink experiment (psychopy v2023.1.2) and i need psychopy to choose first distractor images so that it randomly chooses 2-10 images from a stimulus excel sheet which contains pool of 40 images per trial. However, i still haven’t been able to set this up properly.

I have tried various ways, non of them seeming to work, mostly by defining a few variables in code component which ties to a loop and a image component.

here is an example of one of the codes i used:

import random

excel sheet with distractors

distractor_pool = [‘dist_beforeTr.xlsx’]

pick a random number between 2 and 10

numDistractors = random.randint(2, 10)
thisExp.addData(‘numDistractors_beforeTr’, numDistractors)

create a list of distractors

current_distractor_sequence = random.sample(distractor_pool, numDistractors)

Do you know anything that would help me? Thank you in advance.

Hi @Ondra_Mel,

Could you possibly share your experiment, or a minimal demo of it?

Thanks so much,

Kim

Experiment AB task.zip (109.2 KB)

this is it, the routine i was refferencing is distractors_beforeTr. i am not able to send stimuli as there’s too many of them. thank you in advance

Hi @Ondra_Mel,

Thanks so much for sending that over.

I believe I’ve achieved what you’re looking for, I’ve added code to the choose_distrs code component which I’ve placed in the centralizationTr routine as it needs to run before the tr_loop_d_before loop. I’ve attached the updated .psyexp file here.

I’m using a variable called rows_str in the ‘selected rows’ field in the loop properties to present random rows from your conditions file. This is a list of numbers, which is set in that choose_distrs code component:

# Randomly choose how many distractors to present - between 2-10
numDistractors = randint(2, 10)
thisExp.addData('numDistractors_beforeTr', numDistractors)

selected_rows = sorted(random.sample(range(total_rows), numDistractors))

# Format the selected rows as a string for the loop
rows_str = ",".join(str(row) for row in selected_rows)
print('rows: ', rows_str) # printing it out to check 
# We'll then use rows_str in the tr_loop_d_before loop itself

Hope that helps!

Kim
Attentional blink.psyexp (139.2 KB)

thank you very much, that does work!!

there is one more problem after that. not with the code though. the experiment screen goes blank and none of the following images appear. I can’t find the problem (i thought all of my routines, image components, loop and the corresponding excel sheet are set up properly). would you help please? thank you in advance

Attentional blink2.psyexp.zip (7.6 KB)

When in the experiment does this occur?

directly after the routine distractors_beforeTr is over

Try to look in the psychopy runner to see what the actual error is that causes it not continuing.
In addition, look at the print out to see how many rows are selected, and which ones.
Also not sure if it matters but in your Tr_loop you have the stop condition for d_beforeTr as duration (frames), whereas in your testing loop you have the stop as Frame N.

runner is showing me this error: OSError: Couldn’t find image stimuli/T2/noise/distOT2.jpg; check path? (tried: /Users/ondra/Desktop/Experiment AB task/stimuli/T2/noise/distOT2.jpg).
but

the whole runner message is:
File “/Users/ondra/Desktop/Experiment AB task/Attentional blink2_lastrun.py”, line 1101, in
T2imageTr.setImage(imageT2)
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/visual/image.py”, line 409, in setImage
setAttribute(self, ‘image’, value, log)
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/tools/attributetools.py”, line 134, in setAttribute
setattr(self, attrib, value)
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/tools/attributetools.py”, line 27, in set
newValue = self.func(obj, value)
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/visual/image.py”, line 384, in image
self.isLumImage = self._createTexture(
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/visual/basevisual.py”, line 1046, in _createTexture
raise IOError(msg % (tex, os.path.abspath(tex)))
OSError: Couldn’t find image stimuli/T2/noise/distOT2.jpg; check path? (tried: /Users/ondra/Desktop/Experiment AB task/stimuli/T2/noise/distOT2.jpg)
################ Experiment ended with exit code 1 [pid:23622] #################

But i am fairly sure that the paths to the stimuli folder containing images in excel are properly set up.

paths in excel: stimuli/T2/noise/distOT2.jpg; i checked that multiple times and the path should be correct.

nonetheless, even if T2_tr is the routine that is crashing the experiment, the two routines before that do not show any content.

Experiment AB task.zip (229.4 KB)

Please could you show a screenshot of folder

Users/ondra/Desktop/Experiment AB task/stimuli/T2/noise/

showing the image file in question?

Do you have extensions hidden? If so, perhaps the file is really called distOT2.jpg.jpg

i seem to have fixed that, a mistake in excel which i hadn’t noticed before. thank you.

1 Like